2 * drivers/mmc/host/omap_hsmmc.c
4 * Driver for OMAP2430/3430 MMC controller.
6 * Copyright (C) 2007 Texas Instruments.
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
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.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/seq_file.h>
22 #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>
30 #include <linux/mmc/core.h>
31 #include <linux/mmc/mmc.h>
33 #include <linux/semaphore.h>
34 #include <linux/gpio.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/pm_runtime.h>
38 #include <mach/hardware.h>
39 #include <plat/board.h>
43 /* OMAP HSMMC Host Controller Registers */
44 #define OMAP_HSMMC_SYSCONFIG 0x0010
45 #define OMAP_HSMMC_SYSSTATUS 0x0014
46 #define OMAP_HSMMC_CON 0x002C
47 #define OMAP_HSMMC_BLK 0x0104
48 #define OMAP_HSMMC_ARG 0x0108
49 #define OMAP_HSMMC_CMD 0x010C
50 #define OMAP_HSMMC_RSP10 0x0110
51 #define OMAP_HSMMC_RSP32 0x0114
52 #define OMAP_HSMMC_RSP54 0x0118
53 #define OMAP_HSMMC_RSP76 0x011C
54 #define OMAP_HSMMC_DATA 0x0120
55 #define OMAP_HSMMC_HCTL 0x0128
56 #define OMAP_HSMMC_SYSCTL 0x012C
57 #define OMAP_HSMMC_STAT 0x0130
58 #define OMAP_HSMMC_IE 0x0134
59 #define OMAP_HSMMC_ISE 0x0138
60 #define OMAP_HSMMC_CAPA 0x0140
62 #define VS18 (1 << 26)
63 #define VS30 (1 << 25)
64 #define SDVS18 (0x5 << 9)
65 #define SDVS30 (0x6 << 9)
66 #define SDVS33 (0x7 << 9)
67 #define SDVS_MASK 0x00000E00
68 #define SDVSCLR 0xFFFFF1FF
69 #define SDVSDET 0x00000400
76 #define CLKD_MASK 0x0000FFC0
78 #define DTO_MASK 0x000F0000
80 #define INT_EN_MASK 0x307F0033
81 #define BWR_ENABLE (1 << 4)
82 #define BRR_ENABLE (1 << 5)
83 #define DTO_ENABLE (1 << 20)
84 #define INIT_STREAM (1 << 1)
85 #define DP_SELECT (1 << 21)
90 #define FOUR_BIT (1 << 1)
96 #define CMD_TIMEOUT (1 << 16)
97 #define DATA_TIMEOUT (1 << 20)
98 #define CMD_CRC (1 << 17)
99 #define DATA_CRC (1 << 21)
100 #define CARD_ERR (1 << 28)
101 #define STAT_CLEAR 0xFFFFFFFF
102 #define INIT_STREAM_CMD 0x00000000
103 #define DUAL_VOLT_OCR_BIT 7
104 #define SRC (1 << 25)
105 #define SRD (1 << 26)
106 #define SOFTRESET (1 << 1)
107 #define RESETDONE (1 << 0)
110 * FIXME: Most likely all the data using these _DEVID defines should come
111 * from the platform_data, or implemented in controller and slot specific
114 #define OMAP_MMC1_DEVID 0
115 #define OMAP_MMC2_DEVID 1
116 #define OMAP_MMC3_DEVID 2
117 #define OMAP_MMC4_DEVID 3
118 #define OMAP_MMC5_DEVID 4
120 #define MMC_AUTOSUSPEND_DELAY 100
121 #define MMC_TIMEOUT_MS 20
122 #define OMAP_MMC_MASTER_CLOCK 96000000
123 #define DRIVER_NAME "omap_hsmmc"
126 * One controller can have multiple slots, like on some omap boards using
127 * omap.c controller driver. Luckily this is not currently done on any known
128 * omap_hsmmc.c device.
130 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
133 * MMC Host controller read/write API's
135 #define OMAP_HSMMC_READ(base, reg) \
136 __raw_readl((base) + OMAP_HSMMC_##reg)
138 #define OMAP_HSMMC_WRITE(base, reg, val) \
139 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
141 struct omap_hsmmc_next {
142 unsigned int dma_len;
146 struct omap_hsmmc_host {
148 struct mmc_host *mmc;
149 struct mmc_request *mrq;
150 struct mmc_command *cmd;
151 struct mmc_data *data;
155 * vcc == configured supply
156 * vcc_aux == optional
157 * - MMC1, supply for DAT4..DAT7
158 * - MMC2/MMC2, external level shifter voltage supply, for
159 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
161 struct regulator *vcc;
162 struct regulator *vcc_aux;
163 struct work_struct mmc_carddetect_work;
165 resource_size_t mapbase;
166 spinlock_t irq_lock; /* Prevent races with irq handler */
168 unsigned int dma_len;
169 unsigned int dma_sg_idx;
170 unsigned char bus_mode;
171 unsigned char power_mode;
177 int dma_line_tx, dma_line_rx;
188 struct omap_hsmmc_next next_data;
190 struct omap_mmc_platform_data *pdata;
193 static int omap_hsmmc_card_detect(struct device *dev, int slot)
195 struct omap_mmc_platform_data *mmc = dev->platform_data;
197 /* NOTE: assumes card detect signal is active-low */
198 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
201 static int omap_hsmmc_get_wp(struct device *dev, int slot)
203 struct omap_mmc_platform_data *mmc = dev->platform_data;
205 /* NOTE: assumes write protect signal is active-high */
206 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
209 static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
211 struct omap_mmc_platform_data *mmc = dev->platform_data;
213 /* NOTE: assumes card detect signal is active-low */
214 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
219 static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
221 struct omap_mmc_platform_data *mmc = dev->platform_data;
223 disable_irq(mmc->slots[0].card_detect_irq);
227 static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
229 struct omap_mmc_platform_data *mmc = dev->platform_data;
231 enable_irq(mmc->slots[0].card_detect_irq);
237 #define omap_hsmmc_suspend_cdirq NULL
238 #define omap_hsmmc_resume_cdirq NULL
242 #ifdef CONFIG_REGULATOR
244 static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
247 struct omap_hsmmc_host *host =
248 platform_get_drvdata(to_platform_device(dev));
251 if (mmc_slot(host).before_set_reg)
252 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
255 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
257 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
259 if (mmc_slot(host).after_set_reg)
260 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
265 static int omap_hsmmc_235_set_power(struct device *dev, int slot, int power_on,
268 struct omap_hsmmc_host *host =
269 platform_get_drvdata(to_platform_device(dev));
273 * If we don't see a Vcc regulator, assume it's a fixed
274 * voltage always-on regulator.
279 if (mmc_slot(host).before_set_reg)
280 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
283 * Assume Vcc regulator is used only to power the card ... OMAP
284 * VDDS is used to power the pins, optionally with a transceiver to
285 * support cards using voltages other than VDDS (1.8V nominal). When a
286 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
288 * In some cases this regulator won't support enable/disable;
289 * e.g. it's a fixed rail for a WLAN chip.
291 * In other cases vcc_aux switches interface power. Example, for
292 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
293 * chips/cards need an interface voltage rail too.
296 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
297 /* Enable interface voltage rail, if needed */
298 if (ret == 0 && host->vcc_aux) {
299 ret = regulator_enable(host->vcc_aux);
301 ret = mmc_regulator_set_ocr(host->mmc,
305 /* Shut down the rail */
307 ret = regulator_disable(host->vcc_aux);
309 /* Then proceed to shut down the local regulator */
310 ret = mmc_regulator_set_ocr(host->mmc,
315 if (mmc_slot(host).after_set_reg)
316 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
321 static int omap_hsmmc_4_set_power(struct device *dev, int slot, int power_on,
327 static int omap_hsmmc_1_set_sleep(struct device *dev, int slot, int sleep,
328 int vdd, int cardsleep)
330 struct omap_hsmmc_host *host =
331 platform_get_drvdata(to_platform_device(dev));
332 int mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
334 return regulator_set_mode(host->vcc, mode);
337 static int omap_hsmmc_235_set_sleep(struct device *dev, int slot, int sleep,
338 int vdd, int cardsleep)
340 struct omap_hsmmc_host *host =
341 platform_get_drvdata(to_platform_device(dev));
345 * If we don't see a Vcc regulator, assume it's a fixed
346 * voltage always-on regulator.
351 mode = sleep ? REGULATOR_MODE_STANDBY : REGULATOR_MODE_NORMAL;
354 return regulator_set_mode(host->vcc, mode);
357 /* VCC can be turned off if card is asleep */
359 err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
361 err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
363 err = regulator_set_mode(host->vcc, mode);
367 if (!mmc_slot(host).vcc_aux_disable_is_sleep)
368 return regulator_set_mode(host->vcc_aux, mode);
371 return regulator_disable(host->vcc_aux);
373 return regulator_enable(host->vcc_aux);
376 static int omap_hsmmc_4_set_sleep(struct device *dev, int slot, int sleep,
377 int vdd, int cardsleep)
382 static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
384 struct regulator *reg;
389 case OMAP_MMC1_DEVID:
390 /* On-chip level shifting via PBIAS0/PBIAS1 */
391 mmc_slot(host).set_power = omap_hsmmc_1_set_power;
392 mmc_slot(host).set_sleep = omap_hsmmc_1_set_sleep;
394 case OMAP_MMC2_DEVID:
395 case OMAP_MMC3_DEVID:
396 case OMAP_MMC5_DEVID:
397 /* Off-chip level shifting, or none */
398 mmc_slot(host).set_power = omap_hsmmc_235_set_power;
399 mmc_slot(host).set_sleep = omap_hsmmc_235_set_sleep;
401 case OMAP_MMC4_DEVID:
402 mmc_slot(host).set_power = omap_hsmmc_4_set_power;
403 mmc_slot(host).set_sleep = omap_hsmmc_4_set_sleep;
405 pr_err("MMC%d configuration not supported!\n", host->id);
409 reg = regulator_get(host->dev, "vmmc");
411 dev_dbg(host->dev, "vmmc regulator missing\n");
413 * HACK: until fixed.c regulator is usable,
414 * we don't require a main regulator
417 if (host->id == OMAP_MMC1_DEVID) {
423 ocr_value = mmc_regulator_get_ocrmask(reg);
424 if (!mmc_slot(host).ocr_mask) {
425 mmc_slot(host).ocr_mask = ocr_value;
427 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
428 pr_err("MMC%d ocrmask %x is not supported\n",
429 host->id, mmc_slot(host).ocr_mask);
430 mmc_slot(host).ocr_mask = 0;
435 /* Allow an aux regulator */
436 reg = regulator_get(host->dev, "vmmc_aux");
437 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
439 /* For eMMC do not power off when not in sleep state */
440 if (mmc_slot(host).no_regulator_off_init)
443 * UGLY HACK: workaround regulator framework bugs.
444 * When the bootloader leaves a supply active, it's
445 * initialized with zero usecount ... and we can't
446 * disable it without first enabling it. Until the
447 * framework is fixed, we need a workaround like this
448 * (which is safe for MMC, but not in general).
450 if (regulator_is_enabled(host->vcc) > 0) {
451 regulator_enable(host->vcc);
452 regulator_disable(host->vcc);
455 if (regulator_is_enabled(reg) > 0) {
456 regulator_enable(reg);
457 regulator_disable(reg);
465 mmc_slot(host).set_power = NULL;
466 mmc_slot(host).set_sleep = NULL;
470 static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
472 regulator_put(host->vcc);
473 regulator_put(host->vcc_aux);
474 mmc_slot(host).set_power = NULL;
475 mmc_slot(host).set_sleep = NULL;
478 static inline int omap_hsmmc_have_reg(void)
485 static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
490 static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
494 static inline int omap_hsmmc_have_reg(void)
501 static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
505 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
506 if (pdata->slots[0].cover)
507 pdata->slots[0].get_cover_state =
508 omap_hsmmc_get_cover_state;
510 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
511 pdata->slots[0].card_detect_irq =
512 gpio_to_irq(pdata->slots[0].switch_pin);
513 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
516 ret = gpio_direction_input(pdata->slots[0].switch_pin);
520 pdata->slots[0].switch_pin = -EINVAL;
522 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
523 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
524 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
527 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
531 pdata->slots[0].gpio_wp = -EINVAL;
536 gpio_free(pdata->slots[0].gpio_wp);
538 if (gpio_is_valid(pdata->slots[0].switch_pin))
540 gpio_free(pdata->slots[0].switch_pin);
544 static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
546 if (gpio_is_valid(pdata->slots[0].gpio_wp))
547 gpio_free(pdata->slots[0].gpio_wp);
548 if (gpio_is_valid(pdata->slots[0].switch_pin))
549 gpio_free(pdata->slots[0].switch_pin);
553 * Stop clock to the card
555 static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
557 OMAP_HSMMC_WRITE(host->base, SYSCTL,
558 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
559 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
560 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
563 static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
564 struct mmc_command *cmd)
566 unsigned int irq_mask;
569 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
571 irq_mask = INT_EN_MASK;
573 /* Disable timeout for erases */
574 if (cmd->opcode == MMC_ERASE)
575 irq_mask &= ~DTO_ENABLE;
577 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
578 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
579 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
582 static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
584 OMAP_HSMMC_WRITE(host->base, ISE, 0);
585 OMAP_HSMMC_WRITE(host->base, IE, 0);
586 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
592 * Restore the MMC host context, if it was lost as result of a
593 * power state change.
595 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
597 struct mmc_ios *ios = &host->mmc->ios;
598 struct omap_mmc_platform_data *pdata = host->pdata;
599 int context_loss = 0;
602 unsigned long timeout;
604 if (pdata->get_context_loss_count) {
605 context_loss = pdata->get_context_loss_count(host->dev);
606 if (context_loss < 0)
610 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
611 context_loss == host->context_loss ? "not " : "");
612 if (host->context_loss == context_loss)
615 /* Wait for hardware reset */
616 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
617 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
618 && time_before(jiffies, timeout))
621 /* Do software reset */
622 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
623 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
624 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
625 && time_before(jiffies, timeout))
628 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
629 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
631 if (host->id == OMAP_MMC1_DEVID) {
632 if (host->power_mode != MMC_POWER_OFF &&
633 (1 << ios->vdd) <= MMC_VDD_23_24)
643 OMAP_HSMMC_WRITE(host->base, HCTL,
644 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
646 OMAP_HSMMC_WRITE(host->base, CAPA,
647 OMAP_HSMMC_READ(host->base, CAPA) | capa);
649 OMAP_HSMMC_WRITE(host->base, HCTL,
650 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
652 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
653 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
654 && time_before(jiffies, timeout))
657 omap_hsmmc_disable_irq(host);
659 /* Do not initialize card-specific things if the power is off */
660 if (host->power_mode == MMC_POWER_OFF)
663 con = OMAP_HSMMC_READ(host->base, CON);
664 switch (ios->bus_width) {
665 case MMC_BUS_WIDTH_8:
666 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
668 case MMC_BUS_WIDTH_4:
669 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
670 OMAP_HSMMC_WRITE(host->base, HCTL,
671 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
673 case MMC_BUS_WIDTH_1:
674 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
675 OMAP_HSMMC_WRITE(host->base, HCTL,
676 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
681 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
685 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
692 OMAP_HSMMC_WRITE(host->base, SYSCTL,
693 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
694 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
695 OMAP_HSMMC_WRITE(host->base, SYSCTL,
696 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
698 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
699 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
700 && time_before(jiffies, timeout))
703 OMAP_HSMMC_WRITE(host->base, SYSCTL,
704 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
706 con = OMAP_HSMMC_READ(host->base, CON);
707 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
708 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
710 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
712 host->context_loss = context_loss;
714 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
719 * Save the MMC host context (store the number of power state changes so far).
721 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
723 struct omap_mmc_platform_data *pdata = host->pdata;
726 if (pdata->get_context_loss_count) {
727 context_loss = pdata->get_context_loss_count(host->dev);
728 if (context_loss < 0)
730 host->context_loss = context_loss;
736 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
741 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
748 * Send init stream sequence to card
749 * before sending IDLE command
751 static void send_init_stream(struct omap_hsmmc_host *host)
754 unsigned long timeout;
756 if (host->protect_card)
759 disable_irq(host->irq);
761 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
762 OMAP_HSMMC_WRITE(host->base, CON,
763 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
764 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
766 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
767 while ((reg != CC) && time_before(jiffies, timeout))
768 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
770 OMAP_HSMMC_WRITE(host->base, CON,
771 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
773 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
774 OMAP_HSMMC_READ(host->base, STAT);
776 enable_irq(host->irq);
780 int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
784 if (mmc_slot(host).get_cover_state)
785 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
790 omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
793 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
794 struct omap_hsmmc_host *host = mmc_priv(mmc);
796 return sprintf(buf, "%s\n",
797 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
800 static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
803 omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
806 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
807 struct omap_hsmmc_host *host = mmc_priv(mmc);
809 return sprintf(buf, "%s\n", mmc_slot(host).name);
812 static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
815 * Configure the response type and send the cmd.
818 omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
819 struct mmc_data *data)
821 int cmdreg = 0, resptype = 0, cmdtype = 0;
823 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
824 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
827 omap_hsmmc_enable_irq(host, cmd);
829 host->response_busy = 0;
830 if (cmd->flags & MMC_RSP_PRESENT) {
831 if (cmd->flags & MMC_RSP_136)
833 else if (cmd->flags & MMC_RSP_BUSY) {
835 host->response_busy = 1;
841 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
842 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
843 * a val of 0x3, rest 0x0.
845 if (cmd == host->mrq->stop)
848 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
851 cmdreg |= DP_SELECT | MSBS | BCE;
852 if (data->flags & MMC_DATA_READ)
861 host->req_in_progress = 1;
863 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
864 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
868 omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
870 if (data->flags & MMC_DATA_WRITE)
871 return DMA_TO_DEVICE;
873 return DMA_FROM_DEVICE;
876 static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
880 spin_lock(&host->irq_lock);
881 host->req_in_progress = 0;
882 dma_ch = host->dma_ch;
883 spin_unlock(&host->irq_lock);
885 omap_hsmmc_disable_irq(host);
886 /* Do not complete the request if DMA is still in progress */
887 if (mrq->data && host->use_dma && dma_ch != -1)
890 mmc_request_done(host->mmc, mrq);
894 * Notify the transfer complete to MMC core
897 omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
900 struct mmc_request *mrq = host->mrq;
902 /* TC before CC from CMD6 - don't know why, but it happens */
903 if (host->cmd && host->cmd->opcode == 6 &&
904 host->response_busy) {
905 host->response_busy = 0;
909 omap_hsmmc_request_done(host, mrq);
916 data->bytes_xfered += data->blocks * (data->blksz);
918 data->bytes_xfered = 0;
921 omap_hsmmc_request_done(host, data->mrq);
924 omap_hsmmc_start_command(host, data->stop, NULL);
928 * Notify the core about command completion
931 omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
935 if (cmd->flags & MMC_RSP_PRESENT) {
936 if (cmd->flags & MMC_RSP_136) {
937 /* response type 2 */
938 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
939 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
940 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
941 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
943 /* response types 1, 1b, 3, 4, 5, 6 */
944 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
947 if ((host->data == NULL && !host->response_busy) || cmd->error)
948 omap_hsmmc_request_done(host, cmd->mrq);
952 * DMA clean up for command errors
954 static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
958 host->data->error = errno;
960 spin_lock(&host->irq_lock);
961 dma_ch = host->dma_ch;
963 spin_unlock(&host->irq_lock);
965 if (host->use_dma && dma_ch != -1) {
966 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
968 omap_hsmmc_get_dma_dir(host, host->data));
969 omap_free_dma(dma_ch);
975 * Readable error output
977 #ifdef CONFIG_MMC_DEBUG
978 static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
980 /* --- means reserved bit without definition at documentation */
981 static const char *omap_hsmmc_status_bits[] = {
982 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
983 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
984 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
985 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
991 len = sprintf(buf, "MMC IRQ 0x%x :", status);
994 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
995 if (status & (1 << i)) {
996 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
1000 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
1003 static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1007 #endif /* CONFIG_MMC_DEBUG */
1010 * MMC controller internal state machines reset
1012 * Used to reset command or data internal state machines, using respectively
1013 * SRC or SRD bit of SYSCTL register
1014 * Can be called from interrupt context
1016 static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1019 unsigned long i = 0;
1020 unsigned long limit = (loops_per_jiffy *
1021 msecs_to_jiffies(MMC_TIMEOUT_MS));
1023 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1024 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1027 * OMAP4 ES2 and greater has an updated reset logic.
1028 * Monitor a 0->1 transition first
1030 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
1031 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
1037 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1041 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1042 dev_err(mmc_dev(host->mmc),
1043 "Timeout waiting on controller reset in %s\n",
1047 static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
1049 struct mmc_data *data;
1050 int end_cmd = 0, end_trans = 0;
1052 if (!host->req_in_progress) {
1054 OMAP_HSMMC_WRITE(host->base, STAT, status);
1055 /* Flush posted write */
1056 status = OMAP_HSMMC_READ(host->base, STAT);
1057 } while (status & INT_EN_MASK);
1062 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1065 omap_hsmmc_dbg_report_irq(host, status);
1066 if ((status & CMD_TIMEOUT) ||
1067 (status & CMD_CRC)) {
1069 if (status & CMD_TIMEOUT) {
1070 omap_hsmmc_reset_controller_fsm(host,
1072 host->cmd->error = -ETIMEDOUT;
1074 host->cmd->error = -EILSEQ;
1078 if (host->data || host->response_busy) {
1080 omap_hsmmc_dma_cleanup(host,
1082 host->response_busy = 0;
1083 omap_hsmmc_reset_controller_fsm(host, SRD);
1086 if ((status & DATA_TIMEOUT) ||
1087 (status & DATA_CRC)) {
1088 if (host->data || host->response_busy) {
1089 int err = (status & DATA_TIMEOUT) ?
1090 -ETIMEDOUT : -EILSEQ;
1093 omap_hsmmc_dma_cleanup(host, err);
1095 host->mrq->cmd->error = err;
1096 host->response_busy = 0;
1097 omap_hsmmc_reset_controller_fsm(host, SRD);
1101 if (status & CARD_ERR) {
1102 dev_dbg(mmc_dev(host->mmc),
1103 "Ignoring card err CMD%d\n", host->cmd->opcode);
1111 OMAP_HSMMC_WRITE(host->base, STAT, status);
1113 if (end_cmd || ((status & CC) && host->cmd))
1114 omap_hsmmc_cmd_done(host, host->cmd);
1115 if ((end_trans || (status & TC)) && host->mrq)
1116 omap_hsmmc_xfer_done(host, data);
1120 * MMC controller IRQ handler
1122 static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1124 struct omap_hsmmc_host *host = dev_id;
1127 status = OMAP_HSMMC_READ(host->base, STAT);
1129 omap_hsmmc_do_irq(host, status);
1130 /* Flush posted write */
1131 status = OMAP_HSMMC_READ(host->base, STAT);
1132 } while (status & INT_EN_MASK);
1137 static void set_sd_bus_power(struct omap_hsmmc_host *host)
1141 OMAP_HSMMC_WRITE(host->base, HCTL,
1142 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1143 for (i = 0; i < loops_per_jiffy; i++) {
1144 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1151 * Switch MMC interface voltage ... only relevant for MMC1.
1153 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1154 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1155 * Some chips, like eMMC ones, use internal transceivers.
1157 static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
1162 /* Disable the clocks */
1163 pm_runtime_put_sync(host->dev);
1164 if (host->got_dbclk)
1165 clk_disable(host->dbclk);
1167 /* Turn the power off */
1168 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1170 /* Turn the power ON with given VDD 1.8 or 3.0v */
1172 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1174 pm_runtime_get_sync(host->dev);
1175 if (host->got_dbclk)
1176 clk_enable(host->dbclk);
1181 OMAP_HSMMC_WRITE(host->base, HCTL,
1182 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1183 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
1186 * If a MMC dual voltage card is detected, the set_ios fn calls
1187 * this fn with VDD bit set for 1.8V. Upon card removal from the
1188 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
1190 * Cope with a bit of slop in the range ... per data sheets:
1191 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1192 * but recommended values are 1.71V to 1.89V
1193 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1194 * but recommended values are 2.7V to 3.3V
1196 * Board setup code shouldn't permit anything very out-of-range.
1197 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1198 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
1200 if ((1 << vdd) <= MMC_VDD_23_24)
1205 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
1206 set_sd_bus_power(host);
1210 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1214 /* Protect the card while the cover is open */
1215 static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1217 if (!mmc_slot(host).get_cover_state)
1220 host->reqs_blocked = 0;
1221 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1222 if (host->protect_card) {
1223 printk(KERN_INFO "%s: cover is closed, "
1224 "card is now accessible\n",
1225 mmc_hostname(host->mmc));
1226 host->protect_card = 0;
1229 if (!host->protect_card) {
1230 printk(KERN_INFO "%s: cover is open, "
1231 "card is now inaccessible\n",
1232 mmc_hostname(host->mmc));
1233 host->protect_card = 1;
1239 * Work Item to notify the core about card insertion/removal
1241 static void omap_hsmmc_detect(struct work_struct *work)
1243 struct omap_hsmmc_host *host =
1244 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
1245 struct omap_mmc_slot_data *slot = &mmc_slot(host);
1248 if (host->suspended)
1251 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1253 if (slot->card_detect)
1254 carddetect = slot->card_detect(host->dev, host->slot_id);
1256 omap_hsmmc_protect_card(host);
1257 carddetect = -ENOSYS;
1261 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1263 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1267 * ISR for handling card insertion and removal
1269 static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
1271 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
1273 if (host->suspended)
1275 schedule_work(&host->mmc_carddetect_work);
1280 static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
1281 struct mmc_data *data)
1285 if (data->flags & MMC_DATA_WRITE)
1286 sync_dev = host->dma_line_tx;
1288 sync_dev = host->dma_line_rx;
1292 static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
1293 struct mmc_data *data,
1294 struct scatterlist *sgl)
1296 int blksz, nblk, dma_ch;
1298 dma_ch = host->dma_ch;
1299 if (data->flags & MMC_DATA_WRITE) {
1300 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1301 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1302 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1303 sg_dma_address(sgl), 0, 0);
1305 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1306 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1307 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1308 sg_dma_address(sgl), 0, 0);
1311 blksz = host->data->blksz;
1312 nblk = sg_dma_len(sgl) / blksz;
1314 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1315 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
1316 omap_hsmmc_get_dma_sync_dev(host, data),
1317 !(data->flags & MMC_DATA_WRITE));
1319 omap_start_dma(dma_ch);
1323 * DMA call back function
1325 static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
1327 struct omap_hsmmc_host *host = cb_data;
1328 struct mmc_data *data = host->mrq->data;
1329 int dma_ch, req_in_progress;
1331 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1332 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1337 spin_lock(&host->irq_lock);
1338 if (host->dma_ch < 0) {
1339 spin_unlock(&host->irq_lock);
1344 if (host->dma_sg_idx < host->dma_len) {
1345 /* Fire up the next transfer. */
1346 omap_hsmmc_config_dma_params(host, data,
1347 data->sg + host->dma_sg_idx);
1348 spin_unlock(&host->irq_lock);
1352 if (!data->host_cookie)
1353 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1354 omap_hsmmc_get_dma_dir(host, data));
1356 req_in_progress = host->req_in_progress;
1357 dma_ch = host->dma_ch;
1359 spin_unlock(&host->irq_lock);
1361 omap_free_dma(dma_ch);
1363 /* If DMA has finished after TC, complete the request */
1364 if (!req_in_progress) {
1365 struct mmc_request *mrq = host->mrq;
1368 mmc_request_done(host->mmc, mrq);
1372 static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1373 struct mmc_data *data,
1374 struct omap_hsmmc_next *next)
1378 if (!next && data->host_cookie &&
1379 data->host_cookie != host->next_data.cookie) {
1380 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
1381 " host->next_data.cookie %d\n",
1382 __func__, data->host_cookie, host->next_data.cookie);
1383 data->host_cookie = 0;
1386 /* Check if next job is already prepared */
1388 (!next && data->host_cookie != host->next_data.cookie)) {
1389 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1391 omap_hsmmc_get_dma_dir(host, data));
1394 dma_len = host->next_data.dma_len;
1395 host->next_data.dma_len = 0;
1403 next->dma_len = dma_len;
1404 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1406 host->dma_len = dma_len;
1412 * Routine to configure and start DMA for the MMC card
1414 static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1415 struct mmc_request *req)
1417 int dma_ch = 0, ret = 0, i;
1418 struct mmc_data *data = req->data;
1420 /* Sanity check: all the SG entries must be aligned by block size. */
1421 for (i = 0; i < data->sg_len; i++) {
1422 struct scatterlist *sgl;
1425 if (sgl->length % data->blksz)
1428 if ((data->blksz % 4) != 0)
1429 /* REVISIT: The MMC buffer increments only when MSB is written.
1430 * Return error for blksz which is non multiple of four.
1434 BUG_ON(host->dma_ch != -1);
1436 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1437 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
1439 dev_err(mmc_dev(host->mmc),
1440 "%s: omap_request_dma() failed with %d\n",
1441 mmc_hostname(host->mmc), ret);
1444 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1448 host->dma_ch = dma_ch;
1449 host->dma_sg_idx = 0;
1451 omap_hsmmc_config_dma_params(host, data, data->sg);
1456 static void set_data_timeout(struct omap_hsmmc_host *host,
1457 unsigned int timeout_ns,
1458 unsigned int timeout_clks)
1460 unsigned int timeout, cycle_ns;
1461 uint32_t reg, clkd, dto = 0;
1463 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1464 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1468 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
1469 timeout = timeout_ns / cycle_ns;
1470 timeout += timeout_clks;
1472 while ((timeout & 0x80000000) == 0) {
1489 reg |= dto << DTO_SHIFT;
1490 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1494 * Configure block length for MMC/SD cards and initiate the transfer.
1497 omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
1500 host->data = req->data;
1502 if (req->data == NULL) {
1503 OMAP_HSMMC_WRITE(host->base, BLK, 0);
1505 * Set an arbitrary 100ms data timeout for commands with
1508 if (req->cmd->flags & MMC_RSP_BUSY)
1509 set_data_timeout(host, 100000000U, 0);
1513 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1514 | (req->data->blocks << 16));
1515 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
1517 if (host->use_dma) {
1518 ret = omap_hsmmc_start_dma_transfer(host, req);
1520 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1527 static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1530 struct omap_hsmmc_host *host = mmc_priv(mmc);
1531 struct mmc_data *data = mrq->data;
1533 if (host->use_dma) {
1534 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1535 omap_hsmmc_get_dma_dir(host, data));
1536 data->host_cookie = 0;
1540 static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1543 struct omap_hsmmc_host *host = mmc_priv(mmc);
1545 if (mrq->data->host_cookie) {
1546 mrq->data->host_cookie = 0;
1551 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1553 mrq->data->host_cookie = 0;
1557 * Request function. for read/write operation
1559 static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
1561 struct omap_hsmmc_host *host = mmc_priv(mmc);
1564 BUG_ON(host->req_in_progress);
1565 BUG_ON(host->dma_ch != -1);
1566 if (host->protect_card) {
1567 if (host->reqs_blocked < 3) {
1569 * Ensure the controller is left in a consistent
1570 * state by resetting the command and data state
1573 omap_hsmmc_reset_controller_fsm(host, SRD);
1574 omap_hsmmc_reset_controller_fsm(host, SRC);
1575 host->reqs_blocked += 1;
1577 req->cmd->error = -EBADF;
1579 req->data->error = -EBADF;
1580 req->cmd->retries = 0;
1581 mmc_request_done(mmc, req);
1583 } else if (host->reqs_blocked)
1584 host->reqs_blocked = 0;
1585 WARN_ON(host->mrq != NULL);
1587 err = omap_hsmmc_prepare_data(host, req);
1589 req->cmd->error = err;
1591 req->data->error = err;
1593 mmc_request_done(mmc, req);
1597 omap_hsmmc_start_command(host, req->cmd, req->data);
1600 /* Routine to configure clock values. Exposed API to core */
1601 static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1603 struct omap_hsmmc_host *host = mmc_priv(mmc);
1605 unsigned long regval;
1606 unsigned long timeout;
1608 int do_send_init_stream = 0;
1610 pm_runtime_get_sync(host->dev);
1612 if (ios->power_mode != host->power_mode) {
1613 switch (ios->power_mode) {
1615 mmc_slot(host).set_power(host->dev, host->slot_id,
1620 mmc_slot(host).set_power(host->dev, host->slot_id,
1622 host->vdd = ios->vdd;
1625 do_send_init_stream = 1;
1628 host->power_mode = ios->power_mode;
1631 /* FIXME: set registers based only on changes to ios */
1633 con = OMAP_HSMMC_READ(host->base, CON);
1634 switch (mmc->ios.bus_width) {
1635 case MMC_BUS_WIDTH_8:
1636 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1638 case MMC_BUS_WIDTH_4:
1639 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
1640 OMAP_HSMMC_WRITE(host->base, HCTL,
1641 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1643 case MMC_BUS_WIDTH_1:
1644 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
1645 OMAP_HSMMC_WRITE(host->base, HCTL,
1646 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1650 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1651 /* Only MMC1 can interface at 3V without some flavor
1652 * of external transceiver; but they all handle 1.8V.
1654 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1655 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1657 * The mmc_select_voltage fn of the core does
1658 * not seem to set the power_mode to
1659 * MMC_POWER_UP upon recalculating the voltage.
1662 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1663 dev_dbg(mmc_dev(host->mmc),
1664 "Switch operation failed\n");
1669 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1673 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1679 omap_hsmmc_stop_clock(host);
1680 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1681 regval = regval & ~(CLKD_MASK);
1682 regval = regval | (dsor << 6) | (DTO << 16);
1683 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1684 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1685 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1687 /* Wait till the ICS bit is set */
1688 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
1689 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
1690 && time_before(jiffies, timeout))
1693 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1694 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1696 if (do_send_init_stream)
1697 send_init_stream(host);
1699 con = OMAP_HSMMC_READ(host->base, CON);
1700 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1701 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1703 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
1705 pm_runtime_put_autosuspend(host->dev);
1708 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1710 struct omap_hsmmc_host *host = mmc_priv(mmc);
1712 if (!mmc_slot(host).card_detect)
1714 return mmc_slot(host).card_detect(host->dev, host->slot_id);
1717 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1719 struct omap_hsmmc_host *host = mmc_priv(mmc);
1721 if (!mmc_slot(host).get_ro)
1723 return mmc_slot(host).get_ro(host->dev, 0);
1726 static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1728 struct omap_hsmmc_host *host = mmc_priv(mmc);
1730 if (mmc_slot(host).init_card)
1731 mmc_slot(host).init_card(card);
1734 static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
1736 u32 hctl, capa, value;
1738 /* Only MMC1 supports 3.0V */
1739 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1747 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1748 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1750 value = OMAP_HSMMC_READ(host->base, CAPA);
1751 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1753 /* Set the controller to AUTO IDLE mode */
1754 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1755 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1757 /* Set SD bus power bit */
1758 set_sd_bus_power(host);
1761 static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
1763 struct omap_hsmmc_host *host = mmc_priv(mmc);
1765 pm_runtime_get_sync(host->dev);
1770 static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
1772 struct omap_hsmmc_host *host = mmc_priv(mmc);
1774 pm_runtime_mark_last_busy(host->dev);
1775 pm_runtime_put_autosuspend(host->dev);
1780 static const struct mmc_host_ops omap_hsmmc_ops = {
1781 .enable = omap_hsmmc_enable_fclk,
1782 .disable = omap_hsmmc_disable_fclk,
1783 .post_req = omap_hsmmc_post_req,
1784 .pre_req = omap_hsmmc_pre_req,
1785 .request = omap_hsmmc_request,
1786 .set_ios = omap_hsmmc_set_ios,
1787 .get_cd = omap_hsmmc_get_cd,
1788 .get_ro = omap_hsmmc_get_ro,
1789 .init_card = omap_hsmmc_init_card,
1790 /* NYET -- enable_sdio_irq */
1793 #ifdef CONFIG_DEBUG_FS
1795 static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
1797 struct mmc_host *mmc = s->private;
1798 struct omap_hsmmc_host *host = mmc_priv(mmc);
1799 int context_loss = 0;
1801 if (host->pdata->get_context_loss_count)
1802 context_loss = host->pdata->get_context_loss_count(host->dev);
1804 seq_printf(s, "mmc%d:\n"
1807 " nesting_cnt:\t%d\n"
1808 " ctx_loss:\t%d:%d\n"
1810 mmc->index, mmc->enabled ? 1 : 0,
1811 host->dpm_state, mmc->nesting_cnt,
1812 host->context_loss, context_loss);
1814 if (host->suspended) {
1815 seq_printf(s, "host suspended, can't read registers\n");
1819 pm_runtime_get_sync(host->dev);
1821 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1822 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1823 seq_printf(s, "CON:\t\t0x%08x\n",
1824 OMAP_HSMMC_READ(host->base, CON));
1825 seq_printf(s, "HCTL:\t\t0x%08x\n",
1826 OMAP_HSMMC_READ(host->base, HCTL));
1827 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1828 OMAP_HSMMC_READ(host->base, SYSCTL));
1829 seq_printf(s, "IE:\t\t0x%08x\n",
1830 OMAP_HSMMC_READ(host->base, IE));
1831 seq_printf(s, "ISE:\t\t0x%08x\n",
1832 OMAP_HSMMC_READ(host->base, ISE));
1833 seq_printf(s, "CAPA:\t\t0x%08x\n",
1834 OMAP_HSMMC_READ(host->base, CAPA));
1836 pm_runtime_mark_last_busy(host->dev);
1837 pm_runtime_put_autosuspend(host->dev);
1842 static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
1844 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
1847 static const struct file_operations mmc_regs_fops = {
1848 .open = omap_hsmmc_regs_open,
1850 .llseek = seq_lseek,
1851 .release = single_release,
1854 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1856 if (mmc->debugfs_root)
1857 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1858 mmc, &mmc_regs_fops);
1863 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1869 static int __init omap_hsmmc_probe(struct platform_device *pdev)
1871 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1872 struct mmc_host *mmc;
1873 struct omap_hsmmc_host *host = NULL;
1874 struct resource *res;
1877 if (pdata == NULL) {
1878 dev_err(&pdev->dev, "Platform Data is missing\n");
1882 if (pdata->nr_slots == 0) {
1883 dev_err(&pdev->dev, "No Slots\n");
1887 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1888 irq = platform_get_irq(pdev, 0);
1889 if (res == NULL || irq < 0)
1892 res->start += pdata->reg_offset;
1893 res->end += pdata->reg_offset;
1894 res = request_mem_region(res->start, resource_size(res), pdev->name);
1898 ret = omap_hsmmc_gpio_init(pdata);
1902 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
1908 host = mmc_priv(mmc);
1910 host->pdata = pdata;
1911 host->dev = &pdev->dev;
1913 host->dev->dma_mask = &pdata->dma_mask;
1916 host->id = pdev->id;
1918 host->mapbase = res->start;
1919 host->base = ioremap(host->mapbase, SZ_4K);
1920 host->power_mode = MMC_POWER_OFF;
1921 host->next_data.cookie = 1;
1923 platform_set_drvdata(pdev, host);
1924 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
1926 mmc->ops = &omap_hsmmc_ops;
1929 * If regulator_disable can only put vcc_aux to sleep then there is
1932 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1933 mmc_slot(host).no_off = 1;
1935 mmc->f_min = 400000;
1936 mmc->f_max = 52000000;
1938 spin_lock_init(&host->irq_lock);
1940 host->fclk = clk_get(&pdev->dev, "fck");
1941 if (IS_ERR(host->fclk)) {
1942 ret = PTR_ERR(host->fclk);
1947 omap_hsmmc_context_save(host);
1949 mmc->caps |= MMC_CAP_DISABLE;
1951 pm_runtime_enable(host->dev);
1952 pm_runtime_get_sync(host->dev);
1953 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1954 pm_runtime_use_autosuspend(host->dev);
1956 if (cpu_is_omap2430()) {
1957 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1959 * MMC can still work without debounce clock.
1961 if (IS_ERR(host->dbclk))
1962 dev_warn(mmc_dev(host->mmc),
1963 "Failed to get debounce clock\n");
1965 host->got_dbclk = 1;
1967 if (host->got_dbclk)
1968 if (clk_enable(host->dbclk) != 0)
1969 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1973 /* Since we do only SG emulation, we can have as many segs
1975 mmc->max_segs = 1024;
1977 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1978 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1979 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1980 mmc->max_seg_size = mmc->max_req_size;
1982 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1983 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
1985 mmc->caps |= mmc_slot(host).caps;
1986 if (mmc->caps & MMC_CAP_8_BIT_DATA)
1987 mmc->caps |= MMC_CAP_4_BIT_DATA;
1989 if (mmc_slot(host).nonremovable)
1990 mmc->caps |= MMC_CAP_NONREMOVABLE;
1992 omap_hsmmc_conf_bus_power(host);
1994 /* Select DMA lines */
1996 case OMAP_MMC1_DEVID:
1997 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1998 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
2000 case OMAP_MMC2_DEVID:
2001 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
2002 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
2004 case OMAP_MMC3_DEVID:
2005 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
2006 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
2008 case OMAP_MMC4_DEVID:
2009 host->dma_line_tx = OMAP44XX_DMA_MMC4_TX;
2010 host->dma_line_rx = OMAP44XX_DMA_MMC4_RX;
2012 case OMAP_MMC5_DEVID:
2013 host->dma_line_tx = OMAP44XX_DMA_MMC5_TX;
2014 host->dma_line_rx = OMAP44XX_DMA_MMC5_RX;
2017 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
2021 /* Request IRQ for MMC operations */
2022 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
2023 mmc_hostname(mmc), host);
2025 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2029 if (pdata->init != NULL) {
2030 if (pdata->init(&pdev->dev) != 0) {
2031 dev_dbg(mmc_dev(host->mmc),
2032 "Unable to configure MMC IRQs\n");
2033 goto err_irq_cd_init;
2037 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
2038 ret = omap_hsmmc_reg_get(host);
2044 mmc->ocr_avail = mmc_slot(host).ocr_mask;
2046 /* Request IRQ for card detect */
2047 if ((mmc_slot(host).card_detect_irq)) {
2048 ret = request_irq(mmc_slot(host).card_detect_irq,
2049 omap_hsmmc_cd_handler,
2050 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2052 mmc_hostname(mmc), host);
2054 dev_dbg(mmc_dev(host->mmc),
2055 "Unable to grab MMC CD IRQ\n");
2058 pdata->suspend = omap_hsmmc_suspend_cdirq;
2059 pdata->resume = omap_hsmmc_resume_cdirq;
2062 omap_hsmmc_disable_irq(host);
2064 omap_hsmmc_protect_card(host);
2068 if (mmc_slot(host).name != NULL) {
2069 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2073 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
2074 ret = device_create_file(&mmc->class_dev,
2075 &dev_attr_cover_switch);
2080 omap_hsmmc_debugfs(mmc);
2081 pm_runtime_mark_last_busy(host->dev);
2082 pm_runtime_put_autosuspend(host->dev);
2087 mmc_remove_host(mmc);
2088 free_irq(mmc_slot(host).card_detect_irq, host);
2091 omap_hsmmc_reg_put(host);
2093 if (host->pdata->cleanup)
2094 host->pdata->cleanup(&pdev->dev);
2096 free_irq(host->irq, host);
2098 pm_runtime_mark_last_busy(host->dev);
2099 pm_runtime_put_autosuspend(host->dev);
2100 clk_put(host->fclk);
2101 if (host->got_dbclk) {
2102 clk_disable(host->dbclk);
2103 clk_put(host->dbclk);
2106 iounmap(host->base);
2107 platform_set_drvdata(pdev, NULL);
2110 omap_hsmmc_gpio_free(pdata);
2112 release_mem_region(res->start, resource_size(res));
2116 static int omap_hsmmc_remove(struct platform_device *pdev)
2118 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2119 struct resource *res;
2122 pm_runtime_get_sync(host->dev);
2123 mmc_remove_host(host->mmc);
2125 omap_hsmmc_reg_put(host);
2126 if (host->pdata->cleanup)
2127 host->pdata->cleanup(&pdev->dev);
2128 free_irq(host->irq, host);
2129 if (mmc_slot(host).card_detect_irq)
2130 free_irq(mmc_slot(host).card_detect_irq, host);
2131 flush_work_sync(&host->mmc_carddetect_work);
2133 pm_runtime_put_sync(host->dev);
2134 pm_runtime_disable(host->dev);
2135 clk_put(host->fclk);
2136 if (host->got_dbclk) {
2137 clk_disable(host->dbclk);
2138 clk_put(host->dbclk);
2141 mmc_free_host(host->mmc);
2142 iounmap(host->base);
2143 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2148 release_mem_region(res->start, resource_size(res));
2149 platform_set_drvdata(pdev, NULL);
2155 static int omap_hsmmc_suspend(struct device *dev)
2158 struct platform_device *pdev = to_platform_device(dev);
2159 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2161 if (host && host->suspended)
2165 pm_runtime_get_sync(host->dev);
2166 host->suspended = 1;
2167 if (host->pdata->suspend) {
2168 ret = host->pdata->suspend(&pdev->dev,
2171 dev_dbg(mmc_dev(host->mmc),
2172 "Unable to handle MMC board"
2173 " level suspend\n");
2174 host->suspended = 0;
2178 cancel_work_sync(&host->mmc_carddetect_work);
2179 ret = mmc_suspend_host(host->mmc);
2182 omap_hsmmc_disable_irq(host);
2183 OMAP_HSMMC_WRITE(host->base, HCTL,
2184 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2185 if (host->got_dbclk)
2186 clk_disable(host->dbclk);
2188 host->suspended = 0;
2189 if (host->pdata->resume) {
2190 ret = host->pdata->resume(&pdev->dev,
2193 dev_dbg(mmc_dev(host->mmc),
2194 "Unmask interrupt failed\n");
2197 pm_runtime_put_sync(host->dev);
2202 /* Routine to resume the MMC device */
2203 static int omap_hsmmc_resume(struct device *dev)
2206 struct platform_device *pdev = to_platform_device(dev);
2207 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2209 if (host && !host->suspended)
2213 pm_runtime_get_sync(host->dev);
2215 if (host->got_dbclk)
2216 clk_enable(host->dbclk);
2218 omap_hsmmc_conf_bus_power(host);
2220 if (host->pdata->resume) {
2221 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2223 dev_dbg(mmc_dev(host->mmc),
2224 "Unmask interrupt failed\n");
2227 omap_hsmmc_protect_card(host);
2229 /* Notify the core to resume the host */
2230 ret = mmc_resume_host(host->mmc);
2232 host->suspended = 0;
2234 pm_runtime_mark_last_busy(host->dev);
2235 pm_runtime_put_autosuspend(host->dev);
2243 #define omap_hsmmc_suspend NULL
2244 #define omap_hsmmc_resume NULL
2247 static int omap_hsmmc_runtime_suspend(struct device *dev)
2249 struct omap_hsmmc_host *host;
2251 host = platform_get_drvdata(to_platform_device(dev));
2252 omap_hsmmc_context_save(host);
2253 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2258 static int omap_hsmmc_runtime_resume(struct device *dev)
2260 struct omap_hsmmc_host *host;
2262 host = platform_get_drvdata(to_platform_device(dev));
2263 omap_hsmmc_context_restore(host);
2264 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2269 static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
2270 .suspend = omap_hsmmc_suspend,
2271 .resume = omap_hsmmc_resume,
2272 .runtime_suspend = omap_hsmmc_runtime_suspend,
2273 .runtime_resume = omap_hsmmc_runtime_resume,
2276 static struct platform_driver omap_hsmmc_driver = {
2277 .remove = omap_hsmmc_remove,
2279 .name = DRIVER_NAME,
2280 .owner = THIS_MODULE,
2281 .pm = &omap_hsmmc_dev_pm_ops,
2285 static int __init omap_hsmmc_init(void)
2287 /* Register the MMC driver */
2288 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
2291 static void __exit omap_hsmmc_cleanup(void)
2293 /* Unregister MMC driver */
2294 platform_driver_unregister(&omap_hsmmc_driver);
2297 module_init(omap_hsmmc_init);
2298 module_exit(omap_hsmmc_cleanup);
2300 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2301 MODULE_LICENSE("GPL");
2302 MODULE_ALIAS("platform:" DRIVER_NAME);
2303 MODULE_AUTHOR("Texas Instruments Inc");