blob: 274b6c3ce02a352d6ebd1637eef3eb993e2335da [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 Hunter93caf8e692010-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 Hunter93caf8e692010-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 Hunter93caf8e692010-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 Hunter93caf8e692010-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 Hunter93caf8e692010-08-11 14:17:48 -0700827 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200829 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830 if (cmd->flags & MMC_RSP_PRESENT) {
831 if (cmd->flags & MMC_RSP_136)
832 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200833 else if (cmd->flags & MMC_RSP_BUSY) {
834 resptype = 3;
835 host->response_busy = 1;
836 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837 resptype = 2;
838 }
839
840 /*
841 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
842 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
843 * a val of 0x3, rest 0x0.
844 */
845 if (cmd == host->mrq->stop)
846 cmdtype = 0x3;
847
848 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
849
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
Adrian Hunter699b9582011-05-06 12:14:01 +0300978static void omap_hsmmc_dbg_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[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300982 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
983 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
984 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
985 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 };
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}
Adrian Hunter699b9582011-05-06 12:14:01 +03001002#else
1003static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1004 u32 status)
1005{
1006}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001007#endif /* CONFIG_MMC_DEBUG */
1008
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001009/*
1010 * MMC controller internal state machines reset
1011 *
1012 * Used to reset command or data internal state machines, using respectively
1013 * SRC or SRD bit of SYSCTL register
1014 * Can be called from interrupt context
1015 */
Denis Karpov70a33412009-09-22 16:44:59 -07001016static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1017 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001018{
1019 unsigned long i = 0;
1020 unsigned long limit = (loops_per_jiffy *
1021 msecs_to_jiffies(MMC_TIMEOUT_MS));
1022
1023 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1024 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1025
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001026 /*
1027 * OMAP4 ES2 and greater has an updated reset logic.
1028 * Monitor a 0->1 transition first
1029 */
1030 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001031 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001032 && (i++ < limit))
1033 cpu_relax();
1034 }
1035 i = 0;
1036
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001037 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1038 (i++ < limit))
1039 cpu_relax();
1040
1041 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1042 dev_err(mmc_dev(host->mmc),
1043 "Timeout waiting on controller reset in %s\n",
1044 __func__);
1045}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046
Adrian Hunterb4175772010-05-26 14:42:06 -07001047static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001050 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051
Adrian Hunterb4175772010-05-26 14:42:06 -07001052 if (!host->req_in_progress) {
1053 do {
1054 OMAP_HSMMC_WRITE(host->base, STAT, status);
1055 /* Flush posted write */
1056 status = OMAP_HSMMC_READ(host->base, STAT);
1057 } while (status & INT_EN_MASK);
1058 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059 }
1060
1061 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001062 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1063
1064 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001065 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001066 if ((status & CMD_TIMEOUT) ||
1067 (status & CMD_CRC)) {
1068 if (host->cmd) {
1069 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001070 omap_hsmmc_reset_controller_fsm(host,
1071 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072 host->cmd->error = -ETIMEDOUT;
1073 } else {
1074 host->cmd->error = -EILSEQ;
1075 }
1076 end_cmd = 1;
1077 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001078 if (host->data || host->response_busy) {
1079 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001080 omap_hsmmc_dma_cleanup(host,
1081 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001082 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001083 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001084 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001085 }
1086 if ((status & DATA_TIMEOUT) ||
1087 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001088 if (host->data || host->response_busy) {
1089 int err = (status & DATA_TIMEOUT) ?
1090 -ETIMEDOUT : -EILSEQ;
1091
1092 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001093 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001095 host->mrq->cmd->error = err;
1096 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001097 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001098 end_trans = 1;
1099 }
1100 }
1101 if (status & CARD_ERR) {
1102 dev_dbg(mmc_dev(host->mmc),
1103 "Ignoring card err CMD%d\n", host->cmd->opcode);
1104 if (host->cmd)
1105 end_cmd = 1;
1106 if (host->data)
1107 end_trans = 1;
1108 }
1109 }
1110
1111 OMAP_HSMMC_WRITE(host->base, STAT, status);
1112
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001113 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001114 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001115 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001116 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001117}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118
Adrian Hunterb4175772010-05-26 14:42:06 -07001119/*
1120 * MMC controller IRQ handler
1121 */
1122static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1123{
1124 struct omap_hsmmc_host *host = dev_id;
1125 int status;
1126
1127 status = OMAP_HSMMC_READ(host->base, STAT);
1128 do {
1129 omap_hsmmc_do_irq(host, status);
1130 /* Flush posted write */
1131 status = OMAP_HSMMC_READ(host->base, STAT);
1132 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001133
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001134 return IRQ_HANDLED;
1135}
1136
Denis Karpov70a33412009-09-22 16:44:59 -07001137static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001138{
1139 unsigned long i;
1140
1141 OMAP_HSMMC_WRITE(host->base, HCTL,
1142 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1143 for (i = 0; i < loops_per_jiffy; i++) {
1144 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1145 break;
1146 cpu_relax();
1147 }
1148}
1149
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001150/*
David Brownelleb250822009-02-17 14:49:01 -08001151 * Switch MMC interface voltage ... only relevant for MMC1.
1152 *
1153 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1154 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1155 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001156 */
Denis Karpov70a33412009-09-22 16:44:59 -07001157static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158{
1159 u32 reg_val = 0;
1160 int ret;
1161
1162 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301163 pm_runtime_put_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001164 if (host->got_dbclk)
1165 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001166
1167 /* Turn the power off */
1168 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001169
1170 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001171 if (!ret)
1172 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1173 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301174 pm_runtime_get_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001175 if (host->got_dbclk)
1176 clk_enable(host->dbclk);
1177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178 if (ret != 0)
1179 goto err;
1180
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181 OMAP_HSMMC_WRITE(host->base, HCTL,
1182 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1183 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185 /*
1186 * If a MMC dual voltage card is detected, the set_ios fn calls
1187 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001188 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001189 *
David Brownelleb250822009-02-17 14:49:01 -08001190 * Cope with a bit of slop in the range ... per data sheets:
1191 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1192 * but recommended values are 1.71V to 1.89V
1193 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1194 * but recommended values are 2.7V to 3.3V
1195 *
1196 * Board setup code shouldn't permit anything very out-of-range.
1197 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1198 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199 */
David Brownelleb250822009-02-17 14:49:01 -08001200 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001202 else
1203 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
1205 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001206 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207
1208 return 0;
1209err:
1210 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1211 return ret;
1212}
1213
Adrian Hunterb62f6222009-09-22 16:45:01 -07001214/* Protect the card while the cover is open */
1215static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1216{
1217 if (!mmc_slot(host).get_cover_state)
1218 return;
1219
1220 host->reqs_blocked = 0;
1221 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1222 if (host->protect_card) {
1223 printk(KERN_INFO "%s: cover is closed, "
1224 "card is now accessible\n",
1225 mmc_hostname(host->mmc));
1226 host->protect_card = 0;
1227 }
1228 } else {
1229 if (!host->protect_card) {
1230 printk(KERN_INFO "%s: cover is open, "
1231 "card is now inaccessible\n",
1232 mmc_hostname(host->mmc));
1233 host->protect_card = 1;
1234 }
1235 }
1236}
1237
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238/*
1239 * Work Item to notify the core about card insertion/removal
1240 */
Denis Karpov70a33412009-09-22 16:44:59 -07001241static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001242{
Denis Karpov70a33412009-09-22 16:44:59 -07001243 struct omap_hsmmc_host *host =
1244 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001245 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001246 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001247
Adrian Huntera6b22402009-09-22 16:44:45 -07001248 if (host->suspended)
1249 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001250
1251 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001252
Denis Karpov191d1f12009-09-22 16:44:55 -07001253 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001254 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001255 else {
1256 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001257 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001258 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001259
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001260 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001261 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001262 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001263 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264}
1265
1266/*
1267 * ISR for handling card insertion and removal
1268 */
Denis Karpov70a33412009-09-22 16:44:59 -07001269static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270{
Denis Karpov70a33412009-09-22 16:44:59 -07001271 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272
Adrian Huntera6b22402009-09-22 16:44:45 -07001273 if (host->suspended)
1274 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275 schedule_work(&host->mmc_carddetect_work);
1276
1277 return IRQ_HANDLED;
1278}
1279
Denis Karpov70a33412009-09-22 16:44:59 -07001280static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001281 struct mmc_data *data)
1282{
1283 int sync_dev;
1284
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001285 if (data->flags & MMC_DATA_WRITE)
1286 sync_dev = host->dma_line_tx;
1287 else
1288 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001289 return sync_dev;
1290}
1291
Denis Karpov70a33412009-09-22 16:44:59 -07001292static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001293 struct mmc_data *data,
1294 struct scatterlist *sgl)
1295{
1296 int blksz, nblk, dma_ch;
1297
1298 dma_ch = host->dma_ch;
1299 if (data->flags & MMC_DATA_WRITE) {
1300 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1301 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1302 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1303 sg_dma_address(sgl), 0, 0);
1304 } else {
1305 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001306 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001307 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1308 sg_dma_address(sgl), 0, 0);
1309 }
1310
1311 blksz = host->data->blksz;
1312 nblk = sg_dma_len(sgl) / blksz;
1313
1314 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1315 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001316 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001317 !(data->flags & MMC_DATA_WRITE));
1318
1319 omap_start_dma(dma_ch);
1320}
1321
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322/*
1323 * DMA call back function
1324 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001325static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001326{
Adrian Hunterb4175772010-05-26 14:42:06 -07001327 struct omap_hsmmc_host *host = cb_data;
1328 struct mmc_data *data = host->mrq->data;
1329 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001330
Venkatraman Sf3584e52010-08-10 18:01:54 -07001331 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1332 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1333 ch_status);
1334 return;
1335 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336
Adrian Hunterb4175772010-05-26 14:42:06 -07001337 spin_lock(&host->irq_lock);
1338 if (host->dma_ch < 0) {
1339 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001340 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001341 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001343 host->dma_sg_idx++;
1344 if (host->dma_sg_idx < host->dma_len) {
1345 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001346 omap_hsmmc_config_dma_params(host, data,
1347 data->sg + host->dma_sg_idx);
1348 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001349 return;
1350 }
1351
Per Forlin9782aff2011-07-01 18:55:23 +02001352 if (!data->host_cookie)
1353 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1354 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001355
1356 req_in_progress = host->req_in_progress;
1357 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001359 spin_unlock(&host->irq_lock);
1360
1361 omap_free_dma(dma_ch);
1362
1363 /* If DMA has finished after TC, complete the request */
1364 if (!req_in_progress) {
1365 struct mmc_request *mrq = host->mrq;
1366
1367 host->mrq = NULL;
1368 mmc_request_done(host->mmc, mrq);
1369 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370}
1371
Per Forlin9782aff2011-07-01 18:55:23 +02001372static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1373 struct mmc_data *data,
1374 struct omap_hsmmc_next *next)
1375{
1376 int dma_len;
1377
1378 if (!next && data->host_cookie &&
1379 data->host_cookie != host->next_data.cookie) {
1380 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
1381 " host->next_data.cookie %d\n",
1382 __func__, data->host_cookie, host->next_data.cookie);
1383 data->host_cookie = 0;
1384 }
1385
1386 /* Check if next job is already prepared */
1387 if (next ||
1388 (!next && data->host_cookie != host->next_data.cookie)) {
1389 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1390 data->sg_len,
1391 omap_hsmmc_get_dma_dir(host, data));
1392
1393 } else {
1394 dma_len = host->next_data.dma_len;
1395 host->next_data.dma_len = 0;
1396 }
1397
1398
1399 if (dma_len == 0)
1400 return -EINVAL;
1401
1402 if (next) {
1403 next->dma_len = dma_len;
1404 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1405 } else
1406 host->dma_len = dma_len;
1407
1408 return 0;
1409}
1410
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001411/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001412 * Routine to configure and start DMA for the MMC card
1413 */
Denis Karpov70a33412009-09-22 16:44:59 -07001414static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1415 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001416{
Adrian Hunterb4175772010-05-26 14:42:06 -07001417 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001418 struct mmc_data *data = req->data;
1419
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001420 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001421 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001422 struct scatterlist *sgl;
1423
1424 sgl = data->sg + i;
1425 if (sgl->length % data->blksz)
1426 return -EINVAL;
1427 }
1428 if ((data->blksz % 4) != 0)
1429 /* REVISIT: The MMC buffer increments only when MSB is written.
1430 * Return error for blksz which is non multiple of four.
1431 */
1432 return -EINVAL;
1433
Adrian Hunterb4175772010-05-26 14:42:06 -07001434 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435
Denis Karpov70a33412009-09-22 16:44:59 -07001436 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1437 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001439 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001440 "%s: omap_request_dma() failed with %d\n",
1441 mmc_hostname(host->mmc), ret);
1442 return ret;
1443 }
Per Forlin9782aff2011-07-01 18:55:23 +02001444 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1445 if (ret)
1446 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001448 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001449 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450
Denis Karpov70a33412009-09-22 16:44:59 -07001451 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001452
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001453 return 0;
1454}
1455
Denis Karpov70a33412009-09-22 16:44:59 -07001456static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001457 unsigned int timeout_ns,
1458 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001459{
1460 unsigned int timeout, cycle_ns;
1461 uint32_t reg, clkd, dto = 0;
1462
1463 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1464 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1465 if (clkd == 0)
1466 clkd = 1;
1467
1468 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001469 timeout = timeout_ns / cycle_ns;
1470 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001471 if (timeout) {
1472 while ((timeout & 0x80000000) == 0) {
1473 dto += 1;
1474 timeout <<= 1;
1475 }
1476 dto = 31 - dto;
1477 timeout <<= 1;
1478 if (timeout && dto)
1479 dto += 1;
1480 if (dto >= 13)
1481 dto -= 13;
1482 else
1483 dto = 0;
1484 if (dto > 14)
1485 dto = 14;
1486 }
1487
1488 reg &= ~DTO_MASK;
1489 reg |= dto << DTO_SHIFT;
1490 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1491}
1492
1493/*
1494 * Configure block length for MMC/SD cards and initiate the transfer.
1495 */
1496static int
Denis Karpov70a33412009-09-22 16:44:59 -07001497omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498{
1499 int ret;
1500 host->data = req->data;
1501
1502 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001504 /*
1505 * Set an arbitrary 100ms data timeout for commands with
1506 * busy signal.
1507 */
1508 if (req->cmd->flags & MMC_RSP_BUSY)
1509 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510 return 0;
1511 }
1512
1513 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1514 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001515 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001517 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001518 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001519 if (ret != 0) {
1520 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1521 return ret;
1522 }
1523 }
1524 return 0;
1525}
1526
Per Forlin9782aff2011-07-01 18:55:23 +02001527static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1528 int err)
1529{
1530 struct omap_hsmmc_host *host = mmc_priv(mmc);
1531 struct mmc_data *data = mrq->data;
1532
1533 if (host->use_dma) {
1534 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1535 omap_hsmmc_get_dma_dir(host, data));
1536 data->host_cookie = 0;
1537 }
1538}
1539
1540static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1541 bool is_first_req)
1542{
1543 struct omap_hsmmc_host *host = mmc_priv(mmc);
1544
1545 if (mrq->data->host_cookie) {
1546 mrq->data->host_cookie = 0;
1547 return ;
1548 }
1549
1550 if (host->use_dma)
1551 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1552 &host->next_data))
1553 mrq->data->host_cookie = 0;
1554}
1555
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001556/*
1557 * Request function. for read/write operation
1558 */
Denis Karpov70a33412009-09-22 16:44:59 -07001559static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560{
Denis Karpov70a33412009-09-22 16:44:59 -07001561 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001562 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563
Adrian Hunterb4175772010-05-26 14:42:06 -07001564 BUG_ON(host->req_in_progress);
1565 BUG_ON(host->dma_ch != -1);
1566 if (host->protect_card) {
1567 if (host->reqs_blocked < 3) {
1568 /*
1569 * Ensure the controller is left in a consistent
1570 * state by resetting the command and data state
1571 * machines.
1572 */
1573 omap_hsmmc_reset_controller_fsm(host, SRD);
1574 omap_hsmmc_reset_controller_fsm(host, SRC);
1575 host->reqs_blocked += 1;
1576 }
1577 req->cmd->error = -EBADF;
1578 if (req->data)
1579 req->data->error = -EBADF;
1580 req->cmd->retries = 0;
1581 mmc_request_done(mmc, req);
1582 return;
1583 } else if (host->reqs_blocked)
1584 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001585 WARN_ON(host->mrq != NULL);
1586 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001587 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001588 if (err) {
1589 req->cmd->error = err;
1590 if (req->data)
1591 req->data->error = err;
1592 host->mrq = NULL;
1593 mmc_request_done(mmc, req);
1594 return;
1595 }
1596
Denis Karpov70a33412009-09-22 16:44:59 -07001597 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598}
1599
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001600/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001601static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602{
Denis Karpov70a33412009-09-22 16:44:59 -07001603 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001604 u16 dsor = 0;
1605 unsigned long regval;
1606 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001607 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001608 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301610 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001611
Adrian Huntera3621462009-09-22 16:44:42 -07001612 if (ios->power_mode != host->power_mode) {
1613 switch (ios->power_mode) {
1614 case MMC_POWER_OFF:
1615 mmc_slot(host).set_power(host->dev, host->slot_id,
1616 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001617 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001618 break;
1619 case MMC_POWER_UP:
1620 mmc_slot(host).set_power(host->dev, host->slot_id,
1621 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001622 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001623 break;
1624 case MMC_POWER_ON:
1625 do_send_init_stream = 1;
1626 break;
1627 }
1628 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001629 }
1630
Denis Karpovdd498ef2009-09-22 16:44:49 -07001631 /* FIXME: set registers based only on changes to ios */
1632
Jarkko Lavinen73153012008-11-21 16:49:54 +02001633 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001634 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001635 case MMC_BUS_WIDTH_8:
1636 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1637 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001638 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001639 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001640 OMAP_HSMMC_WRITE(host->base, HCTL,
1641 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1642 break;
1643 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001644 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001645 OMAP_HSMMC_WRITE(host->base, HCTL,
1646 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1647 break;
1648 }
1649
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301650 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001651 /* Only MMC1 can interface at 3V without some flavor
1652 * of external transceiver; but they all handle 1.8V.
1653 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001654 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1655 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1656 /*
1657 * The mmc_select_voltage fn of the core does
1658 * not seem to set the power_mode to
1659 * MMC_POWER_UP upon recalculating the voltage.
1660 * vdd 1.8v.
1661 */
Denis Karpov70a33412009-09-22 16:44:59 -07001662 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1663 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001664 "Switch operation failed\n");
1665 }
1666 }
1667
1668 if (ios->clock) {
1669 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1670 if (dsor < 1)
1671 dsor = 1;
1672
1673 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1674 dsor++;
1675
1676 if (dsor > 250)
1677 dsor = 250;
1678 }
Denis Karpov70a33412009-09-22 16:44:59 -07001679 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001680 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1681 regval = regval & ~(CLKD_MASK);
1682 regval = regval | (dsor << 6) | (DTO << 16);
1683 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1684 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1685 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1686
1687 /* Wait till the ICS bit is set */
1688 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001689 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001690 && time_before(jiffies, timeout))
1691 msleep(1);
1692
1693 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1694 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1695
Adrian Huntera3621462009-09-22 16:44:42 -07001696 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001697 send_init_stream(host);
1698
Denis Karpovabb28e72009-09-22 16:44:44 -07001699 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001700 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001701 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1702 else
1703 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001704
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301705 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001706}
1707
1708static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1709{
Denis Karpov70a33412009-09-22 16:44:59 -07001710 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001711
Denis Karpov191d1f12009-09-22 16:44:55 -07001712 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001713 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001714 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001715}
1716
1717static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1718{
Denis Karpov70a33412009-09-22 16:44:59 -07001719 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001720
Denis Karpov191d1f12009-09-22 16:44:55 -07001721 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001722 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001723 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001724}
1725
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001726static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1727{
1728 struct omap_hsmmc_host *host = mmc_priv(mmc);
1729
1730 if (mmc_slot(host).init_card)
1731 mmc_slot(host).init_card(card);
1732}
1733
Denis Karpov70a33412009-09-22 16:44:59 -07001734static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001735{
1736 u32 hctl, capa, value;
1737
1738 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301739 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001740 hctl = SDVS30;
1741 capa = VS30 | VS18;
1742 } else {
1743 hctl = SDVS18;
1744 capa = VS18;
1745 }
1746
1747 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1748 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1749
1750 value = OMAP_HSMMC_READ(host->base, CAPA);
1751 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1752
1753 /* Set the controller to AUTO IDLE mode */
1754 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1755 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1756
1757 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001758 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001759}
1760
Denis Karpov70a33412009-09-22 16:44:59 -07001761static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001762{
Denis Karpov70a33412009-09-22 16:44:59 -07001763 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001764
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301765 pm_runtime_get_sync(host->dev);
1766
Denis Karpovdd498ef2009-09-22 16:44:49 -07001767 return 0;
1768}
1769
Denis Karpov70a33412009-09-22 16:44:59 -07001770static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001771{
Denis Karpov70a33412009-09-22 16:44:59 -07001772 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001773
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301774 pm_runtime_mark_last_busy(host->dev);
1775 pm_runtime_put_autosuspend(host->dev);
1776
Denis Karpovdd498ef2009-09-22 16:44:49 -07001777 return 0;
1778}
1779
Denis Karpov70a33412009-09-22 16:44:59 -07001780static const struct mmc_host_ops omap_hsmmc_ops = {
1781 .enable = omap_hsmmc_enable_fclk,
1782 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001783 .post_req = omap_hsmmc_post_req,
1784 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001785 .request = omap_hsmmc_request,
1786 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001787 .get_cd = omap_hsmmc_get_cd,
1788 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001789 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001790 /* NYET -- enable_sdio_irq */
1791};
1792
Denis Karpovd900f712009-09-22 16:44:38 -07001793#ifdef CONFIG_DEBUG_FS
1794
Denis Karpov70a33412009-09-22 16:44:59 -07001795static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001796{
1797 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001798 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001799 int context_loss = 0;
1800
Denis Karpov70a33412009-09-22 16:44:59 -07001801 if (host->pdata->get_context_loss_count)
1802 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001803
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001804 seq_printf(s, "mmc%d:\n"
1805 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001806 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001807 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001808 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001809 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001810 mmc->index, mmc->enabled ? 1 : 0,
1811 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001812 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001813
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301814 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001815 seq_printf(s, "host suspended, can't read registers\n");
1816 return 0;
1817 }
1818
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301819 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001820
1821 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1822 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1823 seq_printf(s, "CON:\t\t0x%08x\n",
1824 OMAP_HSMMC_READ(host->base, CON));
1825 seq_printf(s, "HCTL:\t\t0x%08x\n",
1826 OMAP_HSMMC_READ(host->base, HCTL));
1827 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1828 OMAP_HSMMC_READ(host->base, SYSCTL));
1829 seq_printf(s, "IE:\t\t0x%08x\n",
1830 OMAP_HSMMC_READ(host->base, IE));
1831 seq_printf(s, "ISE:\t\t0x%08x\n",
1832 OMAP_HSMMC_READ(host->base, ISE));
1833 seq_printf(s, "CAPA:\t\t0x%08x\n",
1834 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001835
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301836 pm_runtime_mark_last_busy(host->dev);
1837 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838
Denis Karpovd900f712009-09-22 16:44:38 -07001839 return 0;
1840}
1841
Denis Karpov70a33412009-09-22 16:44:59 -07001842static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001843{
Denis Karpov70a33412009-09-22 16:44:59 -07001844 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001845}
1846
1847static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001848 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001849 .read = seq_read,
1850 .llseek = seq_lseek,
1851 .release = single_release,
1852};
1853
Denis Karpov70a33412009-09-22 16:44:59 -07001854static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001855{
1856 if (mmc->debugfs_root)
1857 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1858 mmc, &mmc_regs_fops);
1859}
1860
1861#else
1862
Denis Karpov70a33412009-09-22 16:44:59 -07001863static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001864{
1865}
1866
1867#endif
1868
Denis Karpov70a33412009-09-22 16:44:59 -07001869static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001870{
1871 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1872 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001873 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001874 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001875 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001876
1877 if (pdata == NULL) {
1878 dev_err(&pdev->dev, "Platform Data is missing\n");
1879 return -ENXIO;
1880 }
1881
1882 if (pdata->nr_slots == 0) {
1883 dev_err(&pdev->dev, "No Slots\n");
1884 return -ENXIO;
1885 }
1886
1887 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1888 irq = platform_get_irq(pdev, 0);
1889 if (res == NULL || irq < 0)
1890 return -ENXIO;
1891
kishore kadiyala91a0b082010-10-01 16:35:28 -07001892 res->start += pdata->reg_offset;
1893 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07001894 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001895 if (res == NULL)
1896 return -EBUSY;
1897
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001898 ret = omap_hsmmc_gpio_init(pdata);
1899 if (ret)
1900 goto err;
1901
Denis Karpov70a33412009-09-22 16:44:59 -07001902 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001903 if (!mmc) {
1904 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001905 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001906 }
1907
1908 host = mmc_priv(mmc);
1909 host->mmc = mmc;
1910 host->pdata = pdata;
1911 host->dev = &pdev->dev;
1912 host->use_dma = 1;
1913 host->dev->dma_mask = &pdata->dma_mask;
1914 host->dma_ch = -1;
1915 host->irq = irq;
1916 host->id = pdev->id;
1917 host->slot_id = 0;
1918 host->mapbase = res->start;
1919 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001920 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001921 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001922
1923 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07001924 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001925
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301926 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001927
Adrian Huntere0eb2422010-02-15 10:03:34 -08001928 /*
1929 * If regulator_disable can only put vcc_aux to sleep then there is
1930 * no off state.
1931 */
1932 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1933 mmc_slot(host).no_off = 1;
1934
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001935 mmc->f_min = 400000;
1936 mmc->f_max = 52000000;
1937
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001938 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001939
Russell King6f7607c2009-01-28 10:22:50 +00001940 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001941 if (IS_ERR(host->fclk)) {
1942 ret = PTR_ERR(host->fclk);
1943 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944 goto err1;
1945 }
1946
Denis Karpov70a33412009-09-22 16:44:59 -07001947 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001948
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001949 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001950
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301951 pm_runtime_enable(host->dev);
1952 pm_runtime_get_sync(host->dev);
1953 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1954 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001955
Adrian Hunter2bec0892009-09-22 16:45:02 -07001956 if (cpu_is_omap2430()) {
1957 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1958 /*
1959 * MMC can still work without debounce clock.
1960 */
1961 if (IS_ERR(host->dbclk))
1962 dev_warn(mmc_dev(host->mmc),
1963 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001964 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001965 host->got_dbclk = 1;
1966
1967 if (host->got_dbclk)
1968 if (clk_enable(host->dbclk) != 0)
1969 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1970 " clk failed\n");
1971 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001972
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001973 /* Since we do only SG emulation, we can have as many segs
1974 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001975 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001976
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001977 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1978 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1979 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1980 mmc->max_seg_size = mmc->max_req_size;
1981
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001982 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001983 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001984
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001985 mmc->caps |= mmc_slot(host).caps;
1986 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001987 mmc->caps |= MMC_CAP_4_BIT_DATA;
1988
Denis Karpov191d1f12009-09-22 16:44:55 -07001989 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001990 mmc->caps |= MMC_CAP_NONREMOVABLE;
1991
Denis Karpov70a33412009-09-22 16:44:59 -07001992 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001993
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001994 /* Select DMA lines */
1995 switch (host->id) {
1996 case OMAP_MMC1_DEVID:
1997 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1998 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1999 break;
2000 case OMAP_MMC2_DEVID:
2001 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2002 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2003 break;
2004 case OMAP_MMC3_DEVID:
2005 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2006 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2007 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002008 case OMAP_MMC4_DEVID:
2009 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2010 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2011 break;
2012 case OMAP_MMC5_DEVID:
2013 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2014 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2015 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002016 default:
2017 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2018 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002019 }
2020
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002022 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023 mmc_hostname(mmc), host);
2024 if (ret) {
2025 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2026 goto err_irq;
2027 }
2028
2029 if (pdata->init != NULL) {
2030 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002031 dev_dbg(mmc_dev(host->mmc),
2032 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033 goto err_irq_cd_init;
2034 }
2035 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002036
Adrian Hunterb702b102010-02-15 10:03:35 -08002037 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002038 ret = omap_hsmmc_reg_get(host);
2039 if (ret)
2040 goto err_reg;
2041 host->use_reg = 1;
2042 }
2043
David Brownellb583f262009-05-28 14:04:03 -07002044 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045
2046 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002047 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002049 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002050 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2051 | IRQF_DISABLED,
2052 mmc_hostname(mmc), host);
2053 if (ret) {
2054 dev_dbg(mmc_dev(host->mmc),
2055 "Unable to grab MMC CD IRQ\n");
2056 goto err_irq_cd;
2057 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002058 pdata->suspend = omap_hsmmc_suspend_cdirq;
2059 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060 }
2061
Adrian Hunterb4175772010-05-26 14:42:06 -07002062 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002063
Adrian Hunterb62f6222009-09-22 16:45:01 -07002064 omap_hsmmc_protect_card(host);
2065
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066 mmc_add_host(mmc);
2067
Denis Karpov191d1f12009-09-22 16:44:55 -07002068 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002069 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2070 if (ret < 0)
2071 goto err_slot_name;
2072 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002073 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074 ret = device_create_file(&mmc->class_dev,
2075 &dev_attr_cover_switch);
2076 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002077 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002078 }
2079
Denis Karpov70a33412009-09-22 16:44:59 -07002080 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302081 pm_runtime_mark_last_busy(host->dev);
2082 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002083
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084 return 0;
2085
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086err_slot_name:
2087 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002089err_irq_cd:
2090 if (host->use_reg)
2091 omap_hsmmc_reg_put(host);
2092err_reg:
2093 if (host->pdata->cleanup)
2094 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002095err_irq_cd_init:
2096 free_irq(host->irq, host);
2097err_irq:
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302098 pm_runtime_mark_last_busy(host->dev);
2099 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002100 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002101 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002102 clk_disable(host->dbclk);
2103 clk_put(host->dbclk);
2104 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002105err1:
2106 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002107 platform_set_drvdata(pdev, NULL);
2108 mmc_free_host(mmc);
2109err_alloc:
2110 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111err:
Chris Ball984b2032011-03-22 16:34:42 -07002112 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113 return ret;
2114}
2115
Denis Karpov70a33412009-09-22 16:44:59 -07002116static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117{
Denis Karpov70a33412009-09-22 16:44:59 -07002118 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119 struct resource *res;
2120
2121 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302122 pm_runtime_get_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002123 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002124 if (host->use_reg)
2125 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002126 if (host->pdata->cleanup)
2127 host->pdata->cleanup(&pdev->dev);
2128 free_irq(host->irq, host);
2129 if (mmc_slot(host).card_detect_irq)
2130 free_irq(mmc_slot(host).card_detect_irq, host);
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002131 flush_work_sync(&host->mmc_carddetect_work);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002132
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302133 pm_runtime_put_sync(host->dev);
2134 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002135 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002136 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002137 clk_disable(host->dbclk);
2138 clk_put(host->dbclk);
2139 }
2140
2141 mmc_free_host(host->mmc);
2142 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002143 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002144 }
2145
2146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2147 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002148 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002149 platform_set_drvdata(pdev, NULL);
2150
2151 return 0;
2152}
2153
2154#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002155static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156{
2157 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002158 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002159 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002160
2161 if (host && host->suspended)
2162 return 0;
2163
2164 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302165 pm_runtime_get_sync(host->dev);
Adrian Huntera6b22402009-09-22 16:44:45 -07002166 host->suspended = 1;
2167 if (host->pdata->suspend) {
2168 ret = host->pdata->suspend(&pdev->dev,
2169 host->slot_id);
2170 if (ret) {
2171 dev_dbg(mmc_dev(host->mmc),
2172 "Unable to handle MMC board"
2173 " level suspend\n");
2174 host->suspended = 0;
2175 return ret;
2176 }
2177 }
2178 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002179 ret = mmc_suspend_host(host->mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302180
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002181 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002182 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002183 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002184 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002185 if (host->got_dbclk)
2186 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002187 } else {
2188 host->suspended = 0;
2189 if (host->pdata->resume) {
2190 ret = host->pdata->resume(&pdev->dev,
2191 host->slot_id);
2192 if (ret)
2193 dev_dbg(mmc_dev(host->mmc),
2194 "Unmask interrupt failed\n");
2195 }
Adrian Huntera6b22402009-09-22 16:44:45 -07002196 }
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302197 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002198 }
2199 return ret;
2200}
2201
2202/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002203static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002204{
2205 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002206 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002207 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208
2209 if (host && !host->suspended)
2210 return 0;
2211
2212 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302213 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002214
Adrian Hunter2bec0892009-09-22 16:45:02 -07002215 if (host->got_dbclk)
2216 clk_enable(host->dbclk);
2217
Denis Karpov70a33412009-09-22 16:44:59 -07002218 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002219
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002220 if (host->pdata->resume) {
2221 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2222 if (ret)
2223 dev_dbg(mmc_dev(host->mmc),
2224 "Unmask interrupt failed\n");
2225 }
2226
Adrian Hunterb62f6222009-09-22 16:45:01 -07002227 omap_hsmmc_protect_card(host);
2228
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229 /* Notify the core to resume the host */
2230 ret = mmc_resume_host(host->mmc);
2231 if (ret == 0)
2232 host->suspended = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302233
2234 pm_runtime_mark_last_busy(host->dev);
2235 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002236 }
2237
2238 return ret;
2239
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002240}
2241
2242#else
Denis Karpov70a33412009-09-22 16:44:59 -07002243#define omap_hsmmc_suspend NULL
2244#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002245#endif
2246
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302247static int omap_hsmmc_runtime_suspend(struct device *dev)
2248{
2249 struct omap_hsmmc_host *host;
2250
2251 host = platform_get_drvdata(to_platform_device(dev));
2252 omap_hsmmc_context_save(host);
2253 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2254
2255 return 0;
2256}
2257
2258static int omap_hsmmc_runtime_resume(struct device *dev)
2259{
2260 struct omap_hsmmc_host *host;
2261
2262 host = platform_get_drvdata(to_platform_device(dev));
2263 omap_hsmmc_context_restore(host);
2264 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2265
2266 return 0;
2267}
2268
Kevin Hilmana791daa2010-05-26 14:42:07 -07002269static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002270 .suspend = omap_hsmmc_suspend,
2271 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302272 .runtime_suspend = omap_hsmmc_runtime_suspend,
2273 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002274};
2275
2276static struct platform_driver omap_hsmmc_driver = {
2277 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002278 .driver = {
2279 .name = DRIVER_NAME,
2280 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002281 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002282 },
2283};
2284
Denis Karpov70a33412009-09-22 16:44:59 -07002285static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002286{
2287 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002288 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289}
2290
Denis Karpov70a33412009-09-22 16:44:59 -07002291static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002292{
2293 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002294 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002295}
2296
Denis Karpov70a33412009-09-22 16:44:59 -07002297module_init(omap_hsmmc_init);
2298module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002299
2300MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2301MODULE_LICENSE("GPL");
2302MODULE_ALIAS("platform:" DRIVER_NAME);
2303MODULE_AUTHOR("Texas Instruments Inc");