blob: 8c863ccb1bfeb4e9affc6d651d512e8bd8bcb261 [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>
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070038#include <plat/board.h>
39#include <plat/mmc.h>
40#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041
42/* OMAP HSMMC Host Controller Registers */
43#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070044#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045#define OMAP_HSMMC_CON 0x002C
46#define OMAP_HSMMC_BLK 0x0104
47#define OMAP_HSMMC_ARG 0x0108
48#define OMAP_HSMMC_CMD 0x010C
49#define OMAP_HSMMC_RSP10 0x0110
50#define OMAP_HSMMC_RSP32 0x0114
51#define OMAP_HSMMC_RSP54 0x0118
52#define OMAP_HSMMC_RSP76 0x011C
53#define OMAP_HSMMC_DATA 0x0120
54#define OMAP_HSMMC_HCTL 0x0128
55#define OMAP_HSMMC_SYSCTL 0x012C
56#define OMAP_HSMMC_STAT 0x0130
57#define OMAP_HSMMC_IE 0x0134
58#define OMAP_HSMMC_ISE 0x0138
59#define OMAP_HSMMC_CAPA 0x0140
60
61#define VS18 (1 << 26)
62#define VS30 (1 << 25)
63#define SDVS18 (0x5 << 9)
64#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080065#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010066#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define SDVSCLR 0xFFFFF1FF
68#define SDVSDET 0x00000400
69#define AUTOIDLE 0x1
70#define SDBP (1 << 8)
71#define DTO 0xe
72#define ICE 0x1
73#define ICS 0x2
74#define CEN (1 << 2)
75#define CLKD_MASK 0x0000FFC0
76#define CLKD_SHIFT 6
77#define DTO_MASK 0x000F0000
78#define DTO_SHIFT 16
79#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070080#define BWR_ENABLE (1 << 4)
81#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070082#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010083#define INIT_STREAM (1 << 1)
84#define DP_SELECT (1 << 21)
85#define DDIR (1 << 4)
86#define DMA_EN 0x1
87#define MSBS (1 << 5)
88#define BCE (1 << 1)
89#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020090#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define CC 0x1
92#define TC 0x02
93#define OD 0x1
94#define ERR (1 << 15)
95#define CMD_TIMEOUT (1 << 16)
96#define DATA_TIMEOUT (1 << 20)
97#define CMD_CRC (1 << 17)
98#define DATA_CRC (1 << 21)
99#define CARD_ERR (1 << 28)
100#define STAT_CLEAR 0xFFFFFFFF
101#define INIT_STREAM_CMD 0x00000000
102#define DUAL_VOLT_OCR_BIT 7
103#define SRC (1 << 25)
104#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700105#define SOFTRESET (1 << 1)
106#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100107
108/*
109 * FIXME: Most likely all the data using these _DEVID defines should come
110 * from the platform_data, or implemented in controller and slot specific
111 * functions.
112 */
113#define OMAP_MMC1_DEVID 0
114#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000115#define OMAP_MMC3_DEVID 2
kishore kadiyala82cf8182009-09-22 16:45:25 -0700116#define OMAP_MMC4_DEVID 3
117#define OMAP_MMC5_DEVID 4
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100119#define MMC_TIMEOUT_MS 20
120#define OMAP_MMC_MASTER_CLOCK 96000000
121#define DRIVER_NAME "mmci-omap-hs"
122
Denis Karpovdd498ef2009-09-22 16:44:49 -0700123/* Timeouts for entering power saving states on inactivity, msec */
124#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700125#define OMAP_MMC_SLEEP_TIMEOUT 1000
126#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100128/*
129 * One controller can have multiple slots, like on some omap boards using
130 * omap.c controller driver. Luckily this is not currently done on any known
131 * omap_hsmmc.c device.
132 */
133#define mmc_slot(host) (host->pdata->slots[host->slot_id])
134
135/*
136 * MMC Host controller read/write API's
137 */
138#define OMAP_HSMMC_READ(base, reg) \
139 __raw_readl((base) + OMAP_HSMMC_##reg)
140
141#define OMAP_HSMMC_WRITE(base, reg, val) \
142 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
143
Denis Karpov70a33412009-09-22 16:44:59 -0700144struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100145 struct device *dev;
146 struct mmc_host *mmc;
147 struct mmc_request *mrq;
148 struct mmc_command *cmd;
149 struct mmc_data *data;
150 struct clk *fclk;
151 struct clk *iclk;
152 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800153 /*
154 * vcc == configured supply
155 * vcc_aux == optional
156 * - MMC1, supply for DAT4..DAT7
157 * - MMC2/MMC2, external level shifter voltage supply, for
158 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
159 */
160 struct regulator *vcc;
161 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 struct work_struct mmc_carddetect_work;
163 void __iomem *base;
164 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700165 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 unsigned int id;
167 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200168 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700170 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 u32 *buffer;
172 u32 bytesleft;
173 int suspended;
174 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000176 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700178 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200179 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700180 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700181 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700182 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700183 int protect_card;
184 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800185 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700186 int req_in_progress;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700187
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100188 struct omap_mmc_platform_data *pdata;
189};
190
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800191static int omap_hsmmc_card_detect(struct device *dev, int slot)
192{
193 struct omap_mmc_platform_data *mmc = dev->platform_data;
194
195 /* NOTE: assumes card detect signal is active-low */
196 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
197}
198
199static int omap_hsmmc_get_wp(struct device *dev, int slot)
200{
201 struct omap_mmc_platform_data *mmc = dev->platform_data;
202
203 /* NOTE: assumes write protect signal is active-high */
204 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
205}
206
207static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
208{
209 struct omap_mmc_platform_data *mmc = dev->platform_data;
210
211 /* NOTE: assumes card detect signal is active-low */
212 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
213}
214
215#ifdef CONFIG_PM
216
217static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
218{
219 struct omap_mmc_platform_data *mmc = dev->platform_data;
220
221 disable_irq(mmc->slots[0].card_detect_irq);
222 return 0;
223}
224
225static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
226{
227 struct omap_mmc_platform_data *mmc = dev->platform_data;
228
229 enable_irq(mmc->slots[0].card_detect_irq);
230 return 0;
231}
232
233#else
234
235#define omap_hsmmc_suspend_cdirq NULL
236#define omap_hsmmc_resume_cdirq NULL
237
238#endif
239
Adrian Hunterb702b102010-02-15 10:03:35 -0800240#ifdef CONFIG_REGULATOR
241
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
243 int vdd)
244{
245 struct omap_hsmmc_host *host =
246 platform_get_drvdata(to_platform_device(dev));
247 int ret;
248
249 if (mmc_slot(host).before_set_reg)
250 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
251
252 if (power_on)
Linus Walleij99fc5132010-09-29 01:08:27 -0400253 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800254 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400255 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800256
257 if (mmc_slot(host).after_set_reg)
258 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
259
260 return ret;
261}
262
263static int omap_hsmmc_23_set_power(struct device *dev, int slot, int power_on,
264 int vdd)
265{
266 struct omap_hsmmc_host *host =
267 platform_get_drvdata(to_platform_device(dev));
268 int ret = 0;
269
270 /*
271 * If we don't see a Vcc regulator, assume it's a fixed
272 * voltage always-on regulator.
273 */
274 if (!host->vcc)
275 return 0;
276
277 if (mmc_slot(host).before_set_reg)
278 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
279
280 /*
281 * Assume Vcc regulator is used only to power the card ... OMAP
282 * VDDS is used to power the pins, optionally with a transceiver to
283 * support cards using voltages other than VDDS (1.8V nominal). When a
284 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
285 *
286 * In some cases this regulator won't support enable/disable;
287 * e.g. it's a fixed rail for a WLAN chip.
288 *
289 * In other cases vcc_aux switches interface power. Example, for
290 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
291 * chips/cards need an interface voltage rail too.
292 */
293 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400294 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800295 /* Enable interface voltage rail, if needed */
296 if (ret == 0 && host->vcc_aux) {
297 ret = regulator_enable(host->vcc_aux);
298 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400299 ret = mmc_regulator_set_ocr(host->mmc,
300 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301 }
302 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400303 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800304 if (host->vcc_aux)
305 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400306 if (!ret) {
307 /* Then proceed to shut down the local regulator */
308 ret = mmc_regulator_set_ocr(host->mmc,
309 host->vcc, 0);
310 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311 }
312
313 if (mmc_slot(host).after_set_reg)
314 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
315
316 return ret;
317}
318
319static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
320 int vdd, int cardsleep)
321{
322 struct omap_hsmmc_host *host =
323 platform_get_drvdata(to_platform_device(dev));
324 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
325
326 return regulator_set_mode(host->vcc, mode);
327}
328
329static int omap_hsmmc_23_set_sleep(struct device *dev, int slot, int sleep,
330 int vdd, int cardsleep)
331{
332 struct omap_hsmmc_host *host =
333 platform_get_drvdata(to_platform_device(dev));
334 int err, mode;
335
336 /*
337 * If we don't see a Vcc regulator, assume it's a fixed
338 * voltage always-on regulator.
339 */
340 if (!host->vcc)
341 return 0;
342
343 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
344
345 if (!host->vcc_aux)
346 return regulator_set_mode(host->vcc, mode);
347
348 if (cardsleep) {
349 /* VCC can be turned off if card is asleep */
350 if (sleep)
Linus Walleij99fc5132010-09-29 01:08:27 -0400351 err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800352 else
Linus Walleij99fc5132010-09-29 01:08:27 -0400353 err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800354 } else
355 err = regulator_set_mode(host->vcc, mode);
356 if (err)
357 return err;
Adrian Huntere0eb2422010-02-15 10:03:34 -0800358
359 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
360 return regulator_set_mode(host->vcc_aux, mode);
361
362 if (sleep)
363 return regulator_disable(host->vcc_aux);
364 else
365 return regulator_enable(host->vcc_aux);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800366}
367
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800368static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
369{
370 struct regulator *reg;
371 int ret = 0;
372
373 switch (host->id) {
374 case OMAP_MMC1_DEVID:
375 /* On-chip level shifting via PBIAS0/PBIAS1 */
376 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
377 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
378 break;
379 case OMAP_MMC2_DEVID:
380 case OMAP_MMC3_DEVID:
381 /* Off-chip level shifting, or none */
382 mmc_slot(host).set_power = omap_hsmmc_23_set_power;
383 mmc_slot(host).set_sleep = omap_hsmmc_23_set_sleep;
384 break;
385 default:
386 pr_err("MMC%d configuration not supported!\n", host->id);
387 return -EINVAL;
388 }
389
390 reg = regulator_get(host->dev, "vmmc");
391 if (IS_ERR(reg)) {
392 dev_dbg(host->dev, "vmmc regulator missing\n");
393 /*
394 * HACK: until fixed.c regulator is usable,
395 * we don't require a main regulator
396 * for MMC2 or MMC3
397 */
398 if (host->id == OMAP_MMC1_DEVID) {
399 ret = PTR_ERR(reg);
400 goto err;
401 }
402 } else {
403 host->vcc = reg;
404 mmc_slot(host).ocr_mask = mmc_regulator_get_ocrmask(reg);
405
406 /* Allow an aux regulator */
407 reg = regulator_get(host->dev, "vmmc_aux");
408 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
409
410 /*
411 * UGLY HACK: workaround regulator framework bugs.
412 * When the bootloader leaves a supply active, it's
413 * initialized with zero usecount ... and we can't
414 * disable it without first enabling it. Until the
415 * framework is fixed, we need a workaround like this
416 * (which is safe for MMC, but not in general).
417 */
418 if (regulator_is_enabled(host->vcc) > 0) {
419 regulator_enable(host->vcc);
420 regulator_disable(host->vcc);
421 }
422 if (host->vcc_aux) {
423 if (regulator_is_enabled(reg) > 0) {
424 regulator_enable(reg);
425 regulator_disable(reg);
426 }
427 }
428 }
429
430 return 0;
431
432err:
433 mmc_slot(host).set_power = NULL;
434 mmc_slot(host).set_sleep = NULL;
435 return ret;
436}
437
438static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
439{
440 regulator_put(host->vcc);
441 regulator_put(host->vcc_aux);
442 mmc_slot(host).set_power = NULL;
443 mmc_slot(host).set_sleep = NULL;
444}
445
Adrian Hunterb702b102010-02-15 10:03:35 -0800446static inline int omap_hsmmc_have_reg(void)
447{
448 return 1;
449}
450
451#else
452
453static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
454{
455 return -EINVAL;
456}
457
458static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
459{
460}
461
462static inline int omap_hsmmc_have_reg(void)
463{
464 return 0;
465}
466
467#endif
468
469static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
470{
471 int ret;
472
473 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
474 pdata->suspend = omap_hsmmc_suspend_cdirq;
475 pdata->resume = omap_hsmmc_resume_cdirq;
476 if (pdata->slots[0].cover)
477 pdata->slots[0].get_cover_state =
478 omap_hsmmc_get_cover_state;
479 else
480 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
481 pdata->slots[0].card_detect_irq =
482 gpio_to_irq(pdata->slots[0].switch_pin);
483 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
484 if (ret)
485 return ret;
486 ret = gpio_direction_input(pdata->slots[0].switch_pin);
487 if (ret)
488 goto err_free_sp;
489 } else
490 pdata->slots[0].switch_pin = -EINVAL;
491
492 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
493 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
494 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
495 if (ret)
496 goto err_free_cd;
497 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
498 if (ret)
499 goto err_free_wp;
500 } else
501 pdata->slots[0].gpio_wp = -EINVAL;
502
503 return 0;
504
505err_free_wp:
506 gpio_free(pdata->slots[0].gpio_wp);
507err_free_cd:
508 if (gpio_is_valid(pdata->slots[0].switch_pin))
509err_free_sp:
510 gpio_free(pdata->slots[0].switch_pin);
511 return ret;
512}
513
514static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
515{
516 if (gpio_is_valid(pdata->slots[0].gpio_wp))
517 gpio_free(pdata->slots[0].gpio_wp);
518 if (gpio_is_valid(pdata->slots[0].switch_pin))
519 gpio_free(pdata->slots[0].switch_pin);
520}
521
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100522/*
523 * Stop clock to the card
524 */
Denis Karpov70a33412009-09-22 16:44:59 -0700525static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100526{
527 OMAP_HSMMC_WRITE(host->base, SYSCTL,
528 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
529 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
530 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
531}
532
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700533static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
534 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700535{
536 unsigned int irq_mask;
537
538 if (host->use_dma)
539 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
540 else
541 irq_mask = INT_EN_MASK;
542
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700543 /* Disable timeout for erases */
544 if (cmd->opcode == MMC_ERASE)
545 irq_mask &= ~DTO_ENABLE;
546
Adrian Hunterb4175772010-05-26 14:42:06 -0700547 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
548 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
549 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
550}
551
552static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
553{
554 OMAP_HSMMC_WRITE(host->base, ISE, 0);
555 OMAP_HSMMC_WRITE(host->base, IE, 0);
556 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
557}
558
Denis Karpov11dd62a2009-09-22 16:44:43 -0700559#ifdef CONFIG_PM
560
561/*
562 * Restore the MMC host context, if it was lost as result of a
563 * power state change.
564 */
Denis Karpov70a33412009-09-22 16:44:59 -0700565static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700566{
567 struct mmc_ios *ios = &host->mmc->ios;
568 struct omap_mmc_platform_data *pdata = host->pdata;
569 int context_loss = 0;
570 u32 hctl, capa, con;
571 u16 dsor = 0;
572 unsigned long timeout;
573
574 if (pdata->get_context_loss_count) {
575 context_loss = pdata->get_context_loss_count(host->dev);
576 if (context_loss < 0)
577 return 1;
578 }
579
580 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
581 context_loss == host->context_loss ? "not " : "");
582 if (host->context_loss == context_loss)
583 return 1;
584
585 /* Wait for hardware reset */
586 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
587 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
588 && time_before(jiffies, timeout))
589 ;
590
591 /* Do software reset */
592 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
593 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
594 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
595 && time_before(jiffies, timeout))
596 ;
597
598 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
599 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
600
601 if (host->id == OMAP_MMC1_DEVID) {
602 if (host->power_mode != MMC_POWER_OFF &&
603 (1 << ios->vdd) <= MMC_VDD_23_24)
604 hctl = SDVS18;
605 else
606 hctl = SDVS30;
607 capa = VS30 | VS18;
608 } else {
609 hctl = SDVS18;
610 capa = VS18;
611 }
612
613 OMAP_HSMMC_WRITE(host->base, HCTL,
614 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
615
616 OMAP_HSMMC_WRITE(host->base, CAPA,
617 OMAP_HSMMC_READ(host->base, CAPA) | capa);
618
619 OMAP_HSMMC_WRITE(host->base, HCTL,
620 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
621
622 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
623 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
624 && time_before(jiffies, timeout))
625 ;
626
Adrian Hunterb4175772010-05-26 14:42:06 -0700627 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700628
629 /* Do not initialize card-specific things if the power is off */
630 if (host->power_mode == MMC_POWER_OFF)
631 goto out;
632
633 con = OMAP_HSMMC_READ(host->base, CON);
634 switch (ios->bus_width) {
635 case MMC_BUS_WIDTH_8:
636 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
637 break;
638 case MMC_BUS_WIDTH_4:
639 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
640 OMAP_HSMMC_WRITE(host->base, HCTL,
641 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
642 break;
643 case MMC_BUS_WIDTH_1:
644 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
645 OMAP_HSMMC_WRITE(host->base, HCTL,
646 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
647 break;
648 }
649
650 if (ios->clock) {
651 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
652 if (dsor < 1)
653 dsor = 1;
654
655 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
656 dsor++;
657
658 if (dsor > 250)
659 dsor = 250;
660 }
661
662 OMAP_HSMMC_WRITE(host->base, SYSCTL,
663 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
664 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
665 OMAP_HSMMC_WRITE(host->base, SYSCTL,
666 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
667
668 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
669 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
670 && time_before(jiffies, timeout))
671 ;
672
673 OMAP_HSMMC_WRITE(host->base, SYSCTL,
674 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
675
676 con = OMAP_HSMMC_READ(host->base, CON);
677 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
678 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
679 else
680 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
681out:
682 host->context_loss = context_loss;
683
684 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
685 return 0;
686}
687
688/*
689 * Save the MMC host context (store the number of power state changes so far).
690 */
Denis Karpov70a33412009-09-22 16:44:59 -0700691static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700692{
693 struct omap_mmc_platform_data *pdata = host->pdata;
694 int context_loss;
695
696 if (pdata->get_context_loss_count) {
697 context_loss = pdata->get_context_loss_count(host->dev);
698 if (context_loss < 0)
699 return;
700 host->context_loss = context_loss;
701 }
702}
703
704#else
705
Denis Karpov70a33412009-09-22 16:44:59 -0700706static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700707{
708 return 0;
709}
710
Denis Karpov70a33412009-09-22 16:44:59 -0700711static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700712{
713}
714
715#endif
716
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100717/*
718 * Send init stream sequence to card
719 * before sending IDLE command
720 */
Denis Karpov70a33412009-09-22 16:44:59 -0700721static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100722{
723 int reg = 0;
724 unsigned long timeout;
725
Adrian Hunterb62f6222009-09-22 16:45:01 -0700726 if (host->protect_card)
727 return;
728
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100729 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700730
731 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732 OMAP_HSMMC_WRITE(host->base, CON,
733 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
734 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
735
736 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
737 while ((reg != CC) && time_before(jiffies, timeout))
738 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
739
740 OMAP_HSMMC_WRITE(host->base, CON,
741 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700742
743 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
744 OMAP_HSMMC_READ(host->base, STAT);
745
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746 enable_irq(host->irq);
747}
748
749static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700750int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100751{
752 int r = 1;
753
Denis Karpov191d1f12009-09-22 16:44:55 -0700754 if (mmc_slot(host).get_cover_state)
755 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100756 return r;
757}
758
759static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700760omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100761 char *buf)
762{
763 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700764 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100765
Denis Karpov70a33412009-09-22 16:44:59 -0700766 return sprintf(buf, "%s\n",
767 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100768}
769
Denis Karpov70a33412009-09-22 16:44:59 -0700770static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100771
772static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700773omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774 char *buf)
775{
776 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700777 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100778
Denis Karpov191d1f12009-09-22 16:44:55 -0700779 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100780}
781
Denis Karpov70a33412009-09-22 16:44:59 -0700782static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783
784/*
785 * Configure the response type and send the cmd.
786 */
787static void
Denis Karpov70a33412009-09-22 16:44:59 -0700788omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100789 struct mmc_data *data)
790{
791 int cmdreg = 0, resptype = 0, cmdtype = 0;
792
793 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
794 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
795 host->cmd = cmd;
796
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700797 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200799 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100800 if (cmd->flags & MMC_RSP_PRESENT) {
801 if (cmd->flags & MMC_RSP_136)
802 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200803 else if (cmd->flags & MMC_RSP_BUSY) {
804 resptype = 3;
805 host->response_busy = 1;
806 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100807 resptype = 2;
808 }
809
810 /*
811 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
812 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
813 * a val of 0x3, rest 0x0.
814 */
815 if (cmd == host->mrq->stop)
816 cmdtype = 0x3;
817
818 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
819
820 if (data) {
821 cmdreg |= DP_SELECT | MSBS | BCE;
822 if (data->flags & MMC_DATA_READ)
823 cmdreg |= DDIR;
824 else
825 cmdreg &= ~(DDIR);
826 }
827
828 if (host->use_dma)
829 cmdreg |= DMA_EN;
830
Adrian Hunterb4175772010-05-26 14:42:06 -0700831 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700832
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
834 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
835}
836
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200837static int
Denis Karpov70a33412009-09-22 16:44:59 -0700838omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200839{
840 if (data->flags & MMC_DATA_WRITE)
841 return DMA_TO_DEVICE;
842 else
843 return DMA_FROM_DEVICE;
844}
845
Adrian Hunterb4175772010-05-26 14:42:06 -0700846static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
847{
848 int dma_ch;
849
850 spin_lock(&host->irq_lock);
851 host->req_in_progress = 0;
852 dma_ch = host->dma_ch;
853 spin_unlock(&host->irq_lock);
854
855 omap_hsmmc_disable_irq(host);
856 /* Do not complete the request if DMA is still in progress */
857 if (mrq->data && host->use_dma && dma_ch != -1)
858 return;
859 host->mrq = NULL;
860 mmc_request_done(host->mmc, mrq);
861}
862
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100863/*
864 * Notify the transfer complete to MMC core
865 */
866static void
Denis Karpov70a33412009-09-22 16:44:59 -0700867omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100868{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200869 if (!data) {
870 struct mmc_request *mrq = host->mrq;
871
Adrian Hunter23050102009-09-22 16:44:57 -0700872 /* TC before CC from CMD6 - don't know why, but it happens */
873 if (host->cmd && host->cmd->opcode == 6 &&
874 host->response_busy) {
875 host->response_busy = 0;
876 return;
877 }
878
Adrian Hunterb4175772010-05-26 14:42:06 -0700879 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200880 return;
881 }
882
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100883 host->data = NULL;
884
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100885 if (!data->error)
886 data->bytes_xfered += data->blocks * (data->blksz);
887 else
888 data->bytes_xfered = 0;
889
890 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700891 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100892 return;
893 }
Denis Karpov70a33412009-09-22 16:44:59 -0700894 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100895}
896
897/*
898 * Notify the core about command completion
899 */
900static void
Denis Karpov70a33412009-09-22 16:44:59 -0700901omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902{
903 host->cmd = NULL;
904
905 if (cmd->flags & MMC_RSP_PRESENT) {
906 if (cmd->flags & MMC_RSP_136) {
907 /* response type 2 */
908 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
909 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
910 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
911 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
912 } else {
913 /* response types 1, 1b, 3, 4, 5, 6 */
914 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
915 }
916 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700917 if ((host->data == NULL && !host->response_busy) || cmd->error)
918 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100919}
920
921/*
922 * DMA clean up for command errors
923 */
Denis Karpov70a33412009-09-22 16:44:59 -0700924static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100925{
Adrian Hunterb4175772010-05-26 14:42:06 -0700926 int dma_ch;
927
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200928 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929
Adrian Hunterb4175772010-05-26 14:42:06 -0700930 spin_lock(&host->irq_lock);
931 dma_ch = host->dma_ch;
932 host->dma_ch = -1;
933 spin_unlock(&host->irq_lock);
934
935 if (host->use_dma && dma_ch != -1) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700937 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700938 omap_free_dma(dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939 }
940 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100941}
942
943/*
944 * Readable error output
945 */
946#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700947static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100948{
949 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700950 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100951 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
952 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
953 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
954 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
955 };
956 char res[256];
957 char *buf = res;
958 int len, i;
959
960 len = sprintf(buf, "MMC IRQ 0x%x :", status);
961 buf += len;
962
Denis Karpov70a33412009-09-22 16:44:59 -0700963 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100964 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700965 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100966 buf += len;
967 }
968
969 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
970}
971#endif /* CONFIG_MMC_DEBUG */
972
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100973/*
974 * MMC controller internal state machines reset
975 *
976 * Used to reset command or data internal state machines, using respectively
977 * SRC or SRD bit of SYSCTL register
978 * Can be called from interrupt context
979 */
Denis Karpov70a33412009-09-22 16:44:59 -0700980static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
981 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100982{
983 unsigned long i = 0;
984 unsigned long limit = (loops_per_jiffy *
985 msecs_to_jiffies(MMC_TIMEOUT_MS));
986
987 OMAP_HSMMC_WRITE(host->base, SYSCTL,
988 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
989
990 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
991 (i++ < limit))
992 cpu_relax();
993
994 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
995 dev_err(mmc_dev(host->mmc),
996 "Timeout waiting on controller reset in %s\n",
997 __func__);
998}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999
Adrian Hunterb4175772010-05-26 14:42:06 -07001000static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001001{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001003 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001004
Adrian Hunterb4175772010-05-26 14:42:06 -07001005 if (!host->req_in_progress) {
1006 do {
1007 OMAP_HSMMC_WRITE(host->base, STAT, status);
1008 /* Flush posted write */
1009 status = OMAP_HSMMC_READ(host->base, STAT);
1010 } while (status & INT_EN_MASK);
1011 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001012 }
1013
1014 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001015 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1016
1017 if (status & ERR) {
1018#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -07001019 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001020#endif
1021 if ((status & CMD_TIMEOUT) ||
1022 (status & CMD_CRC)) {
1023 if (host->cmd) {
1024 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001025 omap_hsmmc_reset_controller_fsm(host,
1026 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001027 host->cmd->error = -ETIMEDOUT;
1028 } else {
1029 host->cmd->error = -EILSEQ;
1030 }
1031 end_cmd = 1;
1032 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001033 if (host->data || host->response_busy) {
1034 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001035 omap_hsmmc_dma_cleanup(host,
1036 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001037 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001038 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001039 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001040 }
1041 if ((status & DATA_TIMEOUT) ||
1042 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001043 if (host->data || host->response_busy) {
1044 int err = (status & DATA_TIMEOUT) ?
1045 -ETIMEDOUT : -EILSEQ;
1046
1047 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001048 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001050 host->mrq->cmd->error = err;
1051 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001052 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053 end_trans = 1;
1054 }
1055 }
1056 if (status & CARD_ERR) {
1057 dev_dbg(mmc_dev(host->mmc),
1058 "Ignoring card err CMD%d\n", host->cmd->opcode);
1059 if (host->cmd)
1060 end_cmd = 1;
1061 if (host->data)
1062 end_trans = 1;
1063 }
1064 }
1065
1066 OMAP_HSMMC_WRITE(host->base, STAT, status);
1067
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001068 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001069 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001070 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001071 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001072}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001073
Adrian Hunterb4175772010-05-26 14:42:06 -07001074/*
1075 * MMC controller IRQ handler
1076 */
1077static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1078{
1079 struct omap_hsmmc_host *host = dev_id;
1080 int status;
1081
1082 status = OMAP_HSMMC_READ(host->base, STAT);
1083 do {
1084 omap_hsmmc_do_irq(host, status);
1085 /* Flush posted write */
1086 status = OMAP_HSMMC_READ(host->base, STAT);
1087 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001088
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 return IRQ_HANDLED;
1090}
1091
Denis Karpov70a33412009-09-22 16:44:59 -07001092static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001093{
1094 unsigned long i;
1095
1096 OMAP_HSMMC_WRITE(host->base, HCTL,
1097 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1098 for (i = 0; i < loops_per_jiffy; i++) {
1099 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1100 break;
1101 cpu_relax();
1102 }
1103}
1104
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001105/*
David Brownelleb250822009-02-17 14:49:01 -08001106 * Switch MMC interface voltage ... only relevant for MMC1.
1107 *
1108 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1109 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1110 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001111 */
Denis Karpov70a33412009-09-22 16:44:59 -07001112static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113{
1114 u32 reg_val = 0;
1115 int ret;
1116
1117 /* Disable the clocks */
1118 clk_disable(host->fclk);
1119 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001120 if (host->got_dbclk)
1121 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001122
1123 /* Turn the power off */
1124 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001125
1126 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001127 if (!ret)
1128 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1129 vdd);
1130 clk_enable(host->iclk);
1131 clk_enable(host->fclk);
1132 if (host->got_dbclk)
1133 clk_enable(host->dbclk);
1134
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 if (ret != 0)
1136 goto err;
1137
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001138 OMAP_HSMMC_WRITE(host->base, HCTL,
1139 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1140 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001141
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001142 /*
1143 * If a MMC dual voltage card is detected, the set_ios fn calls
1144 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001145 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001146 *
David Brownelleb250822009-02-17 14:49:01 -08001147 * Cope with a bit of slop in the range ... per data sheets:
1148 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1149 * but recommended values are 1.71V to 1.89V
1150 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1151 * but recommended values are 2.7V to 3.3V
1152 *
1153 * Board setup code shouldn't permit anything very out-of-range.
1154 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1155 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001156 */
David Brownelleb250822009-02-17 14:49:01 -08001157 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001159 else
1160 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001161
1162 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001163 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001164
1165 return 0;
1166err:
1167 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1168 return ret;
1169}
1170
Adrian Hunterb62f6222009-09-22 16:45:01 -07001171/* Protect the card while the cover is open */
1172static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1173{
1174 if (!mmc_slot(host).get_cover_state)
1175 return;
1176
1177 host->reqs_blocked = 0;
1178 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1179 if (host->protect_card) {
1180 printk(KERN_INFO "%s: cover is closed, "
1181 "card is now accessible\n",
1182 mmc_hostname(host->mmc));
1183 host->protect_card = 0;
1184 }
1185 } else {
1186 if (!host->protect_card) {
1187 printk(KERN_INFO "%s: cover is open, "
1188 "card is now inaccessible\n",
1189 mmc_hostname(host->mmc));
1190 host->protect_card = 1;
1191 }
1192 }
1193}
1194
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195/*
1196 * Work Item to notify the core about card insertion/removal
1197 */
Denis Karpov70a33412009-09-22 16:44:59 -07001198static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199{
Denis Karpov70a33412009-09-22 16:44:59 -07001200 struct omap_hsmmc_host *host =
1201 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -08001202 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001203 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001204
Adrian Huntera6b22402009-09-22 16:44:45 -07001205 if (host->suspended)
1206 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207
1208 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001209
Denis Karpov191d1f12009-09-22 16:44:55 -07001210 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001211 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001212 else {
1213 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001214 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001215 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001216
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001217 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001218 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001219 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001221}
1222
1223/*
1224 * ISR for handling card insertion and removal
1225 */
Denis Karpov70a33412009-09-22 16:44:59 -07001226static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001227{
Denis Karpov70a33412009-09-22 16:44:59 -07001228 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001229
Adrian Huntera6b22402009-09-22 16:44:45 -07001230 if (host->suspended)
1231 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001232 schedule_work(&host->mmc_carddetect_work);
1233
1234 return IRQ_HANDLED;
1235}
1236
Denis Karpov70a33412009-09-22 16:44:59 -07001237static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001238 struct mmc_data *data)
1239{
1240 int sync_dev;
1241
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001242 if (data->flags & MMC_DATA_WRITE)
1243 sync_dev = host->dma_line_tx;
1244 else
1245 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001246 return sync_dev;
1247}
1248
Denis Karpov70a33412009-09-22 16:44:59 -07001249static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001250 struct mmc_data *data,
1251 struct scatterlist *sgl)
1252{
1253 int blksz, nblk, dma_ch;
1254
1255 dma_ch = host->dma_ch;
1256 if (data->flags & MMC_DATA_WRITE) {
1257 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1258 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1259 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1260 sg_dma_address(sgl), 0, 0);
1261 } else {
1262 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001263 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001264 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1265 sg_dma_address(sgl), 0, 0);
1266 }
1267
1268 blksz = host->data->blksz;
1269 nblk = sg_dma_len(sgl) / blksz;
1270
1271 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1272 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001273 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001274 !(data->flags & MMC_DATA_WRITE));
1275
1276 omap_start_dma(dma_ch);
1277}
1278
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001279/*
1280 * DMA call back function
1281 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001282static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001283{
Adrian Hunterb4175772010-05-26 14:42:06 -07001284 struct omap_hsmmc_host *host = cb_data;
1285 struct mmc_data *data = host->mrq->data;
1286 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001287
Venkatraman Sf3584e52010-08-10 18:01:54 -07001288 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1289 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1290 ch_status);
1291 return;
1292 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293
Adrian Hunterb4175772010-05-26 14:42:06 -07001294 spin_lock(&host->irq_lock);
1295 if (host->dma_ch < 0) {
1296 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001297 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001298 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001300 host->dma_sg_idx++;
1301 if (host->dma_sg_idx < host->dma_len) {
1302 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001303 omap_hsmmc_config_dma_params(host, data,
1304 data->sg + host->dma_sg_idx);
1305 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001306 return;
1307 }
1308
Adrian Hunterb4175772010-05-26 14:42:06 -07001309 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
1310 omap_hsmmc_get_dma_dir(host, data));
1311
1312 req_in_progress = host->req_in_progress;
1313 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001314 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001315 spin_unlock(&host->irq_lock);
1316
1317 omap_free_dma(dma_ch);
1318
1319 /* If DMA has finished after TC, complete the request */
1320 if (!req_in_progress) {
1321 struct mmc_request *mrq = host->mrq;
1322
1323 host->mrq = NULL;
1324 mmc_request_done(host->mmc, mrq);
1325 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001326}
1327
1328/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001329 * Routine to configure and start DMA for the MMC card
1330 */
Denis Karpov70a33412009-09-22 16:44:59 -07001331static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1332 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333{
Adrian Hunterb4175772010-05-26 14:42:06 -07001334 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335 struct mmc_data *data = req->data;
1336
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001337 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001338 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001339 struct scatterlist *sgl;
1340
1341 sgl = data->sg + i;
1342 if (sgl->length % data->blksz)
1343 return -EINVAL;
1344 }
1345 if ((data->blksz % 4) != 0)
1346 /* REVISIT: The MMC buffer increments only when MSB is written.
1347 * Return error for blksz which is non multiple of four.
1348 */
1349 return -EINVAL;
1350
Adrian Hunterb4175772010-05-26 14:42:06 -07001351 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001352
Denis Karpov70a33412009-09-22 16:44:59 -07001353 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1354 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001355 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001356 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001357 "%s: omap_request_dma() failed with %d\n",
1358 mmc_hostname(host->mmc), ret);
1359 return ret;
1360 }
1361
1362 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -07001363 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001364 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001365 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001366
Denis Karpov70a33412009-09-22 16:44:59 -07001367 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001368
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001369 return 0;
1370}
1371
Denis Karpov70a33412009-09-22 16:44:59 -07001372static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001373 unsigned int timeout_ns,
1374 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375{
1376 unsigned int timeout, cycle_ns;
1377 uint32_t reg, clkd, dto = 0;
1378
1379 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1380 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1381 if (clkd == 0)
1382 clkd = 1;
1383
1384 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001385 timeout = timeout_ns / cycle_ns;
1386 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001387 if (timeout) {
1388 while ((timeout & 0x80000000) == 0) {
1389 dto += 1;
1390 timeout <<= 1;
1391 }
1392 dto = 31 - dto;
1393 timeout <<= 1;
1394 if (timeout && dto)
1395 dto += 1;
1396 if (dto >= 13)
1397 dto -= 13;
1398 else
1399 dto = 0;
1400 if (dto > 14)
1401 dto = 14;
1402 }
1403
1404 reg &= ~DTO_MASK;
1405 reg |= dto << DTO_SHIFT;
1406 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1407}
1408
1409/*
1410 * Configure block length for MMC/SD cards and initiate the transfer.
1411 */
1412static int
Denis Karpov70a33412009-09-22 16:44:59 -07001413omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001414{
1415 int ret;
1416 host->data = req->data;
1417
1418 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001419 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001420 /*
1421 * Set an arbitrary 100ms data timeout for commands with
1422 * busy signal.
1423 */
1424 if (req->cmd->flags & MMC_RSP_BUSY)
1425 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001426 return 0;
1427 }
1428
1429 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1430 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001431 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001432
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001433 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001434 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435 if (ret != 0) {
1436 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1437 return ret;
1438 }
1439 }
1440 return 0;
1441}
1442
1443/*
1444 * Request function. for read/write operation
1445 */
Denis Karpov70a33412009-09-22 16:44:59 -07001446static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447{
Denis Karpov70a33412009-09-22 16:44:59 -07001448 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001449 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450
Adrian Hunterb4175772010-05-26 14:42:06 -07001451 BUG_ON(host->req_in_progress);
1452 BUG_ON(host->dma_ch != -1);
1453 if (host->protect_card) {
1454 if (host->reqs_blocked < 3) {
1455 /*
1456 * Ensure the controller is left in a consistent
1457 * state by resetting the command and data state
1458 * machines.
1459 */
1460 omap_hsmmc_reset_controller_fsm(host, SRD);
1461 omap_hsmmc_reset_controller_fsm(host, SRC);
1462 host->reqs_blocked += 1;
1463 }
1464 req->cmd->error = -EBADF;
1465 if (req->data)
1466 req->data->error = -EBADF;
1467 req->cmd->retries = 0;
1468 mmc_request_done(mmc, req);
1469 return;
1470 } else if (host->reqs_blocked)
1471 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472 WARN_ON(host->mrq != NULL);
1473 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001474 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001475 if (err) {
1476 req->cmd->error = err;
1477 if (req->data)
1478 req->data->error = err;
1479 host->mrq = NULL;
1480 mmc_request_done(mmc, req);
1481 return;
1482 }
1483
Denis Karpov70a33412009-09-22 16:44:59 -07001484 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001485}
1486
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001487/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001488static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001489{
Denis Karpov70a33412009-09-22 16:44:59 -07001490 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491 u16 dsor = 0;
1492 unsigned long regval;
1493 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001494 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001495 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001496
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001497 mmc_host_enable(host->mmc);
1498
Adrian Huntera3621462009-09-22 16:44:42 -07001499 if (ios->power_mode != host->power_mode) {
1500 switch (ios->power_mode) {
1501 case MMC_POWER_OFF:
1502 mmc_slot(host).set_power(host->dev, host->slot_id,
1503 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001504 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001505 break;
1506 case MMC_POWER_UP:
1507 mmc_slot(host).set_power(host->dev, host->slot_id,
1508 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001509 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001510 break;
1511 case MMC_POWER_ON:
1512 do_send_init_stream = 1;
1513 break;
1514 }
1515 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516 }
1517
Denis Karpovdd498ef2009-09-22 16:44:49 -07001518 /* FIXME: set registers based only on changes to ios */
1519
Jarkko Lavinen73153012008-11-21 16:49:54 +02001520 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001522 case MMC_BUS_WIDTH_8:
1523 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1524 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001525 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001526 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527 OMAP_HSMMC_WRITE(host->base, HCTL,
1528 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1529 break;
1530 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001531 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001532 OMAP_HSMMC_WRITE(host->base, HCTL,
1533 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1534 break;
1535 }
1536
1537 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001538 /* Only MMC1 can interface at 3V without some flavor
1539 * of external transceiver; but they all handle 1.8V.
1540 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001541 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1542 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1543 /*
1544 * The mmc_select_voltage fn of the core does
1545 * not seem to set the power_mode to
1546 * MMC_POWER_UP upon recalculating the voltage.
1547 * vdd 1.8v.
1548 */
Denis Karpov70a33412009-09-22 16:44:59 -07001549 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1550 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001551 "Switch operation failed\n");
1552 }
1553 }
1554
1555 if (ios->clock) {
1556 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1557 if (dsor < 1)
1558 dsor = 1;
1559
1560 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1561 dsor++;
1562
1563 if (dsor > 250)
1564 dsor = 250;
1565 }
Denis Karpov70a33412009-09-22 16:44:59 -07001566 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001567 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1568 regval = regval & ~(CLKD_MASK);
1569 regval = regval | (dsor << 6) | (DTO << 16);
1570 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1571 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1572 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1573
1574 /* Wait till the ICS bit is set */
1575 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001576 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001577 && time_before(jiffies, timeout))
1578 msleep(1);
1579
1580 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1581 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1582
Adrian Huntera3621462009-09-22 16:44:42 -07001583 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001584 send_init_stream(host);
1585
Denis Karpovabb28e72009-09-22 16:44:44 -07001586 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001587 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001588 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1589 else
1590 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001591
Denis Karpovdd498ef2009-09-22 16:44:49 -07001592 if (host->power_mode == MMC_POWER_OFF)
1593 mmc_host_disable(host->mmc);
1594 else
1595 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001596}
1597
1598static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1599{
Denis Karpov70a33412009-09-22 16:44:59 -07001600 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001601
Denis Karpov191d1f12009-09-22 16:44:55 -07001602 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001604 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605}
1606
1607static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1608{
Denis Karpov70a33412009-09-22 16:44:59 -07001609 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001610
Denis Karpov191d1f12009-09-22 16:44:55 -07001611 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001613 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001614}
1615
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001616static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1617{
1618 struct omap_hsmmc_host *host = mmc_priv(mmc);
1619
1620 if (mmc_slot(host).init_card)
1621 mmc_slot(host).init_card(card);
1622}
1623
Denis Karpov70a33412009-09-22 16:44:59 -07001624static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001625{
1626 u32 hctl, capa, value;
1627
1628 /* Only MMC1 supports 3.0V */
1629 if (host->id == OMAP_MMC1_DEVID) {
1630 hctl = SDVS30;
1631 capa = VS30 | VS18;
1632 } else {
1633 hctl = SDVS18;
1634 capa = VS18;
1635 }
1636
1637 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1638 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1639
1640 value = OMAP_HSMMC_READ(host->base, CAPA);
1641 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1642
1643 /* Set the controller to AUTO IDLE mode */
1644 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1645 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1646
1647 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001648 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001649}
1650
Denis Karpovdd498ef2009-09-22 16:44:49 -07001651/*
1652 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001653 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1654 * ^___________| | |
1655 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001656 *
1657 * ENABLED: mmc host is fully functional
1658 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001659 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1660 * REGSLEEP: fclk is off, voltage regulator is asleep
1661 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001662 *
1663 * Transition handlers return the timeout for the next state transition
1664 * or negative error.
1665 */
1666
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001667enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001668
1669/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001670static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001671{
Denis Karpov70a33412009-09-22 16:44:59 -07001672 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001673 clk_disable(host->fclk);
1674 host->dpm_state = DISABLED;
1675
1676 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1677
1678 if (host->power_mode == MMC_POWER_OFF)
1679 return 0;
1680
Adrian Hunter4380eea22010-02-15 10:03:34 -08001681 return OMAP_MMC_SLEEP_TIMEOUT;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001682}
1683
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001684/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001685static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001686{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001687 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001688
1689 if (!mmc_try_claim_host(host->mmc))
1690 return 0;
1691
1692 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001693 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001694 if (mmc_card_can_sleep(host->mmc)) {
1695 err = mmc_card_sleep(host->mmc);
1696 if (err < 0) {
1697 clk_disable(host->fclk);
1698 mmc_release_host(host->mmc);
1699 return err;
1700 }
1701 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001702 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001703 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001704 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001705 if (mmc_slot(host).set_sleep)
1706 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1707 new_state == CARDSLEEP);
1708 /* FIXME: turn off bus power and perhaps interrupts too */
1709 clk_disable(host->fclk);
1710 host->dpm_state = new_state;
1711
1712 mmc_release_host(host->mmc);
1713
1714 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1715 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001716
Adrian Hunter1df58db2010-02-15 10:03:34 -08001717 if (mmc_slot(host).no_off)
1718 return 0;
1719
Denis Karpovdd498ef2009-09-22 16:44:49 -07001720 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1721 mmc_slot(host).card_detect ||
1722 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001723 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
Adrian Hunter4380eea22010-02-15 10:03:34 -08001724 return OMAP_MMC_OFF_TIMEOUT;
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001725
1726 return 0;
1727}
1728
1729/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001730static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001731{
1732 if (!mmc_try_claim_host(host->mmc))
1733 return 0;
1734
Adrian Hunter1df58db2010-02-15 10:03:34 -08001735 if (mmc_slot(host).no_off)
1736 return 0;
1737
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001738 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1739 mmc_slot(host).card_detect ||
1740 (mmc_slot(host).get_cover_state &&
1741 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1742 mmc_release_host(host->mmc);
1743 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001744 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001745
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001746 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1747 host->vdd = 0;
1748 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001749
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001750 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1751 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001752
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001753 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001754
1755 mmc_release_host(host->mmc);
1756
1757 return 0;
1758}
1759
1760/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001761static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001762{
1763 int err;
1764
1765 err = clk_enable(host->fclk);
1766 if (err < 0)
1767 return err;
1768
Denis Karpov70a33412009-09-22 16:44:59 -07001769 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001770 host->dpm_state = ENABLED;
1771
1772 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1773
1774 return 0;
1775}
1776
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001777/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001778static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001779{
1780 if (!mmc_try_claim_host(host->mmc))
1781 return 0;
1782
1783 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001784 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001785 if (mmc_slot(host).set_sleep)
1786 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1787 host->vdd, host->dpm_state == CARDSLEEP);
1788 if (mmc_card_can_sleep(host->mmc))
1789 mmc_card_awake(host->mmc);
1790
1791 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1792 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1793
1794 host->dpm_state = ENABLED;
1795
1796 mmc_release_host(host->mmc);
1797
1798 return 0;
1799}
1800
Denis Karpovdd498ef2009-09-22 16:44:49 -07001801/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001802static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001803{
1804 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001805
Denis Karpov70a33412009-09-22 16:44:59 -07001806 omap_hsmmc_context_restore(host);
1807 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001808 mmc_power_restore_host(host->mmc);
1809
1810 host->dpm_state = ENABLED;
1811
1812 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1813
1814 return 0;
1815}
1816
1817/*
1818 * Bring MMC host to ENABLED from any other PM state.
1819 */
Denis Karpov70a33412009-09-22 16:44:59 -07001820static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001821{
Denis Karpov70a33412009-09-22 16:44:59 -07001822 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001823
1824 switch (host->dpm_state) {
1825 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001826 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001827 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001828 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001829 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001830 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001831 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001832 default:
1833 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1834 return -EINVAL;
1835 }
1836}
1837
1838/*
1839 * Bring MMC host in PM state (one level deeper).
1840 */
Denis Karpov70a33412009-09-22 16:44:59 -07001841static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001842{
Denis Karpov70a33412009-09-22 16:44:59 -07001843 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001844
1845 switch (host->dpm_state) {
1846 case ENABLED: {
1847 int delay;
1848
Denis Karpov70a33412009-09-22 16:44:59 -07001849 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001850 if (lazy || delay < 0)
1851 return delay;
1852 return 0;
1853 }
1854 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001855 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001856 case CARDSLEEP:
1857 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001858 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001859 default:
1860 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1861 return -EINVAL;
1862 }
1863}
1864
Denis Karpov70a33412009-09-22 16:44:59 -07001865static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001866{
Denis Karpov70a33412009-09-22 16:44:59 -07001867 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001868 int err;
1869
1870 err = clk_enable(host->fclk);
1871 if (err)
1872 return err;
1873 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001874 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001875 return 0;
1876}
1877
Denis Karpov70a33412009-09-22 16:44:59 -07001878static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001879{
Denis Karpov70a33412009-09-22 16:44:59 -07001880 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001881
Denis Karpov70a33412009-09-22 16:44:59 -07001882 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001883 clk_disable(host->fclk);
1884 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1885 return 0;
1886}
1887
Denis Karpov70a33412009-09-22 16:44:59 -07001888static const struct mmc_host_ops omap_hsmmc_ops = {
1889 .enable = omap_hsmmc_enable_fclk,
1890 .disable = omap_hsmmc_disable_fclk,
1891 .request = omap_hsmmc_request,
1892 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001893 .get_cd = omap_hsmmc_get_cd,
1894 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001895 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001896 /* NYET -- enable_sdio_irq */
1897};
1898
Denis Karpov70a33412009-09-22 16:44:59 -07001899static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1900 .enable = omap_hsmmc_enable,
1901 .disable = omap_hsmmc_disable,
1902 .request = omap_hsmmc_request,
1903 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001904 .get_cd = omap_hsmmc_get_cd,
1905 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001906 .init_card = omap_hsmmc_init_card,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001907 /* NYET -- enable_sdio_irq */
1908};
1909
Denis Karpovd900f712009-09-22 16:44:38 -07001910#ifdef CONFIG_DEBUG_FS
1911
Denis Karpov70a33412009-09-22 16:44:59 -07001912static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001913{
1914 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001915 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001916 int context_loss = 0;
1917
Denis Karpov70a33412009-09-22 16:44:59 -07001918 if (host->pdata->get_context_loss_count)
1919 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001920
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001921 seq_printf(s, "mmc%d:\n"
1922 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001923 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001924 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001925 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001926 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001927 mmc->index, mmc->enabled ? 1 : 0,
1928 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001929 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001930
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001931 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001932 seq_printf(s, "host suspended, can't read registers\n");
1933 return 0;
1934 }
1935
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001936 if (clk_enable(host->fclk) != 0) {
1937 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001938 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001939 }
Denis Karpovd900f712009-09-22 16:44:38 -07001940
1941 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1942 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1943 seq_printf(s, "CON:\t\t0x%08x\n",
1944 OMAP_HSMMC_READ(host->base, CON));
1945 seq_printf(s, "HCTL:\t\t0x%08x\n",
1946 OMAP_HSMMC_READ(host->base, HCTL));
1947 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1948 OMAP_HSMMC_READ(host->base, SYSCTL));
1949 seq_printf(s, "IE:\t\t0x%08x\n",
1950 OMAP_HSMMC_READ(host->base, IE));
1951 seq_printf(s, "ISE:\t\t0x%08x\n",
1952 OMAP_HSMMC_READ(host->base, ISE));
1953 seq_printf(s, "CAPA:\t\t0x%08x\n",
1954 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001955
1956 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001957
Denis Karpovd900f712009-09-22 16:44:38 -07001958 return 0;
1959}
1960
Denis Karpov70a33412009-09-22 16:44:59 -07001961static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001962{
Denis Karpov70a33412009-09-22 16:44:59 -07001963 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001964}
1965
1966static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001967 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001968 .read = seq_read,
1969 .llseek = seq_lseek,
1970 .release = single_release,
1971};
1972
Denis Karpov70a33412009-09-22 16:44:59 -07001973static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001974{
1975 if (mmc->debugfs_root)
1976 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1977 mmc, &mmc_regs_fops);
1978}
1979
1980#else
1981
Denis Karpov70a33412009-09-22 16:44:59 -07001982static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001983{
1984}
1985
1986#endif
1987
Denis Karpov70a33412009-09-22 16:44:59 -07001988static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001989{
1990 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1991 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001992 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001993 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001994 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995
1996 if (pdata == NULL) {
1997 dev_err(&pdev->dev, "Platform Data is missing\n");
1998 return -ENXIO;
1999 }
2000
2001 if (pdata->nr_slots == 0) {
2002 dev_err(&pdev->dev, "No Slots\n");
2003 return -ENXIO;
2004 }
2005
2006 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2007 irq = platform_get_irq(pdev, 0);
2008 if (res == NULL || irq < 0)
2009 return -ENXIO;
2010
2011 res = request_mem_region(res->start, res->end - res->start + 1,
2012 pdev->name);
2013 if (res == NULL)
2014 return -EBUSY;
2015
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002016 ret = omap_hsmmc_gpio_init(pdata);
2017 if (ret)
2018 goto err;
2019
Denis Karpov70a33412009-09-22 16:44:59 -07002020 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 if (!mmc) {
2022 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002023 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024 }
2025
2026 host = mmc_priv(mmc);
2027 host->mmc = mmc;
2028 host->pdata = pdata;
2029 host->dev = &pdev->dev;
2030 host->use_dma = 1;
2031 host->dev->dma_mask = &pdata->dma_mask;
2032 host->dma_ch = -1;
2033 host->irq = irq;
2034 host->id = pdev->id;
2035 host->slot_id = 0;
2036 host->mapbase = res->start;
2037 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08002038 host->power_mode = MMC_POWER_OFF;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002039
2040 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07002041 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002042
Denis Karpov191d1f12009-09-22 16:44:55 -07002043 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07002044 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002045 else
Denis Karpov70a33412009-09-22 16:44:59 -07002046 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002047
Adrian Huntere0eb2422010-02-15 10:03:34 -08002048 /*
2049 * If regulator_disable can only put vcc_aux to sleep then there is
2050 * no off state.
2051 */
2052 if (mmc_slot(host).vcc_aux_disable_is_sleep)
2053 mmc_slot(host).no_off = 1;
2054
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055 mmc->f_min = 400000;
2056 mmc->f_max = 52000000;
2057
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002058 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002059
Russell King6f7607c2009-01-28 10:22:50 +00002060 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002061 if (IS_ERR(host->iclk)) {
2062 ret = PTR_ERR(host->iclk);
2063 host->iclk = NULL;
2064 goto err1;
2065 }
Russell King6f7607c2009-01-28 10:22:50 +00002066 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 if (IS_ERR(host->fclk)) {
2068 ret = PTR_ERR(host->fclk);
2069 host->fclk = NULL;
2070 clk_put(host->iclk);
2071 goto err1;
2072 }
2073
Denis Karpov70a33412009-09-22 16:44:59 -07002074 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002075
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002076 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002077 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
2078 /* we start off in DISABLED state */
2079 host->dpm_state = DISABLED;
2080
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002081 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002082 clk_put(host->iclk);
2083 clk_put(host->fclk);
2084 goto err1;
2085 }
2086
2087 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002088 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002089 clk_put(host->iclk);
2090 clk_put(host->fclk);
2091 goto err1;
2092 }
2093
Adrian Hunter2bec0892009-09-22 16:45:02 -07002094 if (cpu_is_omap2430()) {
2095 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
2096 /*
2097 * MMC can still work without debounce clock.
2098 */
2099 if (IS_ERR(host->dbclk))
2100 dev_warn(mmc_dev(host->mmc),
2101 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002102 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07002103 host->got_dbclk = 1;
2104
2105 if (host->got_dbclk)
2106 if (clk_enable(host->dbclk) != 0)
2107 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
2108 " clk failed\n");
2109 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002110
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002111 /* Since we do only SG emulation, we can have as many segs
2112 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002113 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002114
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2116 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2117 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2118 mmc->max_seg_size = mmc->max_req_size;
2119
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002120 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002121 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002122
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002123 switch (mmc_slot(host).wires) {
2124 case 8:
Jarkko Lavinen73153012008-11-21 16:49:54 +02002125 mmc->caps |= MMC_CAP_8_BIT_DATA;
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002126 /* Fall through */
2127 case 4:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002128 mmc->caps |= MMC_CAP_4_BIT_DATA;
Kishore Kadiyala27151dc2010-08-10 18:01:46 -07002129 break;
2130 case 1:
2131 /* Nothing to crib here */
2132 case 0:
2133 /* Assuming nothing was given by board, Core use's 1-Bit */
2134 break;
2135 default:
2136 /* Completely unexpected.. Core goes with 1-Bit Width */
2137 dev_crit(mmc_dev(host->mmc), "Invalid width %d\n used!"
2138 "using 1 instead\n", mmc_slot(host).wires);
2139 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002140
Denis Karpov191d1f12009-09-22 16:44:55 -07002141 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002142 mmc->caps |= MMC_CAP_NONREMOVABLE;
2143
Denis Karpov70a33412009-09-22 16:44:59 -07002144 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002145
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002146 /* Select DMA lines */
2147 switch (host->id) {
2148 case OMAP_MMC1_DEVID:
2149 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
2150 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2151 break;
2152 case OMAP_MMC2_DEVID:
2153 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2154 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2155 break;
2156 case OMAP_MMC3_DEVID:
2157 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2158 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2159 break;
kishore kadiyala82cf8182009-09-22 16:45:25 -07002160 case OMAP_MMC4_DEVID:
2161 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2162 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2163 break;
2164 case OMAP_MMC5_DEVID:
2165 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2166 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2167 break;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00002168 default:
2169 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2170 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171 }
2172
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002173 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07002174 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002175 mmc_hostname(mmc), host);
2176 if (ret) {
2177 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2178 goto err_irq;
2179 }
2180
2181 if (pdata->init != NULL) {
2182 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07002183 dev_dbg(mmc_dev(host->mmc),
2184 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002185 goto err_irq_cd_init;
2186 }
2187 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002188
Adrian Hunterb702b102010-02-15 10:03:35 -08002189 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002190 ret = omap_hsmmc_reg_get(host);
2191 if (ret)
2192 goto err_reg;
2193 host->use_reg = 1;
2194 }
2195
David Brownellb583f262009-05-28 14:04:03 -07002196 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002197
2198 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002199 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002200 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07002201 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2203 | IRQF_DISABLED,
2204 mmc_hostname(mmc), host);
2205 if (ret) {
2206 dev_dbg(mmc_dev(host->mmc),
2207 "Unable to grab MMC CD IRQ\n");
2208 goto err_irq_cd;
2209 }
2210 }
2211
Adrian Hunterb4175772010-05-26 14:42:06 -07002212 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002213
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002214 mmc_host_lazy_disable(host->mmc);
2215
Adrian Hunterb62f6222009-09-22 16:45:01 -07002216 omap_hsmmc_protect_card(host);
2217
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002218 mmc_add_host(mmc);
2219
Denis Karpov191d1f12009-09-22 16:44:55 -07002220 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002221 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2222 if (ret < 0)
2223 goto err_slot_name;
2224 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002225 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002226 ret = device_create_file(&mmc->class_dev,
2227 &dev_attr_cover_switch);
2228 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002229 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002230 }
2231
Denis Karpov70a33412009-09-22 16:44:59 -07002232 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07002233
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 return 0;
2235
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002236err_slot_name:
2237 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002238 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002239err_irq_cd:
2240 if (host->use_reg)
2241 omap_hsmmc_reg_put(host);
2242err_reg:
2243 if (host->pdata->cleanup)
2244 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002245err_irq_cd_init:
2246 free_irq(host->irq, host);
2247err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002248 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249 clk_disable(host->iclk);
2250 clk_put(host->fclk);
2251 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002252 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002253 clk_disable(host->dbclk);
2254 clk_put(host->dbclk);
2255 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002256err1:
2257 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002258 platform_set_drvdata(pdev, NULL);
2259 mmc_free_host(mmc);
2260err_alloc:
2261 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002262err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263 release_mem_region(res->start, res->end - res->start + 1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002264 return ret;
2265}
2266
Denis Karpov70a33412009-09-22 16:44:59 -07002267static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002268{
Denis Karpov70a33412009-09-22 16:44:59 -07002269 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002270 struct resource *res;
2271
2272 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002273 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002275 if (host->use_reg)
2276 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002277 if (host->pdata->cleanup)
2278 host->pdata->cleanup(&pdev->dev);
2279 free_irq(host->irq, host);
2280 if (mmc_slot(host).card_detect_irq)
2281 free_irq(mmc_slot(host).card_detect_irq, host);
2282 flush_scheduled_work();
2283
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002284 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002285 clk_disable(host->iclk);
2286 clk_put(host->fclk);
2287 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002288 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289 clk_disable(host->dbclk);
2290 clk_put(host->dbclk);
2291 }
2292
2293 mmc_free_host(host->mmc);
2294 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002295 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296 }
2297
2298 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2299 if (res)
2300 release_mem_region(res->start, res->end - res->start + 1);
2301 platform_set_drvdata(pdev, NULL);
2302
2303 return 0;
2304}
2305
2306#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002307static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002308{
2309 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002310 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002311 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002312
2313 if (host && host->suspended)
2314 return 0;
2315
2316 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002317 host->suspended = 1;
2318 if (host->pdata->suspend) {
2319 ret = host->pdata->suspend(&pdev->dev,
2320 host->slot_id);
2321 if (ret) {
2322 dev_dbg(mmc_dev(host->mmc),
2323 "Unable to handle MMC board"
2324 " level suspend\n");
2325 host->suspended = 0;
2326 return ret;
2327 }
2328 }
2329 cancel_work_sync(&host->mmc_carddetect_work);
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002330 ret = mmc_suspend_host(host->mmc);
Ethan Due7cb7562010-09-09 16:37:49 -07002331 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002332 if (ret == 0) {
Adrian Hunterb4175772010-05-26 14:42:06 -07002333 omap_hsmmc_disable_irq(host);
Jarkko Lavinen0683af42009-03-12 15:30:58 +02002334 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07002335 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002336 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002337 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002338 if (host->got_dbclk)
2339 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07002340 } else {
2341 host->suspended = 0;
2342 if (host->pdata->resume) {
2343 ret = host->pdata->resume(&pdev->dev,
2344 host->slot_id);
2345 if (ret)
2346 dev_dbg(mmc_dev(host->mmc),
2347 "Unmask interrupt failed\n");
2348 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002349 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07002350 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002351
2352 }
2353 return ret;
2354}
2355
2356/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002357static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002358{
2359 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002360 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002361 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002362
2363 if (host && !host->suspended)
2364 return 0;
2365
2366 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002367 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002368 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002369 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002370
Denis Karpov11dd62a2009-09-22 16:44:43 -07002371 if (mmc_host_enable(host->mmc) != 0) {
2372 clk_disable(host->iclk);
2373 goto clk_en_err;
2374 }
2375
Adrian Hunter2bec0892009-09-22 16:45:02 -07002376 if (host->got_dbclk)
2377 clk_enable(host->dbclk);
2378
Denis Karpov70a33412009-09-22 16:44:59 -07002379 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002380
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002381 if (host->pdata->resume) {
2382 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2383 if (ret)
2384 dev_dbg(mmc_dev(host->mmc),
2385 "Unmask interrupt failed\n");
2386 }
2387
Adrian Hunterb62f6222009-09-22 16:45:01 -07002388 omap_hsmmc_protect_card(host);
2389
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002390 /* Notify the core to resume the host */
2391 ret = mmc_resume_host(host->mmc);
2392 if (ret == 0)
2393 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07002394
Adrian Hunter5e2ea612009-09-22 16:44:39 -07002395 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002396 }
2397
2398 return ret;
2399
2400clk_en_err:
2401 dev_dbg(mmc_dev(host->mmc),
2402 "Failed to enable MMC clocks during resume\n");
2403 return ret;
2404}
2405
2406#else
Denis Karpov70a33412009-09-22 16:44:59 -07002407#define omap_hsmmc_suspend NULL
2408#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002409#endif
2410
Kevin Hilmana791daa2010-05-26 14:42:07 -07002411static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002412 .suspend = omap_hsmmc_suspend,
2413 .resume = omap_hsmmc_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002414};
2415
2416static struct platform_driver omap_hsmmc_driver = {
2417 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002418 .driver = {
2419 .name = DRIVER_NAME,
2420 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002421 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002422 },
2423};
2424
Denis Karpov70a33412009-09-22 16:44:59 -07002425static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002426{
2427 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002428 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002429}
2430
Denis Karpov70a33412009-09-22 16:44:59 -07002431static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002432{
2433 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002434 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002435}
2436
Denis Karpov70a33412009-09-22 16:44:59 -07002437module_init(omap_hsmmc_init);
2438module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002439
2440MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2441MODULE_LICENSE("GPL");
2442MODULE_ALIAS("platform:" DRIVER_NAME);
2443MODULE_AUTHOR("Texas Instruments Inc");