blob: 6416828d2cdcd54a0555d2a80cc6e002f32d0cb7 [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>
Denis Karpovd900f712009-09-22 16:44:38 -070020#include <linux/debugfs.h>
21#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010022#include <linux/interrupt.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/platform_device.h>
26#include <linux/workqueue.h>
27#include <linux/timer.h>
28#include <linux/clk.h>
29#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070030#include <linux/mmc/core.h>
Adrian Hunter93caf8e2010-08-11 14:17:48 -070031#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010032#include <linux/io.h>
33#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080034#include <linux/gpio.h>
35#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053036#include <linux/pm_runtime.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070037#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010038#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070039#include <plat/board.h>
40#include <plat/mmc.h>
41#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042
43/* OMAP HSMMC Host Controller Registers */
44#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070045#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046#define OMAP_HSMMC_CON 0x002C
47#define OMAP_HSMMC_BLK 0x0104
48#define OMAP_HSMMC_ARG 0x0108
49#define OMAP_HSMMC_CMD 0x010C
50#define OMAP_HSMMC_RSP10 0x0110
51#define OMAP_HSMMC_RSP32 0x0114
52#define OMAP_HSMMC_RSP54 0x0118
53#define OMAP_HSMMC_RSP76 0x011C
54#define OMAP_HSMMC_DATA 0x0120
55#define OMAP_HSMMC_HCTL 0x0128
56#define OMAP_HSMMC_SYSCTL 0x012C
57#define OMAP_HSMMC_STAT 0x0130
58#define OMAP_HSMMC_IE 0x0134
59#define OMAP_HSMMC_ISE 0x0138
60#define OMAP_HSMMC_CAPA 0x0140
61
62#define VS18 (1 << 26)
63#define VS30 (1 << 25)
64#define SDVS18 (0x5 << 9)
65#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080066#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010067#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define SDVSCLR 0xFFFFF1FF
69#define SDVSDET 0x00000400
70#define AUTOIDLE 0x1
71#define SDBP (1 << 8)
72#define DTO 0xe
73#define ICE 0x1
74#define ICS 0x2
75#define CEN (1 << 2)
76#define CLKD_MASK 0x0000FFC0
77#define CLKD_SHIFT 6
78#define DTO_MASK 0x000F0000
79#define DTO_SHIFT 16
80#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070081#define BWR_ENABLE (1 << 4)
82#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e2010-08-11 14:17:48 -070083#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define INIT_STREAM (1 << 1)
85#define DP_SELECT (1 << 21)
86#define DDIR (1 << 4)
87#define DMA_EN 0x1
88#define MSBS (1 << 5)
89#define BCE (1 << 1)
90#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020091#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define CC 0x1
93#define TC 0x02
94#define OD 0x1
95#define ERR (1 << 15)
96#define CMD_TIMEOUT (1 << 16)
97#define DATA_TIMEOUT (1 << 20)
98#define CMD_CRC (1 << 17)
99#define DATA_CRC (1 << 21)
100#define CARD_ERR (1 << 28)
101#define STAT_CLEAR 0xFFFFFFFF
102#define INIT_STREAM_CMD 0x00000000
103#define DUAL_VOLT_OCR_BIT 7
104#define SRC (1 << 25)
105#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700106#define SOFTRESET (1 << 1)
107#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100108
109/*
110 * FIXME: Most likely all the data using these _DEVID defines should come
111 * from the platform_data, or implemented in controller and slot specific
112 * functions.
113 */
114#define OMAP_MMC1_DEVID 0
115#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000116#define OMAP_MMC3_DEVID 2
kishore kadiyala82cf8182009-09-22 16:45:25 -0700117#define OMAP_MMC4_DEVID 3
118#define OMAP_MMC5_DEVID 4
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100119
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530120#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100121#define MMC_TIMEOUT_MS 20
122#define OMAP_MMC_MASTER_CLOCK 96000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530123#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100124
125/*
126 * One controller can have multiple slots, like on some omap boards using
127 * omap.c controller driver. Luckily this is not currently done on any known
128 * omap_hsmmc.c device.
129 */
130#define mmc_slot(host) (host->pdata->slots[host->slot_id])
131
132/*
133 * MMC Host controller read/write API's
134 */
135#define OMAP_HSMMC_READ(base, reg) \
136 __raw_readl((base) + OMAP_HSMMC_##reg)
137
138#define OMAP_HSMMC_WRITE(base, reg, val) \
139 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
140
Per Forlin9782aff2011-07-01 18:55:23 +0200141struct omap_hsmmc_next {
142 unsigned int dma_len;
143 s32 cookie;
144};
145
Denis Karpov70a33412009-09-22 16:44:59 -0700146struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100147 struct device *dev;
148 struct mmc_host *mmc;
149 struct mmc_request *mrq;
150 struct mmc_command *cmd;
151 struct mmc_data *data;
152 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800154 /*
155 * vcc == configured supply
156 * vcc_aux == optional
157 * - MMC1, supply for DAT4..DAT7
158 * - MMC2/MMC2, external level shifter voltage supply, for
159 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
160 */
161 struct regulator *vcc;
162 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100163 struct work_struct mmc_carddetect_work;
164 void __iomem *base;
165 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700166 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100167 unsigned int id;
168 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200169 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100170 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700171 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 u32 *buffer;
173 u32 bytesleft;
174 int suspended;
175 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000177 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100178 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700179 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200180 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700181 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700182 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700183 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700184 int protect_card;
185 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800186 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700187 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200188 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700189
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100190 struct omap_mmc_platform_data *pdata;
191};
192
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800193static int omap_hsmmc_card_detect(struct device *dev, int slot)
194{
195 struct omap_mmc_platform_data *mmc = dev->platform_data;
196
197 /* NOTE: assumes card detect signal is active-low */
198 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
199}
200
201static int omap_hsmmc_get_wp(struct device *dev, int slot)
202{
203 struct omap_mmc_platform_data *mmc = dev->platform_data;
204
205 /* NOTE: assumes write protect signal is active-high */
206 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
207}
208
209static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
210{
211 struct omap_mmc_platform_data *mmc = dev->platform_data;
212
213 /* NOTE: assumes card detect signal is active-low */
214 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
215}
216
217#ifdef CONFIG_PM
218
219static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
220{
221 struct omap_mmc_platform_data *mmc = dev->platform_data;
222
223 disable_irq(mmc->slots[0].card_detect_irq);
224 return 0;
225}
226
227static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
228{
229 struct omap_mmc_platform_data *mmc = dev->platform_data;
230
231 enable_irq(mmc->slots[0].card_detect_irq);
232 return 0;
233}
234
235#else
236
237#define omap_hsmmc_suspend_cdirq NULL
238#define omap_hsmmc_resume_cdirq NULL
239
240#endif
241
Adrian Hunterb702b102010-02-15 10:03:35 -0800242#ifdef CONFIG_REGULATOR
243
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800244static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
245 int vdd)
246{
247 struct omap_hsmmc_host *host =
248 platform_get_drvdata(to_platform_device(dev));
249 int ret;
250
251 if (mmc_slot(host).before_set_reg)
252 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
253
254 if (power_on)
Linus Walleij99fc5132010-09-29 01:08:27 -0400255 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800256 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400257 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258
259 if (mmc_slot(host).after_set_reg)
260 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
261
262 return ret;
263}
264
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500265static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800266 int vdd)
267{
268 struct omap_hsmmc_host *host =
269 platform_get_drvdata(to_platform_device(dev));
270 int ret = 0;
271
272 /*
273 * If we don't see a Vcc regulator, assume it's a fixed
274 * voltage always-on regulator.
275 */
276 if (!host->vcc)
277 return 0;
278
279 if (mmc_slot(host).before_set_reg)
280 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
281
282 /*
283 * Assume Vcc regulator is used only to power the card ... OMAP
284 * VDDS is used to power the pins, optionally with a transceiver to
285 * support cards using voltages other than VDDS (1.8V nominal). When a
286 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
287 *
288 * In some cases this regulator won't support enable/disable;
289 * e.g. it's a fixed rail for a WLAN chip.
290 *
291 * In other cases vcc_aux switches interface power. Example, for
292 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
293 * chips/cards need an interface voltage rail too.
294 */
295 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400296 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800297 /* Enable interface voltage rail, if needed */
298 if (ret == 0 && host->vcc_aux) {
299 ret = regulator_enable(host->vcc_aux);
300 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400301 ret = mmc_regulator_set_ocr(host->mmc,
302 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800303 }
304 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400305 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800306 if (host->vcc_aux)
307 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400308 if (!ret) {
309 /* Then proceed to shut down the local regulator */
310 ret = mmc_regulator_set_ocr(host->mmc,
311 host->vcc, 0);
312 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800313 }
314
315 if (mmc_slot(host).after_set_reg)
316 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
317
318 return ret;
319}
320
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500321static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
322 int vdd)
323{
324 return 0;
325}
326
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800327static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
328 int vdd, int cardsleep)
329{
330 struct omap_hsmmc_host *host =
331 platform_get_drvdata(to_platform_device(dev));
332 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
333
334 return regulator_set_mode(host->vcc, mode);
335}
336
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500337static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800338 int vdd, int cardsleep)
339{
340 struct omap_hsmmc_host *host =
341 platform_get_drvdata(to_platform_device(dev));
342 int err, mode;
343
344 /*
345 * If we don't see a Vcc regulator, assume it's a fixed
346 * voltage always-on regulator.
347 */
348 if (!host->vcc)
349 return 0;
350
351 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
352
353 if (!host->vcc_aux)
354 return regulator_set_mode(host->vcc, mode);
355
356 if (cardsleep) {
357 /* VCC can be turned off if card is asleep */
358 if (sleep)
Linus Walleij99fc5132010-09-29 01:08:27 -0400359 err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400361 err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800362 } else
363 err = regulator_set_mode(host->vcc, mode);
364 if (err)
365 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800366
367 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
368 return regulator_set_mode(host->vcc_aux, mode);
369
370 if (sleep)
371 return regulator_disable(host->vcc_aux);
372 else
373 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800374}
375
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500376static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
377 int vdd, int cardsleep)
378{
379 return 0;
380}
381
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800382static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
383{
384 struct regulator *reg;
385 int ret = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700386 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800387
388 switch (host->id) {
389 case OMAP_MMC1_DEVID:
390 /* On-chip level shifting via PBIAS0/PBIAS1 */
391 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
392 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
393 break;
394 case OMAP_MMC2_DEVID:
395 case OMAP_MMC3_DEVID:
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500396 case OMAP_MMC5_DEVID:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800397 /* Off-chip level shifting, or none */
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500398 mmc_slot(host).set_power = omap_hsmmc_235_set_power;
399 mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800400 break;
Kishore Kadiyala7715db52011-02-15 03:40:36 -0500401 case OMAP_MMC4_DEVID:
402 mmc_slot(host).set_power = omap_hsmmc_4_set_power;
403 mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800404 default:
405 pr_err("MMC%d configuration not supported!\n", host->id);
406 return -EINVAL;
407 }
408
409 reg = regulator_get(host->dev, "vmmc");
410 if (IS_ERR(reg)) {
411 dev_dbg(host->dev, "vmmc regulator missing\n");
412 /*
413 * HACK: until fixed.c regulator is usable,
414 * we don't require a main regulator
415 * for MMC2 or MMC3
416 */
417 if (host->id == OMAP_MMC1_DEVID) {
418 ret = PTR_ERR(reg);
419 goto err;
420 }
421 } else {
422 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700423 ocr_value = mmc_regulator_get_ocrmask(reg);
424 if (!mmc_slot(host).ocr_mask) {
425 mmc_slot(host).ocr_mask = ocr_value;
426 } else {
427 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
428 pr_err("MMC%d ocrmask %x is not supported\n",
429 host->id, mmc_slot(host).ocr_mask);
430 mmc_slot(host).ocr_mask = 0;
431 return -EINVAL;
432 }
433 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800434
435 /* Allow an aux regulator */
436 reg = regulator_get(host->dev, "vmmc_aux");
437 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
438
Balaji T Kb1c1df72011-05-30 19:55:34 +0530439 /* For eMMC do not power off when not in sleep state */
440 if (mmc_slot(host).no_regulator_off_init)
441 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800442 /*
443 * UGLY HACK: workaround regulator framework bugs.
444 * When the bootloader leaves a supply active, it's
445 * initialized with zero usecount ... and we can't
446 * disable it without first enabling it. Until the
447 * framework is fixed, we need a workaround like this
448 * (which is safe for MMC, but not in general).
449 */
450 if (regulator_is_enabled(host->vcc) > 0) {
451 regulator_enable(host->vcc);
452 regulator_disable(host->vcc);
453 }
454 if (host->vcc_aux) {
455 if (regulator_is_enabled(reg) > 0) {
456 regulator_enable(reg);
457 regulator_disable(reg);
458 }
459 }
460 }
461
462 return 0;
463
464err:
465 mmc_slot(host).set_power = NULL;
466 mmc_slot(host).set_sleep = NULL;
467 return ret;
468}
469
470static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
471{
472 regulator_put(host->vcc);
473 regulator_put(host->vcc_aux);
474 mmc_slot(host).set_power = NULL;
475 mmc_slot(host).set_sleep = NULL;
476}
477
Adrian Hunterb702b102010-02-15 10:03:35 -0800478static inline int omap_hsmmc_have_reg(void)
479{
480 return 1;
481}
482
483#else
484
485static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
486{
487 return -EINVAL;
488}
489
490static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
491{
492}
493
494static inline int omap_hsmmc_have_reg(void)
495{
496 return 0;
497}
498
499#endif
500
501static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
502{
503 int ret;
504
505 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800506 if (pdata->slots[0].cover)
507 pdata->slots[0].get_cover_state =
508 omap_hsmmc_get_cover_state;
509 else
510 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
511 pdata->slots[0].card_detect_irq =
512 gpio_to_irq(pdata->slots[0].switch_pin);
513 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
514 if (ret)
515 return ret;
516 ret = gpio_direction_input(pdata->slots[0].switch_pin);
517 if (ret)
518 goto err_free_sp;
519 } else
520 pdata->slots[0].switch_pin = -EINVAL;
521
522 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
523 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
524 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
525 if (ret)
526 goto err_free_cd;
527 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
528 if (ret)
529 goto err_free_wp;
530 } else
531 pdata->slots[0].gpio_wp = -EINVAL;
532
533 return 0;
534
535err_free_wp:
536 gpio_free(pdata->slots[0].gpio_wp);
537err_free_cd:
538 if (gpio_is_valid(pdata->slots[0].switch_pin))
539err_free_sp:
540 gpio_free(pdata->slots[0].switch_pin);
541 return ret;
542}
543
544static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
545{
546 if (gpio_is_valid(pdata->slots[0].gpio_wp))
547 gpio_free(pdata->slots[0].gpio_wp);
548 if (gpio_is_valid(pdata->slots[0].switch_pin))
549 gpio_free(pdata->slots[0].switch_pin);
550}
551
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100552/*
553 * Stop clock to the card
554 */
Denis Karpov70a33412009-09-22 16:44:59 -0700555static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100556{
557 OMAP_HSMMC_WRITE(host->base, SYSCTL,
558 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
559 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
560 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
561}
562
Adrian Hunter93caf8e2010-08-11 14:17:48 -0700563static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
564 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700565{
566 unsigned int irq_mask;
567
568 if (host->use_dma)
569 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
570 else
571 irq_mask = INT_EN_MASK;
572
Adrian Hunter93caf8e2010-08-11 14:17:48 -0700573 /* Disable timeout for erases */
574 if (cmd->opcode == MMC_ERASE)
575 irq_mask &= ~DTO_ENABLE;
576
Adrian Hunterb4175772010-05-26 14:42:06 -0700577 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
578 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
579 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
580}
581
582static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
583{
584 OMAP_HSMMC_WRITE(host->base, ISE, 0);
585 OMAP_HSMMC_WRITE(host->base, IE, 0);
586 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
587}
588
Denis Karpov11dd62a2009-09-22 16:44:43 -0700589#ifdef CONFIG_PM
590
591/*
592 * Restore the MMC host context, if it was lost as result of a
593 * power state change.
594 */
Denis Karpov70a33412009-09-22 16:44:59 -0700595static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700596{
597 struct mmc_ios *ios = &host->mmc->ios;
598 struct omap_mmc_platform_data *pdata = host->pdata;
599 int context_loss = 0;
600 u32 hctl, capa, con;
601 u16 dsor = 0;
602 unsigned long timeout;
603
604 if (pdata->get_context_loss_count) {
605 context_loss = pdata->get_context_loss_count(host->dev);
606 if (context_loss < 0)
607 return 1;
608 }
609
610 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
611 context_loss == host->context_loss ? "not " : "");
612 if (host->context_loss == context_loss)
613 return 1;
614
615 /* Wait for hardware reset */
616 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
617 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
618 && time_before(jiffies, timeout))
619 ;
620
621 /* Do software reset */
622 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
623 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
624 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
625 && time_before(jiffies, timeout))
626 ;
627
628 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
629 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
630
631 if (host->id == OMAP_MMC1_DEVID) {
632 if (host->power_mode != MMC_POWER_OFF &&
633 (1 << ios->vdd) <= MMC_VDD_23_24)
634 hctl = SDVS18;
635 else
636 hctl = SDVS30;
637 capa = VS30 | VS18;
638 } else {
639 hctl = SDVS18;
640 capa = VS18;
641 }
642
643 OMAP_HSMMC_WRITE(host->base, HCTL,
644 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
645
646 OMAP_HSMMC_WRITE(host->base, CAPA,
647 OMAP_HSMMC_READ(host->base, CAPA) | capa);
648
649 OMAP_HSMMC_WRITE(host->base, HCTL,
650 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
651
652 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
653 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
654 && time_before(jiffies, timeout))
655 ;
656
Adrian Hunterb4175772010-05-26 14:42:06 -0700657 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700658
659 /* Do not initialize card-specific things if the power is off */
660 if (host->power_mode == MMC_POWER_OFF)
661 goto out;
662
663 con = OMAP_HSMMC_READ(host->base, CON);
664 switch (ios->bus_width) {
665 case MMC_BUS_WIDTH_8:
666 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
667 break;
668 case MMC_BUS_WIDTH_4:
669 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
670 OMAP_HSMMC_WRITE(host->base, HCTL,
671 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
672 break;
673 case MMC_BUS_WIDTH_1:
674 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
675 OMAP_HSMMC_WRITE(host->base, HCTL,
676 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
677 break;
678 }
679
680 if (ios->clock) {
681 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
682 if (dsor < 1)
683 dsor = 1;
684
685 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
686 dsor++;
687
688 if (dsor > 250)
689 dsor = 250;
690 }
691
692 OMAP_HSMMC_WRITE(host->base, SYSCTL,
693 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
694 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
695 OMAP_HSMMC_WRITE(host->base, SYSCTL,
696 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
697
698 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
699 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
700 && time_before(jiffies, timeout))
701 ;
702
703 OMAP_HSMMC_WRITE(host->base, SYSCTL,
704 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
705
706 con = OMAP_HSMMC_READ(host->base, CON);
707 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
708 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
709 else
710 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
711out:
712 host->context_loss = context_loss;
713
714 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
715 return 0;
716}
717
718/*
719 * Save the MMC host context (store the number of power state changes so far).
720 */
Denis Karpov70a33412009-09-22 16:44:59 -0700721static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700722{
723 struct omap_mmc_platform_data *pdata = host->pdata;
724 int context_loss;
725
726 if (pdata->get_context_loss_count) {
727 context_loss = pdata->get_context_loss_count(host->dev);
728 if (context_loss < 0)
729 return;
730 host->context_loss = context_loss;
731 }
732}
733
734#else
735
Denis Karpov70a33412009-09-22 16:44:59 -0700736static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700737{
738 return 0;
739}
740
Denis Karpov70a33412009-09-22 16:44:59 -0700741static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700742{
743}
744
745#endif
746
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747/*
748 * Send init stream sequence to card
749 * before sending IDLE command
750 */
Denis Karpov70a33412009-09-22 16:44:59 -0700751static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100752{
753 int reg = 0;
754 unsigned long timeout;
755
Adrian Hunterb62f6222009-09-22 16:45:01 -0700756 if (host->protect_card)
757 return;
758
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700760
761 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100762 OMAP_HSMMC_WRITE(host->base, CON,
763 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
764 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
765
766 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
767 while ((reg != CC) && time_before(jiffies, timeout))
768 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
769
770 OMAP_HSMMC_WRITE(host->base, CON,
771 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700772
773 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
774 OMAP_HSMMC_READ(host->base, STAT);
775
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 enable_irq(host->irq);
777}
778
779static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700780int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781{
782 int r = 1;
783
Denis Karpov191d1f12009-09-22 16:44:55 -0700784 if (mmc_slot(host).get_cover_state)
785 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786 return r;
787}
788
789static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700790omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100791 char *buf)
792{
793 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700794 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100795
Denis Karpov70a33412009-09-22 16:44:59 -0700796 return sprintf(buf, "%s\n",
797 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798}
799
Denis Karpov70a33412009-09-22 16:44:59 -0700800static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100801
802static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700803omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100804 char *buf)
805{
806 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700807 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808
Denis Karpov191d1f12009-09-22 16:44:55 -0700809 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100810}
811
Denis Karpov70a33412009-09-22 16:44:59 -0700812static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100813
814/*
815 * Configure the response type and send the cmd.
816 */
817static void
Denis Karpov70a33412009-09-22 16:44:59 -0700818omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100819 struct mmc_data *data)
820{
821 int cmdreg = 0, resptype = 0, cmdtype = 0;
822
823 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
824 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
825 host->cmd = cmd;
826
Adrian Hunter93caf8e2010-08-11 14:17:48 -0700827 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200829 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830 if (cmd->flags & MMC_RSP_PRESENT) {
831 if (cmd->flags & MMC_RSP_136)
832 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200833 else if (cmd->flags & MMC_RSP_BUSY) {
834 resptype = 3;
835 host->response_busy = 1;
836 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837 resptype = 2;
838 }
839
840 /*
841 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
842 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
843 * a val of 0x3, rest 0x0.
844 */
845 if (cmd == host->mrq->stop)
846 cmdtype = 0x3;
847
848 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
849
850 if (data) {
851 cmdreg |= DP_SELECT | MSBS | BCE;
852 if (data->flags & MMC_DATA_READ)
853 cmdreg |= DDIR;
854 else
855 cmdreg &= ~(DDIR);
856 }
857
858 if (host->use_dma)
859 cmdreg |= DMA_EN;
860
Adrian Hunterb4175772010-05-26 14:42:06 -0700861 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700862
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100863 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
864 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
865}
866
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200867static int
Denis Karpov70a33412009-09-22 16:44:59 -0700868omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200869{
870 if (data->flags & MMC_DATA_WRITE)
871 return DMA_TO_DEVICE;
872 else
873 return DMA_FROM_DEVICE;
874}
875
Adrian Hunterb4175772010-05-26 14:42:06 -0700876static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
877{
878 int dma_ch;
879
880 spin_lock(&host->irq_lock);
881 host->req_in_progress = 0;
882 dma_ch = host->dma_ch;
883 spin_unlock(&host->irq_lock);
884
885 omap_hsmmc_disable_irq(host);
886 /* Do not complete the request if DMA is still in progress */
887 if (mrq->data && host->use_dma && dma_ch != -1)
888 return;
889 host->mrq = NULL;
890 mmc_request_done(host->mmc, mrq);
891}
892
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100893/*
894 * Notify the transfer complete to MMC core
895 */
896static void
Denis Karpov70a33412009-09-22 16:44:59 -0700897omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100898{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200899 if (!data) {
900 struct mmc_request *mrq = host->mrq;
901
Adrian Hunter23050102009-09-22 16:44:57 -0700902 /* TC before CC from CMD6 - don't know why, but it happens */
903 if (host->cmd && host->cmd->opcode == 6 &&
904 host->response_busy) {
905 host->response_busy = 0;
906 return;
907 }
908
Adrian Hunterb4175772010-05-26 14:42:06 -0700909 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200910 return;
911 }
912
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100913 host->data = NULL;
914
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915 if (!data->error)
916 data->bytes_xfered += data->blocks * (data->blksz);
917 else
918 data->bytes_xfered = 0;
919
920 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700921 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100922 return;
923 }
Denis Karpov70a33412009-09-22 16:44:59 -0700924 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100925}
926
927/*
928 * Notify the core about command completion
929 */
930static void
Denis Karpov70a33412009-09-22 16:44:59 -0700931omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100932{
933 host->cmd = NULL;
934
935 if (cmd->flags & MMC_RSP_PRESENT) {
936 if (cmd->flags & MMC_RSP_136) {
937 /* response type 2 */
938 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
939 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
940 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
941 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
942 } else {
943 /* response types 1, 1b, 3, 4, 5, 6 */
944 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
945 }
946 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700947 if ((host->data == NULL && !host->response_busy) || cmd->error)
948 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100949}
950
951/*
952 * DMA clean up for command errors
953 */
Denis Karpov70a33412009-09-22 16:44:59 -0700954static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955{
Adrian Hunterb4175772010-05-26 14:42:06 -0700956 int dma_ch;
957
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200958 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959
Adrian Hunterb4175772010-05-26 14:42:06 -0700960 spin_lock(&host->irq_lock);
961 dma_ch = host->dma_ch;
962 host->dma_ch = -1;
963 spin_unlock(&host->irq_lock);
964
965 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200966 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
967 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700968 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700969 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100970 }
971 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100972}
973
974/*
975 * Readable error output
976 */
977#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700978static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100979{
980 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700981 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
983 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
984 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
985 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
986 };
987 char res[256];
988 char *buf = res;
989 int len, i;
990
991 len = sprintf(buf, "MMC IRQ 0x%x :", status);
992 buf += len;
993
Denis Karpov70a33412009-09-22 16:44:59 -0700994 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700996 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997 buf += len;
998 }
999
1000 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
1001}
1002#endif /* CONFIG_MMC_DEBUG */
1003
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001004/*
1005 * MMC controller internal state machines reset
1006 *
1007 * Used to reset command or data internal state machines, using respectively
1008 * SRC or SRD bit of SYSCTL register
1009 * Can be called from interrupt context
1010 */
Denis Karpov70a33412009-09-22 16:44:59 -07001011static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1012 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001013{
1014 unsigned long i = 0;
1015 unsigned long limit = (loops_per_jiffy *
1016 msecs_to_jiffies(MMC_TIMEOUT_MS));
1017
1018 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1019 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1020
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001021 /*
1022 * OMAP4 ES2 and greater has an updated reset logic.
1023 * Monitor a 0->1 transition first
1024 */
1025 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001026 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001027 && (i++ < limit))
1028 cpu_relax();
1029 }
1030 i = 0;
1031
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001032 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1033 (i++ < limit))
1034 cpu_relax();
1035
1036 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1037 dev_err(mmc_dev(host->mmc),
1038 "Timeout waiting on controller reset in %s\n",
1039 __func__);
1040}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001041
Adrian Hunterb4175772010-05-26 14:42:06 -07001042static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001043{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001044 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001045 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046
Adrian Hunterb4175772010-05-26 14:42:06 -07001047 if (!host->req_in_progress) {
1048 do {
1049 OMAP_HSMMC_WRITE(host->base, STAT, status);
1050 /* Flush posted write */
1051 status = OMAP_HSMMC_READ(host->base, STAT);
1052 } while (status & INT_EN_MASK);
1053 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001054 }
1055
1056 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001057 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1058
1059 if (status & ERR) {
1060#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001061 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001062#endif
1063 if ((status & CMD_TIMEOUT) ||
1064 (status & CMD_CRC)) {
1065 if (host->cmd) {
1066 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001067 omap_hsmmc_reset_controller_fsm(host,
1068 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001069 host->cmd->error = -ETIMEDOUT;
1070 } else {
1071 host->cmd->error = -EILSEQ;
1072 }
1073 end_cmd = 1;
1074 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001075 if (host->data || host->response_busy) {
1076 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001077 omap_hsmmc_dma_cleanup(host,
1078 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001079 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001080 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001081 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001082 }
1083 if ((status & DATA_TIMEOUT) ||
1084 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001085 if (host->data || host->response_busy) {
1086 int err = (status & DATA_TIMEOUT) ?
1087 -ETIMEDOUT : -EILSEQ;
1088
1089 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001090 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001091 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001092 host->mrq->cmd->error = err;
1093 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001094 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095 end_trans = 1;
1096 }
1097 }
1098 if (status & CARD_ERR) {
1099 dev_dbg(mmc_dev(host->mmc),
1100 "Ignoring card err CMD%d\n", host->cmd->opcode);
1101 if (host->cmd)
1102 end_cmd = 1;
1103 if (host->data)
1104 end_trans = 1;
1105 }
1106 }
1107
1108 OMAP_HSMMC_WRITE(host->base, STAT, status);
1109
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001110 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001111 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001112 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001113 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001114}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001115
Adrian Hunterb4175772010-05-26 14:42:06 -07001116/*
1117 * MMC controller IRQ handler
1118 */
1119static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1120{
1121 struct omap_hsmmc_host *host = dev_id;
1122 int status;
1123
1124 status = OMAP_HSMMC_READ(host->base, STAT);
1125 do {
1126 omap_hsmmc_do_irq(host, status);
1127 /* Flush posted write */
1128 status = OMAP_HSMMC_READ(host->base, STAT);
1129 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001130
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001131 return IRQ_HANDLED;
1132}
1133
Denis Karpov70a33412009-09-22 16:44:59 -07001134static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001135{
1136 unsigned long i;
1137
1138 OMAP_HSMMC_WRITE(host->base, HCTL,
1139 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1140 for (i = 0; i < loops_per_jiffy; i++) {
1141 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1142 break;
1143 cpu_relax();
1144 }
1145}
1146
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001147/*
David Brownelleb250822009-02-17 14:49:01 -08001148 * Switch MMC interface voltage ... only relevant for MMC1.
1149 *
1150 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1151 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1152 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001153 */
Denis Karpov70a33412009-09-22 16:44:59 -07001154static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001155{
1156 u32 reg_val = 0;
1157 int ret;
1158
1159 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301160 pm_runtime_put_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001161 if (host->got_dbclk)
1162 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163
1164 /* Turn the power off */
1165 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001166
1167 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001168 if (!ret)
1169 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1170 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301171 pm_runtime_get_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001172 if (host->got_dbclk)
1173 clk_enable(host->dbclk);
1174
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175 if (ret != 0)
1176 goto err;
1177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178 OMAP_HSMMC_WRITE(host->base, HCTL,
1179 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1180 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001181
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182 /*
1183 * If a MMC dual voltage card is detected, the set_ios fn calls
1184 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001185 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001186 *
David Brownelleb250822009-02-17 14:49:01 -08001187 * Cope with a bit of slop in the range ... per data sheets:
1188 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1189 * but recommended values are 1.71V to 1.89V
1190 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1191 * but recommended values are 2.7V to 3.3V
1192 *
1193 * Board setup code shouldn't permit anything very out-of-range.
1194 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1195 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 */
David Brownelleb250822009-02-17 14:49:01 -08001197 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001199 else
1200 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201
1202 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001203 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
1205 return 0;
1206err:
1207 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1208 return ret;
1209}
1210
Adrian Hunterb62f6222009-09-22 16:45:01 -07001211/* Protect the card while the cover is open */
1212static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1213{
1214 if (!mmc_slot(host).get_cover_state)
1215 return;
1216
1217 host->reqs_blocked = 0;
1218 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1219 if (host->protect_card) {
1220 printk(KERN_INFO "%s: cover is closed, "
1221 "card is now accessible\n",
1222 mmc_hostname(host->mmc));
1223 host->protect_card = 0;
1224 }
1225 } else {
1226 if (!host->protect_card) {
1227 printk(KERN_INFO "%s: cover is open, "
1228 "card is now inaccessible\n",
1229 mmc_hostname(host->mmc));
1230 host->protect_card = 1;
1231 }
1232 }
1233}
1234
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235/*
1236 * Work Item to notify the core about card insertion/removal
1237 */
Denis Karpov70a33412009-09-22 16:44:59 -07001238static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001239{
Denis Karpov70a33412009-09-22 16:44:59 -07001240 struct omap_hsmmc_host *host =
1241 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001242 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001243 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001244
Adrian Huntera6b22402009-09-22 16:44:45 -07001245 if (host->suspended)
1246 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001247
1248 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001249
Denis Karpov191d1f12009-09-22 16:44:55 -07001250 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001251 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001252 else {
1253 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001254 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001255 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001256
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001257 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001258 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001259 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001260 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001261}
1262
1263/*
1264 * ISR for handling card insertion and removal
1265 */
Denis Karpov70a33412009-09-22 16:44:59 -07001266static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267{
Denis Karpov70a33412009-09-22 16:44:59 -07001268 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269
Adrian Huntera6b22402009-09-22 16:44:45 -07001270 if (host->suspended)
1271 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272 schedule_work(&host->mmc_carddetect_work);
1273
1274 return IRQ_HANDLED;
1275}
1276
Denis Karpov70a33412009-09-22 16:44:59 -07001277static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001278 struct mmc_data *data)
1279{
1280 int sync_dev;
1281
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001282 if (data->flags & MMC_DATA_WRITE)
1283 sync_dev = host->dma_line_tx;
1284 else
1285 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001286 return sync_dev;
1287}
1288
Denis Karpov70a33412009-09-22 16:44:59 -07001289static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001290 struct mmc_data *data,
1291 struct scatterlist *sgl)
1292{
1293 int blksz, nblk, dma_ch;
1294
1295 dma_ch = host->dma_ch;
1296 if (data->flags & MMC_DATA_WRITE) {
1297 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1298 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1299 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1300 sg_dma_address(sgl), 0, 0);
1301 } else {
1302 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001303 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001304 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1305 sg_dma_address(sgl), 0, 0);
1306 }
1307
1308 blksz = host->data->blksz;
1309 nblk = sg_dma_len(sgl) / blksz;
1310
1311 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1312 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001313 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001314 !(data->flags & MMC_DATA_WRITE));
1315
1316 omap_start_dma(dma_ch);
1317}
1318
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001319/*
1320 * DMA call back function
1321 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001322static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001323{
Adrian Hunterb4175772010-05-26 14:42:06 -07001324 struct omap_hsmmc_host *host = cb_data;
1325 struct mmc_data *data = host->mrq->data;
1326 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001327
Venkatraman Sf3584e52010-08-10 18:01:54 -07001328 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1329 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1330 ch_status);
1331 return;
1332 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333
Adrian Hunterb4175772010-05-26 14:42:06 -07001334 spin_lock(&host->irq_lock);
1335 if (host->dma_ch < 0) {
1336 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001338 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001340 host->dma_sg_idx++;
1341 if (host->dma_sg_idx < host->dma_len) {
1342 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001343 omap_hsmmc_config_dma_params(host, data,
1344 data->sg + host->dma_sg_idx);
1345 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001346 return;
1347 }
1348
Per Forlin9782aff2011-07-01 18:55:23 +02001349 if (!data->host_cookie)
1350 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1351 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001352
1353 req_in_progress = host->req_in_progress;
1354 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001355 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001356 spin_unlock(&host->irq_lock);
1357
1358 omap_free_dma(dma_ch);
1359
1360 /* If DMA has finished after TC, complete the request */
1361 if (!req_in_progress) {
1362 struct mmc_request *mrq = host->mrq;
1363
1364 host->mrq = NULL;
1365 mmc_request_done(host->mmc, mrq);
1366 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001367}
1368
Per Forlin9782aff2011-07-01 18:55:23 +02001369static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1370 struct mmc_data *data,
1371 struct omap_hsmmc_next *next)
1372{
1373 int dma_len;
1374
1375 if (!next && data->host_cookie &&
1376 data->host_cookie != host->next_data.cookie) {
1377 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
1378 " host->next_data.cookie %d\n",
1379 __func__, data->host_cookie, host->next_data.cookie);
1380 data->host_cookie = 0;
1381 }
1382
1383 /* Check if next job is already prepared */
1384 if (next ||
1385 (!next && data->host_cookie != host->next_data.cookie)) {
1386 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1387 data->sg_len,
1388 omap_hsmmc_get_dma_dir(host, data));
1389
1390 } else {
1391 dma_len = host->next_data.dma_len;
1392 host->next_data.dma_len = 0;
1393 }
1394
1395
1396 if (dma_len == 0)
1397 return -EINVAL;
1398
1399 if (next) {
1400 next->dma_len = dma_len;
1401 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1402 } else
1403 host->dma_len = dma_len;
1404
1405 return 0;
1406}
1407
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001408/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409 * Routine to configure and start DMA for the MMC card
1410 */
Denis Karpov70a33412009-09-22 16:44:59 -07001411static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1412 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001413{
Adrian Hunterb4175772010-05-26 14:42:06 -07001414 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415 struct mmc_data *data = req->data;
1416
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001417 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001418 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001419 struct scatterlist *sgl;
1420
1421 sgl = data->sg + i;
1422 if (sgl->length % data->blksz)
1423 return -EINVAL;
1424 }
1425 if ((data->blksz % 4) != 0)
1426 /* REVISIT: The MMC buffer increments only when MSB is written.
1427 * Return error for blksz which is non multiple of four.
1428 */
1429 return -EINVAL;
1430
Adrian Hunterb4175772010-05-26 14:42:06 -07001431 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001432
Denis Karpov70a33412009-09-22 16:44:59 -07001433 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1434 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001436 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001437 "%s: omap_request_dma() failed with %d\n",
1438 mmc_hostname(host->mmc), ret);
1439 return ret;
1440 }
Per Forlin9782aff2011-07-01 18:55:23 +02001441 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1442 if (ret)
1443 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001444
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001446 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447
Denis Karpov70a33412009-09-22 16:44:59 -07001448 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450 return 0;
1451}
1452
Denis Karpov70a33412009-09-22 16:44:59 -07001453static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001454 unsigned int timeout_ns,
1455 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001456{
1457 unsigned int timeout, cycle_ns;
1458 uint32_t reg, clkd, dto = 0;
1459
1460 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1461 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1462 if (clkd == 0)
1463 clkd = 1;
1464
1465 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001466 timeout = timeout_ns / cycle_ns;
1467 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001468 if (timeout) {
1469 while ((timeout & 0x80000000) == 0) {
1470 dto += 1;
1471 timeout <<= 1;
1472 }
1473 dto = 31 - dto;
1474 timeout <<= 1;
1475 if (timeout && dto)
1476 dto += 1;
1477 if (dto >= 13)
1478 dto -= 13;
1479 else
1480 dto = 0;
1481 if (dto > 14)
1482 dto = 14;
1483 }
1484
1485 reg &= ~DTO_MASK;
1486 reg |= dto << DTO_SHIFT;
1487 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1488}
1489
1490/*
1491 * Configure block length for MMC/SD cards and initiate the transfer.
1492 */
1493static int
Denis Karpov70a33412009-09-22 16:44:59 -07001494omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001495{
1496 int ret;
1497 host->data = req->data;
1498
1499 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001500 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001501 /*
1502 * Set an arbitrary 100ms data timeout for commands with
1503 * busy signal.
1504 */
1505 if (req->cmd->flags & MMC_RSP_BUSY)
1506 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001507 return 0;
1508 }
1509
1510 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1511 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001512 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001513
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001515 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516 if (ret != 0) {
1517 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1518 return ret;
1519 }
1520 }
1521 return 0;
1522}
1523
Per Forlin9782aff2011-07-01 18:55:23 +02001524static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1525 int err)
1526{
1527 struct omap_hsmmc_host *host = mmc_priv(mmc);
1528 struct mmc_data *data = mrq->data;
1529
1530 if (host->use_dma) {
1531 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1532 omap_hsmmc_get_dma_dir(host, data));
1533 data->host_cookie = 0;
1534 }
1535}
1536
1537static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1538 bool is_first_req)
1539{
1540 struct omap_hsmmc_host *host = mmc_priv(mmc);
1541
1542 if (mrq->data->host_cookie) {
1543 mrq->data->host_cookie = 0;
1544 return ;
1545 }
1546
1547 if (host->use_dma)
1548 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1549 &host->next_data))
1550 mrq->data->host_cookie = 0;
1551}
1552
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553/*
1554 * Request function. for read/write operation
1555 */
Denis Karpov70a33412009-09-22 16:44:59 -07001556static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557{
Denis Karpov70a33412009-09-22 16:44:59 -07001558 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001559 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560
Adrian Hunterb4175772010-05-26 14:42:06 -07001561 BUG_ON(host->req_in_progress);
1562 BUG_ON(host->dma_ch != -1);
1563 if (host->protect_card) {
1564 if (host->reqs_blocked < 3) {
1565 /*
1566 * Ensure the controller is left in a consistent
1567 * state by resetting the command and data state
1568 * machines.
1569 */
1570 omap_hsmmc_reset_controller_fsm(host, SRD);
1571 omap_hsmmc_reset_controller_fsm(host, SRC);
1572 host->reqs_blocked += 1;
1573 }
1574 req->cmd->error = -EBADF;
1575 if (req->data)
1576 req->data->error = -EBADF;
1577 req->cmd->retries = 0;
1578 mmc_request_done(mmc, req);
1579 return;
1580 } else if (host->reqs_blocked)
1581 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001582 WARN_ON(host->mrq != NULL);
1583 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001584 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001585 if (err) {
1586 req->cmd->error = err;
1587 if (req->data)
1588 req->data->error = err;
1589 host->mrq = NULL;
1590 mmc_request_done(mmc, req);
1591 return;
1592 }
1593
Denis Karpov70a33412009-09-22 16:44:59 -07001594 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001595}
1596
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001597/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001598static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001599{
Denis Karpov70a33412009-09-22 16:44:59 -07001600 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001601 u16 dsor = 0;
1602 unsigned long regval;
1603 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001604 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001605 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001606
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301607 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001608
Adrian Huntera3621462009-09-22 16:44:42 -07001609 if (ios->power_mode != host->power_mode) {
1610 switch (ios->power_mode) {
1611 case MMC_POWER_OFF:
1612 mmc_slot(host).set_power(host->dev, host->slot_id,
1613 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001614 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001615 break;
1616 case MMC_POWER_UP:
1617 mmc_slot(host).set_power(host->dev, host->slot_id,
1618 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001619 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001620 break;
1621 case MMC_POWER_ON:
1622 do_send_init_stream = 1;
1623 break;
1624 }
1625 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001626 }
1627
Denis Karpovdd498ef2009-09-22 16:44:49 -07001628 /* FIXME: set registers based only on changes to ios */
1629
Jarkko Lavinen73153012008-11-21 16:49:54 +02001630 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001631 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001632 case MMC_BUS_WIDTH_8:
1633 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1634 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001635 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001636 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001637 OMAP_HSMMC_WRITE(host->base, HCTL,
1638 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1639 break;
1640 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001641 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001642 OMAP_HSMMC_WRITE(host->base, HCTL,
1643 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1644 break;
1645 }
1646
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301647 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001648 /* Only MMC1 can interface at 3V without some flavor
1649 * of external transceiver; but they all handle 1.8V.
1650 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001651 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1652 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1653 /*
1654 * The mmc_select_voltage fn of the core does
1655 * not seem to set the power_mode to
1656 * MMC_POWER_UP upon recalculating the voltage.
1657 * vdd 1.8v.
1658 */
Denis Karpov70a33412009-09-22 16:44:59 -07001659 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1660 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001661 "Switch operation failed\n");
1662 }
1663 }
1664
1665 if (ios->clock) {
1666 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1667 if (dsor < 1)
1668 dsor = 1;
1669
1670 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1671 dsor++;
1672
1673 if (dsor > 250)
1674 dsor = 250;
1675 }
Denis Karpov70a33412009-09-22 16:44:59 -07001676 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001677 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1678 regval = regval & ~(CLKD_MASK);
1679 regval = regval | (dsor << 6) | (DTO << 16);
1680 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1681 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1682 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1683
1684 /* Wait till the ICS bit is set */
1685 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001686 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001687 && time_before(jiffies, timeout))
1688 msleep(1);
1689
1690 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1691 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1692
Adrian Huntera3621462009-09-22 16:44:42 -07001693 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001694 send_init_stream(host);
1695
Denis Karpovabb28e72009-09-22 16:44:44 -07001696 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001697 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001698 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1699 else
1700 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001701
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301702 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001703}
1704
1705static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1706{
Denis Karpov70a33412009-09-22 16:44:59 -07001707 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001708
Denis Karpov191d1f12009-09-22 16:44:55 -07001709 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001710 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001711 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001712}
1713
1714static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1715{
Denis Karpov70a33412009-09-22 16:44:59 -07001716 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001717
Denis Karpov191d1f12009-09-22 16:44:55 -07001718 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001719 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001720 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001721}
1722
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001723static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1724{
1725 struct omap_hsmmc_host *host = mmc_priv(mmc);
1726
1727 if (mmc_slot(host).init_card)
1728 mmc_slot(host).init_card(card);
1729}
1730
Denis Karpov70a33412009-09-22 16:44:59 -07001731static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001732{
1733 u32 hctl, capa, value;
1734
1735 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301736 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001737 hctl = SDVS30;
1738 capa = VS30 | VS18;
1739 } else {
1740 hctl = SDVS18;
1741 capa = VS18;
1742 }
1743
1744 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1745 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1746
1747 value = OMAP_HSMMC_READ(host->base, CAPA);
1748 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1749
1750 /* Set the controller to AUTO IDLE mode */
1751 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1752 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1753
1754 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001755 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001756}
1757
Denis Karpov70a33412009-09-22 16:44:59 -07001758static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001759{
Denis Karpov70a33412009-09-22 16:44:59 -07001760 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001761
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301762 pm_runtime_get_sync(host->dev);
1763
Denis Karpovdd498ef2009-09-22 16:44:49 -07001764 return 0;
1765}
1766
Denis Karpov70a33412009-09-22 16:44:59 -07001767static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001768{
Denis Karpov70a33412009-09-22 16:44:59 -07001769 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001770
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301771 pm_runtime_mark_last_busy(host->dev);
1772 pm_runtime_put_autosuspend(host->dev);
1773
Denis Karpovdd498ef2009-09-22 16:44:49 -07001774 return 0;
1775}
1776
Denis Karpov70a33412009-09-22 16:44:59 -07001777static const struct mmc_host_ops omap_hsmmc_ops = {
1778 .enable = omap_hsmmc_enable_fclk,
1779 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001780 .post_req = omap_hsmmc_post_req,
1781 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001782 .request = omap_hsmmc_request,
1783 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001784 .get_cd = omap_hsmmc_get_cd,
1785 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001786 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001787 /* NYET -- enable_sdio_irq */
1788};
1789
Denis Karpovd900f712009-09-22 16:44:38 -07001790#ifdef CONFIG_DEBUG_FS
1791
Denis Karpov70a33412009-09-22 16:44:59 -07001792static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001793{
1794 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001795 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001796 int context_loss = 0;
1797
Denis Karpov70a33412009-09-22 16:44:59 -07001798 if (host->pdata->get_context_loss_count)
1799 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001800
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001801 seq_printf(s, "mmc%d:\n"
1802 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001803 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001804 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001805 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001806 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001807 mmc->index, mmc->enabled ? 1 : 0,
1808 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001809 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001810
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301811 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001812 seq_printf(s, "host suspended, can't read registers\n");
1813 return 0;
1814 }
1815
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301816 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001817
1818 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1819 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1820 seq_printf(s, "CON:\t\t0x%08x\n",
1821 OMAP_HSMMC_READ(host->base, CON));
1822 seq_printf(s, "HCTL:\t\t0x%08x\n",
1823 OMAP_HSMMC_READ(host->base, HCTL));
1824 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1825 OMAP_HSMMC_READ(host->base, SYSCTL));
1826 seq_printf(s, "IE:\t\t0x%08x\n",
1827 OMAP_HSMMC_READ(host->base, IE));
1828 seq_printf(s, "ISE:\t\t0x%08x\n",
1829 OMAP_HSMMC_READ(host->base, ISE));
1830 seq_printf(s, "CAPA:\t\t0x%08x\n",
1831 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001832
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301833 pm_runtime_mark_last_busy(host->dev);
1834 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001835
Denis Karpovd900f712009-09-22 16:44:38 -07001836 return 0;
1837}
1838
Denis Karpov70a33412009-09-22 16:44:59 -07001839static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001840{
Denis Karpov70a33412009-09-22 16:44:59 -07001841 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001842}
1843
1844static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001845 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001846 .read = seq_read,
1847 .llseek = seq_lseek,
1848 .release = single_release,
1849};
1850
Denis Karpov70a33412009-09-22 16:44:59 -07001851static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001852{
1853 if (mmc->debugfs_root)
1854 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1855 mmc, &mmc_regs_fops);
1856}
1857
1858#else
1859
Denis Karpov70a33412009-09-22 16:44:59 -07001860static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001861{
1862}
1863
1864#endif
1865
Denis Karpov70a33412009-09-22 16:44:59 -07001866static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001867{
1868 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1869 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001870 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001871 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001872 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001873
1874 if (pdata == NULL) {
1875 dev_err(&pdev->dev, "Platform Data is missing\n");
1876 return -ENXIO;
1877 }
1878
1879 if (pdata->nr_slots == 0) {
1880 dev_err(&pdev->dev, "No Slots\n");
1881 return -ENXIO;
1882 }
1883
1884 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1885 irq = platform_get_irq(pdev, 0);
1886 if (res == NULL || irq < 0)
1887 return -ENXIO;
1888
kishore kadiyala91a0b082010-10-01 16:35:28 -07001889 res->start += pdata->reg_offset;
1890 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07001891 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892 if (res == NULL)
1893 return -EBUSY;
1894
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001895 ret = omap_hsmmc_gpio_init(pdata);
1896 if (ret)
1897 goto err;
1898
Denis Karpov70a33412009-09-22 16:44:59 -07001899 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001900 if (!mmc) {
1901 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001902 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001903 }
1904
1905 host = mmc_priv(mmc);
1906 host->mmc = mmc;
1907 host->pdata = pdata;
1908 host->dev = &pdev->dev;
1909 host->use_dma = 1;
1910 host->dev->dma_mask = &pdata->dma_mask;
1911 host->dma_ch = -1;
1912 host->irq = irq;
1913 host->id = pdev->id;
1914 host->slot_id = 0;
1915 host->mapbase = res->start;
1916 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001917 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001918 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001919
1920 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07001921 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001922
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301923 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001924
Adrian Huntere0eb2422010-02-15 10:03:34 -08001925 /*
1926 * If regulator_disable can only put vcc_aux to sleep then there is
1927 * no off state.
1928 */
1929 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1930 mmc_slot(host).no_off = 1;
1931
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001932 mmc->f_min = 400000;
1933 mmc->f_max = 52000000;
1934
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001935 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001936
Russell King6f7607c2009-01-28 10:22:50 +00001937 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001938 if (IS_ERR(host->fclk)) {
1939 ret = PTR_ERR(host->fclk);
1940 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001941 goto err1;
1942 }
1943
Denis Karpov70a33412009-09-22 16:44:59 -07001944 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001945
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001946 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001947
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301948 pm_runtime_enable(host->dev);
1949 pm_runtime_get_sync(host->dev);
1950 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1951 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952
Adrian Hunter2bec0892009-09-22 16:45:02 -07001953 if (cpu_is_omap2430()) {
1954 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1955 /*
1956 * MMC can still work without debounce clock.
1957 */
1958 if (IS_ERR(host->dbclk))
1959 dev_warn(mmc_dev(host->mmc),
1960 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001962 host->got_dbclk = 1;
1963
1964 if (host->got_dbclk)
1965 if (clk_enable(host->dbclk) != 0)
1966 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1967 " clk failed\n");
1968 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001969
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001970 /* Since we do only SG emulation, we can have as many segs
1971 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001972 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001973
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001974 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1975 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1976 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1977 mmc->max_seg_size = mmc->max_req_size;
1978
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001979 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e2010-08-11 14:17:48 -07001980 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001982 mmc->caps |= mmc_slot(host).caps;
1983 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001984 mmc->caps |= MMC_CAP_4_BIT_DATA;
1985
Denis Karpov191d1f12009-09-22 16:44:55 -07001986 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001987 mmc->caps |= MMC_CAP_NONREMOVABLE;
1988
Denis Karpov70a33412009-09-22 16:44:59 -07001989 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001991 /* Select DMA lines */
1992 switch (host->id) {
1993 case OMAP_MMC1_DEVID:
1994 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1995 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1996 break;
1997 case OMAP_MMC2_DEVID:
1998 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
1999 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2000 break;
2001 case OMAP_MMC3_DEVID:
2002 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2003 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2004 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002005 case OMAP_MMC4_DEVID:
2006 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2007 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2008 break;
2009 case OMAP_MMC5_DEVID:
2010 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2011 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2012 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002013 default:
2014 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2015 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002016 }
2017
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002018 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002019 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020 mmc_hostname(mmc), host);
2021 if (ret) {
2022 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2023 goto err_irq;
2024 }
2025
2026 if (pdata->init != NULL) {
2027 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002028 dev_dbg(mmc_dev(host->mmc),
2029 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030 goto err_irq_cd_init;
2031 }
2032 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002033
Adrian Hunterb702b102010-02-15 10:03:35 -08002034 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002035 ret = omap_hsmmc_reg_get(host);
2036 if (ret)
2037 goto err_reg;
2038 host->use_reg = 1;
2039 }
2040
David Brownellb583f262009-05-28 14:04:03 -07002041 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002042
2043 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002044 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002046 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2048 | IRQF_DISABLED,
2049 mmc_hostname(mmc), host);
2050 if (ret) {
2051 dev_dbg(mmc_dev(host->mmc),
2052 "Unable to grab MMC CD IRQ\n");
2053 goto err_irq_cd;
2054 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002055 pdata->suspend = omap_hsmmc_suspend_cdirq;
2056 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002057 }
2058
Adrian Hunterb4175772010-05-26 14:42:06 -07002059 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060
Adrian Hunterb62f6222009-09-22 16:45:01 -07002061 omap_hsmmc_protect_card(host);
2062
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002063 mmc_add_host(mmc);
2064
Denis Karpov191d1f12009-09-22 16:44:55 -07002065 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2067 if (ret < 0)
2068 goto err_slot_name;
2069 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002070 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071 ret = device_create_file(&mmc->class_dev,
2072 &dev_attr_cover_switch);
2073 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002074 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075 }
2076
Denis Karpov70a33412009-09-22 16:44:59 -07002077 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302078 pm_runtime_mark_last_busy(host->dev);
2079 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002080
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081 return 0;
2082
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002083err_slot_name:
2084 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002085 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002086err_irq_cd:
2087 if (host->use_reg)
2088 omap_hsmmc_reg_put(host);
2089err_reg:
2090 if (host->pdata->cleanup)
2091 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002092err_irq_cd_init:
2093 free_irq(host->irq, host);
2094err_irq:
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302095 pm_runtime_mark_last_busy(host->dev);
2096 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002098 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002099 clk_disable(host->dbclk);
2100 clk_put(host->dbclk);
2101 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002102err1:
2103 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002104 platform_set_drvdata(pdev, NULL);
2105 mmc_free_host(mmc);
2106err_alloc:
2107 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108err:
Chris Ball984b2032011-03-22 16:34:42 -07002109 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002110 return ret;
2111}
2112
Denis Karpov70a33412009-09-22 16:44:59 -07002113static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002114{
Denis Karpov70a33412009-09-22 16:44:59 -07002115 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116 struct resource *res;
2117
2118 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302119 pm_runtime_get_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002120 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002121 if (host->use_reg)
2122 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002123 if (host->pdata->cleanup)
2124 host->pdata->cleanup(&pdev->dev);
2125 free_irq(host->irq, host);
2126 if (mmc_slot(host).card_detect_irq)
2127 free_irq(mmc_slot(host).card_detect_irq, host);
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002128 flush_work_sync(&host->mmc_carddetect_work);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002129
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302130 pm_runtime_put_sync(host->dev);
2131 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002132 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002133 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002134 clk_disable(host->dbclk);
2135 clk_put(host->dbclk);
2136 }
2137
2138 mmc_free_host(host->mmc);
2139 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002140 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141 }
2142
2143 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2144 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002145 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 platform_set_drvdata(pdev, NULL);
2147
2148 return 0;
2149}
2150
2151#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002152static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153{
2154 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002155 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002156 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002157
2158 if (host && host->suspended)
2159 return 0;
2160
2161 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302162 pm_runtime_get_sync(host->dev);
Adrian Huntera6b22402009-09-22 16:44:45 -07002163 host->suspended = 1;
2164 if (host->pdata->suspend) {
2165 ret = host->pdata->suspend(&pdev->dev,
2166 host->slot_id);
2167 if (ret) {
2168 dev_dbg(mmc_dev(host->mmc),
2169 "Unable to handle MMC board"
2170 " level suspend\n");
2171 host->suspended = 0;
2172 return ret;
2173 }
2174 }
2175 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002176 ret = mmc_suspend_host(host->mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002178 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002179 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002180 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002181 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002182 if (host->got_dbclk)
2183 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002184 } else {
2185 host->suspended = 0;
2186 if (host->pdata->resume) {
2187 ret = host->pdata->resume(&pdev->dev,
2188 host->slot_id);
2189 if (ret)
2190 dev_dbg(mmc_dev(host->mmc),
2191 "Unmask interrupt failed\n");
2192 }
Adrian Huntera6b22402009-09-22 16:44:45 -07002193 }
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302194 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002195 }
2196 return ret;
2197}
2198
2199/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002200static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002201{
2202 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002203 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002204 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002205
2206 if (host && !host->suspended)
2207 return 0;
2208
2209 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302210 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002211
Adrian Hunter2bec0892009-09-22 16:45:02 -07002212 if (host->got_dbclk)
2213 clk_enable(host->dbclk);
2214
Denis Karpov70a33412009-09-22 16:44:59 -07002215 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002216
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002217 if (host->pdata->resume) {
2218 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2219 if (ret)
2220 dev_dbg(mmc_dev(host->mmc),
2221 "Unmask interrupt failed\n");
2222 }
2223
Adrian Hunterb62f6222009-09-22 16:45:01 -07002224 omap_hsmmc_protect_card(host);
2225
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002226 /* Notify the core to resume the host */
2227 ret = mmc_resume_host(host->mmc);
2228 if (ret == 0)
2229 host->suspended = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302230
2231 pm_runtime_mark_last_busy(host->dev);
2232 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002233 }
2234
2235 return ret;
2236
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237}
2238
2239#else
Denis Karpov70a33412009-09-22 16:44:59 -07002240#define omap_hsmmc_suspend NULL
2241#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242#endif
2243
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302244static int omap_hsmmc_runtime_suspend(struct device *dev)
2245{
2246 struct omap_hsmmc_host *host;
2247
2248 host = platform_get_drvdata(to_platform_device(dev));
2249 omap_hsmmc_context_save(host);
2250 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2251
2252 return 0;
2253}
2254
2255static int omap_hsmmc_runtime_resume(struct device *dev)
2256{
2257 struct omap_hsmmc_host *host;
2258
2259 host = platform_get_drvdata(to_platform_device(dev));
2260 omap_hsmmc_context_restore(host);
2261 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2262
2263 return 0;
2264}
2265
Kevin Hilmana791daa2010-05-26 14:42:07 -07002266static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002267 .suspend = omap_hsmmc_suspend,
2268 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302269 .runtime_suspend = omap_hsmmc_runtime_suspend,
2270 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002271};
2272
2273static struct platform_driver omap_hsmmc_driver = {
2274 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002275 .driver = {
2276 .name = DRIVER_NAME,
2277 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002278 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002279 },
2280};
2281
Denis Karpov70a33412009-09-22 16:44:59 -07002282static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002283{
2284 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002285 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002286}
2287
Denis Karpov70a33412009-09-22 16:44:59 -07002288static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289{
2290 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002291 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002292}
2293
Denis Karpov70a33412009-09-22 16:44:59 -07002294module_init(omap_hsmmc_init);
2295module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296
2297MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2298MODULE_LICENSE("GPL");
2299MODULE_ALIAS("platform:" DRIVER_NAME);
2300MODULE_AUTHOR("Texas Instruments Inc");