blob: b12a2882ec742c854e8281cc7c28a40fcf8f034f [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010039#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020040#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080041#include <linux/gpio.h>
42#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053043#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053044#include <linux/pm_runtime.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070045#include <linux/platform_data/mmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046
47/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053050#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_BLK 0x0104
52#define OMAP_HSMMC_ARG 0x0108
53#define OMAP_HSMMC_CMD 0x010C
54#define OMAP_HSMMC_RSP10 0x0110
55#define OMAP_HSMMC_RSP32 0x0114
56#define OMAP_HSMMC_RSP54 0x0118
57#define OMAP_HSMMC_RSP76 0x011C
58#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020059#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010060#define OMAP_HSMMC_HCTL 0x0128
61#define OMAP_HSMMC_SYSCTL 0x012C
62#define OMAP_HSMMC_STAT 0x0130
63#define OMAP_HSMMC_IE 0x0134
64#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053065#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#define OMAP_HSMMC_CAPA 0x0140
67
68#define VS18 (1 << 26)
69#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053070#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVS18 (0x5 << 9)
72#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080073#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010074#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010075#define SDVSCLR 0xFFFFF1FF
76#define SDVSDET 0x00000400
77#define AUTOIDLE 0x1
78#define SDBP (1 << 8)
79#define DTO 0xe
80#define ICE 0x1
81#define ICS 0x2
82#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053083#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define CLKD_MASK 0x0000FFC0
85#define CLKD_SHIFT 6
86#define DTO_MASK 0x000F0000
87#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010088#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053089#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define DP_SELECT (1 << 21)
91#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053092#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010093#define MSBS (1 << 5)
94#define BCE (1 << 1)
95#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053096#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020097#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +053098#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +020099#define CLKEXTFREE (1 << 16)
100#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200101#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100102#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define STAT_CLEAR 0xFFFFFFFF
104#define INIT_STREAM_CMD 0x00000000
105#define DUAL_VOLT_OCR_BIT 7
106#define SRC (1 << 25)
107#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700108#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100109
Venkatraman Sa7e96872012-11-19 22:00:01 +0530110/* Interrupt masks for IE and ISE register */
111#define CC_EN (1 << 0)
112#define TC_EN (1 << 1)
113#define BWR_EN (1 << 4)
114#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200115#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530116#define ERR_EN (1 << 15)
117#define CTO_EN (1 << 16)
118#define CCRC_EN (1 << 17)
119#define CEB_EN (1 << 18)
120#define CIE_EN (1 << 19)
121#define DTO_EN (1 << 20)
122#define DCRC_EN (1 << 21)
123#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530124#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530125#define CERR_EN (1 << 28)
126#define BADA_EN (1 << 29)
127
Balaji T Ka2e77152014-01-21 19:54:42 +0530128#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530129 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
130 BRR_EN | BWR_EN | TC_EN | CC_EN)
131
Balaji T Ka2e77152014-01-21 19:54:42 +0530132#define CNI (1 << 7)
133#define ACIE (1 << 4)
134#define ACEB (1 << 3)
135#define ACCE (1 << 2)
136#define ACTO (1 << 1)
137#define ACNE (1 << 0)
138
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530139#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530140#define MMC_TIMEOUT_MS 20 /* 20 mSec */
141#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400142#define OMAP_MMC_MIN_CLOCK 400000
143#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530144#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100145
Balaji T Ke99448f2014-02-19 20:26:40 +0530146#define VDD_1V8 1800000 /* 180000 uV */
147#define VDD_3V0 3000000 /* 300000 uV */
148#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
149
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100150/*
151 * One controller can have multiple slots, like on some omap boards using
152 * omap.c controller driver. Luckily this is not currently done on any known
153 * omap_hsmmc.c device.
154 */
155#define mmc_slot(host) (host->pdata->slots[host->slot_id])
156
157/*
158 * MMC Host controller read/write API's
159 */
160#define OMAP_HSMMC_READ(base, reg) \
161 __raw_readl((base) + OMAP_HSMMC_##reg)
162
163#define OMAP_HSMMC_WRITE(base, reg, val) \
164 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
165
Per Forlin9782aff2011-07-01 18:55:23 +0200166struct omap_hsmmc_next {
167 unsigned int dma_len;
168 s32 cookie;
169};
170
Denis Karpov70a33412009-09-22 16:44:59 -0700171struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 struct device *dev;
173 struct mmc_host *mmc;
174 struct mmc_request *mrq;
175 struct mmc_command *cmd;
176 struct mmc_data *data;
177 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100178 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800179 /*
180 * vcc == configured supply
181 * vcc_aux == optional
182 * - MMC1, supply for DAT4..DAT7
183 * - MMC2/MMC2, external level shifter voltage supply, for
184 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
185 */
186 struct regulator *vcc;
187 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530188 struct regulator *pbias;
189 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100190 void __iomem *base;
191 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700192 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100193 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200194 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100195 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700196 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530198 u32 con;
199 u32 hctl;
200 u32 sysctl;
201 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100202 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200203 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100204 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100205 struct dma_chan *tx_chan;
206 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100207 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200208 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700209 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700210 int protect_card;
211 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800212 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700213 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530214 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530215 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200216#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
217#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
218#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200219 struct omap_hsmmc_next next_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100220 struct omap_mmc_platform_data *pdata;
221};
222
Nishanth Menon59445b12014-02-13 23:45:48 -0600223struct omap_mmc_of_data {
224 u32 reg_offset;
225 u8 controller_flags;
226};
227
Balaji T Kbf129e12014-01-21 19:54:42 +0530228static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
229
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800230static int omap_hsmmc_card_detect(struct device *dev, int slot)
231{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530232 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
233 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800234
235 /* NOTE: assumes card detect signal is active-low */
236 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
237}
238
239static int omap_hsmmc_get_wp(struct device *dev, int slot)
240{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530241 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
242 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800243
244 /* NOTE: assumes write protect signal is active-high */
245 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
246}
247
248static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
249{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530250 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
251 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800252
253 /* NOTE: assumes card detect signal is active-low */
254 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
255}
256
257#ifdef CONFIG_PM
258
259static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
260{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530261 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
262 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800263
264 disable_irq(mmc->slots[0].card_detect_irq);
265 return 0;
266}
267
268static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
269{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530270 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
271 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800272
273 enable_irq(mmc->slots[0].card_detect_irq);
274 return 0;
275}
276
277#else
278
279#define omap_hsmmc_suspend_cdirq NULL
280#define omap_hsmmc_resume_cdirq NULL
281
282#endif
283
Adrian Hunterb702b102010-02-15 10:03:35 -0800284#ifdef CONFIG_REGULATOR
285
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500286static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800287 int vdd)
288{
289 struct omap_hsmmc_host *host =
290 platform_get_drvdata(to_platform_device(dev));
291 int ret = 0;
292
293 /*
294 * If we don't see a Vcc regulator, assume it's a fixed
295 * voltage always-on regulator.
296 */
297 if (!host->vcc)
298 return 0;
299
300 if (mmc_slot(host).before_set_reg)
301 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
302
Balaji T Ke99448f2014-02-19 20:26:40 +0530303 if (host->pbias) {
304 if (host->pbias_enabled == 1) {
305 ret = regulator_disable(host->pbias);
306 if (!ret)
307 host->pbias_enabled = 0;
308 }
309 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
310 }
311
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800312 /*
313 * Assume Vcc regulator is used only to power the card ... OMAP
314 * VDDS is used to power the pins, optionally with a transceiver to
315 * support cards using voltages other than VDDS (1.8V nominal). When a
316 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
317 *
318 * In some cases this regulator won't support enable/disable;
319 * e.g. it's a fixed rail for a WLAN chip.
320 *
321 * In other cases vcc_aux switches interface power. Example, for
322 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
323 * chips/cards need an interface voltage rail too.
324 */
325 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530326 if (host->vcc)
327 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800328 /* Enable interface voltage rail, if needed */
329 if (ret == 0 && host->vcc_aux) {
330 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530331 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400332 ret = mmc_regulator_set_ocr(host->mmc,
333 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800334 }
335 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400336 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800337 if (host->vcc_aux)
338 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530339 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400340 /* Then proceed to shut down the local regulator */
341 ret = mmc_regulator_set_ocr(host->mmc,
342 host->vcc, 0);
343 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800344 }
345
Balaji T Ke99448f2014-02-19 20:26:40 +0530346 if (host->pbias) {
347 if (vdd <= VDD_165_195)
348 ret = regulator_set_voltage(host->pbias, VDD_1V8,
349 VDD_1V8);
350 else
351 ret = regulator_set_voltage(host->pbias, VDD_3V0,
352 VDD_3V0);
353 if (ret < 0)
354 goto error_set_power;
355
356 if (host->pbias_enabled == 0) {
357 ret = regulator_enable(host->pbias);
358 if (!ret)
359 host->pbias_enabled = 1;
360 }
361 }
362
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800363 if (mmc_slot(host).after_set_reg)
364 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
365
Balaji T Ke99448f2014-02-19 20:26:40 +0530366error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800367 return ret;
368}
369
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
371{
372 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700373 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800374
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530375 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800376 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530377 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
378 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400379 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800380 } else {
381 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700382 ocr_value = mmc_regulator_get_ocrmask(reg);
383 if (!mmc_slot(host).ocr_mask) {
384 mmc_slot(host).ocr_mask = ocr_value;
385 } else {
386 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530387 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500388 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700389 mmc_slot(host).ocr_mask = 0;
390 return -EINVAL;
391 }
392 }
Balaji T K987fd492014-02-19 20:26:40 +0530393 }
394 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800395
Balaji T K987fd492014-02-19 20:26:40 +0530396 /* Allow an aux regulator */
397 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
398 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800399
Balaji T Ke99448f2014-02-19 20:26:40 +0530400 reg = devm_regulator_get_optional(host->dev, "pbias");
401 host->pbias = IS_ERR(reg) ? NULL : reg;
402
Balaji T K987fd492014-02-19 20:26:40 +0530403 /* For eMMC do not power off when not in sleep state */
404 if (mmc_slot(host).no_regulator_off_init)
405 return 0;
406 /*
407 * To disable boot_on regulator, enable regulator
408 * to increase usecount and then disable it.
409 */
410 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
411 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
412 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300413
Balaji T K987fd492014-02-19 20:26:40 +0530414 mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
415 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800416 }
417
418 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800419}
420
421static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
422{
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800423 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800424}
425
Adrian Hunterb702b102010-02-15 10:03:35 -0800426static inline int omap_hsmmc_have_reg(void)
427{
428 return 1;
429}
430
431#else
432
433static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
434{
435 return -EINVAL;
436}
437
438static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
439{
440}
441
442static inline int omap_hsmmc_have_reg(void)
443{
444 return 0;
445}
446
447#endif
448
449static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
450{
451 int ret;
452
453 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800454 if (pdata->slots[0].cover)
455 pdata->slots[0].get_cover_state =
456 omap_hsmmc_get_cover_state;
457 else
458 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
459 pdata->slots[0].card_detect_irq =
460 gpio_to_irq(pdata->slots[0].switch_pin);
461 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
462 if (ret)
463 return ret;
464 ret = gpio_direction_input(pdata->slots[0].switch_pin);
465 if (ret)
466 goto err_free_sp;
467 } else
468 pdata->slots[0].switch_pin = -EINVAL;
469
470 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
471 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
472 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
473 if (ret)
474 goto err_free_cd;
475 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
476 if (ret)
477 goto err_free_wp;
478 } else
479 pdata->slots[0].gpio_wp = -EINVAL;
480
481 return 0;
482
483err_free_wp:
484 gpio_free(pdata->slots[0].gpio_wp);
485err_free_cd:
486 if (gpio_is_valid(pdata->slots[0].switch_pin))
487err_free_sp:
488 gpio_free(pdata->slots[0].switch_pin);
489 return ret;
490}
491
492static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
493{
494 if (gpio_is_valid(pdata->slots[0].gpio_wp))
495 gpio_free(pdata->slots[0].gpio_wp);
496 if (gpio_is_valid(pdata->slots[0].switch_pin))
497 gpio_free(pdata->slots[0].switch_pin);
498}
499
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100500/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300501 * Start clock to the card
502 */
503static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
504{
505 OMAP_HSMMC_WRITE(host->base, SYSCTL,
506 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
507}
508
509/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100510 * Stop clock to the card
511 */
Denis Karpov70a33412009-09-22 16:44:59 -0700512static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100513{
514 OMAP_HSMMC_WRITE(host->base, SYSCTL,
515 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
516 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900517 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100518}
519
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700520static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
521 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700522{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200523 u32 irq_mask = INT_EN_MASK;
524 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700525
526 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200527 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700528
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700529 /* Disable timeout for erases */
530 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530531 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700532
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200533 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700534 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
535 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200536
537 /* latch pending CIRQ, but don't signal MMC core */
538 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
539 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700540 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200541 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700542}
543
544static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
545{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200546 u32 irq_mask = 0;
547 unsigned long flags;
548
549 spin_lock_irqsave(&host->irq_lock, flags);
550 /* no transfer running but need to keep cirq if enabled */
551 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
552 irq_mask |= CIRQ_EN;
553 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
554 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700555 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200556 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700557}
558
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300559/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530560static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300561{
562 u16 dsor = 0;
563
564 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530565 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530566 if (dsor > CLKD_MAX)
567 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300568 }
569
570 return dsor;
571}
572
Andy Shevchenko5934df22011-05-06 12:14:06 +0300573static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
574{
575 struct mmc_ios *ios = &host->mmc->ios;
576 unsigned long regval;
577 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530578 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300579
Venkatraman S8986d312012-08-07 19:10:38 +0530580 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300581
582 omap_hsmmc_stop_clock(host);
583
584 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
585 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530586 clkdiv = calc_divisor(host, ios);
587 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300588 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
589 OMAP_HSMMC_WRITE(host->base, SYSCTL,
590 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
591
592 /* Wait till the ICS bit is set */
593 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
594 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
595 && time_before(jiffies, timeout))
596 cpu_relax();
597
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530598 /*
599 * Enable High-Speed Support
600 * Pre-Requisites
601 * - Controller should support High-Speed-Enable Bit
602 * - Controller should not be using DDR Mode
603 * - Controller should advertise that it supports High Speed
604 * in capabilities register
605 * - MMC/SD clock coming out of controller > 25MHz
606 */
607 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900608 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530609 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
610 regval = OMAP_HSMMC_READ(host->base, HCTL);
611 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
612 regval |= HSPE;
613 else
614 regval &= ~HSPE;
615
616 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
617 }
618
Andy Shevchenko5934df22011-05-06 12:14:06 +0300619 omap_hsmmc_start_clock(host);
620}
621
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400622static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
623{
624 struct mmc_ios *ios = &host->mmc->ios;
625 u32 con;
626
627 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900628 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d922012-04-09 12:08:33 +0530629 con |= DDR; /* configure in DDR mode */
630 else
631 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400632 switch (ios->bus_width) {
633 case MMC_BUS_WIDTH_8:
634 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
635 break;
636 case MMC_BUS_WIDTH_4:
637 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
638 OMAP_HSMMC_WRITE(host->base, HCTL,
639 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
640 break;
641 case MMC_BUS_WIDTH_1:
642 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
643 OMAP_HSMMC_WRITE(host->base, HCTL,
644 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
645 break;
646 }
647}
648
649static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
650{
651 struct mmc_ios *ios = &host->mmc->ios;
652 u32 con;
653
654 con = OMAP_HSMMC_READ(host->base, CON);
655 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
656 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
657 else
658 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
659}
660
Denis Karpov11dd62a2009-09-22 16:44:43 -0700661#ifdef CONFIG_PM
662
663/*
664 * Restore the MMC host context, if it was lost as result of a
665 * power state change.
666 */
Denis Karpov70a33412009-09-22 16:44:59 -0700667static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700668{
669 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400670 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700671 unsigned long timeout;
672
Tony Lindgren0a82e062013-10-21 00:25:19 +0530673 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
674 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
675 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
676 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
677 return 0;
678
679 host->context_loss++;
680
Balaji T Kc2200ef2012-03-07 09:55:30 -0500681 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700682 if (host->power_mode != MMC_POWER_OFF &&
683 (1 << ios->vdd) <= MMC_VDD_23_24)
684 hctl = SDVS18;
685 else
686 hctl = SDVS30;
687 capa = VS30 | VS18;
688 } else {
689 hctl = SDVS18;
690 capa = VS18;
691 }
692
Balaji T K5a52b082014-05-29 10:28:02 +0200693 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
694 hctl |= IWE;
695
Denis Karpov11dd62a2009-09-22 16:44:43 -0700696 OMAP_HSMMC_WRITE(host->base, HCTL,
697 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
698
699 OMAP_HSMMC_WRITE(host->base, CAPA,
700 OMAP_HSMMC_READ(host->base, CAPA) | capa);
701
702 OMAP_HSMMC_WRITE(host->base, HCTL,
703 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
704
705 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
706 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
707 && time_before(jiffies, timeout))
708 ;
709
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200710 OMAP_HSMMC_WRITE(host->base, ISE, 0);
711 OMAP_HSMMC_WRITE(host->base, IE, 0);
712 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700713
714 /* Do not initialize card-specific things if the power is off */
715 if (host->power_mode == MMC_POWER_OFF)
716 goto out;
717
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400718 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700719
Andy Shevchenko5934df22011-05-06 12:14:06 +0300720 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700721
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400722 omap_hsmmc_set_bus_mode(host);
723
Denis Karpov11dd62a2009-09-22 16:44:43 -0700724out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530725 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
726 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700727 return 0;
728}
729
730/*
731 * Save the MMC host context (store the number of power state changes so far).
732 */
Denis Karpov70a33412009-09-22 16:44:59 -0700733static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700734{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530735 host->con = OMAP_HSMMC_READ(host->base, CON);
736 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
737 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
738 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700739}
740
741#else
742
Denis Karpov70a33412009-09-22 16:44:59 -0700743static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700744{
745 return 0;
746}
747
Denis Karpov70a33412009-09-22 16:44:59 -0700748static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700749{
750}
751
752#endif
753
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100754/*
755 * Send init stream sequence to card
756 * before sending IDLE command
757 */
Denis Karpov70a33412009-09-22 16:44:59 -0700758static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759{
760 int reg = 0;
761 unsigned long timeout;
762
Adrian Hunterb62f6222009-09-22 16:45:01 -0700763 if (host->protect_card)
764 return;
765
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100766 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700767
768 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769 OMAP_HSMMC_WRITE(host->base, CON,
770 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
771 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
772
773 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530774 while ((reg != CC_EN) && time_before(jiffies, timeout))
775 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776
777 OMAP_HSMMC_WRITE(host->base, CON,
778 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700779
780 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
781 OMAP_HSMMC_READ(host->base, STAT);
782
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783 enable_irq(host->irq);
784}
785
786static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700787int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788{
789 int r = 1;
790
Denis Karpov191d1f12009-09-22 16:44:55 -0700791 if (mmc_slot(host).get_cover_state)
792 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793 return r;
794}
795
796static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700797omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798 char *buf)
799{
800 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700801 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802
Denis Karpov70a33412009-09-22 16:44:59 -0700803 return sprintf(buf, "%s\n",
804 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805}
806
Denis Karpov70a33412009-09-22 16:44:59 -0700807static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808
809static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700810omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100811 char *buf)
812{
813 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700814 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815
Denis Karpov191d1f12009-09-22 16:44:55 -0700816 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100817}
818
Denis Karpov70a33412009-09-22 16:44:59 -0700819static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100820
821/*
822 * Configure the response type and send the cmd.
823 */
824static void
Denis Karpov70a33412009-09-22 16:44:59 -0700825omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100826 struct mmc_data *data)
827{
828 int cmdreg = 0, resptype = 0, cmdtype = 0;
829
Venkatraman S8986d312012-08-07 19:10:38 +0530830 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100831 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
832 host->cmd = cmd;
833
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700834 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200836 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837 if (cmd->flags & MMC_RSP_PRESENT) {
838 if (cmd->flags & MMC_RSP_136)
839 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200840 else if (cmd->flags & MMC_RSP_BUSY) {
841 resptype = 3;
842 host->response_busy = 1;
843 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844 resptype = 2;
845 }
846
847 /*
848 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
849 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
850 * a val of 0x3, rest 0x0.
851 */
852 if (cmd == host->mrq->stop)
853 cmdtype = 0x3;
854
855 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
856
Balaji T Ka2e77152014-01-21 19:54:42 +0530857 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
858 host->mrq->sbc) {
859 cmdreg |= ACEN_ACMD23;
860 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
861 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100862 if (data) {
863 cmdreg |= DP_SELECT | MSBS | BCE;
864 if (data->flags & MMC_DATA_READ)
865 cmdreg |= DDIR;
866 else
867 cmdreg &= ~(DDIR);
868 }
869
870 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530871 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100872
Adrian Hunterb4175772010-05-26 14:42:06 -0700873 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700874
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100875 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
876 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
877}
878
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200879static int
Denis Karpov70a33412009-09-22 16:44:59 -0700880omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200881{
882 if (data->flags & MMC_DATA_WRITE)
883 return DMA_TO_DEVICE;
884 else
885 return DMA_FROM_DEVICE;
886}
887
Russell Kingc5c98922012-04-13 12:14:39 +0100888static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
889 struct mmc_data *data)
890{
891 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
892}
893
Adrian Hunterb4175772010-05-26 14:42:06 -0700894static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
895{
896 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530897 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700898
Venkatraman S31463b12012-04-09 12:08:34 +0530899 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700900 host->req_in_progress = 0;
901 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530902 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700903
904 omap_hsmmc_disable_irq(host);
905 /* Do not complete the request if DMA is still in progress */
906 if (mrq->data && host->use_dma && dma_ch != -1)
907 return;
908 host->mrq = NULL;
909 mmc_request_done(host->mmc, mrq);
910}
911
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100912/*
913 * Notify the transfer complete to MMC core
914 */
915static void
Denis Karpov70a33412009-09-22 16:44:59 -0700916omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100917{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200918 if (!data) {
919 struct mmc_request *mrq = host->mrq;
920
Adrian Hunter23050102009-09-22 16:44:57 -0700921 /* TC before CC from CMD6 - don't know why, but it happens */
922 if (host->cmd && host->cmd->opcode == 6 &&
923 host->response_busy) {
924 host->response_busy = 0;
925 return;
926 }
927
Adrian Hunterb4175772010-05-26 14:42:06 -0700928 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200929 return;
930 }
931
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100932 host->data = NULL;
933
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100934 if (!data->error)
935 data->bytes_xfered += data->blocks * (data->blksz);
936 else
937 data->bytes_xfered = 0;
938
Balaji T Kbf129e12014-01-21 19:54:42 +0530939 if (data->stop && (data->error || !host->mrq->sbc))
940 omap_hsmmc_start_command(host, data->stop, NULL);
941 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700942 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943}
944
945/*
946 * Notify the core about command completion
947 */
948static void
Denis Karpov70a33412009-09-22 16:44:59 -0700949omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100950{
Balaji T Kbf129e12014-01-21 19:54:42 +0530951 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530952 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530953 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530954 omap_hsmmc_start_dma_transfer(host);
955 omap_hsmmc_start_command(host, host->mrq->cmd,
956 host->mrq->data);
957 return;
958 }
959
Balaji T K2177fa92014-05-09 22:16:52 +0530960 host->cmd = NULL;
961
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100962 if (cmd->flags & MMC_RSP_PRESENT) {
963 if (cmd->flags & MMC_RSP_136) {
964 /* response type 2 */
965 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
966 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
967 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
968 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
969 } else {
970 /* response types 1, 1b, 3, 4, 5, 6 */
971 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
972 }
973 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700974 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530975 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100976}
977
978/*
979 * DMA clean up for command errors
980 */
Denis Karpov70a33412009-09-22 16:44:59 -0700981static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982{
Adrian Hunterb4175772010-05-26 14:42:06 -0700983 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530984 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700985
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200986 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100987
Venkatraman S31463b12012-04-09 12:08:34 +0530988 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700989 dma_ch = host->dma_ch;
990 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530991 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700992
993 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100994 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
995
996 dmaengine_terminate_all(chan);
997 dma_unmap_sg(chan->device->dev,
998 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700999 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001000
Per Forlin053bf342011-11-07 21:55:11 +05301001 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002 }
1003 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001004}
1005
1006/*
1007 * Readable error output
1008 */
1009#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001010static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001011{
1012 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001013 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001014 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1015 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1016 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1017 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001018 };
1019 char res[256];
1020 char *buf = res;
1021 int len, i;
1022
1023 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1024 buf += len;
1025
Denis Karpov70a33412009-09-22 16:44:59 -07001026 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001027 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001028 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001029 buf += len;
1030 }
1031
Venkatraman S8986d312012-08-07 19:10:38 +05301032 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001033}
Adrian Hunter699b9582011-05-06 12:14:01 +03001034#else
1035static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1036 u32 status)
1037{
1038}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001039#endif /* CONFIG_MMC_DEBUG */
1040
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001041/*
1042 * MMC controller internal state machines reset
1043 *
1044 * Used to reset command or data internal state machines, using respectively
1045 * SRC or SRD bit of SYSCTL register
1046 * Can be called from interrupt context
1047 */
Denis Karpov70a33412009-09-22 16:44:59 -07001048static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1049 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001050{
1051 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301052 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001053
1054 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1055 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1056
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001057 /*
1058 * OMAP4 ES2 and greater has an updated reset logic.
1059 * Monitor a 0->1 transition first
1060 */
1061 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001062 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001063 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301064 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001065 }
1066 i = 0;
1067
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001068 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1069 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301070 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001071
1072 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1073 dev_err(mmc_dev(host->mmc),
1074 "Timeout waiting on controller reset in %s\n",
1075 __func__);
1076}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001077
Balaji T K25e18972012-11-19 21:59:55 +05301078static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1079 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301080{
Balaji T K25e18972012-11-19 21:59:55 +05301081 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301082 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301083 if (host->cmd)
1084 host->cmd->error = err;
1085 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301086
1087 if (host->data) {
1088 omap_hsmmc_reset_controller_fsm(host, SRD);
1089 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301090 } else if (host->mrq && host->mrq->cmd)
1091 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301092}
1093
Adrian Hunterb4175772010-05-26 14:42:06 -07001094static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001097 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301098 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001100 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301101 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102
Venkatraman Sa7e96872012-11-19 22:00:01 +05301103 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001104 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001105
Venkatraman Sa7e96872012-11-19 22:00:01 +05301106 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301107 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301108 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301109 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301110 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301111 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1112
Balaji T Ka2e77152014-01-21 19:54:42 +05301113 if (status & ACE_EN) {
1114 u32 ac12;
1115 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1116 if (!(ac12 & ACNE) && host->mrq->sbc) {
1117 end_cmd = 1;
1118 if (ac12 & ACTO)
1119 error = -ETIMEDOUT;
1120 else if (ac12 & (ACCE | ACEB | ACIE))
1121 error = -EILSEQ;
1122 host->mrq->sbc->error = error;
1123 hsmmc_command_incomplete(host, error, end_cmd);
1124 }
1125 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1126 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301127 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301128 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301129 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001130 }
1131 }
1132
Francesco Lavra7472bab2013-06-29 08:25:12 +02001133 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301134 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001135 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301136 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001137 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001138}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001139
Adrian Hunterb4175772010-05-26 14:42:06 -07001140/*
1141 * MMC controller IRQ handler
1142 */
1143static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1144{
1145 struct omap_hsmmc_host *host = dev_id;
1146 int status;
1147
1148 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001149 while (status & (INT_EN_MASK | CIRQ_EN)) {
1150 if (host->req_in_progress)
1151 omap_hsmmc_do_irq(host, status);
1152
1153 if (status & CIRQ_EN)
1154 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301155
Adrian Hunterb4175772010-05-26 14:42:06 -07001156 /* Flush posted write */
1157 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301158 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001159
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160 return IRQ_HANDLED;
1161}
1162
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001163static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1164{
1165 struct omap_hsmmc_host *host = dev_id;
1166
1167 /* cirq is level triggered, disable to avoid infinite loop */
1168 spin_lock(&host->irq_lock);
1169 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1170 disable_irq_nosync(host->wake_irq);
1171 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1172 }
1173 spin_unlock(&host->irq_lock);
1174 pm_request_resume(host->dev); /* no use counter */
1175
1176 return IRQ_HANDLED;
1177}
1178
Denis Karpov70a33412009-09-22 16:44:59 -07001179static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001180{
1181 unsigned long i;
1182
1183 OMAP_HSMMC_WRITE(host->base, HCTL,
1184 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1185 for (i = 0; i < loops_per_jiffy; i++) {
1186 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1187 break;
1188 cpu_relax();
1189 }
1190}
1191
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192/*
David Brownelleb250822009-02-17 14:49:01 -08001193 * Switch MMC interface voltage ... only relevant for MMC1.
1194 *
1195 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1196 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1197 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198 */
Denis Karpov70a33412009-09-22 16:44:59 -07001199static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001200{
1201 u32 reg_val = 0;
1202 int ret;
1203
1204 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301205 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301206 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301207 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001208
1209 /* Turn the power off */
1210 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001211
1212 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001213 if (!ret)
1214 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1215 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301216 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301217 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301218 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001219
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 if (ret != 0)
1221 goto err;
1222
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001223 OMAP_HSMMC_WRITE(host->base, HCTL,
1224 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1225 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001226
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001227 /*
1228 * If a MMC dual voltage card is detected, the set_ios fn calls
1229 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001230 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001231 *
David Brownelleb250822009-02-17 14:49:01 -08001232 * Cope with a bit of slop in the range ... per data sheets:
1233 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1234 * but recommended values are 1.71V to 1.89V
1235 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1236 * but recommended values are 2.7V to 3.3V
1237 *
1238 * Board setup code shouldn't permit anything very out-of-range.
1239 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1240 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001241 */
David Brownelleb250822009-02-17 14:49:01 -08001242 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001243 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001244 else
1245 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246
1247 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001248 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001249
1250 return 0;
1251err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301252 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253 return ret;
1254}
1255
Adrian Hunterb62f6222009-09-22 16:45:01 -07001256/* Protect the card while the cover is open */
1257static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1258{
1259 if (!mmc_slot(host).get_cover_state)
1260 return;
1261
1262 host->reqs_blocked = 0;
1263 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1264 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301265 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001266 "card is now accessible\n",
1267 mmc_hostname(host->mmc));
1268 host->protect_card = 0;
1269 }
1270 } else {
1271 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301272 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001273 "card is now inaccessible\n",
1274 mmc_hostname(host->mmc));
1275 host->protect_card = 1;
1276 }
1277 }
1278}
1279
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001280/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001281 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001282 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001283static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001284{
NeilBrown7efab4f2011-12-30 12:35:13 +11001285 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001286 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001287 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001288
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001289 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001290
Denis Karpov191d1f12009-09-22 16:44:55 -07001291 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001292 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001293 else {
1294 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001295 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001296 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001297
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001298 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001300 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001301 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001302 return IRQ_HANDLED;
1303}
1304
Russell Kingc5c98922012-04-13 12:14:39 +01001305static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001306{
Russell Kingc5c98922012-04-13 12:14:39 +01001307 struct omap_hsmmc_host *host = param;
1308 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001309 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001310 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001311
Russell Kingc5c98922012-04-13 12:14:39 +01001312 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001313 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001314 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001315 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001316 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001317
Adrian Hunter770d7432011-05-06 12:14:11 +03001318 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001319 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001320 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001321 dma_unmap_sg(chan->device->dev,
1322 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001323 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001324
1325 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001326 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001327 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001328
1329 /* If DMA has finished after TC, complete the request */
1330 if (!req_in_progress) {
1331 struct mmc_request *mrq = host->mrq;
1332
1333 host->mrq = NULL;
1334 mmc_request_done(host->mmc, mrq);
1335 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336}
1337
Per Forlin9782aff2011-07-01 18:55:23 +02001338static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1339 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001340 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001341 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001342{
1343 int dma_len;
1344
1345 if (!next && data->host_cookie &&
1346 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301347 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001348 " host->next_data.cookie %d\n",
1349 __func__, data->host_cookie, host->next_data.cookie);
1350 data->host_cookie = 0;
1351 }
1352
1353 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001354 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001355 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001356 omap_hsmmc_get_dma_dir(host, data));
1357
1358 } else {
1359 dma_len = host->next_data.dma_len;
1360 host->next_data.dma_len = 0;
1361 }
1362
1363
1364 if (dma_len == 0)
1365 return -EINVAL;
1366
1367 if (next) {
1368 next->dma_len = dma_len;
1369 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1370 } else
1371 host->dma_len = dma_len;
1372
1373 return 0;
1374}
1375
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001376/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001377 * Routine to configure and start DMA for the MMC card
1378 */
Balaji T K9d025332014-01-21 19:54:42 +05301379static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001380 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381{
Russell King26b88522012-04-13 12:27:37 +01001382 struct dma_slave_config cfg;
1383 struct dma_async_tx_descriptor *tx;
1384 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001385 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001386 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001387
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001388 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001389 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001390 struct scatterlist *sgl;
1391
1392 sgl = data->sg + i;
1393 if (sgl->length % data->blksz)
1394 return -EINVAL;
1395 }
1396 if ((data->blksz % 4) != 0)
1397 /* REVISIT: The MMC buffer increments only when MSB is written.
1398 * Return error for blksz which is non multiple of four.
1399 */
1400 return -EINVAL;
1401
Adrian Hunterb4175772010-05-26 14:42:06 -07001402 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001403
Russell Kingc5c98922012-04-13 12:14:39 +01001404 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001405
Russell King26b88522012-04-13 12:27:37 +01001406 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1407 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1408 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1409 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1410 cfg.src_maxburst = data->blksz / 4;
1411 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001412
Russell King26b88522012-04-13 12:27:37 +01001413 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001414 if (ret)
1415 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001416
Russell King26b88522012-04-13 12:27:37 +01001417 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1418 if (ret)
1419 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001420
Russell King26b88522012-04-13 12:27:37 +01001421 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1422 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1423 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1424 if (!tx) {
1425 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1426 /* FIXME: cleanup */
1427 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001428 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001429
Russell King26b88522012-04-13 12:27:37 +01001430 tx->callback = omap_hsmmc_dma_callback;
1431 tx->callback_param = host;
1432
1433 /* Does not fail */
1434 dmaengine_submit(tx);
1435
1436 host->dma_ch = 1;
1437
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438 return 0;
1439}
1440
Denis Karpov70a33412009-09-22 16:44:59 -07001441static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001442 unsigned int timeout_ns,
1443 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001444{
1445 unsigned int timeout, cycle_ns;
1446 uint32_t reg, clkd, dto = 0;
1447
1448 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1449 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1450 if (clkd == 0)
1451 clkd = 1;
1452
Balaji T K6e3076c2014-01-21 19:54:42 +05301453 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001454 timeout = timeout_ns / cycle_ns;
1455 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001456 if (timeout) {
1457 while ((timeout & 0x80000000) == 0) {
1458 dto += 1;
1459 timeout <<= 1;
1460 }
1461 dto = 31 - dto;
1462 timeout <<= 1;
1463 if (timeout && dto)
1464 dto += 1;
1465 if (dto >= 13)
1466 dto -= 13;
1467 else
1468 dto = 0;
1469 if (dto > 14)
1470 dto = 14;
1471 }
1472
1473 reg &= ~DTO_MASK;
1474 reg |= dto << DTO_SHIFT;
1475 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1476}
1477
Balaji T K9d025332014-01-21 19:54:42 +05301478static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1479{
1480 struct mmc_request *req = host->mrq;
1481 struct dma_chan *chan;
1482
1483 if (!req->data)
1484 return;
1485 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1486 | (req->data->blocks << 16));
1487 set_data_timeout(host, req->data->timeout_ns,
1488 req->data->timeout_clks);
1489 chan = omap_hsmmc_get_dma_chan(host, req->data);
1490 dma_async_issue_pending(chan);
1491}
1492
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001493/*
1494 * Configure block length for MMC/SD cards and initiate the transfer.
1495 */
1496static int
Denis Karpov70a33412009-09-22 16:44:59 -07001497omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498{
1499 int ret;
1500 host->data = req->data;
1501
1502 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001504 /*
1505 * Set an arbitrary 100ms data timeout for commands with
1506 * busy signal.
1507 */
1508 if (req->cmd->flags & MMC_RSP_BUSY)
1509 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510 return 0;
1511 }
1512
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001513 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301514 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301516 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001517 return ret;
1518 }
1519 }
1520 return 0;
1521}
1522
Per Forlin9782aff2011-07-01 18:55:23 +02001523static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1524 int err)
1525{
1526 struct omap_hsmmc_host *host = mmc_priv(mmc);
1527 struct mmc_data *data = mrq->data;
1528
Russell King26b88522012-04-13 12:27:37 +01001529 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001530 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001531
Russell King26b88522012-04-13 12:27:37 +01001532 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1533 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001534 data->host_cookie = 0;
1535 }
1536}
1537
1538static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1539 bool is_first_req)
1540{
1541 struct omap_hsmmc_host *host = mmc_priv(mmc);
1542
1543 if (mrq->data->host_cookie) {
1544 mrq->data->host_cookie = 0;
1545 return ;
1546 }
1547
Russell Kingc5c98922012-04-13 12:14:39 +01001548 if (host->use_dma) {
1549 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001550
Per Forlin9782aff2011-07-01 18:55:23 +02001551 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001552 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001553 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001554 }
Per Forlin9782aff2011-07-01 18:55:23 +02001555}
1556
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557/*
1558 * Request function. for read/write operation
1559 */
Denis Karpov70a33412009-09-22 16:44:59 -07001560static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561{
Denis Karpov70a33412009-09-22 16:44:59 -07001562 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001563 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001564
Adrian Hunterb4175772010-05-26 14:42:06 -07001565 BUG_ON(host->req_in_progress);
1566 BUG_ON(host->dma_ch != -1);
1567 if (host->protect_card) {
1568 if (host->reqs_blocked < 3) {
1569 /*
1570 * Ensure the controller is left in a consistent
1571 * state by resetting the command and data state
1572 * machines.
1573 */
1574 omap_hsmmc_reset_controller_fsm(host, SRD);
1575 omap_hsmmc_reset_controller_fsm(host, SRC);
1576 host->reqs_blocked += 1;
1577 }
1578 req->cmd->error = -EBADF;
1579 if (req->data)
1580 req->data->error = -EBADF;
1581 req->cmd->retries = 0;
1582 mmc_request_done(mmc, req);
1583 return;
1584 } else if (host->reqs_blocked)
1585 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001586 WARN_ON(host->mrq != NULL);
1587 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301588 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001589 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001590 if (err) {
1591 req->cmd->error = err;
1592 if (req->data)
1593 req->data->error = err;
1594 host->mrq = NULL;
1595 mmc_request_done(mmc, req);
1596 return;
1597 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301598 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301599 omap_hsmmc_start_command(host, req->sbc, NULL);
1600 return;
1601 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001602
Balaji T K9d025332014-01-21 19:54:42 +05301603 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001604 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605}
1606
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001608static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609{
Denis Karpov70a33412009-09-22 16:44:59 -07001610 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001611 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301613 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001614
Adrian Huntera3621462009-09-22 16:44:42 -07001615 if (ios->power_mode != host->power_mode) {
1616 switch (ios->power_mode) {
1617 case MMC_POWER_OFF:
1618 mmc_slot(host).set_power(host->dev, host->slot_id,
1619 0, 0);
1620 break;
1621 case MMC_POWER_UP:
1622 mmc_slot(host).set_power(host->dev, host->slot_id,
1623 1, ios->vdd);
1624 break;
1625 case MMC_POWER_ON:
1626 do_send_init_stream = 1;
1627 break;
1628 }
1629 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001630 }
1631
Denis Karpovdd498ef2009-09-22 16:44:49 -07001632 /* FIXME: set registers based only on changes to ios */
1633
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001634 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001635
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301636 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001637 /* Only MMC1 can interface at 3V without some flavor
1638 * of external transceiver; but they all handle 1.8V.
1639 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001640 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301641 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001642 /*
1643 * The mmc_select_voltage fn of the core does
1644 * not seem to set the power_mode to
1645 * MMC_POWER_UP upon recalculating the voltage.
1646 * vdd 1.8v.
1647 */
Denis Karpov70a33412009-09-22 16:44:59 -07001648 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1649 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001650 "Switch operation failed\n");
1651 }
1652 }
1653
Andy Shevchenko5934df22011-05-06 12:14:06 +03001654 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001655
Adrian Huntera3621462009-09-22 16:44:42 -07001656 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001657 send_init_stream(host);
1658
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001659 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001660
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301661 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001662}
1663
1664static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1665{
Denis Karpov70a33412009-09-22 16:44:59 -07001666 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001667
Denis Karpov191d1f12009-09-22 16:44:55 -07001668 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001669 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001670 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001671}
1672
1673static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1674{
Denis Karpov70a33412009-09-22 16:44:59 -07001675 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001676
Denis Karpov191d1f12009-09-22 16:44:55 -07001677 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001678 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001679 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001680}
1681
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001682static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1683{
1684 struct omap_hsmmc_host *host = mmc_priv(mmc);
1685
1686 if (mmc_slot(host).init_card)
1687 mmc_slot(host).init_card(card);
1688}
1689
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001690static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1691{
1692 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001693 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001694 unsigned long flags;
1695
1696 spin_lock_irqsave(&host->irq_lock, flags);
1697
Balaji T K5a52b082014-05-29 10:28:02 +02001698 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001699 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1700 if (enable) {
1701 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1702 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001703 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001704 } else {
1705 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1706 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001707 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001708 }
Balaji T K5a52b082014-05-29 10:28:02 +02001709 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001710 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1711
1712 /*
1713 * if enable, piggy back detection on current request
1714 * but always disable immediately
1715 */
1716 if (!host->req_in_progress || !enable)
1717 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1718
1719 /* flush posted write */
1720 OMAP_HSMMC_READ(host->base, IE);
1721
1722 spin_unlock_irqrestore(&host->irq_lock, flags);
1723}
1724
1725static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1726{
1727 struct mmc_host *mmc = host->mmc;
1728 int ret;
1729
1730 /*
1731 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1732 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1733 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1734 * with functional clock disabled.
1735 */
1736 if (!host->dev->of_node || !host->wake_irq)
1737 return -ENODEV;
1738
1739 /* Prevent auto-enabling of IRQ */
1740 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1741 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1742 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1743 mmc_hostname(mmc), host);
1744 if (ret) {
1745 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1746 goto err;
1747 }
1748
1749 /*
1750 * Some omaps don't have wake-up path from deeper idle states
1751 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1752 */
1753 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
1754 ret = -ENODEV;
1755 devm_free_irq(host->dev, host->wake_irq, host);
1756 goto err;
1757 }
1758
Balaji T K5a52b082014-05-29 10:28:02 +02001759 OMAP_HSMMC_WRITE(host->base, HCTL,
1760 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001761 return 0;
1762
1763err:
1764 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1765 host->wake_irq = 0;
1766 return ret;
1767}
1768
Denis Karpov70a33412009-09-22 16:44:59 -07001769static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001770{
1771 u32 hctl, capa, value;
1772
1773 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301774 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001775 hctl = SDVS30;
1776 capa = VS30 | VS18;
1777 } else {
1778 hctl = SDVS18;
1779 capa = VS18;
1780 }
1781
1782 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1783 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1784
1785 value = OMAP_HSMMC_READ(host->base, CAPA);
1786 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1787
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001788 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001789 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001790}
1791
Denis Karpov70a33412009-09-22 16:44:59 -07001792static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001793{
Denis Karpov70a33412009-09-22 16:44:59 -07001794 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001795
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301796 pm_runtime_get_sync(host->dev);
1797
Denis Karpovdd498ef2009-09-22 16:44:49 -07001798 return 0;
1799}
1800
Adrian Hunter907d2e72012-02-29 09:17:21 +02001801static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001802{
Denis Karpov70a33412009-09-22 16:44:59 -07001803 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001804
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301805 pm_runtime_mark_last_busy(host->dev);
1806 pm_runtime_put_autosuspend(host->dev);
1807
Denis Karpovdd498ef2009-09-22 16:44:49 -07001808 return 0;
1809}
1810
Denis Karpov70a33412009-09-22 16:44:59 -07001811static const struct mmc_host_ops omap_hsmmc_ops = {
1812 .enable = omap_hsmmc_enable_fclk,
1813 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001814 .post_req = omap_hsmmc_post_req,
1815 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001816 .request = omap_hsmmc_request,
1817 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001818 .get_cd = omap_hsmmc_get_cd,
1819 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001820 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001821 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001822};
1823
Denis Karpovd900f712009-09-22 16:44:38 -07001824#ifdef CONFIG_DEBUG_FS
1825
Denis Karpov70a33412009-09-22 16:44:59 -07001826static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001827{
1828 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001829 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001830
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001831 seq_printf(s, "mmc%d:\n", mmc->index);
1832 seq_printf(s, "sdio irq mode\t%s\n",
1833 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1834
1835 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1836 seq_printf(s, "sdio irq \t%s\n",
1837 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1838 : "disabled");
1839 }
1840 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001841
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301842 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001843 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001844 seq_printf(s, "CON:\t\t0x%08x\n",
1845 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001846 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1847 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001848 seq_printf(s, "HCTL:\t\t0x%08x\n",
1849 OMAP_HSMMC_READ(host->base, HCTL));
1850 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1851 OMAP_HSMMC_READ(host->base, SYSCTL));
1852 seq_printf(s, "IE:\t\t0x%08x\n",
1853 OMAP_HSMMC_READ(host->base, IE));
1854 seq_printf(s, "ISE:\t\t0x%08x\n",
1855 OMAP_HSMMC_READ(host->base, ISE));
1856 seq_printf(s, "CAPA:\t\t0x%08x\n",
1857 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001858
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301859 pm_runtime_mark_last_busy(host->dev);
1860 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001861
Denis Karpovd900f712009-09-22 16:44:38 -07001862 return 0;
1863}
1864
Denis Karpov70a33412009-09-22 16:44:59 -07001865static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001866{
Denis Karpov70a33412009-09-22 16:44:59 -07001867 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001868}
1869
1870static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001871 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001872 .read = seq_read,
1873 .llseek = seq_lseek,
1874 .release = single_release,
1875};
1876
Denis Karpov70a33412009-09-22 16:44:59 -07001877static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001878{
1879 if (mmc->debugfs_root)
1880 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1881 mmc, &mmc_regs_fops);
1882}
1883
1884#else
1885
Denis Karpov70a33412009-09-22 16:44:59 -07001886static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001887{
1888}
1889
1890#endif
1891
Rajendra Nayak46856a62012-03-12 20:32:37 +05301892#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001893static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1894 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1895 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1896};
1897
1898static const struct omap_mmc_of_data omap4_mmc_of_data = {
1899 .reg_offset = 0x100,
1900};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001901static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1902 .reg_offset = 0x100,
1903 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1904};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301905
1906static const struct of_device_id omap_mmc_of_match[] = {
1907 {
1908 .compatible = "ti,omap2-hsmmc",
1909 },
1910 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001911 .compatible = "ti,omap3-pre-es3-hsmmc",
1912 .data = &omap3_pre_es3_mmc_of_data,
1913 },
1914 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301915 .compatible = "ti,omap3-hsmmc",
1916 },
1917 {
1918 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001919 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301920 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001921 {
1922 .compatible = "ti,am33xx-hsmmc",
1923 .data = &am33xx_mmc_of_data,
1924 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301925 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001926};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301927MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1928
1929static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1930{
1931 struct omap_mmc_platform_data *pdata;
1932 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301933 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001934 int cd_gpio, wp_gpio;
1935
1936 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1937 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1938 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1939 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301940
1941 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1942 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301943 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301944
1945 if (of_find_property(np, "ti,dual-volt", NULL))
1946 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1947
1948 /* This driver only supports 1 slot */
1949 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001950 pdata->slots[0].switch_pin = cd_gpio;
1951 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301952
1953 if (of_find_property(np, "ti,non-removable", NULL)) {
1954 pdata->slots[0].nonremovable = true;
1955 pdata->slots[0].no_regulator_off_init = true;
1956 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001957 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301958 if (bus_width == 4)
1959 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1960 else if (bus_width == 8)
1961 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1962
1963 if (of_find_property(np, "ti,needs-special-reset", NULL))
1964 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1965
Daniel Mackd8714e82012-10-15 21:35:06 +05301966 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1967 pdata->max_freq = max_freq;
1968
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301969 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1970 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1971
Daniel Mackc9ae64d2014-02-17 12:36:33 +01001972 if (of_find_property(np, "keep-power-in-suspend", NULL))
1973 pdata->slots[0].pm_caps |= MMC_PM_KEEP_POWER;
1974
1975 if (of_find_property(np, "enable-sdio-wakeup", NULL))
1976 pdata->slots[0].pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
1977
Rajendra Nayak46856a62012-03-12 20:32:37 +05301978 return pdata;
1979}
1980#else
1981static inline struct omap_mmc_platform_data
1982 *of_get_hsmmc_pdata(struct device *dev)
1983{
Balaji T K19df45b2014-02-28 19:08:18 +05301984 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301985}
1986#endif
1987
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001988static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001989{
1990 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1991 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001992 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001993 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001994 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301995 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001996 dma_cap_mask_t mask;
1997 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301998 struct pinctrl *pinctrl;
Nishanth Menon59445b12014-02-13 23:45:48 -06001999 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302000 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302001
2002 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2003 if (match) {
2004 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002005
2006 if (IS_ERR(pdata))
2007 return PTR_ERR(pdata);
2008
Rajendra Nayak46856a62012-03-12 20:32:37 +05302009 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002010 data = match->data;
2011 pdata->reg_offset = data->reg_offset;
2012 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302013 }
2014 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002015
2016 if (pdata == NULL) {
2017 dev_err(&pdev->dev, "Platform Data is missing\n");
2018 return -ENXIO;
2019 }
2020
2021 if (pdata->nr_slots == 0) {
2022 dev_err(&pdev->dev, "No Slots\n");
2023 return -ENXIO;
2024 }
2025
2026 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2027 irq = platform_get_irq(pdev, 0);
2028 if (res == NULL || irq < 0)
2029 return -ENXIO;
2030
Balaji T K77fae212014-05-09 22:16:51 +05302031 base = devm_ioremap_resource(&pdev->dev, res);
2032 if (IS_ERR(base))
2033 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002034
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002035 ret = omap_hsmmc_gpio_init(pdata);
2036 if (ret)
2037 goto err;
2038
Denis Karpov70a33412009-09-22 16:44:59 -07002039 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040 if (!mmc) {
2041 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002042 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002043 }
2044
2045 host = mmc_priv(mmc);
2046 host->mmc = mmc;
2047 host->pdata = pdata;
2048 host->dev = &pdev->dev;
2049 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002050 host->dma_ch = -1;
2051 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05302053 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302054 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002055 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002056 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302057 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002058
2059 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002061 if (pdev->dev.of_node)
2062 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2063
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302064 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002065
Daniel Mackd418ed82012-02-19 13:20:33 +01002066 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2067
2068 if (pdata->max_freq > 0)
2069 mmc->f_max = pdata->max_freq;
2070 else
2071 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002072
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002073 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074
Balaji T K96181952014-05-09 22:16:48 +05302075 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002076 if (IS_ERR(host->fclk)) {
2077 ret = PTR_ERR(host->fclk);
2078 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002079 goto err1;
2080 }
2081
Paul Walmsley9b682562011-10-06 14:50:35 -06002082 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2083 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
2084 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
2085 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002086
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302087 pm_runtime_enable(host->dev);
2088 pm_runtime_get_sync(host->dev);
2089 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2090 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091
Balaji T K92a3aeb2012-02-24 21:14:34 +05302092 omap_hsmmc_context_save(host);
2093
Balaji T K96181952014-05-09 22:16:48 +05302094 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302095 /*
2096 * MMC can still work without debounce clock.
2097 */
2098 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302099 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302100 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302101 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302102 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002103 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002105 /* Since we do only SG emulation, we can have as many segs
2106 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002107 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002108
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002109 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2110 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2111 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2112 mmc->max_seg_size = mmc->max_req_size;
2113
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002114 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002115 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002117 mmc->caps |= mmc_slot(host).caps;
2118 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119 mmc->caps |= MMC_CAP_4_BIT_DATA;
2120
Denis Karpov191d1f12009-09-22 16:44:55 -07002121 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002122 mmc->caps |= MMC_CAP_NONREMOVABLE;
2123
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002124 mmc->pm_caps = mmc_slot(host).pm_caps;
2125
Denis Karpov70a33412009-09-22 16:44:59 -07002126 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002127
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302128 if (!pdev->dev.of_node) {
2129 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2130 if (!res) {
2131 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2132 ret = -ENXIO;
2133 goto err_irq;
2134 }
2135 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002136
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302137 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2138 if (!res) {
2139 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2140 ret = -ENXIO;
2141 goto err_irq;
2142 }
2143 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002144 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002145
Russell King26b88522012-04-13 12:27:37 +01002146 dma_cap_zero(mask);
2147 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002148
Matt Porterd272fbf2013-05-10 17:42:34 +05302149 host->rx_chan =
2150 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2151 &rx_req, &pdev->dev, "rx");
2152
Russell King26b88522012-04-13 12:27:37 +01002153 if (!host->rx_chan) {
2154 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002155 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002156 goto err_irq;
2157 }
2158
Matt Porterd272fbf2013-05-10 17:42:34 +05302159 host->tx_chan =
2160 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2161 &tx_req, &pdev->dev, "tx");
2162
Russell King26b88522012-04-13 12:27:37 +01002163 if (!host->tx_chan) {
2164 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002165 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002166 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002167 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002168
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002169 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302170 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171 mmc_hostname(mmc), host);
2172 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302173 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002174 goto err_irq;
2175 }
2176
2177 if (pdata->init != NULL) {
2178 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302179 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07002180 "Unable to configure MMC IRQs\n");
Balaji T Ke1538ed2014-05-09 22:16:49 +05302181 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182 }
2183 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002184
Adrian Hunterb702b102010-02-15 10:03:35 -08002185 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002186 ret = omap_hsmmc_reg_get(host);
2187 if (ret)
2188 goto err_reg;
2189 host->use_reg = 1;
2190 }
2191
David Brownellb583f262009-05-28 14:04:03 -07002192 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002193
2194 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002195 if ((mmc_slot(host).card_detect_irq)) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302196 ret = devm_request_threaded_irq(&pdev->dev,
2197 mmc_slot(host).card_detect_irq,
2198 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002199 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002200 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002201 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302202 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002203 "Unable to grab MMC CD IRQ\n");
2204 goto err_irq_cd;
2205 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002206 pdata->suspend = omap_hsmmc_suspend_cdirq;
2207 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 }
2209
Adrian Hunterb4175772010-05-26 14:42:06 -07002210 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211
Daniel Mack46b76032012-10-15 21:35:05 +05302212 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2213 if (IS_ERR(pinctrl))
2214 dev_warn(&pdev->dev,
2215 "pins are not configured from the driver\n");
2216
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002217 /*
2218 * For now, only support SDIO interrupt if we have a separate
2219 * wake-up interrupt configured from device tree. This is because
2220 * the wake-up interrupt is needed for idle state and some
2221 * platforms need special quirks. And we don't want to add new
2222 * legacy mux platform init code callbacks any longer as we
2223 * are moving to DT based booting anyways.
2224 */
2225 ret = omap_hsmmc_configure_wake_irq(host);
2226 if (!ret)
2227 mmc->caps |= MMC_CAP_SDIO_IRQ;
2228
Adrian Hunterb62f6222009-09-22 16:45:01 -07002229 omap_hsmmc_protect_card(host);
2230
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 mmc_add_host(mmc);
2232
Denis Karpov191d1f12009-09-22 16:44:55 -07002233 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2235 if (ret < 0)
2236 goto err_slot_name;
2237 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002238 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002239 ret = device_create_file(&mmc->class_dev,
2240 &dev_attr_cover_switch);
2241 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002242 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002243 }
2244
Denis Karpov70a33412009-09-22 16:44:59 -07002245 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302246 pm_runtime_mark_last_busy(host->dev);
2247 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002248
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249 return 0;
2250
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002251err_slot_name:
2252 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002253err_irq_cd:
2254 if (host->use_reg)
2255 omap_hsmmc_reg_put(host);
2256err_reg:
2257 if (host->pdata->cleanup)
2258 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002259err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002260 if (host->tx_chan)
2261 dma_release_channel(host->tx_chan);
2262 if (host->rx_chan)
2263 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302264 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002265 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302266 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302267 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002268err1:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002269 mmc_free_host(mmc);
2270err_alloc:
2271 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002272err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273 return ret;
2274}
2275
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002276static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002277{
Denis Karpov70a33412009-09-22 16:44:59 -07002278 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002279
Felipe Balbi927ce942012-03-14 11:18:27 +02002280 pm_runtime_get_sync(host->dev);
2281 mmc_remove_host(host->mmc);
2282 if (host->use_reg)
2283 omap_hsmmc_reg_put(host);
2284 if (host->pdata->cleanup)
2285 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002286
Russell Kingc5c98922012-04-13 12:14:39 +01002287 if (host->tx_chan)
2288 dma_release_channel(host->tx_chan);
2289 if (host->rx_chan)
2290 dma_release_channel(host->rx_chan);
2291
Felipe Balbi927ce942012-03-14 11:18:27 +02002292 pm_runtime_put_sync(host->dev);
2293 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302294 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302295 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296
Balaji T K9ea28ec2012-10-15 21:35:08 +05302297 omap_hsmmc_gpio_free(host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302298 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002299
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002300 return 0;
2301}
2302
2303#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302304static int omap_hsmmc_prepare(struct device *dev)
2305{
2306 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2307
2308 if (host->pdata->suspend)
2309 return host->pdata->suspend(dev, host->slot_id);
2310
2311 return 0;
2312}
2313
2314static void omap_hsmmc_complete(struct device *dev)
2315{
2316 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2317
2318 if (host->pdata->resume)
2319 host->pdata->resume(dev, host->slot_id);
2320
2321}
2322
Kevin Hilmana791daa2010-05-26 14:42:07 -07002323static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002324{
Felipe Balbi927ce942012-03-14 11:18:27 +02002325 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2326
2327 if (!host)
2328 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002329
Felipe Balbi927ce942012-03-14 11:18:27 +02002330 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002331
2332 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002333 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2334 OMAP_HSMMC_WRITE(host->base, IE, 0);
2335 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002336 OMAP_HSMMC_WRITE(host->base, HCTL,
2337 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2338 }
2339
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002340 /* do not wake up due to sdio irq */
2341 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2342 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2343 disable_irq(host->wake_irq);
2344
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302345 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302346 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002347
Eliad Peller31f9d462011-11-22 16:02:17 +02002348 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002349 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002350}
2351
2352/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002353static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002354{
Felipe Balbi927ce942012-03-14 11:18:27 +02002355 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2356
2357 if (!host)
2358 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002359
Felipe Balbi927ce942012-03-14 11:18:27 +02002360 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002361
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302362 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302363 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002364
Felipe Balbi927ce942012-03-14 11:18:27 +02002365 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2366 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002367
Felipe Balbi927ce942012-03-14 11:18:27 +02002368 omap_hsmmc_protect_card(host);
2369
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002370 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2371 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2372 enable_irq(host->wake_irq);
2373
Felipe Balbi927ce942012-03-14 11:18:27 +02002374 pm_runtime_mark_last_busy(host->dev);
2375 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002376 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002377}
2378
2379#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302380#define omap_hsmmc_prepare NULL
2381#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002382#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302383#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002384#endif
2385
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302386static int omap_hsmmc_runtime_suspend(struct device *dev)
2387{
2388 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002389 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302390
2391 host = platform_get_drvdata(to_platform_device(dev));
2392 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002393 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302394
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002395 spin_lock_irqsave(&host->irq_lock, flags);
2396 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2397 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2398 /* disable sdio irq handling to prevent race */
2399 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2400 OMAP_HSMMC_WRITE(host->base, IE, 0);
2401 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2402
2403 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2404 enable_irq(host->wake_irq);
2405 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
2406 }
2407 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302408 return 0;
2409}
2410
2411static int omap_hsmmc_runtime_resume(struct device *dev)
2412{
2413 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002414 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302415
2416 host = platform_get_drvdata(to_platform_device(dev));
2417 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002418 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302419
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002420 spin_lock_irqsave(&host->irq_lock, flags);
2421 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2422 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2423 /* sdio irq flag can't change while in runtime suspend */
2424 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2425 disable_irq_nosync(host->wake_irq);
2426 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2427 }
2428
2429 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2430 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2431 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2432 }
2433 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302434 return 0;
2435}
2436
Kevin Hilmana791daa2010-05-26 14:42:07 -07002437static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002438 .suspend = omap_hsmmc_suspend,
2439 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302440 .prepare = omap_hsmmc_prepare,
2441 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302442 .runtime_suspend = omap_hsmmc_runtime_suspend,
2443 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002444};
2445
2446static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002447 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002448 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002449 .driver = {
2450 .name = DRIVER_NAME,
2451 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002452 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302453 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002454 },
2455};
2456
Felipe Balbib7964502012-03-14 11:18:32 +02002457module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002458MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2459MODULE_LICENSE("GPL");
2460MODULE_ALIAS("platform:" DRIVER_NAME);
2461MODULE_AUTHOR("Texas Instruments Inc");