blob: 2b2c98773f152221a2aa299e3ae1d9ac0a7a7821 [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 Shevchenkoac330f42011-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>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010024#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010028#include <linux/timer.h>
29#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010033#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070034#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070035#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/io.h>
37#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080038#include <linux/gpio.h>
39#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053040#include <linux/pm_runtime.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/board.h>
43#include <plat/mmc.h>
44#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045
46/* OMAP HSMMC Host Controller Registers */
47#define OMAP_HSMMC_SYSCONFIG 0x0010
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
50#define OMAP_HSMMC_BLK 0x0104
51#define OMAP_HSMMC_ARG 0x0108
52#define OMAP_HSMMC_CMD 0x010C
53#define OMAP_HSMMC_RSP10 0x0110
54#define OMAP_HSMMC_RSP32 0x0114
55#define OMAP_HSMMC_RSP54 0x0118
56#define OMAP_HSMMC_RSP76 0x011C
57#define OMAP_HSMMC_DATA 0x0120
58#define OMAP_HSMMC_HCTL 0x0128
59#define OMAP_HSMMC_SYSCTL 0x012C
60#define OMAP_HSMMC_STAT 0x0130
61#define OMAP_HSMMC_IE 0x0134
62#define OMAP_HSMMC_ISE 0x0138
63#define OMAP_HSMMC_CAPA 0x0140
64
65#define VS18 (1 << 26)
66#define VS30 (1 << 25)
67#define SDVS18 (0x5 << 9)
68#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080069#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010070#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVSCLR 0xFFFFF1FF
72#define SDVSDET 0x00000400
73#define AUTOIDLE 0x1
74#define SDBP (1 << 8)
75#define DTO 0xe
76#define ICE 0x1
77#define ICS 0x2
78#define CEN (1 << 2)
79#define CLKD_MASK 0x0000FFC0
80#define CLKD_SHIFT 6
81#define DTO_MASK 0x000F0000
82#define DTO_SHIFT 16
83#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070084#define BWR_ENABLE (1 << 4)
85#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070086#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define INIT_STREAM (1 << 1)
88#define DP_SELECT (1 << 21)
89#define DDIR (1 << 4)
90#define DMA_EN 0x1
91#define MSBS (1 << 5)
92#define BCE (1 << 1)
93#define FOUR_BIT (1 << 1)
Balaji T K03b5d922012-04-09 12:08:33 +053094#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020095#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010096#define CC 0x1
97#define TC 0x02
98#define OD 0x1
99#define ERR (1 << 15)
100#define CMD_TIMEOUT (1 << 16)
101#define DATA_TIMEOUT (1 << 20)
102#define CMD_CRC (1 << 17)
103#define DATA_CRC (1 << 21)
104#define CARD_ERR (1 << 28)
105#define STAT_CLEAR 0xFFFFFFFF
106#define INIT_STREAM_CMD 0x00000000
107#define DUAL_VOLT_OCR_BIT 7
108#define SRC (1 << 25)
109#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700110#define SOFTRESET (1 << 1)
111#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100112
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530113#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100114#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400115#define OMAP_MMC_MIN_CLOCK 400000
116#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530117#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100118
119/*
120 * One controller can have multiple slots, like on some omap boards using
121 * omap.c controller driver. Luckily this is not currently done on any known
122 * omap_hsmmc.c device.
123 */
124#define mmc_slot(host) (host->pdata->slots[host->slot_id])
125
126/*
127 * MMC Host controller read/write API's
128 */
129#define OMAP_HSMMC_READ(base, reg) \
130 __raw_readl((base) + OMAP_HSMMC_##reg)
131
132#define OMAP_HSMMC_WRITE(base, reg, val) \
133 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
134
Per Forlin9782aff2011-07-01 18:55:23 +0200135struct omap_hsmmc_next {
136 unsigned int dma_len;
137 s32 cookie;
138};
139
Denis Karpov70a33412009-09-22 16:44:59 -0700140struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100141 struct device *dev;
142 struct mmc_host *mmc;
143 struct mmc_request *mrq;
144 struct mmc_command *cmd;
145 struct mmc_data *data;
146 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100147 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800148 /*
149 * vcc == configured supply
150 * vcc_aux == optional
151 * - MMC1, supply for DAT4..DAT7
152 * - MMC2/MMC2, external level shifter voltage supply, for
153 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
154 */
155 struct regulator *vcc;
156 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100157 void __iomem *base;
158 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700159 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200161 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700163 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 u32 *buffer;
165 u32 bytesleft;
166 int suspended;
167 int irq;
Russell King26b88522012-04-13 12:27:37 +0100168 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100169 struct dma_chan *tx_chan;
170 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200172 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700173 int context_loss;
Adrian Hunter623821f2009-09-22 16:44:51 -0700174 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700175 int protect_card;
176 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800177 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700178 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200179 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700180
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100181 struct omap_mmc_platform_data *pdata;
182};
183
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800184static int omap_hsmmc_card_detect(struct device *dev, int slot)
185{
186 struct omap_mmc_platform_data *mmc = dev->platform_data;
187
188 /* NOTE: assumes card detect signal is active-low */
189 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
190}
191
192static int omap_hsmmc_get_wp(struct device *dev, int slot)
193{
194 struct omap_mmc_platform_data *mmc = dev->platform_data;
195
196 /* NOTE: assumes write protect signal is active-high */
197 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
198}
199
200static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
201{
202 struct omap_mmc_platform_data *mmc = dev->platform_data;
203
204 /* NOTE: assumes card detect signal is active-low */
205 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
206}
207
208#ifdef CONFIG_PM
209
210static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
211{
212 struct omap_mmc_platform_data *mmc = dev->platform_data;
213
214 disable_irq(mmc->slots[0].card_detect_irq);
215 return 0;
216}
217
218static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
219{
220 struct omap_mmc_platform_data *mmc = dev->platform_data;
221
222 enable_irq(mmc->slots[0].card_detect_irq);
223 return 0;
224}
225
226#else
227
228#define omap_hsmmc_suspend_cdirq NULL
229#define omap_hsmmc_resume_cdirq NULL
230
231#endif
232
Adrian Hunterb702b102010-02-15 10:03:35 -0800233#ifdef CONFIG_REGULATOR
234
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500235static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800236 int vdd)
237{
238 struct omap_hsmmc_host *host =
239 platform_get_drvdata(to_platform_device(dev));
240 int ret = 0;
241
242 /*
243 * If we don't see a Vcc regulator, assume it's a fixed
244 * voltage always-on regulator.
245 */
246 if (!host->vcc)
247 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530248 /*
249 * With DT, never turn OFF the regulator. This is because
250 * the pbias cell programming support is still missing when
251 * booting with Device tree
252 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530253 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530254 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800255
256 if (mmc_slot(host).before_set_reg)
257 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
258
259 /*
260 * Assume Vcc regulator is used only to power the card ... OMAP
261 * VDDS is used to power the pins, optionally with a transceiver to
262 * support cards using voltages other than VDDS (1.8V nominal). When a
263 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
264 *
265 * In some cases this regulator won't support enable/disable;
266 * e.g. it's a fixed rail for a WLAN chip.
267 *
268 * In other cases vcc_aux switches interface power. Example, for
269 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
270 * chips/cards need an interface voltage rail too.
271 */
272 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400273 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800274 /* Enable interface voltage rail, if needed */
275 if (ret == 0 && host->vcc_aux) {
276 ret = regulator_enable(host->vcc_aux);
277 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400278 ret = mmc_regulator_set_ocr(host->mmc,
279 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800280 }
281 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400282 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800283 if (host->vcc_aux)
284 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400285 if (!ret) {
286 /* Then proceed to shut down the local regulator */
287 ret = mmc_regulator_set_ocr(host->mmc,
288 host->vcc, 0);
289 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800290 }
291
292 if (mmc_slot(host).after_set_reg)
293 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
294
295 return ret;
296}
297
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800298static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
299{
300 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700301 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800302
Rajendra Nayak1cb9af42012-03-07 09:55:31 -0500303 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800304
305 reg = regulator_get(host->dev, "vmmc");
306 if (IS_ERR(reg)) {
307 dev_dbg(host->dev, "vmmc regulator missing\n");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800308 } else {
309 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700310 ocr_value = mmc_regulator_get_ocrmask(reg);
311 if (!mmc_slot(host).ocr_mask) {
312 mmc_slot(host).ocr_mask = ocr_value;
313 } else {
314 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530315 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500316 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700317 mmc_slot(host).ocr_mask = 0;
318 return -EINVAL;
319 }
320 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800321
322 /* Allow an aux regulator */
323 reg = regulator_get(host->dev, "vmmc_aux");
324 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
325
Balaji T Kb1c1df72011-05-30 19:55:34 +0530326 /* For eMMC do not power off when not in sleep state */
327 if (mmc_slot(host).no_regulator_off_init)
328 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800329 /*
330 * UGLY HACK: workaround regulator framework bugs.
331 * When the bootloader leaves a supply active, it's
332 * initialized with zero usecount ... and we can't
333 * disable it without first enabling it. Until the
334 * framework is fixed, we need a workaround like this
335 * (which is safe for MMC, but not in general).
336 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300337 if (regulator_is_enabled(host->vcc) > 0 ||
338 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
339 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
340
341 mmc_slot(host).set_power(host->dev, host->slot_id,
342 1, vdd);
343 mmc_slot(host).set_power(host->dev, host->slot_id,
344 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800345 }
346 }
347
348 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800349}
350
351static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
352{
353 regulator_put(host->vcc);
354 regulator_put(host->vcc_aux);
355 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800356}
357
Adrian Hunterb702b102010-02-15 10:03:35 -0800358static inline int omap_hsmmc_have_reg(void)
359{
360 return 1;
361}
362
363#else
364
365static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
366{
367 return -EINVAL;
368}
369
370static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
371{
372}
373
374static inline int omap_hsmmc_have_reg(void)
375{
376 return 0;
377}
378
379#endif
380
381static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
382{
383 int ret;
384
385 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800386 if (pdata->slots[0].cover)
387 pdata->slots[0].get_cover_state =
388 omap_hsmmc_get_cover_state;
389 else
390 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
391 pdata->slots[0].card_detect_irq =
392 gpio_to_irq(pdata->slots[0].switch_pin);
393 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
394 if (ret)
395 return ret;
396 ret = gpio_direction_input(pdata->slots[0].switch_pin);
397 if (ret)
398 goto err_free_sp;
399 } else
400 pdata->slots[0].switch_pin = -EINVAL;
401
402 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
403 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
404 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
405 if (ret)
406 goto err_free_cd;
407 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
408 if (ret)
409 goto err_free_wp;
410 } else
411 pdata->slots[0].gpio_wp = -EINVAL;
412
413 return 0;
414
415err_free_wp:
416 gpio_free(pdata->slots[0].gpio_wp);
417err_free_cd:
418 if (gpio_is_valid(pdata->slots[0].switch_pin))
419err_free_sp:
420 gpio_free(pdata->slots[0].switch_pin);
421 return ret;
422}
423
424static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
425{
426 if (gpio_is_valid(pdata->slots[0].gpio_wp))
427 gpio_free(pdata->slots[0].gpio_wp);
428 if (gpio_is_valid(pdata->slots[0].switch_pin))
429 gpio_free(pdata->slots[0].switch_pin);
430}
431
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100432/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300433 * Start clock to the card
434 */
435static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
436{
437 OMAP_HSMMC_WRITE(host->base, SYSCTL,
438 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
439}
440
441/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100442 * Stop clock to the card
443 */
Denis Karpov70a33412009-09-22 16:44:59 -0700444static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100445{
446 OMAP_HSMMC_WRITE(host->base, SYSCTL,
447 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
448 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
449 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
450}
451
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700452static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
453 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700454{
455 unsigned int irq_mask;
456
457 if (host->use_dma)
458 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
459 else
460 irq_mask = INT_EN_MASK;
461
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700462 /* Disable timeout for erases */
463 if (cmd->opcode == MMC_ERASE)
464 irq_mask &= ~DTO_ENABLE;
465
Adrian Hunterb4175772010-05-26 14:42:06 -0700466 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
467 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
468 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
469}
470
471static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
472{
473 OMAP_HSMMC_WRITE(host->base, ISE, 0);
474 OMAP_HSMMC_WRITE(host->base, IE, 0);
475 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
476}
477
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300478/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530479static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300480{
481 u16 dsor = 0;
482
483 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530484 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300485 if (dsor > 250)
486 dsor = 250;
487 }
488
489 return dsor;
490}
491
Andy Shevchenko5934df22011-05-06 12:14:06 +0300492static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
493{
494 struct mmc_ios *ios = &host->mmc->ios;
495 unsigned long regval;
496 unsigned long timeout;
497
498 dev_dbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
499
500 omap_hsmmc_stop_clock(host);
501
502 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
503 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530504 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300505 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
506 OMAP_HSMMC_WRITE(host->base, SYSCTL,
507 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
508
509 /* Wait till the ICS bit is set */
510 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
511 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
512 && time_before(jiffies, timeout))
513 cpu_relax();
514
515 omap_hsmmc_start_clock(host);
516}
517
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400518static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
519{
520 struct mmc_ios *ios = &host->mmc->ios;
521 u32 con;
522
523 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530524 if (ios->timing == MMC_TIMING_UHS_DDR50)
525 con |= DDR; /* configure in DDR mode */
526 else
527 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400528 switch (ios->bus_width) {
529 case MMC_BUS_WIDTH_8:
530 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
531 break;
532 case MMC_BUS_WIDTH_4:
533 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
534 OMAP_HSMMC_WRITE(host->base, HCTL,
535 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
536 break;
537 case MMC_BUS_WIDTH_1:
538 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
539 OMAP_HSMMC_WRITE(host->base, HCTL,
540 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
541 break;
542 }
543}
544
545static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
546{
547 struct mmc_ios *ios = &host->mmc->ios;
548 u32 con;
549
550 con = OMAP_HSMMC_READ(host->base, CON);
551 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
552 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
553 else
554 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
555}
556
Denis Karpov11dd62a2009-09-22 16:44:43 -0700557#ifdef CONFIG_PM
558
559/*
560 * Restore the MMC host context, if it was lost as result of a
561 * power state change.
562 */
Denis Karpov70a33412009-09-22 16:44:59 -0700563static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700564{
565 struct mmc_ios *ios = &host->mmc->ios;
566 struct omap_mmc_platform_data *pdata = host->pdata;
567 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400568 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700569 unsigned long timeout;
570
571 if (pdata->get_context_loss_count) {
572 context_loss = pdata->get_context_loss_count(host->dev);
573 if (context_loss < 0)
574 return 1;
575 }
576
577 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
578 context_loss == host->context_loss ? "not " : "");
579 if (host->context_loss == context_loss)
580 return 1;
581
582 /* Wait for hardware reset */
583 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
584 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
585 && time_before(jiffies, timeout))
586 ;
587
588 /* Do software reset */
589 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
590 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
591 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
592 && time_before(jiffies, timeout))
593 ;
594
595 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
596 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
597
Balaji T Kc2200ef2012-03-07 09:55:30 -0500598 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700599 if (host->power_mode != MMC_POWER_OFF &&
600 (1 << ios->vdd) <= MMC_VDD_23_24)
601 hctl = SDVS18;
602 else
603 hctl = SDVS30;
604 capa = VS30 | VS18;
605 } else {
606 hctl = SDVS18;
607 capa = VS18;
608 }
609
610 OMAP_HSMMC_WRITE(host->base, HCTL,
611 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
612
613 OMAP_HSMMC_WRITE(host->base, CAPA,
614 OMAP_HSMMC_READ(host->base, CAPA) | capa);
615
616 OMAP_HSMMC_WRITE(host->base, HCTL,
617 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
618
619 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
620 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
621 && time_before(jiffies, timeout))
622 ;
623
Adrian Hunterb4175772010-05-26 14:42:06 -0700624 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700625
626 /* Do not initialize card-specific things if the power is off */
627 if (host->power_mode == MMC_POWER_OFF)
628 goto out;
629
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400630 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700631
Andy Shevchenko5934df22011-05-06 12:14:06 +0300632 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700633
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400634 omap_hsmmc_set_bus_mode(host);
635
Denis Karpov11dd62a2009-09-22 16:44:43 -0700636out:
637 host->context_loss = context_loss;
638
639 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
640 return 0;
641}
642
643/*
644 * Save the MMC host context (store the number of power state changes so far).
645 */
Denis Karpov70a33412009-09-22 16:44:59 -0700646static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700647{
648 struct omap_mmc_platform_data *pdata = host->pdata;
649 int context_loss;
650
651 if (pdata->get_context_loss_count) {
652 context_loss = pdata->get_context_loss_count(host->dev);
653 if (context_loss < 0)
654 return;
655 host->context_loss = context_loss;
656 }
657}
658
659#else
660
Denis Karpov70a33412009-09-22 16:44:59 -0700661static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700662{
663 return 0;
664}
665
Denis Karpov70a33412009-09-22 16:44:59 -0700666static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700667{
668}
669
670#endif
671
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100672/*
673 * Send init stream sequence to card
674 * before sending IDLE command
675 */
Denis Karpov70a33412009-09-22 16:44:59 -0700676static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100677{
678 int reg = 0;
679 unsigned long timeout;
680
Adrian Hunterb62f6222009-09-22 16:45:01 -0700681 if (host->protect_card)
682 return;
683
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100684 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700685
686 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100687 OMAP_HSMMC_WRITE(host->base, CON,
688 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
689 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
690
691 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
692 while ((reg != CC) && time_before(jiffies, timeout))
693 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
694
695 OMAP_HSMMC_WRITE(host->base, CON,
696 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700697
698 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
699 OMAP_HSMMC_READ(host->base, STAT);
700
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100701 enable_irq(host->irq);
702}
703
704static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700705int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100706{
707 int r = 1;
708
Denis Karpov191d1f12009-09-22 16:44:55 -0700709 if (mmc_slot(host).get_cover_state)
710 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100711 return r;
712}
713
714static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700715omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100716 char *buf)
717{
718 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700719 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100720
Denis Karpov70a33412009-09-22 16:44:59 -0700721 return sprintf(buf, "%s\n",
722 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723}
724
Denis Karpov70a33412009-09-22 16:44:59 -0700725static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726
727static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700728omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100729 char *buf)
730{
731 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700732 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100733
Denis Karpov191d1f12009-09-22 16:44:55 -0700734 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735}
736
Denis Karpov70a33412009-09-22 16:44:59 -0700737static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100738
739/*
740 * Configure the response type and send the cmd.
741 */
742static void
Denis Karpov70a33412009-09-22 16:44:59 -0700743omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100744 struct mmc_data *data)
745{
746 int cmdreg = 0, resptype = 0, cmdtype = 0;
747
748 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
749 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
750 host->cmd = cmd;
751
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700752 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200754 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755 if (cmd->flags & MMC_RSP_PRESENT) {
756 if (cmd->flags & MMC_RSP_136)
757 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200758 else if (cmd->flags & MMC_RSP_BUSY) {
759 resptype = 3;
760 host->response_busy = 1;
761 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100762 resptype = 2;
763 }
764
765 /*
766 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
767 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
768 * a val of 0x3, rest 0x0.
769 */
770 if (cmd == host->mrq->stop)
771 cmdtype = 0x3;
772
773 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
774
775 if (data) {
776 cmdreg |= DP_SELECT | MSBS | BCE;
777 if (data->flags & MMC_DATA_READ)
778 cmdreg |= DDIR;
779 else
780 cmdreg &= ~(DDIR);
781 }
782
783 if (host->use_dma)
784 cmdreg |= DMA_EN;
785
Adrian Hunterb4175772010-05-26 14:42:06 -0700786 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700787
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
789 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
790}
791
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200792static int
Denis Karpov70a33412009-09-22 16:44:59 -0700793omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200794{
795 if (data->flags & MMC_DATA_WRITE)
796 return DMA_TO_DEVICE;
797 else
798 return DMA_FROM_DEVICE;
799}
800
Russell Kingc5c98922012-04-13 12:14:39 +0100801static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
802 struct mmc_data *data)
803{
804 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
805}
806
Adrian Hunterb4175772010-05-26 14:42:06 -0700807static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
808{
Russell King26b88522012-04-13 12:27:37 +0100809 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530810 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700811
Venkatraman S31463b12012-04-09 12:08:34 +0530812 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700813 host->req_in_progress = 0;
814 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530815 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700816
817 omap_hsmmc_disable_irq(host);
818 /* Do not complete the request if DMA is still in progress */
Russell King26b88522012-04-13 12:27:37 +0100819 if (mrq->data && host->use_dma && dma_ch != -1)
Adrian Hunterb4175772010-05-26 14:42:06 -0700820 return;
821 host->mrq = NULL;
822 mmc_request_done(host->mmc, mrq);
823}
824
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100825/*
826 * Notify the transfer complete to MMC core
827 */
828static void
Denis Karpov70a33412009-09-22 16:44:59 -0700829omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200831 if (!data) {
832 struct mmc_request *mrq = host->mrq;
833
Adrian Hunter23050102009-09-22 16:44:57 -0700834 /* TC before CC from CMD6 - don't know why, but it happens */
835 if (host->cmd && host->cmd->opcode == 6 &&
836 host->response_busy) {
837 host->response_busy = 0;
838 return;
839 }
840
Adrian Hunterb4175772010-05-26 14:42:06 -0700841 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200842 return;
843 }
844
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100845 host->data = NULL;
846
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100847 if (!data->error)
848 data->bytes_xfered += data->blocks * (data->blksz);
849 else
850 data->bytes_xfered = 0;
851
Ming Leife852272012-06-22 18:49:35 +0800852 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700853 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800854 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100855 }
Ming Leife852272012-06-22 18:49:35 +0800856 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100857}
858
859/*
860 * Notify the core about command completion
861 */
862static void
Denis Karpov70a33412009-09-22 16:44:59 -0700863omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100864{
865 host->cmd = NULL;
866
867 if (cmd->flags & MMC_RSP_PRESENT) {
868 if (cmd->flags & MMC_RSP_136) {
869 /* response type 2 */
870 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
871 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
872 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
873 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
874 } else {
875 /* response types 1, 1b, 3, 4, 5, 6 */
876 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
877 }
878 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700879 if ((host->data == NULL && !host->response_busy) || cmd->error)
880 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100881}
882
883/*
884 * DMA clean up for command errors
885 */
Denis Karpov70a33412009-09-22 16:44:59 -0700886static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100887{
Russell King26b88522012-04-13 12:27:37 +0100888 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530889 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700890
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200891 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100892
Venkatraman S31463b12012-04-09 12:08:34 +0530893 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700894 dma_ch = host->dma_ch;
895 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530896 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700897
Russell King26b88522012-04-13 12:27:37 +0100898 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100899 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
900
901 dmaengine_terminate_all(chan);
902 dma_unmap_sg(chan->device->dev,
903 host->data->sg, host->data->sg_len,
904 omap_hsmmc_get_dma_dir(host, host->data));
905
906 host->data->host_cookie = 0;
907 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100908 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909}
910
911/*
912 * Readable error output
913 */
914#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300915static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100916{
917 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700918 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300919 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
920 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
921 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
922 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100923 };
924 char res[256];
925 char *buf = res;
926 int len, i;
927
928 len = sprintf(buf, "MMC IRQ 0x%x :", status);
929 buf += len;
930
Denis Karpov70a33412009-09-22 16:44:59 -0700931 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100932 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700933 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100934 buf += len;
935 }
936
937 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
938}
Adrian Hunter699b9582011-05-06 12:14:01 +0300939#else
940static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
941 u32 status)
942{
943}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944#endif /* CONFIG_MMC_DEBUG */
945
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100946/*
947 * MMC controller internal state machines reset
948 *
949 * Used to reset command or data internal state machines, using respectively
950 * SRC or SRD bit of SYSCTL register
951 * Can be called from interrupt context
952 */
Denis Karpov70a33412009-09-22 16:44:59 -0700953static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
954 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100955{
956 unsigned long i = 0;
957 unsigned long limit = (loops_per_jiffy *
958 msecs_to_jiffies(MMC_TIMEOUT_MS));
959
960 OMAP_HSMMC_WRITE(host->base, SYSCTL,
961 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
962
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700963 /*
964 * OMAP4 ES2 and greater has an updated reset logic.
965 * Monitor a 0->1 transition first
966 */
967 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500968 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700969 && (i++ < limit))
970 cpu_relax();
971 }
972 i = 0;
973
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100974 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
975 (i++ < limit))
976 cpu_relax();
977
978 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
979 dev_err(mmc_dev(host->mmc),
980 "Timeout waiting on controller reset in %s\n",
981 __func__);
982}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983
Adrian Hunterb4175772010-05-26 14:42:06 -0700984static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100985{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700987 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100988
Adrian Hunterb4175772010-05-26 14:42:06 -0700989 if (!host->req_in_progress) {
990 do {
991 OMAP_HSMMC_WRITE(host->base, STAT, status);
992 /* Flush posted write */
993 status = OMAP_HSMMC_READ(host->base, STAT);
994 } while (status & INT_EN_MASK);
995 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100996 }
997
998 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1000
1001 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001002 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001003 if ((status & CMD_TIMEOUT) ||
1004 (status & CMD_CRC)) {
1005 if (host->cmd) {
1006 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001007 omap_hsmmc_reset_controller_fsm(host,
1008 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001009 host->cmd->error = -ETIMEDOUT;
1010 } else {
1011 host->cmd->error = -EILSEQ;
1012 }
1013 end_cmd = 1;
1014 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001015 if (host->data || host->response_busy) {
1016 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001017 omap_hsmmc_dma_cleanup(host,
1018 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001019 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001020 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001021 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022 }
1023 if ((status & DATA_TIMEOUT) ||
1024 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001025 if (host->data || host->response_busy) {
1026 int err = (status & DATA_TIMEOUT) ?
1027 -ETIMEDOUT : -EILSEQ;
1028
1029 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001030 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001031 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001032 host->mrq->cmd->error = err;
1033 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001034 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001035 end_trans = 1;
1036 }
1037 }
1038 if (status & CARD_ERR) {
1039 dev_dbg(mmc_dev(host->mmc),
1040 "Ignoring card err CMD%d\n", host->cmd->opcode);
1041 if (host->cmd)
1042 end_cmd = 1;
1043 if (host->data)
1044 end_trans = 1;
1045 }
1046 }
1047
1048 OMAP_HSMMC_WRITE(host->base, STAT, status);
1049
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001050 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001051 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001052 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001053 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001054}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055
Adrian Hunterb4175772010-05-26 14:42:06 -07001056/*
1057 * MMC controller IRQ handler
1058 */
1059static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1060{
1061 struct omap_hsmmc_host *host = dev_id;
1062 int status;
1063
1064 status = OMAP_HSMMC_READ(host->base, STAT);
1065 do {
1066 omap_hsmmc_do_irq(host, status);
1067 /* Flush posted write */
1068 status = OMAP_HSMMC_READ(host->base, STAT);
1069 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071 return IRQ_HANDLED;
1072}
1073
Denis Karpov70a33412009-09-22 16:44:59 -07001074static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001075{
1076 unsigned long i;
1077
1078 OMAP_HSMMC_WRITE(host->base, HCTL,
1079 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1080 for (i = 0; i < loops_per_jiffy; i++) {
1081 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1082 break;
1083 cpu_relax();
1084 }
1085}
1086
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087/*
David Brownelleb250822009-02-17 14:49:01 -08001088 * Switch MMC interface voltage ... only relevant for MMC1.
1089 *
1090 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1091 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1092 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093 */
Denis Karpov70a33412009-09-22 16:44:59 -07001094static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095{
1096 u32 reg_val = 0;
1097 int ret;
1098
1099 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301100 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301101 if (host->dbclk)
Adrian Hunter2bec0892009-09-22 16:45:02 -07001102 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001103
1104 /* Turn the power off */
1105 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001106
1107 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001108 if (!ret)
1109 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1110 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301111 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301112 if (host->dbclk)
Adrian Hunter2bec0892009-09-22 16:45:02 -07001113 clk_enable(host->dbclk);
1114
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001115 if (ret != 0)
1116 goto err;
1117
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118 OMAP_HSMMC_WRITE(host->base, HCTL,
1119 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1120 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001121
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001122 /*
1123 * If a MMC dual voltage card is detected, the set_ios fn calls
1124 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001125 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001126 *
David Brownelleb250822009-02-17 14:49:01 -08001127 * Cope with a bit of slop in the range ... per data sheets:
1128 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1129 * but recommended values are 1.71V to 1.89V
1130 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1131 * but recommended values are 2.7V to 3.3V
1132 *
1133 * Board setup code shouldn't permit anything very out-of-range.
1134 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1135 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001136 */
David Brownelleb250822009-02-17 14:49:01 -08001137 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001138 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001139 else
1140 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141
1142 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001143 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144
1145 return 0;
1146err:
1147 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1148 return ret;
1149}
1150
Adrian Hunterb62f6222009-09-22 16:45:01 -07001151/* Protect the card while the cover is open */
1152static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1153{
1154 if (!mmc_slot(host).get_cover_state)
1155 return;
1156
1157 host->reqs_blocked = 0;
1158 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1159 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301160 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001161 "card is now accessible\n",
1162 mmc_hostname(host->mmc));
1163 host->protect_card = 0;
1164 }
1165 } else {
1166 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301167 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001168 "card is now inaccessible\n",
1169 mmc_hostname(host->mmc));
1170 host->protect_card = 1;
1171 }
1172 }
1173}
1174
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001176 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001177 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001178static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179{
NeilBrown7efab4f2011-12-30 12:35:13 +11001180 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001181 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001182 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001183
Adrian Huntera6b22402009-09-22 16:44:45 -07001184 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001185 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001186
1187 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001188
Denis Karpov191d1f12009-09-22 16:44:55 -07001189 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001190 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001191 else {
1192 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001193 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001194 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001195
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001196 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001198 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001200 return IRQ_HANDLED;
1201}
1202
Russell Kingc5c98922012-04-13 12:14:39 +01001203static void omap_hsmmc_dma_callback(void *param)
1204{
1205 struct omap_hsmmc_host *host = param;
1206 struct dma_chan *chan;
1207 struct mmc_data *data;
1208 int req_in_progress;
1209
1210 spin_lock_irq(&host->irq_lock);
Russell King26b88522012-04-13 12:27:37 +01001211 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001212 spin_unlock_irq(&host->irq_lock);
1213 return;
1214 }
1215
1216 data = host->mrq->data;
1217 chan = omap_hsmmc_get_dma_chan(host, data);
1218 if (!data->host_cookie)
1219 dma_unmap_sg(chan->device->dev,
1220 data->sg, data->sg_len,
1221 omap_hsmmc_get_dma_dir(host, data));
1222
1223 req_in_progress = host->req_in_progress;
Russell King26b88522012-04-13 12:27:37 +01001224 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001225 spin_unlock_irq(&host->irq_lock);
1226
1227 /* If DMA has finished after TC, complete the request */
1228 if (!req_in_progress) {
1229 struct mmc_request *mrq = host->mrq;
1230
1231 host->mrq = NULL;
1232 mmc_request_done(host->mmc, mrq);
1233 }
1234}
1235
Per Forlin9782aff2011-07-01 18:55:23 +02001236static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1237 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001238 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001239 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001240{
1241 int dma_len;
1242
1243 if (!next && data->host_cookie &&
1244 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301245 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001246 " host->next_data.cookie %d\n",
1247 __func__, data->host_cookie, host->next_data.cookie);
1248 data->host_cookie = 0;
1249 }
1250
1251 /* Check if next job is already prepared */
1252 if (next ||
1253 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001254 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001255 omap_hsmmc_get_dma_dir(host, data));
1256
1257 } else {
1258 dma_len = host->next_data.dma_len;
1259 host->next_data.dma_len = 0;
1260 }
1261
1262
1263 if (dma_len == 0)
1264 return -EINVAL;
1265
1266 if (next) {
1267 next->dma_len = dma_len;
1268 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1269 } else
1270 host->dma_len = dma_len;
1271
1272 return 0;
1273}
1274
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001276 * Routine to configure and start DMA for the MMC card
1277 */
Denis Karpov70a33412009-09-22 16:44:59 -07001278static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1279 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001280{
Russell King26b88522012-04-13 12:27:37 +01001281 struct dma_slave_config cfg;
1282 struct dma_async_tx_descriptor *tx;
1283 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001284 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001285 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001286
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001287 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001288 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001289 struct scatterlist *sgl;
1290
1291 sgl = data->sg + i;
1292 if (sgl->length % data->blksz)
1293 return -EINVAL;
1294 }
1295 if ((data->blksz % 4) != 0)
1296 /* REVISIT: The MMC buffer increments only when MSB is written.
1297 * Return error for blksz which is non multiple of four.
1298 */
1299 return -EINVAL;
1300
Russell King26b88522012-04-13 12:27:37 +01001301 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001302
Russell Kingc5c98922012-04-13 12:14:39 +01001303 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001304
Russell King26b88522012-04-13 12:27:37 +01001305 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1306 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1307 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1308 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1309 cfg.src_maxburst = data->blksz / 4;
1310 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001311
Russell King26b88522012-04-13 12:27:37 +01001312 ret = dmaengine_slave_config(chan, &cfg);
1313 if (ret)
1314 return ret;
Russell Kingc5c98922012-04-13 12:14:39 +01001315
Russell King26b88522012-04-13 12:27:37 +01001316 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1317 if (ret)
1318 return ret;
Russell Kingc5c98922012-04-13 12:14:39 +01001319
Russell King26b88522012-04-13 12:27:37 +01001320 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1321 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1322 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1323 if (!tx) {
1324 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1325 /* FIXME: cleanup */
1326 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001327 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001328
Russell King26b88522012-04-13 12:27:37 +01001329 tx->callback = omap_hsmmc_dma_callback;
1330 tx->callback_param = host;
1331
1332 /* Does not fail */
1333 dmaengine_submit(tx);
1334
1335 host->dma_ch = 1;
1336
1337 dma_async_issue_pending(chan);
1338
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339 return 0;
1340}
1341
Denis Karpov70a33412009-09-22 16:44:59 -07001342static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001343 unsigned int timeout_ns,
1344 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001345{
1346 unsigned int timeout, cycle_ns;
1347 uint32_t reg, clkd, dto = 0;
1348
1349 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1350 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1351 if (clkd == 0)
1352 clkd = 1;
1353
1354 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001355 timeout = timeout_ns / cycle_ns;
1356 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001357 if (timeout) {
1358 while ((timeout & 0x80000000) == 0) {
1359 dto += 1;
1360 timeout <<= 1;
1361 }
1362 dto = 31 - dto;
1363 timeout <<= 1;
1364 if (timeout && dto)
1365 dto += 1;
1366 if (dto >= 13)
1367 dto -= 13;
1368 else
1369 dto = 0;
1370 if (dto > 14)
1371 dto = 14;
1372 }
1373
1374 reg &= ~DTO_MASK;
1375 reg |= dto << DTO_SHIFT;
1376 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1377}
1378
1379/*
1380 * Configure block length for MMC/SD cards and initiate the transfer.
1381 */
1382static int
Denis Karpov70a33412009-09-22 16:44:59 -07001383omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001384{
1385 int ret;
1386 host->data = req->data;
1387
1388 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001389 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001390 /*
1391 * Set an arbitrary 100ms data timeout for commands with
1392 * busy signal.
1393 */
1394 if (req->cmd->flags & MMC_RSP_BUSY)
1395 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001396 return 0;
1397 }
1398
1399 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1400 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001401 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001402
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001403 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001404 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001405 if (ret != 0) {
1406 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1407 return ret;
1408 }
1409 }
1410 return 0;
1411}
1412
Per Forlin9782aff2011-07-01 18:55:23 +02001413static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1414 int err)
1415{
1416 struct omap_hsmmc_host *host = mmc_priv(mmc);
1417 struct mmc_data *data = mrq->data;
1418
Russell King26b88522012-04-13 12:27:37 +01001419 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001420 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001421
Russell King26b88522012-04-13 12:27:37 +01001422 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1423 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001424 data->host_cookie = 0;
1425 }
1426}
1427
1428static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1429 bool is_first_req)
1430{
1431 struct omap_hsmmc_host *host = mmc_priv(mmc);
1432
1433 if (mrq->data->host_cookie) {
1434 mrq->data->host_cookie = 0;
1435 return ;
1436 }
1437
Russell Kingc5c98922012-04-13 12:14:39 +01001438 if (host->use_dma) {
1439 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001440
Per Forlin9782aff2011-07-01 18:55:23 +02001441 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001442 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001443 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001444 }
Per Forlin9782aff2011-07-01 18:55:23 +02001445}
1446
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447/*
1448 * Request function. for read/write operation
1449 */
Denis Karpov70a33412009-09-22 16:44:59 -07001450static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451{
Denis Karpov70a33412009-09-22 16:44:59 -07001452 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001453 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001454
Adrian Hunterb4175772010-05-26 14:42:06 -07001455 BUG_ON(host->req_in_progress);
Russell King26b88522012-04-13 12:27:37 +01001456 BUG_ON(host->dma_ch != -1);
Adrian Hunterb4175772010-05-26 14:42:06 -07001457 if (host->protect_card) {
1458 if (host->reqs_blocked < 3) {
1459 /*
1460 * Ensure the controller is left in a consistent
1461 * state by resetting the command and data state
1462 * machines.
1463 */
1464 omap_hsmmc_reset_controller_fsm(host, SRD);
1465 omap_hsmmc_reset_controller_fsm(host, SRC);
1466 host->reqs_blocked += 1;
1467 }
1468 req->cmd->error = -EBADF;
1469 if (req->data)
1470 req->data->error = -EBADF;
1471 req->cmd->retries = 0;
1472 mmc_request_done(mmc, req);
1473 return;
1474 } else if (host->reqs_blocked)
1475 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001476 WARN_ON(host->mrq != NULL);
1477 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001478 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001479 if (err) {
1480 req->cmd->error = err;
1481 if (req->data)
1482 req->data->error = err;
1483 host->mrq = NULL;
1484 mmc_request_done(mmc, req);
1485 return;
1486 }
1487
Denis Karpov70a33412009-09-22 16:44:59 -07001488 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001489}
1490
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001492static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001493{
Denis Karpov70a33412009-09-22 16:44:59 -07001494 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001495 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001496
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301497 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001498
Adrian Huntera3621462009-09-22 16:44:42 -07001499 if (ios->power_mode != host->power_mode) {
1500 switch (ios->power_mode) {
1501 case MMC_POWER_OFF:
1502 mmc_slot(host).set_power(host->dev, host->slot_id,
1503 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001504 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001505 break;
1506 case MMC_POWER_UP:
1507 mmc_slot(host).set_power(host->dev, host->slot_id,
1508 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001509 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001510 break;
1511 case MMC_POWER_ON:
1512 do_send_init_stream = 1;
1513 break;
1514 }
1515 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516 }
1517
Denis Karpovdd498ef2009-09-22 16:44:49 -07001518 /* FIXME: set registers based only on changes to ios */
1519
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001520 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301522 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001523 /* Only MMC1 can interface at 3V without some flavor
1524 * of external transceiver; but they all handle 1.8V.
1525 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001526 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301527 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1528 /*
1529 * With pbias cell programming missing, this
1530 * can't be allowed when booting with device
1531 * tree.
1532 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301533 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001534 /*
1535 * The mmc_select_voltage fn of the core does
1536 * not seem to set the power_mode to
1537 * MMC_POWER_UP upon recalculating the voltage.
1538 * vdd 1.8v.
1539 */
Denis Karpov70a33412009-09-22 16:44:59 -07001540 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1541 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001542 "Switch operation failed\n");
1543 }
1544 }
1545
Andy Shevchenko5934df22011-05-06 12:14:06 +03001546 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001547
Adrian Huntera3621462009-09-22 16:44:42 -07001548 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001549 send_init_stream(host);
1550
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001551 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001552
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301553 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001554}
1555
1556static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1557{
Denis Karpov70a33412009-09-22 16:44:59 -07001558 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001559
Denis Karpov191d1f12009-09-22 16:44:55 -07001560 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001562 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563}
1564
1565static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1566{
Denis Karpov70a33412009-09-22 16:44:59 -07001567 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001568
Denis Karpov191d1f12009-09-22 16:44:55 -07001569 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001570 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001571 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001572}
1573
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001574static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1575{
1576 struct omap_hsmmc_host *host = mmc_priv(mmc);
1577
1578 if (mmc_slot(host).init_card)
1579 mmc_slot(host).init_card(card);
1580}
1581
Denis Karpov70a33412009-09-22 16:44:59 -07001582static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001583{
1584 u32 hctl, capa, value;
1585
1586 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301587 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001588 hctl = SDVS30;
1589 capa = VS30 | VS18;
1590 } else {
1591 hctl = SDVS18;
1592 capa = VS18;
1593 }
1594
1595 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1596 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1597
1598 value = OMAP_HSMMC_READ(host->base, CAPA);
1599 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1600
1601 /* Set the controller to AUTO IDLE mode */
1602 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1603 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1604
1605 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001606 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001607}
1608
Denis Karpov70a33412009-09-22 16:44:59 -07001609static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001610{
Denis Karpov70a33412009-09-22 16:44:59 -07001611 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001612
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301613 pm_runtime_get_sync(host->dev);
1614
Denis Karpovdd498ef2009-09-22 16:44:49 -07001615 return 0;
1616}
1617
Adrian Hunter907d2e72012-02-29 09:17:21 +02001618static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001619{
Denis Karpov70a33412009-09-22 16:44:59 -07001620 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001621
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301622 pm_runtime_mark_last_busy(host->dev);
1623 pm_runtime_put_autosuspend(host->dev);
1624
Denis Karpovdd498ef2009-09-22 16:44:49 -07001625 return 0;
1626}
1627
Denis Karpov70a33412009-09-22 16:44:59 -07001628static const struct mmc_host_ops omap_hsmmc_ops = {
1629 .enable = omap_hsmmc_enable_fclk,
1630 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001631 .post_req = omap_hsmmc_post_req,
1632 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001633 .request = omap_hsmmc_request,
1634 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001635 .get_cd = omap_hsmmc_get_cd,
1636 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001637 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001638 /* NYET -- enable_sdio_irq */
1639};
1640
Denis Karpovd900f712009-09-22 16:44:38 -07001641#ifdef CONFIG_DEBUG_FS
1642
Denis Karpov70a33412009-09-22 16:44:59 -07001643static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001644{
1645 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001646 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001647 int context_loss = 0;
1648
Denis Karpov70a33412009-09-22 16:44:59 -07001649 if (host->pdata->get_context_loss_count)
1650 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001651
Adrian Hunter907d2e72012-02-29 09:17:21 +02001652 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1653 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001654
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301655 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001656 seq_printf(s, "host suspended, can't read registers\n");
1657 return 0;
1658 }
1659
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301660 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001661
1662 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1663 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1664 seq_printf(s, "CON:\t\t0x%08x\n",
1665 OMAP_HSMMC_READ(host->base, CON));
1666 seq_printf(s, "HCTL:\t\t0x%08x\n",
1667 OMAP_HSMMC_READ(host->base, HCTL));
1668 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1669 OMAP_HSMMC_READ(host->base, SYSCTL));
1670 seq_printf(s, "IE:\t\t0x%08x\n",
1671 OMAP_HSMMC_READ(host->base, IE));
1672 seq_printf(s, "ISE:\t\t0x%08x\n",
1673 OMAP_HSMMC_READ(host->base, ISE));
1674 seq_printf(s, "CAPA:\t\t0x%08x\n",
1675 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001676
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301677 pm_runtime_mark_last_busy(host->dev);
1678 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001679
Denis Karpovd900f712009-09-22 16:44:38 -07001680 return 0;
1681}
1682
Denis Karpov70a33412009-09-22 16:44:59 -07001683static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001684{
Denis Karpov70a33412009-09-22 16:44:59 -07001685 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001686}
1687
1688static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001689 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001690 .read = seq_read,
1691 .llseek = seq_lseek,
1692 .release = single_release,
1693};
1694
Denis Karpov70a33412009-09-22 16:44:59 -07001695static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001696{
1697 if (mmc->debugfs_root)
1698 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1699 mmc, &mmc_regs_fops);
1700}
1701
1702#else
1703
Denis Karpov70a33412009-09-22 16:44:59 -07001704static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001705{
1706}
1707
1708#endif
1709
Rajendra Nayak46856a62012-03-12 20:32:37 +05301710#ifdef CONFIG_OF
1711static u16 omap4_reg_offset = 0x100;
1712
1713static const struct of_device_id omap_mmc_of_match[] = {
1714 {
1715 .compatible = "ti,omap2-hsmmc",
1716 },
1717 {
1718 .compatible = "ti,omap3-hsmmc",
1719 },
1720 {
1721 .compatible = "ti,omap4-hsmmc",
1722 .data = &omap4_reg_offset,
1723 },
1724 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001725};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301726MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1727
1728static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1729{
1730 struct omap_mmc_platform_data *pdata;
1731 struct device_node *np = dev->of_node;
1732 u32 bus_width;
1733
1734 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1735 if (!pdata)
1736 return NULL; /* out of memory */
1737
1738 if (of_find_property(np, "ti,dual-volt", NULL))
1739 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1740
1741 /* This driver only supports 1 slot */
1742 pdata->nr_slots = 1;
1743 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1744 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1745
1746 if (of_find_property(np, "ti,non-removable", NULL)) {
1747 pdata->slots[0].nonremovable = true;
1748 pdata->slots[0].no_regulator_off_init = true;
1749 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001750 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301751 if (bus_width == 4)
1752 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1753 else if (bus_width == 8)
1754 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1755
1756 if (of_find_property(np, "ti,needs-special-reset", NULL))
1757 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1758
1759 return pdata;
1760}
1761#else
1762static inline struct omap_mmc_platform_data
1763 *of_get_hsmmc_pdata(struct device *dev)
1764{
1765 return NULL;
1766}
1767#endif
1768
Russell King26b88522012-04-13 12:27:37 +01001769extern bool omap_dma_filter_fn(struct dma_chan *chan, void *param);
1770
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001771static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001772{
1773 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1774 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001775 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001776 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001777 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301778 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001779 dma_cap_mask_t mask;
1780 unsigned tx_req, rx_req;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301781
1782 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1783 if (match) {
1784 pdata = of_get_hsmmc_pdata(&pdev->dev);
1785 if (match->data) {
1786 u16 *offsetp = match->data;
1787 pdata->reg_offset = *offsetp;
1788 }
1789 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001790
1791 if (pdata == NULL) {
1792 dev_err(&pdev->dev, "Platform Data is missing\n");
1793 return -ENXIO;
1794 }
1795
1796 if (pdata->nr_slots == 0) {
1797 dev_err(&pdev->dev, "No Slots\n");
1798 return -ENXIO;
1799 }
1800
1801 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1802 irq = platform_get_irq(pdev, 0);
1803 if (res == NULL || irq < 0)
1804 return -ENXIO;
1805
Chris Ball984b2032011-03-22 16:34:42 -07001806 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001807 if (res == NULL)
1808 return -EBUSY;
1809
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001810 ret = omap_hsmmc_gpio_init(pdata);
1811 if (ret)
1812 goto err;
1813
Denis Karpov70a33412009-09-22 16:44:59 -07001814 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001815 if (!mmc) {
1816 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001817 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001818 }
1819
1820 host = mmc_priv(mmc);
1821 host->mmc = mmc;
1822 host->pdata = pdata;
1823 host->dev = &pdev->dev;
1824 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001825 host->dma_ch = -1;
1826 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001827 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301828 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001829 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001830 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001831 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001832
1833 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001834
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301835 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001836
Adrian Huntere0eb2422010-02-15 10:03:34 -08001837 /*
1838 * If regulator_disable can only put vcc_aux to sleep then there is
1839 * no off state.
1840 */
1841 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1842 mmc_slot(host).no_off = 1;
1843
Daniel Mackd418ed82012-02-19 13:20:33 +01001844 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1845
1846 if (pdata->max_freq > 0)
1847 mmc->f_max = pdata->max_freq;
1848 else
1849 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001850
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001851 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001852
Russell King6f7607c2009-01-28 10:22:50 +00001853 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001854 if (IS_ERR(host->fclk)) {
1855 ret = PTR_ERR(host->fclk);
1856 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001857 goto err1;
1858 }
1859
Paul Walmsley9b682562011-10-06 14:50:35 -06001860 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1861 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1862 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1863 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001864
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301865 pm_runtime_enable(host->dev);
1866 pm_runtime_get_sync(host->dev);
1867 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1868 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001869
Balaji T K92a3aeb2012-02-24 21:14:34 +05301870 omap_hsmmc_context_save(host);
1871
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301872 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1873 /*
1874 * MMC can still work without debounce clock.
1875 */
1876 if (IS_ERR(host->dbclk)) {
1877 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clk\n");
1878 host->dbclk = NULL;
1879 } else if (clk_enable(host->dbclk) != 0) {
1880 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1881 clk_put(host->dbclk);
1882 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001883 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001884
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001885 /* Since we do only SG emulation, we can have as many segs
1886 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001887 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001888
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1890 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1891 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1892 mmc->max_seg_size = mmc->max_req_size;
1893
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001894 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001895 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001896
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001897 mmc->caps |= mmc_slot(host).caps;
1898 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001899 mmc->caps |= MMC_CAP_4_BIT_DATA;
1900
Denis Karpov191d1f12009-09-22 16:44:55 -07001901 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001902 mmc->caps |= MMC_CAP_NONREMOVABLE;
1903
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001904 mmc->pm_caps = mmc_slot(host).pm_caps;
1905
Denis Karpov70a33412009-09-22 16:44:59 -07001906 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001907
Balaji T Kb7bf7732012-03-07 09:55:30 -05001908 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1909 if (!res) {
1910 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001911 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001912 }
Russell King26b88522012-04-13 12:27:37 +01001913 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001914
1915 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1916 if (!res) {
1917 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1918 goto err_irq;
1919 }
Russell King26b88522012-04-13 12:27:37 +01001920 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001921
Russell King26b88522012-04-13 12:27:37 +01001922 dma_cap_zero(mask);
1923 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001924
Russell King26b88522012-04-13 12:27:37 +01001925 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1926 if (!host->rx_chan) {
1927 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
1928 goto err_irq;
1929 }
1930
1931 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1932 if (!host->tx_chan) {
1933 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
1934 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001935 }
1936
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001937 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001938 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001939 mmc_hostname(mmc), host);
1940 if (ret) {
1941 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1942 goto err_irq;
1943 }
1944
1945 if (pdata->init != NULL) {
1946 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001947 dev_dbg(mmc_dev(host->mmc),
1948 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 goto err_irq_cd_init;
1950 }
1951 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001952
Adrian Hunterb702b102010-02-15 10:03:35 -08001953 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001954 ret = omap_hsmmc_reg_get(host);
1955 if (ret)
1956 goto err_reg;
1957 host->use_reg = 1;
1958 }
1959
David Brownellb583f262009-05-28 14:04:03 -07001960 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961
1962 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001963 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001964 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1965 NULL,
1966 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001967 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001968 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001969 if (ret) {
1970 dev_dbg(mmc_dev(host->mmc),
1971 "Unable to grab MMC CD IRQ\n");
1972 goto err_irq_cd;
1973 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001974 pdata->suspend = omap_hsmmc_suspend_cdirq;
1975 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001976 }
1977
Adrian Hunterb4175772010-05-26 14:42:06 -07001978 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979
Adrian Hunterb62f6222009-09-22 16:45:01 -07001980 omap_hsmmc_protect_card(host);
1981
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001982 mmc_add_host(mmc);
1983
Denis Karpov191d1f12009-09-22 16:44:55 -07001984 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001985 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1986 if (ret < 0)
1987 goto err_slot_name;
1988 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001989 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990 ret = device_create_file(&mmc->class_dev,
1991 &dev_attr_cover_switch);
1992 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001993 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001994 }
1995
Denis Karpov70a33412009-09-22 16:44:59 -07001996 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301997 pm_runtime_mark_last_busy(host->dev);
1998 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001999
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002000 return 0;
2001
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002002err_slot_name:
2003 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002004 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002005err_irq_cd:
2006 if (host->use_reg)
2007 omap_hsmmc_reg_put(host);
2008err_reg:
2009 if (host->pdata->cleanup)
2010 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011err_irq_cd_init:
2012 free_irq(host->irq, host);
2013err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002014 if (host->tx_chan)
2015 dma_release_channel(host->tx_chan);
2016 if (host->rx_chan)
2017 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302018 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002019 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302021 if (host->dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022 clk_disable(host->dbclk);
2023 clk_put(host->dbclk);
2024 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002025err1:
2026 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002027 platform_set_drvdata(pdev, NULL);
2028 mmc_free_host(mmc);
2029err_alloc:
2030 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002031err:
Russell King48b332f2012-04-18 11:11:57 +01002032 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2033 if (res)
2034 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035 return ret;
2036}
2037
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002038static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002039{
Denis Karpov70a33412009-09-22 16:44:59 -07002040 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002041 struct resource *res;
2042
Felipe Balbi927ce942012-03-14 11:18:27 +02002043 pm_runtime_get_sync(host->dev);
2044 mmc_remove_host(host->mmc);
2045 if (host->use_reg)
2046 omap_hsmmc_reg_put(host);
2047 if (host->pdata->cleanup)
2048 host->pdata->cleanup(&pdev->dev);
2049 free_irq(host->irq, host);
2050 if (mmc_slot(host).card_detect_irq)
2051 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052
Russell Kingc5c98922012-04-13 12:14:39 +01002053 if (host->tx_chan)
2054 dma_release_channel(host->tx_chan);
2055 if (host->rx_chan)
2056 dma_release_channel(host->rx_chan);
2057
Felipe Balbi927ce942012-03-14 11:18:27 +02002058 pm_runtime_put_sync(host->dev);
2059 pm_runtime_disable(host->dev);
2060 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302061 if (host->dbclk) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002062 clk_disable(host->dbclk);
2063 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064 }
2065
Felipe Balbi927ce942012-03-14 11:18:27 +02002066 mmc_free_host(host->mmc);
2067 iounmap(host->base);
2068 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2069
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2071 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002072 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002073 platform_set_drvdata(pdev, NULL);
2074
2075 return 0;
2076}
2077
2078#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002079static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080{
2081 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002082 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2083
2084 if (!host)
2085 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086
2087 if (host && host->suspended)
2088 return 0;
2089
Felipe Balbi927ce942012-03-14 11:18:27 +02002090 pm_runtime_get_sync(host->dev);
2091 host->suspended = 1;
2092 if (host->pdata->suspend) {
2093 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002094 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002095 dev_dbg(dev, "Unable to handle MMC board"
2096 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002097 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002098 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002099 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002100 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002101 ret = mmc_suspend_host(host->mmc);
2102
2103 if (ret) {
2104 host->suspended = 0;
2105 if (host->pdata->resume) {
2106 ret = host->pdata->resume(dev, host->slot_id);
2107 if (ret)
2108 dev_dbg(dev, "Unmask interrupt failed\n");
2109 }
2110 goto err;
2111 }
2112
2113 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2114 omap_hsmmc_disable_irq(host);
2115 OMAP_HSMMC_WRITE(host->base, HCTL,
2116 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2117 }
2118
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302119 if (host->dbclk)
Felipe Balbi927ce942012-03-14 11:18:27 +02002120 clk_disable(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002121err:
2122 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002123 return ret;
2124}
2125
2126/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002127static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002128{
2129 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002130 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2131
2132 if (!host)
2133 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002134
2135 if (host && !host->suspended)
2136 return 0;
2137
Felipe Balbi927ce942012-03-14 11:18:27 +02002138 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002139
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302140 if (host->dbclk)
Felipe Balbi927ce942012-03-14 11:18:27 +02002141 clk_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002142
Felipe Balbi927ce942012-03-14 11:18:27 +02002143 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2144 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002145
Felipe Balbi927ce942012-03-14 11:18:27 +02002146 if (host->pdata->resume) {
2147 ret = host->pdata->resume(dev, host->slot_id);
2148 if (ret)
2149 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150 }
2151
Felipe Balbi927ce942012-03-14 11:18:27 +02002152 omap_hsmmc_protect_card(host);
2153
2154 /* Notify the core to resume the host */
2155 ret = mmc_resume_host(host->mmc);
2156 if (ret == 0)
2157 host->suspended = 0;
2158
2159 pm_runtime_mark_last_busy(host->dev);
2160 pm_runtime_put_autosuspend(host->dev);
2161
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162 return ret;
2163
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002164}
2165
2166#else
Denis Karpov70a33412009-09-22 16:44:59 -07002167#define omap_hsmmc_suspend NULL
2168#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002169#endif
2170
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302171static int omap_hsmmc_runtime_suspend(struct device *dev)
2172{
2173 struct omap_hsmmc_host *host;
2174
2175 host = platform_get_drvdata(to_platform_device(dev));
2176 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002177 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302178
2179 return 0;
2180}
2181
2182static int omap_hsmmc_runtime_resume(struct device *dev)
2183{
2184 struct omap_hsmmc_host *host;
2185
2186 host = platform_get_drvdata(to_platform_device(dev));
2187 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002188 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302189
2190 return 0;
2191}
2192
Kevin Hilmana791daa2010-05-26 14:42:07 -07002193static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002194 .suspend = omap_hsmmc_suspend,
2195 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302196 .runtime_suspend = omap_hsmmc_runtime_suspend,
2197 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002198};
2199
2200static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002201 .probe = omap_hsmmc_probe,
2202 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002203 .driver = {
2204 .name = DRIVER_NAME,
2205 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002206 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302207 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 },
2209};
2210
Felipe Balbib7964502012-03-14 11:18:32 +02002211module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002212MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2213MODULE_LICENSE("GPL");
2214MODULE_ALIAS("platform:" DRIVER_NAME);
2215MODULE_AUTHOR("Texas Instruments Inc");