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/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/platform_device.h>
28 #include <linux/timer.h>
29 #include <linux/clk.h>
31 #include <linux/of_gpio.h>
32 #include <linux/of_device.h>
33 #include <linux/omap-dma.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/core.h>
36 #include <linux/mmc/mmc.h>
38 #include <linux/gpio.h>
39 #include <linux/regulator/consumer.h>
40 #include <linux/pinctrl/consumer.h>
41 #include <linux/pm_runtime.h>
42 #include <mach/hardware.h>
46 /* OMAP HSMMC Host Controller Registers */
47 #define OMAP_HSMMC_SYSSTATUS 0x0014
48 #define OMAP_HSMMC_CON 0x002C
49 #define OMAP_HSMMC_BLK 0x0104
50 #define OMAP_HSMMC_ARG 0x0108
51 #define OMAP_HSMMC_CMD 0x010C
52 #define OMAP_HSMMC_RSP10 0x0110
53 #define OMAP_HSMMC_RSP32 0x0114
54 #define OMAP_HSMMC_RSP54 0x0118
55 #define OMAP_HSMMC_RSP76 0x011C
56 #define OMAP_HSMMC_DATA 0x0120
57 #define OMAP_HSMMC_HCTL 0x0128
58 #define OMAP_HSMMC_SYSCTL 0x012C
59 #define OMAP_HSMMC_STAT 0x0130
60 #define OMAP_HSMMC_IE 0x0134
61 #define OMAP_HSMMC_ISE 0x0138
62 #define OMAP_HSMMC_CAPA 0x0140
64 #define VS18 (1 << 26)
65 #define VS30 (1 << 25)
67 #define SDVS18 (0x5 << 9)
68 #define SDVS30 (0x6 << 9)
69 #define SDVS33 (0x7 << 9)
70 #define SDVS_MASK 0x00000E00
71 #define SDVSCLR 0xFFFFF1FF
72 #define SDVSDET 0x00000400
79 #define CLKD_MASK 0x0000FFC0
81 #define DTO_MASK 0x000F0000
83 #define INIT_STREAM (1 << 1)
84 #define DP_SELECT (1 << 21)
89 #define FOUR_BIT (1 << 1)
94 #define STAT_CLEAR 0xFFFFFFFF
95 #define INIT_STREAM_CMD 0x00000000
96 #define DUAL_VOLT_OCR_BIT 7
99 #define SOFTRESET (1 << 1)
100 #define RESETDONE (1 << 0)
102 /* Interrupt masks for IE and ISE register */
103 #define CC_EN (1 << 0)
104 #define TC_EN (1 << 1)
105 #define BWR_EN (1 << 4)
106 #define BRR_EN (1 << 5)
107 #define ERR_EN (1 << 15)
108 #define CTO_EN (1 << 16)
109 #define CCRC_EN (1 << 17)
110 #define CEB_EN (1 << 18)
111 #define CIE_EN (1 << 19)
112 #define DTO_EN (1 << 20)
113 #define DCRC_EN (1 << 21)
114 #define DEB_EN (1 << 22)
115 #define CERR_EN (1 << 28)
116 #define BADA_EN (1 << 29)
118 #define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
119 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
120 BRR_EN | BWR_EN | TC_EN | CC_EN)
122 #define MMC_AUTOSUSPEND_DELAY 100
123 #define MMC_TIMEOUT_MS 20
124 #define OMAP_MMC_MIN_CLOCK 400000
125 #define OMAP_MMC_MAX_CLOCK 52000000
126 #define DRIVER_NAME "omap_hsmmc"
129 * One controller can have multiple slots, like on some omap boards using
130 * omap.c controller driver. Luckily this is not currently done on any known
131 * omap_hsmmc.c device.
133 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
136 * MMC Host controller read/write API's
138 #define OMAP_HSMMC_READ(base, reg) \
139 __raw_readl((base) + OMAP_HSMMC_##reg)
141 #define OMAP_HSMMC_WRITE(base, reg, val) \
142 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
144 struct omap_hsmmc_next {
145 unsigned int dma_len;
149 struct omap_hsmmc_host {
151 struct mmc_host *mmc;
152 struct mmc_request *mrq;
153 struct mmc_command *cmd;
154 struct mmc_data *data;
158 * vcc == configured supply
159 * vcc_aux == optional
160 * - MMC1, supply for DAT4..DAT7
161 * - MMC2/MMC2, external level shifter voltage supply, for
162 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
164 struct regulator *vcc;
165 struct regulator *vcc_aux;
167 resource_size_t mapbase;
168 spinlock_t irq_lock; /* Prevent races with irq handler */
169 unsigned int dma_len;
170 unsigned int dma_sg_idx;
171 unsigned char bus_mode;
172 unsigned char power_mode;
176 struct dma_chan *tx_chan;
177 struct dma_chan *rx_chan;
185 struct omap_hsmmc_next next_data;
187 struct omap_mmc_platform_data *pdata;
190 static int omap_hsmmc_card_detect(struct device *dev, int slot)
192 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
193 struct omap_mmc_platform_data *mmc = host->pdata;
195 /* NOTE: assumes card detect signal is active-low */
196 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
199 static int omap_hsmmc_get_wp(struct device *dev, int slot)
201 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
202 struct omap_mmc_platform_data *mmc = host->pdata;
204 /* NOTE: assumes write protect signal is active-high */
205 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
208 static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
210 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
211 struct omap_mmc_platform_data *mmc = host->pdata;
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_hsmmc_host *host = dev_get_drvdata(dev);
222 struct omap_mmc_platform_data *mmc = host->pdata;
224 disable_irq(mmc->slots[0].card_detect_irq);
228 static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
230 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
231 struct omap_mmc_platform_data *mmc = host->pdata;
233 enable_irq(mmc->slots[0].card_detect_irq);
239 #define omap_hsmmc_suspend_cdirq NULL
240 #define omap_hsmmc_resume_cdirq NULL
244 #ifdef CONFIG_REGULATOR
246 static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
249 struct omap_hsmmc_host *host =
250 platform_get_drvdata(to_platform_device(dev));
254 * If we don't see a Vcc regulator, assume it's a fixed
255 * voltage always-on regulator.
260 * With DT, never turn OFF the regulator. This is because
261 * the pbias cell programming support is still missing when
262 * booting with Device tree
264 if (dev->of_node && !vdd)
267 if (mmc_slot(host).before_set_reg)
268 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
271 * Assume Vcc regulator is used only to power the card ... OMAP
272 * VDDS is used to power the pins, optionally with a transceiver to
273 * support cards using voltages other than VDDS (1.8V nominal). When a
274 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
276 * In some cases this regulator won't support enable/disable;
277 * e.g. it's a fixed rail for a WLAN chip.
279 * In other cases vcc_aux switches interface power. Example, for
280 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
281 * chips/cards need an interface voltage rail too.
284 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
285 /* Enable interface voltage rail, if needed */
286 if (ret == 0 && host->vcc_aux) {
287 ret = regulator_enable(host->vcc_aux);
289 ret = mmc_regulator_set_ocr(host->mmc,
293 /* Shut down the rail */
295 ret = regulator_disable(host->vcc_aux);
297 /* Then proceed to shut down the local regulator */
298 ret = mmc_regulator_set_ocr(host->mmc,
303 if (mmc_slot(host).after_set_reg)
304 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
309 static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
311 struct regulator *reg;
314 reg = regulator_get(host->dev, "vmmc");
316 dev_err(host->dev, "vmmc regulator missing\n");
319 mmc_slot(host).set_power = omap_hsmmc_set_power;
321 ocr_value = mmc_regulator_get_ocrmask(reg);
322 if (!mmc_slot(host).ocr_mask) {
323 mmc_slot(host).ocr_mask = ocr_value;
325 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
326 dev_err(host->dev, "ocrmask %x is not supported\n",
327 mmc_slot(host).ocr_mask);
328 mmc_slot(host).ocr_mask = 0;
333 /* Allow an aux regulator */
334 reg = regulator_get(host->dev, "vmmc_aux");
335 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
337 /* For eMMC do not power off when not in sleep state */
338 if (mmc_slot(host).no_regulator_off_init)
341 * UGLY HACK: workaround regulator framework bugs.
342 * When the bootloader leaves a supply active, it's
343 * initialized with zero usecount ... and we can't
344 * disable it without first enabling it. Until the
345 * framework is fixed, we need a workaround like this
346 * (which is safe for MMC, but not in general).
348 if (regulator_is_enabled(host->vcc) > 0 ||
349 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
350 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
352 mmc_slot(host).set_power(host->dev, host->slot_id,
354 mmc_slot(host).set_power(host->dev, host->slot_id,
362 static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
364 regulator_put(host->vcc);
365 regulator_put(host->vcc_aux);
366 mmc_slot(host).set_power = NULL;
369 static inline int omap_hsmmc_have_reg(void)
376 static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
381 static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
385 static inline int omap_hsmmc_have_reg(void)
392 static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
396 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
397 if (pdata->slots[0].cover)
398 pdata->slots[0].get_cover_state =
399 omap_hsmmc_get_cover_state;
401 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
402 pdata->slots[0].card_detect_irq =
403 gpio_to_irq(pdata->slots[0].switch_pin);
404 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
407 ret = gpio_direction_input(pdata->slots[0].switch_pin);
411 pdata->slots[0].switch_pin = -EINVAL;
413 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
414 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
415 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
418 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
422 pdata->slots[0].gpio_wp = -EINVAL;
427 gpio_free(pdata->slots[0].gpio_wp);
429 if (gpio_is_valid(pdata->slots[0].switch_pin))
431 gpio_free(pdata->slots[0].switch_pin);
435 static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
437 if (gpio_is_valid(pdata->slots[0].gpio_wp))
438 gpio_free(pdata->slots[0].gpio_wp);
439 if (gpio_is_valid(pdata->slots[0].switch_pin))
440 gpio_free(pdata->slots[0].switch_pin);
444 * Start clock to the card
446 static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
448 OMAP_HSMMC_WRITE(host->base, SYSCTL,
449 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
453 * Stop clock to the card
455 static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
457 OMAP_HSMMC_WRITE(host->base, SYSCTL,
458 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
459 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
460 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
463 static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
464 struct mmc_command *cmd)
466 unsigned int irq_mask;
469 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
471 irq_mask = INT_EN_MASK;
473 /* Disable timeout for erases */
474 if (cmd->opcode == MMC_ERASE)
477 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
478 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
479 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
482 static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
484 OMAP_HSMMC_WRITE(host->base, ISE, 0);
485 OMAP_HSMMC_WRITE(host->base, IE, 0);
486 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
489 /* Calculate divisor for the given clock frequency */
490 static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
495 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
503 static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
505 struct mmc_ios *ios = &host->mmc->ios;
506 unsigned long regval;
507 unsigned long timeout;
508 unsigned long clkdiv;
510 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
512 omap_hsmmc_stop_clock(host);
514 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
515 regval = regval & ~(CLKD_MASK | DTO_MASK);
516 clkdiv = calc_divisor(host, ios);
517 regval = regval | (clkdiv << 6) | (DTO << 16);
518 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
519 OMAP_HSMMC_WRITE(host->base, SYSCTL,
520 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
522 /* Wait till the ICS bit is set */
523 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
524 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
525 && time_before(jiffies, timeout))
529 * Enable High-Speed Support
531 * - Controller should support High-Speed-Enable Bit
532 * - Controller should not be using DDR Mode
533 * - Controller should advertise that it supports High Speed
534 * in capabilities register
535 * - MMC/SD clock coming out of controller > 25MHz
537 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
538 (ios->timing != MMC_TIMING_UHS_DDR50) &&
539 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
540 regval = OMAP_HSMMC_READ(host->base, HCTL);
541 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
546 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
549 omap_hsmmc_start_clock(host);
552 static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
554 struct mmc_ios *ios = &host->mmc->ios;
557 con = OMAP_HSMMC_READ(host->base, CON);
558 if (ios->timing == MMC_TIMING_UHS_DDR50)
559 con |= DDR; /* configure in DDR mode */
562 switch (ios->bus_width) {
563 case MMC_BUS_WIDTH_8:
564 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
566 case MMC_BUS_WIDTH_4:
567 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
568 OMAP_HSMMC_WRITE(host->base, HCTL,
569 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
571 case MMC_BUS_WIDTH_1:
572 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
573 OMAP_HSMMC_WRITE(host->base, HCTL,
574 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
579 static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
581 struct mmc_ios *ios = &host->mmc->ios;
584 con = OMAP_HSMMC_READ(host->base, CON);
585 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
586 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
588 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
594 * Restore the MMC host context, if it was lost as result of a
595 * power state change.
597 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
599 struct mmc_ios *ios = &host->mmc->ios;
600 struct omap_mmc_platform_data *pdata = host->pdata;
601 int context_loss = 0;
603 unsigned long timeout;
605 if (pdata->get_context_loss_count) {
606 context_loss = pdata->get_context_loss_count(host->dev);
607 if (context_loss < 0)
611 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
612 context_loss == host->context_loss ? "not " : "");
613 if (host->context_loss == context_loss)
616 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
619 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
620 if (host->power_mode != MMC_POWER_OFF &&
621 (1 << ios->vdd) <= MMC_VDD_23_24)
631 OMAP_HSMMC_WRITE(host->base, HCTL,
632 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
634 OMAP_HSMMC_WRITE(host->base, CAPA,
635 OMAP_HSMMC_READ(host->base, CAPA) | capa);
637 OMAP_HSMMC_WRITE(host->base, HCTL,
638 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
640 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
641 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
642 && time_before(jiffies, timeout))
645 omap_hsmmc_disable_irq(host);
647 /* Do not initialize card-specific things if the power is off */
648 if (host->power_mode == MMC_POWER_OFF)
651 omap_hsmmc_set_bus_width(host);
653 omap_hsmmc_set_clock(host);
655 omap_hsmmc_set_bus_mode(host);
658 host->context_loss = context_loss;
660 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
665 * Save the MMC host context (store the number of power state changes so far).
667 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
669 struct omap_mmc_platform_data *pdata = host->pdata;
672 if (pdata->get_context_loss_count) {
673 context_loss = pdata->get_context_loss_count(host->dev);
674 if (context_loss < 0)
676 host->context_loss = context_loss;
682 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
687 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
694 * Send init stream sequence to card
695 * before sending IDLE command
697 static void send_init_stream(struct omap_hsmmc_host *host)
700 unsigned long timeout;
702 if (host->protect_card)
705 disable_irq(host->irq);
707 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
708 OMAP_HSMMC_WRITE(host->base, CON,
709 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
710 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
712 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
713 while ((reg != CC_EN) && time_before(jiffies, timeout))
714 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
716 OMAP_HSMMC_WRITE(host->base, CON,
717 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
719 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
720 OMAP_HSMMC_READ(host->base, STAT);
722 enable_irq(host->irq);
726 int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
730 if (mmc_slot(host).get_cover_state)
731 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
736 omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
739 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
740 struct omap_hsmmc_host *host = mmc_priv(mmc);
742 return sprintf(buf, "%s\n",
743 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
746 static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
749 omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
752 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
753 struct omap_hsmmc_host *host = mmc_priv(mmc);
755 return sprintf(buf, "%s\n", mmc_slot(host).name);
758 static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
761 * Configure the response type and send the cmd.
764 omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
765 struct mmc_data *data)
767 int cmdreg = 0, resptype = 0, cmdtype = 0;
769 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
770 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
773 omap_hsmmc_enable_irq(host, cmd);
775 host->response_busy = 0;
776 if (cmd->flags & MMC_RSP_PRESENT) {
777 if (cmd->flags & MMC_RSP_136)
779 else if (cmd->flags & MMC_RSP_BUSY) {
781 host->response_busy = 1;
787 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
788 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
789 * a val of 0x3, rest 0x0.
791 if (cmd == host->mrq->stop)
794 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
797 cmdreg |= DP_SELECT | MSBS | BCE;
798 if (data->flags & MMC_DATA_READ)
807 host->req_in_progress = 1;
809 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
810 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
814 omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
816 if (data->flags & MMC_DATA_WRITE)
817 return DMA_TO_DEVICE;
819 return DMA_FROM_DEVICE;
822 static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
823 struct mmc_data *data)
825 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
828 static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
833 spin_lock_irqsave(&host->irq_lock, flags);
834 host->req_in_progress = 0;
835 dma_ch = host->dma_ch;
836 spin_unlock_irqrestore(&host->irq_lock, flags);
838 omap_hsmmc_disable_irq(host);
839 /* Do not complete the request if DMA is still in progress */
840 if (mrq->data && host->use_dma && dma_ch != -1)
843 mmc_request_done(host->mmc, mrq);
847 * Notify the transfer complete to MMC core
850 omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
853 struct mmc_request *mrq = host->mrq;
855 /* TC before CC from CMD6 - don't know why, but it happens */
856 if (host->cmd && host->cmd->opcode == 6 &&
857 host->response_busy) {
858 host->response_busy = 0;
862 omap_hsmmc_request_done(host, mrq);
869 data->bytes_xfered += data->blocks * (data->blksz);
871 data->bytes_xfered = 0;
874 omap_hsmmc_request_done(host, data->mrq);
877 omap_hsmmc_start_command(host, data->stop, NULL);
881 * Notify the core about command completion
884 omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
888 if (cmd->flags & MMC_RSP_PRESENT) {
889 if (cmd->flags & MMC_RSP_136) {
890 /* response type 2 */
891 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
892 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
893 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
894 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
896 /* response types 1, 1b, 3, 4, 5, 6 */
897 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
900 if ((host->data == NULL && !host->response_busy) || cmd->error)
901 omap_hsmmc_request_done(host, cmd->mrq);
905 * DMA clean up for command errors
907 static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
912 host->data->error = errno;
914 spin_lock_irqsave(&host->irq_lock, flags);
915 dma_ch = host->dma_ch;
917 spin_unlock_irqrestore(&host->irq_lock, flags);
919 if (host->use_dma && dma_ch != -1) {
920 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
922 dmaengine_terminate_all(chan);
923 dma_unmap_sg(chan->device->dev,
924 host->data->sg, host->data->sg_len,
925 omap_hsmmc_get_dma_dir(host, host->data));
927 host->data->host_cookie = 0;
933 * Readable error output
935 #ifdef CONFIG_MMC_DEBUG
936 static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
938 /* --- means reserved bit without definition at documentation */
939 static const char *omap_hsmmc_status_bits[] = {
940 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
941 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
942 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
943 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
949 len = sprintf(buf, "MMC IRQ 0x%x :", status);
952 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
953 if (status & (1 << i)) {
954 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
958 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
961 static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
965 #endif /* CONFIG_MMC_DEBUG */
968 * MMC controller internal state machines reset
970 * Used to reset command or data internal state machines, using respectively
971 * SRC or SRD bit of SYSCTL register
972 * Can be called from interrupt context
974 static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
978 unsigned long limit = (loops_per_jiffy *
979 msecs_to_jiffies(MMC_TIMEOUT_MS));
981 OMAP_HSMMC_WRITE(host->base, SYSCTL,
982 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
985 * OMAP4 ES2 and greater has an updated reset logic.
986 * Monitor a 0->1 transition first
988 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
989 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
995 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
999 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1000 dev_err(mmc_dev(host->mmc),
1001 "Timeout waiting on controller reset in %s\n",
1005 static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1006 int err, int end_cmd)
1009 omap_hsmmc_reset_controller_fsm(host, SRC);
1011 host->cmd->error = err;
1015 omap_hsmmc_reset_controller_fsm(host, SRD);
1016 omap_hsmmc_dma_cleanup(host, err);
1017 } else if (host->mrq && host->mrq->cmd)
1018 host->mrq->cmd->error = err;
1021 static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
1023 struct mmc_data *data;
1024 int end_cmd = 0, end_trans = 0;
1027 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1029 if (status & ERR_EN) {
1030 omap_hsmmc_dbg_report_irq(host, status);
1032 if (status & (CTO_EN | CCRC_EN))
1034 if (status & (CTO_EN | DTO_EN))
1035 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
1036 else if (status & (CCRC_EN | DCRC_EN))
1037 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1039 if (host->data || host->response_busy) {
1040 end_trans = !end_cmd;
1041 host->response_busy = 0;
1045 if (end_cmd || ((status & CC_EN) && host->cmd))
1046 omap_hsmmc_cmd_done(host, host->cmd);
1047 if ((end_trans || (status & TC_EN)) && host->mrq)
1048 omap_hsmmc_xfer_done(host, data);
1052 * MMC controller IRQ handler
1054 static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1056 struct omap_hsmmc_host *host = dev_id;
1059 status = OMAP_HSMMC_READ(host->base, STAT);
1060 while (status & INT_EN_MASK && host->req_in_progress) {
1061 omap_hsmmc_do_irq(host, status);
1063 /* Flush posted write */
1064 OMAP_HSMMC_WRITE(host->base, STAT, status);
1065 status = OMAP_HSMMC_READ(host->base, STAT);
1071 static void set_sd_bus_power(struct omap_hsmmc_host *host)
1075 OMAP_HSMMC_WRITE(host->base, HCTL,
1076 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1077 for (i = 0; i < loops_per_jiffy; i++) {
1078 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1085 * Switch MMC interface voltage ... only relevant for MMC1.
1087 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1088 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1089 * Some chips, like eMMC ones, use internal transceivers.
1091 static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
1096 /* Disable the clocks */
1097 pm_runtime_put_sync(host->dev);
1099 clk_disable_unprepare(host->dbclk);
1101 /* Turn the power off */
1102 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1104 /* Turn the power ON with given VDD 1.8 or 3.0v */
1106 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1108 pm_runtime_get_sync(host->dev);
1110 clk_prepare_enable(host->dbclk);
1115 OMAP_HSMMC_WRITE(host->base, HCTL,
1116 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1117 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
1120 * If a MMC dual voltage card is detected, the set_ios fn calls
1121 * this fn with VDD bit set for 1.8V. Upon card removal from the
1122 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
1124 * Cope with a bit of slop in the range ... per data sheets:
1125 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1126 * but recommended values are 1.71V to 1.89V
1127 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1128 * but recommended values are 2.7V to 3.3V
1130 * Board setup code shouldn't permit anything very out-of-range.
1131 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1132 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
1134 if ((1 << vdd) <= MMC_VDD_23_24)
1139 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
1140 set_sd_bus_power(host);
1144 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1148 /* Protect the card while the cover is open */
1149 static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1151 if (!mmc_slot(host).get_cover_state)
1154 host->reqs_blocked = 0;
1155 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1156 if (host->protect_card) {
1157 dev_info(host->dev, "%s: cover is closed, "
1158 "card is now accessible\n",
1159 mmc_hostname(host->mmc));
1160 host->protect_card = 0;
1163 if (!host->protect_card) {
1164 dev_info(host->dev, "%s: cover is open, "
1165 "card is now inaccessible\n",
1166 mmc_hostname(host->mmc));
1167 host->protect_card = 1;
1173 * irq handler to notify the core about card insertion/removal
1175 static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
1177 struct omap_hsmmc_host *host = dev_id;
1178 struct omap_mmc_slot_data *slot = &mmc_slot(host);
1181 if (host->suspended)
1184 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1186 if (slot->card_detect)
1187 carddetect = slot->card_detect(host->dev, host->slot_id);
1189 omap_hsmmc_protect_card(host);
1190 carddetect = -ENOSYS;
1194 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1196 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1200 static void omap_hsmmc_dma_callback(void *param)
1202 struct omap_hsmmc_host *host = param;
1203 struct dma_chan *chan;
1204 struct mmc_data *data;
1205 int req_in_progress;
1207 spin_lock_irq(&host->irq_lock);
1208 if (host->dma_ch < 0) {
1209 spin_unlock_irq(&host->irq_lock);
1213 data = host->mrq->data;
1214 chan = omap_hsmmc_get_dma_chan(host, data);
1215 if (!data->host_cookie)
1216 dma_unmap_sg(chan->device->dev,
1217 data->sg, data->sg_len,
1218 omap_hsmmc_get_dma_dir(host, data));
1220 req_in_progress = host->req_in_progress;
1222 spin_unlock_irq(&host->irq_lock);
1224 /* If DMA has finished after TC, complete the request */
1225 if (!req_in_progress) {
1226 struct mmc_request *mrq = host->mrq;
1229 mmc_request_done(host->mmc, mrq);
1233 static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1234 struct mmc_data *data,
1235 struct omap_hsmmc_next *next,
1236 struct dma_chan *chan)
1240 if (!next && data->host_cookie &&
1241 data->host_cookie != host->next_data.cookie) {
1242 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
1243 " host->next_data.cookie %d\n",
1244 __func__, data->host_cookie, host->next_data.cookie);
1245 data->host_cookie = 0;
1248 /* Check if next job is already prepared */
1250 (!next && data->host_cookie != host->next_data.cookie)) {
1251 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
1252 omap_hsmmc_get_dma_dir(host, data));
1255 dma_len = host->next_data.dma_len;
1256 host->next_data.dma_len = 0;
1264 next->dma_len = dma_len;
1265 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1267 host->dma_len = dma_len;
1273 * Routine to configure and start DMA for the MMC card
1275 static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1276 struct mmc_request *req)
1278 struct dma_slave_config cfg;
1279 struct dma_async_tx_descriptor *tx;
1281 struct mmc_data *data = req->data;
1282 struct dma_chan *chan;
1284 /* Sanity check: all the SG entries must be aligned by block size. */
1285 for (i = 0; i < data->sg_len; i++) {
1286 struct scatterlist *sgl;
1289 if (sgl->length % data->blksz)
1292 if ((data->blksz % 4) != 0)
1293 /* REVISIT: The MMC buffer increments only when MSB is written.
1294 * Return error for blksz which is non multiple of four.
1298 BUG_ON(host->dma_ch != -1);
1300 chan = omap_hsmmc_get_dma_chan(host, data);
1302 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1303 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1304 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1305 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1306 cfg.src_maxburst = data->blksz / 4;
1307 cfg.dst_maxburst = data->blksz / 4;
1309 ret = dmaengine_slave_config(chan, &cfg);
1313 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1317 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1318 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1319 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1321 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1322 /* FIXME: cleanup */
1326 tx->callback = omap_hsmmc_dma_callback;
1327 tx->callback_param = host;
1330 dmaengine_submit(tx);
1334 dma_async_issue_pending(chan);
1339 static void set_data_timeout(struct omap_hsmmc_host *host,
1340 unsigned int timeout_ns,
1341 unsigned int timeout_clks)
1343 unsigned int timeout, cycle_ns;
1344 uint32_t reg, clkd, dto = 0;
1346 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1347 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1351 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
1352 timeout = timeout_ns / cycle_ns;
1353 timeout += timeout_clks;
1355 while ((timeout & 0x80000000) == 0) {
1372 reg |= dto << DTO_SHIFT;
1373 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1377 * Configure block length for MMC/SD cards and initiate the transfer.
1380 omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
1383 host->data = req->data;
1385 if (req->data == NULL) {
1386 OMAP_HSMMC_WRITE(host->base, BLK, 0);
1388 * Set an arbitrary 100ms data timeout for commands with
1391 if (req->cmd->flags & MMC_RSP_BUSY)
1392 set_data_timeout(host, 100000000U, 0);
1396 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1397 | (req->data->blocks << 16));
1398 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
1400 if (host->use_dma) {
1401 ret = omap_hsmmc_start_dma_transfer(host, req);
1403 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
1410 static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1413 struct omap_hsmmc_host *host = mmc_priv(mmc);
1414 struct mmc_data *data = mrq->data;
1416 if (host->use_dma && data->host_cookie) {
1417 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
1419 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1420 omap_hsmmc_get_dma_dir(host, data));
1421 data->host_cookie = 0;
1425 static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1428 struct omap_hsmmc_host *host = mmc_priv(mmc);
1430 if (mrq->data->host_cookie) {
1431 mrq->data->host_cookie = 0;
1435 if (host->use_dma) {
1436 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
1438 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1439 &host->next_data, c))
1440 mrq->data->host_cookie = 0;
1445 * Request function. for read/write operation
1447 static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
1449 struct omap_hsmmc_host *host = mmc_priv(mmc);
1452 BUG_ON(host->req_in_progress);
1453 BUG_ON(host->dma_ch != -1);
1454 if (host->protect_card) {
1455 if (host->reqs_blocked < 3) {
1457 * Ensure the controller is left in a consistent
1458 * state by resetting the command and data state
1461 omap_hsmmc_reset_controller_fsm(host, SRD);
1462 omap_hsmmc_reset_controller_fsm(host, SRC);
1463 host->reqs_blocked += 1;
1465 req->cmd->error = -EBADF;
1467 req->data->error = -EBADF;
1468 req->cmd->retries = 0;
1469 mmc_request_done(mmc, req);
1471 } else if (host->reqs_blocked)
1472 host->reqs_blocked = 0;
1473 WARN_ON(host->mrq != NULL);
1475 err = omap_hsmmc_prepare_data(host, req);
1477 req->cmd->error = err;
1479 req->data->error = err;
1481 mmc_request_done(mmc, req);
1485 omap_hsmmc_start_command(host, req->cmd, req->data);
1488 /* Routine to configure clock values. Exposed API to core */
1489 static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1491 struct omap_hsmmc_host *host = mmc_priv(mmc);
1492 int do_send_init_stream = 0;
1494 pm_runtime_get_sync(host->dev);
1496 if (ios->power_mode != host->power_mode) {
1497 switch (ios->power_mode) {
1499 mmc_slot(host).set_power(host->dev, host->slot_id,
1503 mmc_slot(host).set_power(host->dev, host->slot_id,
1507 do_send_init_stream = 1;
1510 host->power_mode = ios->power_mode;
1513 /* FIXME: set registers based only on changes to ios */
1515 omap_hsmmc_set_bus_width(host);
1517 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1518 /* Only MMC1 can interface at 3V without some flavor
1519 * of external transceiver; but they all handle 1.8V.
1521 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1522 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1524 * With pbias cell programming missing, this
1525 * can't be allowed when booting with device
1528 !host->dev->of_node) {
1530 * The mmc_select_voltage fn of the core does
1531 * not seem to set the power_mode to
1532 * MMC_POWER_UP upon recalculating the voltage.
1535 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1536 dev_dbg(mmc_dev(host->mmc),
1537 "Switch operation failed\n");
1541 omap_hsmmc_set_clock(host);
1543 if (do_send_init_stream)
1544 send_init_stream(host);
1546 omap_hsmmc_set_bus_mode(host);
1548 pm_runtime_put_autosuspend(host->dev);
1551 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1553 struct omap_hsmmc_host *host = mmc_priv(mmc);
1555 if (!mmc_slot(host).card_detect)
1557 return mmc_slot(host).card_detect(host->dev, host->slot_id);
1560 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1562 struct omap_hsmmc_host *host = mmc_priv(mmc);
1564 if (!mmc_slot(host).get_ro)
1566 return mmc_slot(host).get_ro(host->dev, 0);
1569 static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1571 struct omap_hsmmc_host *host = mmc_priv(mmc);
1573 if (mmc_slot(host).init_card)
1574 mmc_slot(host).init_card(card);
1577 static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
1579 u32 hctl, capa, value;
1581 /* Only MMC1 supports 3.0V */
1582 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1590 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1591 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1593 value = OMAP_HSMMC_READ(host->base, CAPA);
1594 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1596 /* Set SD bus power bit */
1597 set_sd_bus_power(host);
1600 static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
1602 struct omap_hsmmc_host *host = mmc_priv(mmc);
1604 pm_runtime_get_sync(host->dev);
1609 static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
1611 struct omap_hsmmc_host *host = mmc_priv(mmc);
1613 pm_runtime_mark_last_busy(host->dev);
1614 pm_runtime_put_autosuspend(host->dev);
1619 static const struct mmc_host_ops omap_hsmmc_ops = {
1620 .enable = omap_hsmmc_enable_fclk,
1621 .disable = omap_hsmmc_disable_fclk,
1622 .post_req = omap_hsmmc_post_req,
1623 .pre_req = omap_hsmmc_pre_req,
1624 .request = omap_hsmmc_request,
1625 .set_ios = omap_hsmmc_set_ios,
1626 .get_cd = omap_hsmmc_get_cd,
1627 .get_ro = omap_hsmmc_get_ro,
1628 .init_card = omap_hsmmc_init_card,
1629 /* NYET -- enable_sdio_irq */
1632 #ifdef CONFIG_DEBUG_FS
1634 static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
1636 struct mmc_host *mmc = s->private;
1637 struct omap_hsmmc_host *host = mmc_priv(mmc);
1638 int context_loss = 0;
1640 if (host->pdata->get_context_loss_count)
1641 context_loss = host->pdata->get_context_loss_count(host->dev);
1643 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1644 mmc->index, host->context_loss, context_loss);
1646 if (host->suspended) {
1647 seq_printf(s, "host suspended, can't read registers\n");
1651 pm_runtime_get_sync(host->dev);
1653 seq_printf(s, "CON:\t\t0x%08x\n",
1654 OMAP_HSMMC_READ(host->base, CON));
1655 seq_printf(s, "HCTL:\t\t0x%08x\n",
1656 OMAP_HSMMC_READ(host->base, HCTL));
1657 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1658 OMAP_HSMMC_READ(host->base, SYSCTL));
1659 seq_printf(s, "IE:\t\t0x%08x\n",
1660 OMAP_HSMMC_READ(host->base, IE));
1661 seq_printf(s, "ISE:\t\t0x%08x\n",
1662 OMAP_HSMMC_READ(host->base, ISE));
1663 seq_printf(s, "CAPA:\t\t0x%08x\n",
1664 OMAP_HSMMC_READ(host->base, CAPA));
1666 pm_runtime_mark_last_busy(host->dev);
1667 pm_runtime_put_autosuspend(host->dev);
1672 static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
1674 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
1677 static const struct file_operations mmc_regs_fops = {
1678 .open = omap_hsmmc_regs_open,
1680 .llseek = seq_lseek,
1681 .release = single_release,
1684 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1686 if (mmc->debugfs_root)
1687 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1688 mmc, &mmc_regs_fops);
1693 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1700 static u16 omap4_reg_offset = 0x100;
1702 static const struct of_device_id omap_mmc_of_match[] = {
1704 .compatible = "ti,omap2-hsmmc",
1707 .compatible = "ti,omap3-hsmmc",
1710 .compatible = "ti,omap4-hsmmc",
1711 .data = &omap4_reg_offset,
1715 MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1717 static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1719 struct omap_mmc_platform_data *pdata;
1720 struct device_node *np = dev->of_node;
1721 u32 bus_width, max_freq;
1723 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1725 return NULL; /* out of memory */
1727 if (of_find_property(np, "ti,dual-volt", NULL))
1728 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1730 /* This driver only supports 1 slot */
1731 pdata->nr_slots = 1;
1732 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1733 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1735 if (of_find_property(np, "ti,non-removable", NULL)) {
1736 pdata->slots[0].nonremovable = true;
1737 pdata->slots[0].no_regulator_off_init = true;
1739 of_property_read_u32(np, "bus-width", &bus_width);
1741 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1742 else if (bus_width == 8)
1743 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1745 if (of_find_property(np, "ti,needs-special-reset", NULL))
1746 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1748 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1749 pdata->max_freq = max_freq;
1751 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1752 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1757 static inline struct omap_mmc_platform_data
1758 *of_get_hsmmc_pdata(struct device *dev)
1764 static int omap_hsmmc_probe(struct platform_device *pdev)
1766 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1767 struct mmc_host *mmc;
1768 struct omap_hsmmc_host *host = NULL;
1769 struct resource *res;
1771 const struct of_device_id *match;
1772 dma_cap_mask_t mask;
1773 unsigned tx_req, rx_req;
1774 struct pinctrl *pinctrl;
1776 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1778 pdata = of_get_hsmmc_pdata(&pdev->dev);
1780 const u16 *offsetp = match->data;
1781 pdata->reg_offset = *offsetp;
1785 if (pdata == NULL) {
1786 dev_err(&pdev->dev, "Platform Data is missing\n");
1790 if (pdata->nr_slots == 0) {
1791 dev_err(&pdev->dev, "No Slots\n");
1795 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1796 irq = platform_get_irq(pdev, 0);
1797 if (res == NULL || irq < 0)
1800 res = request_mem_region(res->start, resource_size(res), pdev->name);
1804 ret = omap_hsmmc_gpio_init(pdata);
1808 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
1814 host = mmc_priv(mmc);
1816 host->pdata = pdata;
1817 host->dev = &pdev->dev;
1822 host->mapbase = res->start + pdata->reg_offset;
1823 host->base = ioremap(host->mapbase, SZ_4K);
1824 host->power_mode = MMC_POWER_OFF;
1825 host->next_data.cookie = 1;
1827 platform_set_drvdata(pdev, host);
1829 mmc->ops = &omap_hsmmc_ops;
1832 * If regulator_disable can only put vcc_aux to sleep then there is
1835 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1836 mmc_slot(host).no_off = 1;
1838 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1840 if (pdata->max_freq > 0)
1841 mmc->f_max = pdata->max_freq;
1843 mmc->f_max = OMAP_MMC_MAX_CLOCK;
1845 spin_lock_init(&host->irq_lock);
1847 host->fclk = clk_get(&pdev->dev, "fck");
1848 if (IS_ERR(host->fclk)) {
1849 ret = PTR_ERR(host->fclk);
1854 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1855 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1856 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1859 pm_runtime_enable(host->dev);
1860 pm_runtime_get_sync(host->dev);
1861 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1862 pm_runtime_use_autosuspend(host->dev);
1864 omap_hsmmc_context_save(host);
1866 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1868 * MMC can still work without debounce clock.
1870 if (IS_ERR(host->dbclk)) {
1872 } else if (clk_prepare_enable(host->dbclk) != 0) {
1873 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1874 clk_put(host->dbclk);
1878 /* Since we do only SG emulation, we can have as many segs
1880 mmc->max_segs = 1024;
1882 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1883 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1884 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1885 mmc->max_seg_size = mmc->max_req_size;
1887 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1888 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
1890 mmc->caps |= mmc_slot(host).caps;
1891 if (mmc->caps & MMC_CAP_8_BIT_DATA)
1892 mmc->caps |= MMC_CAP_4_BIT_DATA;
1894 if (mmc_slot(host).nonremovable)
1895 mmc->caps |= MMC_CAP_NONREMOVABLE;
1897 mmc->pm_caps = mmc_slot(host).pm_caps;
1899 omap_hsmmc_conf_bus_power(host);
1901 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1903 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1907 tx_req = res->start;
1909 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1911 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1915 rx_req = res->start;
1918 dma_cap_set(DMA_SLAVE, mask);
1920 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1921 if (!host->rx_chan) {
1922 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
1927 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1928 if (!host->tx_chan) {
1929 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
1934 /* Request IRQ for MMC operations */
1935 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
1936 mmc_hostname(mmc), host);
1938 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1942 if (pdata->init != NULL) {
1943 if (pdata->init(&pdev->dev) != 0) {
1944 dev_err(mmc_dev(host->mmc),
1945 "Unable to configure MMC IRQs\n");
1946 goto err_irq_cd_init;
1950 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
1951 ret = omap_hsmmc_reg_get(host);
1957 mmc->ocr_avail = mmc_slot(host).ocr_mask;
1959 /* Request IRQ for card detect */
1960 if ((mmc_slot(host).card_detect_irq)) {
1961 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1964 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1965 mmc_hostname(mmc), host);
1967 dev_err(mmc_dev(host->mmc),
1968 "Unable to grab MMC CD IRQ\n");
1971 pdata->suspend = omap_hsmmc_suspend_cdirq;
1972 pdata->resume = omap_hsmmc_resume_cdirq;
1975 omap_hsmmc_disable_irq(host);
1977 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1978 if (IS_ERR(pinctrl))
1979 dev_warn(&pdev->dev,
1980 "pins are not configured from the driver\n");
1982 omap_hsmmc_protect_card(host);
1986 if (mmc_slot(host).name != NULL) {
1987 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1991 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
1992 ret = device_create_file(&mmc->class_dev,
1993 &dev_attr_cover_switch);
1998 omap_hsmmc_debugfs(mmc);
1999 pm_runtime_mark_last_busy(host->dev);
2000 pm_runtime_put_autosuspend(host->dev);
2005 mmc_remove_host(mmc);
2006 free_irq(mmc_slot(host).card_detect_irq, host);
2009 omap_hsmmc_reg_put(host);
2011 if (host->pdata->cleanup)
2012 host->pdata->cleanup(&pdev->dev);
2014 free_irq(host->irq, host);
2017 dma_release_channel(host->tx_chan);
2019 dma_release_channel(host->rx_chan);
2020 pm_runtime_put_sync(host->dev);
2021 pm_runtime_disable(host->dev);
2022 clk_put(host->fclk);
2024 clk_disable_unprepare(host->dbclk);
2025 clk_put(host->dbclk);
2028 iounmap(host->base);
2029 platform_set_drvdata(pdev, NULL);
2032 omap_hsmmc_gpio_free(pdata);
2034 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2036 release_mem_region(res->start, resource_size(res));
2040 static int omap_hsmmc_remove(struct platform_device *pdev)
2042 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2043 struct resource *res;
2045 pm_runtime_get_sync(host->dev);
2046 mmc_remove_host(host->mmc);
2048 omap_hsmmc_reg_put(host);
2049 if (host->pdata->cleanup)
2050 host->pdata->cleanup(&pdev->dev);
2051 free_irq(host->irq, host);
2052 if (mmc_slot(host).card_detect_irq)
2053 free_irq(mmc_slot(host).card_detect_irq, host);
2056 dma_release_channel(host->tx_chan);
2058 dma_release_channel(host->rx_chan);
2060 pm_runtime_put_sync(host->dev);
2061 pm_runtime_disable(host->dev);
2062 clk_put(host->fclk);
2064 clk_disable_unprepare(host->dbclk);
2065 clk_put(host->dbclk);
2068 omap_hsmmc_gpio_free(host->pdata);
2069 iounmap(host->base);
2070 mmc_free_host(host->mmc);
2072 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2074 release_mem_region(res->start, resource_size(res));
2075 platform_set_drvdata(pdev, NULL);
2081 static int omap_hsmmc_prepare(struct device *dev)
2083 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2085 if (host->pdata->suspend)
2086 return host->pdata->suspend(dev, host->slot_id);
2091 static void omap_hsmmc_complete(struct device *dev)
2093 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2095 if (host->pdata->resume)
2096 host->pdata->resume(dev, host->slot_id);
2100 static int omap_hsmmc_suspend(struct device *dev)
2103 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2108 if (host && host->suspended)
2111 pm_runtime_get_sync(host->dev);
2112 host->suspended = 1;
2113 ret = mmc_suspend_host(host->mmc);
2116 host->suspended = 0;
2120 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2121 omap_hsmmc_disable_irq(host);
2122 OMAP_HSMMC_WRITE(host->base, HCTL,
2123 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2127 clk_disable_unprepare(host->dbclk);
2129 pm_runtime_put_sync(host->dev);
2133 /* Routine to resume the MMC device */
2134 static int omap_hsmmc_resume(struct device *dev)
2137 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2142 if (host && !host->suspended)
2145 pm_runtime_get_sync(host->dev);
2148 clk_prepare_enable(host->dbclk);
2150 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2151 omap_hsmmc_conf_bus_power(host);
2153 omap_hsmmc_protect_card(host);
2155 /* Notify the core to resume the host */
2156 ret = mmc_resume_host(host->mmc);
2158 host->suspended = 0;
2160 pm_runtime_mark_last_busy(host->dev);
2161 pm_runtime_put_autosuspend(host->dev);
2168 #define omap_hsmmc_prepare NULL
2169 #define omap_hsmmc_complete NULL
2170 #define omap_hsmmc_suspend NULL
2171 #define omap_hsmmc_resume NULL
2174 static int omap_hsmmc_runtime_suspend(struct device *dev)
2176 struct omap_hsmmc_host *host;
2178 host = platform_get_drvdata(to_platform_device(dev));
2179 omap_hsmmc_context_save(host);
2180 dev_dbg(dev, "disabled\n");
2185 static int omap_hsmmc_runtime_resume(struct device *dev)
2187 struct omap_hsmmc_host *host;
2189 host = platform_get_drvdata(to_platform_device(dev));
2190 omap_hsmmc_context_restore(host);
2191 dev_dbg(dev, "enabled\n");
2196 static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
2197 .suspend = omap_hsmmc_suspend,
2198 .resume = omap_hsmmc_resume,
2199 .prepare = omap_hsmmc_prepare,
2200 .complete = omap_hsmmc_complete,
2201 .runtime_suspend = omap_hsmmc_runtime_suspend,
2202 .runtime_resume = omap_hsmmc_runtime_resume,
2205 static struct platform_driver omap_hsmmc_driver = {
2206 .probe = omap_hsmmc_probe,
2207 .remove = omap_hsmmc_remove,
2209 .name = DRIVER_NAME,
2210 .owner = THIS_MODULE,
2211 .pm = &omap_hsmmc_dev_pm_ops,
2212 .of_match_table = of_match_ptr(omap_mmc_of_match),
2216 module_platform_driver(omap_hsmmc_driver);
2217 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2218 MODULE_LICENSE("GPL");
2219 MODULE_ALIAS("platform:" DRIVER_NAME);
2220 MODULE_AUTHOR("Texas Instruments Inc");