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/kernel.h>
21 #include <linux/debugfs.h>
22 #include <linux/dmaengine.h>
23 #include <linux/seq_file.h>
24 #include <linux/sizes.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/platform_device.h>
29 #include <linux/timer.h>
30 #include <linux/clk.h>
32 #include <linux/of_irq.h>
33 #include <linux/of_gpio.h>
34 #include <linux/of_device.h>
35 #include <linux/omap-dmaengine.h>
36 #include <linux/mmc/host.h>
37 #include <linux/mmc/core.h>
38 #include <linux/mmc/mmc.h>
40 #include <linux/irq.h>
41 #include <linux/gpio.h>
42 #include <linux/regulator/consumer.h>
43 #include <linux/pinctrl/consumer.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/platform_data/hsmmc-omap.h>
47 /* OMAP HSMMC Host Controller Registers */
48 #define OMAP_HSMMC_SYSSTATUS 0x0014
49 #define OMAP_HSMMC_CON 0x002C
50 #define OMAP_HSMMC_SDMASA 0x0100
51 #define OMAP_HSMMC_BLK 0x0104
52 #define OMAP_HSMMC_ARG 0x0108
53 #define OMAP_HSMMC_CMD 0x010C
54 #define OMAP_HSMMC_RSP10 0x0110
55 #define OMAP_HSMMC_RSP32 0x0114
56 #define OMAP_HSMMC_RSP54 0x0118
57 #define OMAP_HSMMC_RSP76 0x011C
58 #define OMAP_HSMMC_DATA 0x0120
59 #define OMAP_HSMMC_PSTATE 0x0124
60 #define OMAP_HSMMC_HCTL 0x0128
61 #define OMAP_HSMMC_SYSCTL 0x012C
62 #define OMAP_HSMMC_STAT 0x0130
63 #define OMAP_HSMMC_IE 0x0134
64 #define OMAP_HSMMC_ISE 0x0138
65 #define OMAP_HSMMC_AC12 0x013C
66 #define OMAP_HSMMC_CAPA 0x0140
68 #define VS18 (1 << 26)
69 #define VS30 (1 << 25)
71 #define SDVS18 (0x5 << 9)
72 #define SDVS30 (0x6 << 9)
73 #define SDVS33 (0x7 << 9)
74 #define SDVS_MASK 0x00000E00
75 #define SDVSCLR 0xFFFFF1FF
76 #define SDVSDET 0x00000400
83 #define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
84 #define CLKD_MASK 0x0000FFC0
86 #define DTO_MASK 0x000F0000
88 #define INIT_STREAM (1 << 1)
89 #define ACEN_ACMD23 (2 << 2)
90 #define DP_SELECT (1 << 21)
95 #define FOUR_BIT (1 << 1)
99 #define CLKEXTFREE (1 << 16)
100 #define CTPL (1 << 11)
103 #define STAT_CLEAR 0xFFFFFFFF
104 #define INIT_STREAM_CMD 0x00000000
105 #define DUAL_VOLT_OCR_BIT 7
106 #define SRC (1 << 25)
107 #define SRD (1 << 26)
108 #define SOFTRESET (1 << 1)
111 #define DLEV_DAT(x) (1 << (20 + (x)))
113 /* Interrupt masks for IE and ISE register */
114 #define CC_EN (1 << 0)
115 #define TC_EN (1 << 1)
116 #define BWR_EN (1 << 4)
117 #define BRR_EN (1 << 5)
118 #define CIRQ_EN (1 << 8)
119 #define ERR_EN (1 << 15)
120 #define CTO_EN (1 << 16)
121 #define CCRC_EN (1 << 17)
122 #define CEB_EN (1 << 18)
123 #define CIE_EN (1 << 19)
124 #define DTO_EN (1 << 20)
125 #define DCRC_EN (1 << 21)
126 #define DEB_EN (1 << 22)
127 #define ACE_EN (1 << 24)
128 #define CERR_EN (1 << 28)
129 #define BADA_EN (1 << 29)
131 #define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
132 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
133 BRR_EN | BWR_EN | TC_EN | CC_EN)
136 #define ACIE (1 << 4)
137 #define ACEB (1 << 3)
138 #define ACCE (1 << 2)
139 #define ACTO (1 << 1)
140 #define ACNE (1 << 0)
142 #define MMC_AUTOSUSPEND_DELAY 100
143 #define MMC_TIMEOUT_MS 20 /* 20 mSec */
144 #define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
145 #define OMAP_MMC_MIN_CLOCK 400000
146 #define OMAP_MMC_MAX_CLOCK 52000000
147 #define DRIVER_NAME "omap_hsmmc"
149 #define VDD_1V8 1800000 /* 180000 uV */
150 #define VDD_3V0 3000000 /* 300000 uV */
151 #define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
154 * One controller can have multiple slots, like on some omap boards using
155 * omap.c controller driver. Luckily this is not currently done on any known
156 * omap_hsmmc.c device.
158 #define mmc_pdata(host) host->pdata
161 * MMC Host controller read/write API's
163 #define OMAP_HSMMC_READ(base, reg) \
164 __raw_readl((base) + OMAP_HSMMC_##reg)
166 #define OMAP_HSMMC_WRITE(base, reg, val) \
167 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
169 struct omap_hsmmc_next {
170 unsigned int dma_len;
174 struct omap_hsmmc_host {
176 struct mmc_host *mmc;
177 struct mmc_request *mrq;
178 struct mmc_command *cmd;
179 struct mmc_data *data;
183 * vcc == configured supply
184 * vcc_aux == optional
185 * - MMC1, supply for DAT4..DAT7
186 * - MMC2/MMC2, external level shifter voltage supply, for
187 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
189 struct regulator *vcc;
190 struct regulator *vcc_aux;
191 struct regulator *pbias;
194 resource_size_t mapbase;
195 spinlock_t irq_lock; /* Prevent races with irq handler */
196 unsigned int dma_len;
197 unsigned int dma_sg_idx;
198 unsigned char bus_mode;
199 unsigned char power_mode;
208 struct dma_chan *tx_chan;
209 struct dma_chan *rx_chan;
217 unsigned long clk_rate;
219 #define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
220 #define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
221 #define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
222 struct omap_hsmmc_next next_data;
223 struct omap_hsmmc_platform_data *pdata;
226 struct omap_mmc_of_data {
231 static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
233 static int omap_hsmmc_card_detect(struct device *dev, int slot)
235 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
236 struct omap_hsmmc_platform_data *mmc = host->pdata;
238 /* NOTE: assumes card detect signal is active-low */
239 return !gpio_get_value_cansleep(mmc->switch_pin);
242 static int omap_hsmmc_get_wp(struct device *dev, int slot)
244 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
245 struct omap_hsmmc_platform_data *mmc = host->pdata;
247 /* NOTE: assumes write protect signal is active-high */
248 return gpio_get_value_cansleep(mmc->gpio_wp);
251 static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
253 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
254 struct omap_hsmmc_platform_data *mmc = host->pdata;
256 /* NOTE: assumes card detect signal is active-low */
257 return !gpio_get_value_cansleep(mmc->switch_pin);
262 static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
264 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
265 struct omap_hsmmc_platform_data *mmc = host->pdata;
267 disable_irq(mmc->card_detect_irq);
271 static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
273 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
274 struct omap_hsmmc_platform_data *mmc = host->pdata;
276 enable_irq(mmc->card_detect_irq);
282 #define omap_hsmmc_suspend_cdirq NULL
283 #define omap_hsmmc_resume_cdirq NULL
287 #ifdef CONFIG_REGULATOR
289 static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
292 struct omap_hsmmc_host *host =
293 platform_get_drvdata(to_platform_device(dev));
297 * If we don't see a Vcc regulator, assume it's a fixed
298 * voltage always-on regulator.
303 if (mmc_pdata(host)->before_set_reg)
304 mmc_pdata(host)->before_set_reg(dev, slot, power_on, vdd);
307 if (host->pbias_enabled == 1) {
308 ret = regulator_disable(host->pbias);
310 host->pbias_enabled = 0;
312 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
316 * Assume Vcc regulator is used only to power the card ... OMAP
317 * VDDS is used to power the pins, optionally with a transceiver to
318 * support cards using voltages other than VDDS (1.8V nominal). When a
319 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
321 * In some cases this regulator won't support enable/disable;
322 * e.g. it's a fixed rail for a WLAN chip.
324 * In other cases vcc_aux switches interface power. Example, for
325 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
326 * chips/cards need an interface voltage rail too.
330 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
331 /* Enable interface voltage rail, if needed */
332 if (ret == 0 && host->vcc_aux) {
333 ret = regulator_enable(host->vcc_aux);
334 if (ret < 0 && host->vcc)
335 ret = mmc_regulator_set_ocr(host->mmc,
339 /* Shut down the rail */
341 ret = regulator_disable(host->vcc_aux);
343 /* Then proceed to shut down the local regulator */
344 ret = mmc_regulator_set_ocr(host->mmc,
350 if (vdd <= VDD_165_195)
351 ret = regulator_set_voltage(host->pbias, VDD_1V8,
354 ret = regulator_set_voltage(host->pbias, VDD_3V0,
357 goto error_set_power;
359 if (host->pbias_enabled == 0) {
360 ret = regulator_enable(host->pbias);
362 host->pbias_enabled = 1;
366 if (mmc_pdata(host)->after_set_reg)
367 mmc_pdata(host)->after_set_reg(dev, slot, power_on, vdd);
373 static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
375 struct regulator *reg;
378 reg = devm_regulator_get(host->dev, "vmmc");
380 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
385 ocr_value = mmc_regulator_get_ocrmask(reg);
386 if (!mmc_pdata(host)->ocr_mask) {
387 mmc_pdata(host)->ocr_mask = ocr_value;
389 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
390 dev_err(host->dev, "ocrmask %x is not supported\n",
391 mmc_pdata(host)->ocr_mask);
392 mmc_pdata(host)->ocr_mask = 0;
397 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
399 /* Allow an aux regulator */
400 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
401 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
403 reg = devm_regulator_get_optional(host->dev, "pbias");
404 host->pbias = IS_ERR(reg) ? NULL : reg;
406 /* For eMMC do not power off when not in sleep state */
407 if (mmc_pdata(host)->no_regulator_off_init)
410 * To disable boot_on regulator, enable regulator
411 * to increase usecount and then disable it.
413 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
414 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
415 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
417 mmc_pdata(host)->set_power(host->dev, host->slot_id, 1, vdd);
418 mmc_pdata(host)->set_power(host->dev, host->slot_id, 0, 0);
424 static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
426 mmc_pdata(host)->set_power = NULL;
429 static inline int omap_hsmmc_have_reg(void)
436 static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
441 static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
445 static inline int omap_hsmmc_have_reg(void)
452 static int omap_hsmmc_gpio_init(struct omap_hsmmc_platform_data *pdata)
456 if (gpio_is_valid(pdata->switch_pin)) {
458 pdata->get_cover_state =
459 omap_hsmmc_get_cover_state;
461 pdata->card_detect = omap_hsmmc_card_detect;
462 pdata->card_detect_irq =
463 gpio_to_irq(pdata->switch_pin);
464 ret = gpio_request(pdata->switch_pin, "mmc_cd");
467 ret = gpio_direction_input(pdata->switch_pin);
471 pdata->switch_pin = -EINVAL;
474 if (gpio_is_valid(pdata->gpio_wp)) {
475 pdata->get_ro = omap_hsmmc_get_wp;
476 ret = gpio_request(pdata->gpio_wp, "mmc_wp");
479 ret = gpio_direction_input(pdata->gpio_wp);
483 pdata->gpio_wp = -EINVAL;
489 gpio_free(pdata->gpio_wp);
491 if (gpio_is_valid(pdata->switch_pin))
493 gpio_free(pdata->switch_pin);
497 static void omap_hsmmc_gpio_free(struct omap_hsmmc_platform_data *pdata)
499 if (gpio_is_valid(pdata->gpio_wp))
500 gpio_free(pdata->gpio_wp);
501 if (gpio_is_valid(pdata->switch_pin))
502 gpio_free(pdata->switch_pin);
506 * Start clock to the card
508 static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
510 OMAP_HSMMC_WRITE(host->base, SYSCTL,
511 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
515 * Stop clock to the card
517 static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
519 OMAP_HSMMC_WRITE(host->base, SYSCTL,
520 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
521 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
522 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
525 static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
526 struct mmc_command *cmd)
528 u32 irq_mask = INT_EN_MASK;
532 irq_mask &= ~(BRR_EN | BWR_EN);
534 /* Disable timeout for erases */
535 if (cmd->opcode == MMC_ERASE)
538 spin_lock_irqsave(&host->irq_lock, flags);
539 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
540 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
542 /* latch pending CIRQ, but don't signal MMC core */
543 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
545 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
546 spin_unlock_irqrestore(&host->irq_lock, flags);
549 static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
554 spin_lock_irqsave(&host->irq_lock, flags);
555 /* no transfer running but need to keep cirq if enabled */
556 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
558 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
559 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
560 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
561 spin_unlock_irqrestore(&host->irq_lock, flags);
564 /* Calculate divisor for the given clock frequency */
565 static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
570 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
578 static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
580 struct mmc_ios *ios = &host->mmc->ios;
581 unsigned long regval;
582 unsigned long timeout;
583 unsigned long clkdiv;
585 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
587 omap_hsmmc_stop_clock(host);
589 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
590 regval = regval & ~(CLKD_MASK | DTO_MASK);
591 clkdiv = calc_divisor(host, ios);
592 regval = regval | (clkdiv << 6) | (DTO << 16);
593 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
594 OMAP_HSMMC_WRITE(host->base, SYSCTL,
595 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
597 /* Wait till the ICS bit is set */
598 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
599 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
600 && time_before(jiffies, timeout))
604 * Enable High-Speed Support
606 * - Controller should support High-Speed-Enable Bit
607 * - Controller should not be using DDR Mode
608 * - Controller should advertise that it supports High Speed
609 * in capabilities register
610 * - MMC/SD clock coming out of controller > 25MHz
612 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
613 (ios->timing != MMC_TIMING_MMC_DDR52) &&
614 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
615 regval = OMAP_HSMMC_READ(host->base, HCTL);
616 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
621 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
624 omap_hsmmc_start_clock(host);
627 static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
629 struct mmc_ios *ios = &host->mmc->ios;
632 con = OMAP_HSMMC_READ(host->base, CON);
633 if (ios->timing == MMC_TIMING_MMC_DDR52)
634 con |= DDR; /* configure in DDR mode */
637 switch (ios->bus_width) {
638 case MMC_BUS_WIDTH_8:
639 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
641 case MMC_BUS_WIDTH_4:
642 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
643 OMAP_HSMMC_WRITE(host->base, HCTL,
644 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
646 case MMC_BUS_WIDTH_1:
647 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
648 OMAP_HSMMC_WRITE(host->base, HCTL,
649 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
654 static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
656 struct mmc_ios *ios = &host->mmc->ios;
659 con = OMAP_HSMMC_READ(host->base, CON);
660 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
661 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
663 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
669 * Restore the MMC host context, if it was lost as result of a
670 * power state change.
672 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
674 struct mmc_ios *ios = &host->mmc->ios;
676 unsigned long timeout;
678 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
679 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
680 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
681 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
684 host->context_loss++;
686 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
687 if (host->power_mode != MMC_POWER_OFF &&
688 (1 << ios->vdd) <= MMC_VDD_23_24)
698 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
701 OMAP_HSMMC_WRITE(host->base, HCTL,
702 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
704 OMAP_HSMMC_WRITE(host->base, CAPA,
705 OMAP_HSMMC_READ(host->base, CAPA) | capa);
707 OMAP_HSMMC_WRITE(host->base, HCTL,
708 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
710 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
711 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
712 && time_before(jiffies, timeout))
715 OMAP_HSMMC_WRITE(host->base, ISE, 0);
716 OMAP_HSMMC_WRITE(host->base, IE, 0);
717 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
719 /* Do not initialize card-specific things if the power is off */
720 if (host->power_mode == MMC_POWER_OFF)
723 omap_hsmmc_set_bus_width(host);
725 omap_hsmmc_set_clock(host);
727 omap_hsmmc_set_bus_mode(host);
730 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
736 * Save the MMC host context (store the number of power state changes so far).
738 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
740 host->con = OMAP_HSMMC_READ(host->base, CON);
741 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
742 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
743 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
748 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
753 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
760 * Send init stream sequence to card
761 * before sending IDLE command
763 static void send_init_stream(struct omap_hsmmc_host *host)
766 unsigned long timeout;
768 if (host->protect_card)
771 disable_irq(host->irq);
773 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
774 OMAP_HSMMC_WRITE(host->base, CON,
775 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
776 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
778 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
779 while ((reg != CC_EN) && time_before(jiffies, timeout))
780 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
782 OMAP_HSMMC_WRITE(host->base, CON,
783 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
785 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
786 OMAP_HSMMC_READ(host->base, STAT);
788 enable_irq(host->irq);
792 int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
796 if (mmc_pdata(host)->get_cover_state)
797 r = mmc_pdata(host)->get_cover_state(host->dev, host->slot_id);
802 omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
805 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
806 struct omap_hsmmc_host *host = mmc_priv(mmc);
808 return sprintf(buf, "%s\n",
809 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
812 static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
815 omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
818 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
819 struct omap_hsmmc_host *host = mmc_priv(mmc);
821 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
824 static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
827 * Configure the response type and send the cmd.
830 omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
831 struct mmc_data *data)
833 int cmdreg = 0, resptype = 0, cmdtype = 0;
835 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
836 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
839 omap_hsmmc_enable_irq(host, cmd);
841 host->response_busy = 0;
842 if (cmd->flags & MMC_RSP_PRESENT) {
843 if (cmd->flags & MMC_RSP_136)
845 else if (cmd->flags & MMC_RSP_BUSY) {
847 host->response_busy = 1;
853 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
854 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
855 * a val of 0x3, rest 0x0.
857 if (cmd == host->mrq->stop)
860 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
862 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
864 cmdreg |= ACEN_ACMD23;
865 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
868 cmdreg |= DP_SELECT | MSBS | BCE;
869 if (data->flags & MMC_DATA_READ)
878 host->req_in_progress = 1;
880 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
881 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
885 omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
887 if (data->flags & MMC_DATA_WRITE)
888 return DMA_TO_DEVICE;
890 return DMA_FROM_DEVICE;
893 static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
894 struct mmc_data *data)
896 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
899 static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
904 spin_lock_irqsave(&host->irq_lock, flags);
905 host->req_in_progress = 0;
906 dma_ch = host->dma_ch;
907 spin_unlock_irqrestore(&host->irq_lock, flags);
909 omap_hsmmc_disable_irq(host);
910 /* Do not complete the request if DMA is still in progress */
911 if (mrq->data && host->use_dma && dma_ch != -1)
914 mmc_request_done(host->mmc, mrq);
918 * Notify the transfer complete to MMC core
921 omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
924 struct mmc_request *mrq = host->mrq;
926 /* TC before CC from CMD6 - don't know why, but it happens */
927 if (host->cmd && host->cmd->opcode == 6 &&
928 host->response_busy) {
929 host->response_busy = 0;
933 omap_hsmmc_request_done(host, mrq);
940 data->bytes_xfered += data->blocks * (data->blksz);
942 data->bytes_xfered = 0;
944 if (data->stop && (data->error || !host->mrq->sbc))
945 omap_hsmmc_start_command(host, data->stop, NULL);
947 omap_hsmmc_request_done(host, data->mrq);
951 * Notify the core about command completion
954 omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
956 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
957 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
959 omap_hsmmc_start_dma_transfer(host);
960 omap_hsmmc_start_command(host, host->mrq->cmd,
967 if (cmd->flags & MMC_RSP_PRESENT) {
968 if (cmd->flags & MMC_RSP_136) {
969 /* response type 2 */
970 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
971 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
972 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
973 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
975 /* response types 1, 1b, 3, 4, 5, 6 */
976 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
979 if ((host->data == NULL && !host->response_busy) || cmd->error)
980 omap_hsmmc_request_done(host, host->mrq);
984 * DMA clean up for command errors
986 static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
991 host->data->error = errno;
993 spin_lock_irqsave(&host->irq_lock, flags);
994 dma_ch = host->dma_ch;
996 spin_unlock_irqrestore(&host->irq_lock, flags);
998 if (host->use_dma && dma_ch != -1) {
999 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1001 dmaengine_terminate_all(chan);
1002 dma_unmap_sg(chan->device->dev,
1003 host->data->sg, host->data->sg_len,
1004 omap_hsmmc_get_dma_dir(host, host->data));
1006 host->data->host_cookie = 0;
1012 * Readable error output
1014 #ifdef CONFIG_MMC_DEBUG
1015 static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
1017 /* --- means reserved bit without definition at documentation */
1018 static const char *omap_hsmmc_status_bits[] = {
1019 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1020 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1021 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1022 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
1028 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1031 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
1032 if (status & (1 << i)) {
1033 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
1037 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
1040 static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1044 #endif /* CONFIG_MMC_DEBUG */
1047 * MMC controller internal state machines reset
1049 * Used to reset command or data internal state machines, using respectively
1050 * SRC or SRD bit of SYSCTL register
1051 * Can be called from interrupt context
1053 static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1056 unsigned long i = 0;
1057 unsigned long limit = MMC_TIMEOUT_US;
1059 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1060 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1063 * OMAP4 ES2 and greater has an updated reset logic.
1064 * Monitor a 0->1 transition first
1066 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
1067 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
1073 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1077 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1078 dev_err(mmc_dev(host->mmc),
1079 "Timeout waiting on controller reset in %s\n",
1083 static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1084 int err, int end_cmd)
1087 omap_hsmmc_reset_controller_fsm(host, SRC);
1089 host->cmd->error = err;
1093 omap_hsmmc_reset_controller_fsm(host, SRD);
1094 omap_hsmmc_dma_cleanup(host, err);
1095 } else if (host->mrq && host->mrq->cmd)
1096 host->mrq->cmd->error = err;
1099 static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
1101 struct mmc_data *data;
1102 int end_cmd = 0, end_trans = 0;
1106 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1108 if (status & ERR_EN) {
1109 omap_hsmmc_dbg_report_irq(host, status);
1111 if (status & (CTO_EN | CCRC_EN))
1113 if (status & (CTO_EN | DTO_EN))
1114 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
1115 else if (status & (CCRC_EN | DCRC_EN))
1116 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1118 if (status & ACE_EN) {
1120 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1121 if (!(ac12 & ACNE) && host->mrq->sbc) {
1125 else if (ac12 & (ACCE | ACEB | ACIE))
1127 host->mrq->sbc->error = error;
1128 hsmmc_command_incomplete(host, error, end_cmd);
1130 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1132 if (host->data || host->response_busy) {
1133 end_trans = !end_cmd;
1134 host->response_busy = 0;
1138 OMAP_HSMMC_WRITE(host->base, STAT, status);
1139 if (end_cmd || ((status & CC_EN) && host->cmd))
1140 omap_hsmmc_cmd_done(host, host->cmd);
1141 if ((end_trans || (status & TC_EN)) && host->mrq)
1142 omap_hsmmc_xfer_done(host, data);
1146 * MMC controller IRQ handler
1148 static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1150 struct omap_hsmmc_host *host = dev_id;
1153 status = OMAP_HSMMC_READ(host->base, STAT);
1154 while (status & (INT_EN_MASK | CIRQ_EN)) {
1155 if (host->req_in_progress)
1156 omap_hsmmc_do_irq(host, status);
1158 if (status & CIRQ_EN)
1159 mmc_signal_sdio_irq(host->mmc);
1161 /* Flush posted write */
1162 status = OMAP_HSMMC_READ(host->base, STAT);
1168 static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1170 struct omap_hsmmc_host *host = dev_id;
1172 /* cirq is level triggered, disable to avoid infinite loop */
1173 spin_lock(&host->irq_lock);
1174 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1175 disable_irq_nosync(host->wake_irq);
1176 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1178 spin_unlock(&host->irq_lock);
1179 pm_request_resume(host->dev); /* no use counter */
1184 static void set_sd_bus_power(struct omap_hsmmc_host *host)
1188 OMAP_HSMMC_WRITE(host->base, HCTL,
1189 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1190 for (i = 0; i < loops_per_jiffy; i++) {
1191 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1198 * Switch MMC interface voltage ... only relevant for MMC1.
1200 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1201 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1202 * Some chips, like eMMC ones, use internal transceivers.
1204 static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
1209 /* Disable the clocks */
1210 pm_runtime_put_sync(host->dev);
1212 clk_disable_unprepare(host->dbclk);
1214 /* Turn the power off */
1215 ret = mmc_pdata(host)->set_power(host->dev, host->slot_id, 0, 0);
1217 /* Turn the power ON with given VDD 1.8 or 3.0v */
1219 ret = mmc_pdata(host)->set_power(host->dev, host->slot_id, 1,
1221 pm_runtime_get_sync(host->dev);
1223 clk_prepare_enable(host->dbclk);
1228 OMAP_HSMMC_WRITE(host->base, HCTL,
1229 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1230 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
1233 * If a MMC dual voltage card is detected, the set_ios fn calls
1234 * this fn with VDD bit set for 1.8V. Upon card removal from the
1235 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
1237 * Cope with a bit of slop in the range ... per data sheets:
1238 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1239 * but recommended values are 1.71V to 1.89V
1240 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1241 * but recommended values are 2.7V to 3.3V
1243 * Board setup code shouldn't permit anything very out-of-range.
1244 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1245 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
1247 if ((1 << vdd) <= MMC_VDD_23_24)
1252 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
1253 set_sd_bus_power(host);
1257 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1261 /* Protect the card while the cover is open */
1262 static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1264 if (!mmc_pdata(host)->get_cover_state)
1267 host->reqs_blocked = 0;
1268 if (mmc_pdata(host)->get_cover_state(host->dev, host->slot_id)) {
1269 if (host->protect_card) {
1270 dev_info(host->dev, "%s: cover is closed, "
1271 "card is now accessible\n",
1272 mmc_hostname(host->mmc));
1273 host->protect_card = 0;
1276 if (!host->protect_card) {
1277 dev_info(host->dev, "%s: cover is open, "
1278 "card is now inaccessible\n",
1279 mmc_hostname(host->mmc));
1280 host->protect_card = 1;
1286 * irq handler to notify the core about card insertion/removal
1288 static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
1290 struct omap_hsmmc_host *host = dev_id;
1291 struct omap_hsmmc_platform_data *pdata = host->pdata;
1294 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1296 if (pdata->card_detect)
1297 carddetect = pdata->card_detect(host->dev, host->slot_id);
1299 omap_hsmmc_protect_card(host);
1300 carddetect = -ENOSYS;
1304 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1306 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1310 static void omap_hsmmc_dma_callback(void *param)
1312 struct omap_hsmmc_host *host = param;
1313 struct dma_chan *chan;
1314 struct mmc_data *data;
1315 int req_in_progress;
1317 spin_lock_irq(&host->irq_lock);
1318 if (host->dma_ch < 0) {
1319 spin_unlock_irq(&host->irq_lock);
1323 data = host->mrq->data;
1324 chan = omap_hsmmc_get_dma_chan(host, data);
1325 if (!data->host_cookie)
1326 dma_unmap_sg(chan->device->dev,
1327 data->sg, data->sg_len,
1328 omap_hsmmc_get_dma_dir(host, data));
1330 req_in_progress = host->req_in_progress;
1332 spin_unlock_irq(&host->irq_lock);
1334 /* If DMA has finished after TC, complete the request */
1335 if (!req_in_progress) {
1336 struct mmc_request *mrq = host->mrq;
1339 mmc_request_done(host->mmc, mrq);
1343 static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1344 struct mmc_data *data,
1345 struct omap_hsmmc_next *next,
1346 struct dma_chan *chan)
1350 if (!next && data->host_cookie &&
1351 data->host_cookie != host->next_data.cookie) {
1352 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
1353 " host->next_data.cookie %d\n",
1354 __func__, data->host_cookie, host->next_data.cookie);
1355 data->host_cookie = 0;
1358 /* Check if next job is already prepared */
1359 if (next || data->host_cookie != host->next_data.cookie) {
1360 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
1361 omap_hsmmc_get_dma_dir(host, data));
1364 dma_len = host->next_data.dma_len;
1365 host->next_data.dma_len = 0;
1373 next->dma_len = dma_len;
1374 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1376 host->dma_len = dma_len;
1382 * Routine to configure and start DMA for the MMC card
1384 static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
1385 struct mmc_request *req)
1387 struct dma_slave_config cfg;
1388 struct dma_async_tx_descriptor *tx;
1390 struct mmc_data *data = req->data;
1391 struct dma_chan *chan;
1393 /* Sanity check: all the SG entries must be aligned by block size. */
1394 for (i = 0; i < data->sg_len; i++) {
1395 struct scatterlist *sgl;
1398 if (sgl->length % data->blksz)
1401 if ((data->blksz % 4) != 0)
1402 /* REVISIT: The MMC buffer increments only when MSB is written.
1403 * Return error for blksz which is non multiple of four.
1407 BUG_ON(host->dma_ch != -1);
1409 chan = omap_hsmmc_get_dma_chan(host, data);
1411 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1412 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1413 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1414 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1415 cfg.src_maxburst = data->blksz / 4;
1416 cfg.dst_maxburst = data->blksz / 4;
1418 ret = dmaengine_slave_config(chan, &cfg);
1422 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1426 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1427 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1428 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1430 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1431 /* FIXME: cleanup */
1435 tx->callback = omap_hsmmc_dma_callback;
1436 tx->callback_param = host;
1439 dmaengine_submit(tx);
1446 static void set_data_timeout(struct omap_hsmmc_host *host,
1447 unsigned int timeout_ns,
1448 unsigned int timeout_clks)
1450 unsigned int timeout, cycle_ns;
1451 uint32_t reg, clkd, dto = 0;
1453 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1454 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1458 cycle_ns = 1000000000 / (host->clk_rate / clkd);
1459 timeout = timeout_ns / cycle_ns;
1460 timeout += timeout_clks;
1462 while ((timeout & 0x80000000) == 0) {
1479 reg |= dto << DTO_SHIFT;
1480 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1483 static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1485 struct mmc_request *req = host->mrq;
1486 struct dma_chan *chan;
1490 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1491 | (req->data->blocks << 16));
1492 set_data_timeout(host, req->data->timeout_ns,
1493 req->data->timeout_clks);
1494 chan = omap_hsmmc_get_dma_chan(host, req->data);
1495 dma_async_issue_pending(chan);
1499 * Configure block length for MMC/SD cards and initiate the transfer.
1502 omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
1505 host->data = req->data;
1507 if (req->data == NULL) {
1508 OMAP_HSMMC_WRITE(host->base, BLK, 0);
1510 * Set an arbitrary 100ms data timeout for commands with
1513 if (req->cmd->flags & MMC_RSP_BUSY)
1514 set_data_timeout(host, 100000000U, 0);
1518 if (host->use_dma) {
1519 ret = omap_hsmmc_setup_dma_transfer(host, req);
1521 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
1528 static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1531 struct omap_hsmmc_host *host = mmc_priv(mmc);
1532 struct mmc_data *data = mrq->data;
1534 if (host->use_dma && data->host_cookie) {
1535 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
1537 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1538 omap_hsmmc_get_dma_dir(host, data));
1539 data->host_cookie = 0;
1543 static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1546 struct omap_hsmmc_host *host = mmc_priv(mmc);
1548 if (mrq->data->host_cookie) {
1549 mrq->data->host_cookie = 0;
1553 if (host->use_dma) {
1554 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
1556 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1557 &host->next_data, c))
1558 mrq->data->host_cookie = 0;
1563 * Request function. for read/write operation
1565 static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
1567 struct omap_hsmmc_host *host = mmc_priv(mmc);
1570 BUG_ON(host->req_in_progress);
1571 BUG_ON(host->dma_ch != -1);
1572 if (host->protect_card) {
1573 if (host->reqs_blocked < 3) {
1575 * Ensure the controller is left in a consistent
1576 * state by resetting the command and data state
1579 omap_hsmmc_reset_controller_fsm(host, SRD);
1580 omap_hsmmc_reset_controller_fsm(host, SRC);
1581 host->reqs_blocked += 1;
1583 req->cmd->error = -EBADF;
1585 req->data->error = -EBADF;
1586 req->cmd->retries = 0;
1587 mmc_request_done(mmc, req);
1589 } else if (host->reqs_blocked)
1590 host->reqs_blocked = 0;
1591 WARN_ON(host->mrq != NULL);
1593 host->clk_rate = clk_get_rate(host->fclk);
1594 err = omap_hsmmc_prepare_data(host, req);
1596 req->cmd->error = err;
1598 req->data->error = err;
1600 mmc_request_done(mmc, req);
1603 if (req->sbc && !(host->flags & AUTO_CMD23)) {
1604 omap_hsmmc_start_command(host, req->sbc, NULL);
1608 omap_hsmmc_start_dma_transfer(host);
1609 omap_hsmmc_start_command(host, req->cmd, req->data);
1612 /* Routine to configure clock values. Exposed API to core */
1613 static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1615 struct omap_hsmmc_host *host = mmc_priv(mmc);
1616 int do_send_init_stream = 0;
1618 pm_runtime_get_sync(host->dev);
1620 if (ios->power_mode != host->power_mode) {
1621 switch (ios->power_mode) {
1623 mmc_pdata(host)->set_power(host->dev, host->slot_id,
1627 mmc_pdata(host)->set_power(host->dev, host->slot_id,
1631 do_send_init_stream = 1;
1634 host->power_mode = ios->power_mode;
1637 /* FIXME: set registers based only on changes to ios */
1639 omap_hsmmc_set_bus_width(host);
1641 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1642 /* Only MMC1 can interface at 3V without some flavor
1643 * of external transceiver; but they all handle 1.8V.
1645 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1646 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1648 * The mmc_select_voltage fn of the core does
1649 * not seem to set the power_mode to
1650 * MMC_POWER_UP upon recalculating the voltage.
1653 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1654 dev_dbg(mmc_dev(host->mmc),
1655 "Switch operation failed\n");
1659 omap_hsmmc_set_clock(host);
1661 if (do_send_init_stream)
1662 send_init_stream(host);
1664 omap_hsmmc_set_bus_mode(host);
1666 pm_runtime_put_autosuspend(host->dev);
1669 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1671 struct omap_hsmmc_host *host = mmc_priv(mmc);
1673 if (!mmc_pdata(host)->card_detect)
1675 return mmc_pdata(host)->card_detect(host->dev, host->slot_id);
1678 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1680 struct omap_hsmmc_host *host = mmc_priv(mmc);
1682 if (!mmc_pdata(host)->get_ro)
1684 return mmc_pdata(host)->get_ro(host->dev, 0);
1687 static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1689 struct omap_hsmmc_host *host = mmc_priv(mmc);
1691 if (mmc_pdata(host)->init_card)
1692 mmc_pdata(host)->init_card(card);
1695 static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1697 struct omap_hsmmc_host *host = mmc_priv(mmc);
1699 unsigned long flags;
1701 spin_lock_irqsave(&host->irq_lock, flags);
1703 con = OMAP_HSMMC_READ(host->base, CON);
1704 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1706 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1707 irq_mask |= CIRQ_EN;
1708 con |= CTPL | CLKEXTFREE;
1710 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1711 irq_mask &= ~CIRQ_EN;
1712 con &= ~(CTPL | CLKEXTFREE);
1714 OMAP_HSMMC_WRITE(host->base, CON, con);
1715 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1718 * if enable, piggy back detection on current request
1719 * but always disable immediately
1721 if (!host->req_in_progress || !enable)
1722 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1724 /* flush posted write */
1725 OMAP_HSMMC_READ(host->base, IE);
1727 spin_unlock_irqrestore(&host->irq_lock, flags);
1730 static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1732 struct mmc_host *mmc = host->mmc;
1736 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1737 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1738 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1739 * with functional clock disabled.
1741 if (!host->dev->of_node || !host->wake_irq)
1744 /* Prevent auto-enabling of IRQ */
1745 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1746 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1747 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1748 mmc_hostname(mmc), host);
1750 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1755 * Some omaps don't have wake-up path from deeper idle states
1756 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1758 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
1759 struct pinctrl *p = devm_pinctrl_get(host->dev);
1764 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1765 dev_info(host->dev, "missing default pinctrl state\n");
1766 devm_pinctrl_put(p);
1771 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1772 dev_info(host->dev, "missing idle pinctrl state\n");
1773 devm_pinctrl_put(p);
1777 devm_pinctrl_put(p);
1780 OMAP_HSMMC_WRITE(host->base, HCTL,
1781 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
1785 devm_free_irq(host->dev, host->wake_irq, host);
1787 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1792 static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
1794 u32 hctl, capa, value;
1796 /* Only MMC1 supports 3.0V */
1797 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1805 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1806 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1808 value = OMAP_HSMMC_READ(host->base, CAPA);
1809 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1811 /* Set SD bus power bit */
1812 set_sd_bus_power(host);
1815 static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
1817 struct omap_hsmmc_host *host = mmc_priv(mmc);
1819 pm_runtime_get_sync(host->dev);
1824 static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
1826 struct omap_hsmmc_host *host = mmc_priv(mmc);
1828 pm_runtime_mark_last_busy(host->dev);
1829 pm_runtime_put_autosuspend(host->dev);
1834 static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1835 unsigned int direction, int blk_size)
1837 /* This controller can't do multiblock reads due to hw bugs */
1838 if (direction == MMC_DATA_READ)
1844 static struct mmc_host_ops omap_hsmmc_ops = {
1845 .enable = omap_hsmmc_enable_fclk,
1846 .disable = omap_hsmmc_disable_fclk,
1847 .post_req = omap_hsmmc_post_req,
1848 .pre_req = omap_hsmmc_pre_req,
1849 .request = omap_hsmmc_request,
1850 .set_ios = omap_hsmmc_set_ios,
1851 .get_cd = omap_hsmmc_get_cd,
1852 .get_ro = omap_hsmmc_get_ro,
1853 .init_card = omap_hsmmc_init_card,
1854 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
1857 #ifdef CONFIG_DEBUG_FS
1859 static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
1861 struct mmc_host *mmc = s->private;
1862 struct omap_hsmmc_host *host = mmc_priv(mmc);
1864 seq_printf(s, "mmc%d:\n", mmc->index);
1865 seq_printf(s, "sdio irq mode\t%s\n",
1866 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1868 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1869 seq_printf(s, "sdio irq \t%s\n",
1870 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1873 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
1875 pm_runtime_get_sync(host->dev);
1876 seq_puts(s, "\nregs:\n");
1877 seq_printf(s, "CON:\t\t0x%08x\n",
1878 OMAP_HSMMC_READ(host->base, CON));
1879 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1880 OMAP_HSMMC_READ(host->base, PSTATE));
1881 seq_printf(s, "HCTL:\t\t0x%08x\n",
1882 OMAP_HSMMC_READ(host->base, HCTL));
1883 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1884 OMAP_HSMMC_READ(host->base, SYSCTL));
1885 seq_printf(s, "IE:\t\t0x%08x\n",
1886 OMAP_HSMMC_READ(host->base, IE));
1887 seq_printf(s, "ISE:\t\t0x%08x\n",
1888 OMAP_HSMMC_READ(host->base, ISE));
1889 seq_printf(s, "CAPA:\t\t0x%08x\n",
1890 OMAP_HSMMC_READ(host->base, CAPA));
1892 pm_runtime_mark_last_busy(host->dev);
1893 pm_runtime_put_autosuspend(host->dev);
1898 static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
1900 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
1903 static const struct file_operations mmc_regs_fops = {
1904 .open = omap_hsmmc_regs_open,
1906 .llseek = seq_lseek,
1907 .release = single_release,
1910 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1912 if (mmc->debugfs_root)
1913 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1914 mmc, &mmc_regs_fops);
1919 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1926 static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1927 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1928 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1931 static const struct omap_mmc_of_data omap4_mmc_of_data = {
1932 .reg_offset = 0x100,
1934 static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1935 .reg_offset = 0x100,
1936 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1939 static const struct of_device_id omap_mmc_of_match[] = {
1941 .compatible = "ti,omap2-hsmmc",
1944 .compatible = "ti,omap3-pre-es3-hsmmc",
1945 .data = &omap3_pre_es3_mmc_of_data,
1948 .compatible = "ti,omap3-hsmmc",
1951 .compatible = "ti,omap4-hsmmc",
1952 .data = &omap4_mmc_of_data,
1955 .compatible = "ti,am33xx-hsmmc",
1956 .data = &am33xx_mmc_of_data,
1960 MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1962 static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1964 struct omap_hsmmc_platform_data *pdata;
1965 struct device_node *np = dev->of_node;
1966 u32 bus_width, max_freq;
1967 int cd_gpio, wp_gpio;
1969 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1970 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1971 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1972 return ERR_PTR(-EPROBE_DEFER);
1974 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1976 return ERR_PTR(-ENOMEM); /* out of memory */
1978 if (of_find_property(np, "ti,dual-volt", NULL))
1979 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1981 /* This driver only supports 1 slot */
1982 pdata->nr_slots = 1;
1983 pdata->switch_pin = cd_gpio;
1984 pdata->gpio_wp = wp_gpio;
1986 if (of_find_property(np, "ti,non-removable", NULL)) {
1987 pdata->nonremovable = true;
1988 pdata->no_regulator_off_init = true;
1990 of_property_read_u32(np, "bus-width", &bus_width);
1992 pdata->caps |= MMC_CAP_4_BIT_DATA;
1993 else if (bus_width == 8)
1994 pdata->caps |= MMC_CAP_8_BIT_DATA;
1996 if (of_find_property(np, "ti,needs-special-reset", NULL))
1997 pdata->features |= HSMMC_HAS_UPDATED_RESET;
1999 if (!of_property_read_u32(np, "max-frequency", &max_freq))
2000 pdata->max_freq = max_freq;
2002 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
2003 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
2005 if (of_find_property(np, "keep-power-in-suspend", NULL))
2006 pdata->pm_caps |= MMC_PM_KEEP_POWER;
2008 if (of_find_property(np, "enable-sdio-wakeup", NULL))
2009 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
2014 static inline struct omap_hsmmc_platform_data
2015 *of_get_hsmmc_pdata(struct device *dev)
2017 return ERR_PTR(-EINVAL);
2021 static int omap_hsmmc_probe(struct platform_device *pdev)
2023 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
2024 struct mmc_host *mmc;
2025 struct omap_hsmmc_host *host = NULL;
2026 struct resource *res;
2028 const struct of_device_id *match;
2029 dma_cap_mask_t mask;
2030 unsigned tx_req, rx_req;
2031 const struct omap_mmc_of_data *data;
2034 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2036 pdata = of_get_hsmmc_pdata(&pdev->dev);
2039 return PTR_ERR(pdata);
2043 pdata->reg_offset = data->reg_offset;
2044 pdata->controller_flags |= data->controller_flags;
2048 if (pdata == NULL) {
2049 dev_err(&pdev->dev, "Platform Data is missing\n");
2053 if (pdata->nr_slots == 0) {
2054 dev_err(&pdev->dev, "No Slots\n");
2058 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2059 irq = platform_get_irq(pdev, 0);
2060 if (res == NULL || irq < 0)
2063 base = devm_ioremap_resource(&pdev->dev, res);
2065 return PTR_ERR(base);
2067 ret = omap_hsmmc_gpio_init(pdata);
2071 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
2077 host = mmc_priv(mmc);
2079 host->pdata = pdata;
2080 host->dev = &pdev->dev;
2085 host->mapbase = res->start + pdata->reg_offset;
2086 host->base = base + pdata->reg_offset;
2087 host->power_mode = MMC_POWER_OFF;
2088 host->next_data.cookie = 1;
2089 host->pbias_enabled = 0;
2091 platform_set_drvdata(pdev, host);
2093 if (pdev->dev.of_node)
2094 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2096 mmc->ops = &omap_hsmmc_ops;
2098 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2100 if (pdata->max_freq > 0)
2101 mmc->f_max = pdata->max_freq;
2103 mmc->f_max = OMAP_MMC_MAX_CLOCK;
2105 spin_lock_init(&host->irq_lock);
2107 host->fclk = devm_clk_get(&pdev->dev, "fck");
2108 if (IS_ERR(host->fclk)) {
2109 ret = PTR_ERR(host->fclk);
2114 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2115 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
2116 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
2119 pm_runtime_enable(host->dev);
2120 pm_runtime_get_sync(host->dev);
2121 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2122 pm_runtime_use_autosuspend(host->dev);
2124 omap_hsmmc_context_save(host);
2126 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
2128 * MMC can still work without debounce clock.
2130 if (IS_ERR(host->dbclk)) {
2132 } else if (clk_prepare_enable(host->dbclk) != 0) {
2133 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
2137 /* Since we do only SG emulation, we can have as many segs
2139 mmc->max_segs = 1024;
2141 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2142 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2143 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2144 mmc->max_seg_size = mmc->max_req_size;
2146 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
2147 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
2149 mmc->caps |= mmc_pdata(host)->caps;
2150 if (mmc->caps & MMC_CAP_8_BIT_DATA)
2151 mmc->caps |= MMC_CAP_4_BIT_DATA;
2153 if (mmc_pdata(host)->nonremovable)
2154 mmc->caps |= MMC_CAP_NONREMOVABLE;
2156 mmc->pm_caps = mmc_pdata(host)->pm_caps;
2158 omap_hsmmc_conf_bus_power(host);
2160 if (!pdev->dev.of_node) {
2161 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2163 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2167 tx_req = res->start;
2169 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2171 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2175 rx_req = res->start;
2179 dma_cap_set(DMA_SLAVE, mask);
2182 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2183 &rx_req, &pdev->dev, "rx");
2185 if (!host->rx_chan) {
2186 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
2192 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2193 &tx_req, &pdev->dev, "tx");
2195 if (!host->tx_chan) {
2196 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
2201 /* Request IRQ for MMC operations */
2202 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
2203 mmc_hostname(mmc), host);
2205 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
2209 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
2210 ret = omap_hsmmc_reg_get(host);
2216 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
2218 /* Request IRQ for card detect */
2219 if ((mmc_pdata(host)->card_detect_irq)) {
2220 ret = devm_request_threaded_irq(&pdev->dev,
2221 mmc_pdata(host)->card_detect_irq,
2222 NULL, omap_hsmmc_detect,
2223 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
2224 mmc_hostname(mmc), host);
2226 dev_err(mmc_dev(host->mmc),
2227 "Unable to grab MMC CD IRQ\n");
2230 pdata->suspend = omap_hsmmc_suspend_cdirq;
2231 pdata->resume = omap_hsmmc_resume_cdirq;
2234 omap_hsmmc_disable_irq(host);
2237 * For now, only support SDIO interrupt if we have a separate
2238 * wake-up interrupt configured from device tree. This is because
2239 * the wake-up interrupt is needed for idle state and some
2240 * platforms need special quirks. And we don't want to add new
2241 * legacy mux platform init code callbacks any longer as we
2242 * are moving to DT based booting anyways.
2244 ret = omap_hsmmc_configure_wake_irq(host);
2246 mmc->caps |= MMC_CAP_SDIO_IRQ;
2248 omap_hsmmc_protect_card(host);
2252 if (mmc_pdata(host)->name != NULL) {
2253 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2257 if (mmc_pdata(host)->card_detect_irq &&
2258 mmc_pdata(host)->get_cover_state) {
2259 ret = device_create_file(&mmc->class_dev,
2260 &dev_attr_cover_switch);
2265 omap_hsmmc_debugfs(mmc);
2266 pm_runtime_mark_last_busy(host->dev);
2267 pm_runtime_put_autosuspend(host->dev);
2272 mmc_remove_host(mmc);
2275 omap_hsmmc_reg_put(host);
2278 dma_release_channel(host->tx_chan);
2280 dma_release_channel(host->rx_chan);
2281 pm_runtime_put_sync(host->dev);
2282 pm_runtime_disable(host->dev);
2284 clk_disable_unprepare(host->dbclk);
2288 omap_hsmmc_gpio_free(pdata);
2293 static int omap_hsmmc_remove(struct platform_device *pdev)
2295 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2297 pm_runtime_get_sync(host->dev);
2298 mmc_remove_host(host->mmc);
2300 omap_hsmmc_reg_put(host);
2303 dma_release_channel(host->tx_chan);
2305 dma_release_channel(host->rx_chan);
2307 pm_runtime_put_sync(host->dev);
2308 pm_runtime_disable(host->dev);
2310 clk_disable_unprepare(host->dbclk);
2312 omap_hsmmc_gpio_free(host->pdata);
2313 mmc_free_host(host->mmc);
2319 static int omap_hsmmc_prepare(struct device *dev)
2321 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2323 if (host->pdata->suspend)
2324 return host->pdata->suspend(dev, host->slot_id);
2329 static void omap_hsmmc_complete(struct device *dev)
2331 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2333 if (host->pdata->resume)
2334 host->pdata->resume(dev, host->slot_id);
2338 static int omap_hsmmc_suspend(struct device *dev)
2340 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2345 pm_runtime_get_sync(host->dev);
2347 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2348 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2349 OMAP_HSMMC_WRITE(host->base, IE, 0);
2350 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2351 OMAP_HSMMC_WRITE(host->base, HCTL,
2352 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2355 /* do not wake up due to sdio irq */
2356 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2357 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2358 disable_irq(host->wake_irq);
2361 clk_disable_unprepare(host->dbclk);
2363 pm_runtime_put_sync(host->dev);
2367 /* Routine to resume the MMC device */
2368 static int omap_hsmmc_resume(struct device *dev)
2370 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2375 pm_runtime_get_sync(host->dev);
2378 clk_prepare_enable(host->dbclk);
2380 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2381 omap_hsmmc_conf_bus_power(host);
2383 omap_hsmmc_protect_card(host);
2385 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2386 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2387 enable_irq(host->wake_irq);
2389 pm_runtime_mark_last_busy(host->dev);
2390 pm_runtime_put_autosuspend(host->dev);
2395 #define omap_hsmmc_prepare NULL
2396 #define omap_hsmmc_complete NULL
2397 #define omap_hsmmc_suspend NULL
2398 #define omap_hsmmc_resume NULL
2401 static int omap_hsmmc_runtime_suspend(struct device *dev)
2403 struct omap_hsmmc_host *host;
2404 unsigned long flags;
2407 host = platform_get_drvdata(to_platform_device(dev));
2408 omap_hsmmc_context_save(host);
2409 dev_dbg(dev, "disabled\n");
2411 spin_lock_irqsave(&host->irq_lock, flags);
2412 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2413 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2414 /* disable sdio irq handling to prevent race */
2415 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2416 OMAP_HSMMC_WRITE(host->base, IE, 0);
2418 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2420 * dat1 line low, pending sdio irq
2421 * race condition: possible irq handler running on
2424 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2425 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2426 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2427 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2428 pm_runtime_mark_last_busy(dev);
2433 pinctrl_pm_select_idle_state(dev);
2435 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2436 enable_irq(host->wake_irq);
2437 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
2439 pinctrl_pm_select_idle_state(dev);
2443 spin_unlock_irqrestore(&host->irq_lock, flags);
2447 static int omap_hsmmc_runtime_resume(struct device *dev)
2449 struct omap_hsmmc_host *host;
2450 unsigned long flags;
2452 host = platform_get_drvdata(to_platform_device(dev));
2453 omap_hsmmc_context_restore(host);
2454 dev_dbg(dev, "enabled\n");
2456 spin_lock_irqsave(&host->irq_lock, flags);
2457 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2458 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2459 /* sdio irq flag can't change while in runtime suspend */
2460 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2461 disable_irq_nosync(host->wake_irq);
2462 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2465 pinctrl_pm_select_default_state(host->dev);
2467 /* irq lost, if pinmux incorrect */
2468 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2469 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2470 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2472 pinctrl_pm_select_default_state(host->dev);
2474 spin_unlock_irqrestore(&host->irq_lock, flags);
2478 static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
2479 .suspend = omap_hsmmc_suspend,
2480 .resume = omap_hsmmc_resume,
2481 .prepare = omap_hsmmc_prepare,
2482 .complete = omap_hsmmc_complete,
2483 .runtime_suspend = omap_hsmmc_runtime_suspend,
2484 .runtime_resume = omap_hsmmc_runtime_resume,
2487 static struct platform_driver omap_hsmmc_driver = {
2488 .probe = omap_hsmmc_probe,
2489 .remove = omap_hsmmc_remove,
2491 .name = DRIVER_NAME,
2492 .pm = &omap_hsmmc_dev_pm_ops,
2493 .of_match_table = of_match_ptr(omap_mmc_of_match),
2497 module_platform_driver(omap_hsmmc_driver);
2498 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2499 MODULE_LICENSE("GPL");
2500 MODULE_ALIAS("platform:" DRIVER_NAME);
2501 MODULE_AUTHOR("Texas Instruments Inc");