2 * drivers/mmc/host/omap_hsmmc.c
4 * Driver for OMAP2430/3430 MMC controller.
6 * Copyright (C) 2007 Texas Instruments.
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 <linux/platform_data/mmc-omap.h>
44 /* OMAP HSMMC Host Controller Registers */
45 #define OMAP_HSMMC_SYSSTATUS 0x0014
46 #define OMAP_HSMMC_CON 0x002C
47 #define OMAP_HSMMC_BLK 0x0104
48 #define OMAP_HSMMC_ARG 0x0108
49 #define OMAP_HSMMC_CMD 0x010C
50 #define OMAP_HSMMC_RSP10 0x0110
51 #define OMAP_HSMMC_RSP32 0x0114
52 #define OMAP_HSMMC_RSP54 0x0118
53 #define OMAP_HSMMC_RSP76 0x011C
54 #define OMAP_HSMMC_DATA 0x0120
55 #define OMAP_HSMMC_HCTL 0x0128
56 #define OMAP_HSMMC_SYSCTL 0x012C
57 #define OMAP_HSMMC_STAT 0x0130
58 #define OMAP_HSMMC_IE 0x0134
59 #define OMAP_HSMMC_ISE 0x0138
60 #define OMAP_HSMMC_CAPA 0x0140
62 #define VS18 (1 << 26)
63 #define VS30 (1 << 25)
65 #define SDVS18 (0x5 << 9)
66 #define SDVS30 (0x6 << 9)
67 #define SDVS33 (0x7 << 9)
68 #define SDVS_MASK 0x00000E00
69 #define SDVSCLR 0xFFFFF1FF
70 #define SDVSDET 0x00000400
77 #define CLKD_MASK 0x0000FFC0
79 #define DTO_MASK 0x000F0000
81 #define INIT_STREAM (1 << 1)
82 #define DP_SELECT (1 << 21)
87 #define FOUR_BIT (1 << 1)
92 #define STAT_CLEAR 0xFFFFFFFF
93 #define INIT_STREAM_CMD 0x00000000
94 #define DUAL_VOLT_OCR_BIT 7
97 #define SOFTRESET (1 << 1)
98 #define RESETDONE (1 << 0)
100 /* Interrupt masks for IE and ISE register */
101 #define CC_EN (1 << 0)
102 #define TC_EN (1 << 1)
103 #define BWR_EN (1 << 4)
104 #define BRR_EN (1 << 5)
105 #define ERR_EN (1 << 15)
106 #define CTO_EN (1 << 16)
107 #define CCRC_EN (1 << 17)
108 #define CEB_EN (1 << 18)
109 #define CIE_EN (1 << 19)
110 #define DTO_EN (1 << 20)
111 #define DCRC_EN (1 << 21)
112 #define DEB_EN (1 << 22)
113 #define CERR_EN (1 << 28)
114 #define BADA_EN (1 << 29)
116 #define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
117 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
118 BRR_EN | BWR_EN | TC_EN | CC_EN)
120 #define MMC_AUTOSUSPEND_DELAY 100
121 #define MMC_TIMEOUT_MS 20
122 #define OMAP_MMC_MIN_CLOCK 400000
123 #define OMAP_MMC_MAX_CLOCK 52000000
124 #define DRIVER_NAME "omap_hsmmc"
127 * One controller can have multiple slots, like on some omap boards using
128 * omap.c controller driver. Luckily this is not currently done on any known
129 * omap_hsmmc.c device.
131 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
134 * MMC Host controller read/write API's
136 #define OMAP_HSMMC_READ(base, reg) \
137 __raw_readl((base) + OMAP_HSMMC_##reg)
139 #define OMAP_HSMMC_WRITE(base, reg, val) \
140 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
142 struct omap_hsmmc_next {
143 unsigned int dma_len;
147 struct omap_hsmmc_host {
149 struct mmc_host *mmc;
150 struct mmc_request *mrq;
151 struct mmc_command *cmd;
152 struct mmc_data *data;
156 * vcc == configured supply
157 * vcc_aux == optional
158 * - MMC1, supply for DAT4..DAT7
159 * - MMC2/MMC2, external level shifter voltage supply, for
160 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
162 struct regulator *vcc;
163 struct regulator *vcc_aux;
165 resource_size_t mapbase;
166 spinlock_t irq_lock; /* Prevent races with irq handler */
167 unsigned int dma_len;
168 unsigned int dma_sg_idx;
169 unsigned char bus_mode;
170 unsigned char power_mode;
174 struct dma_chan *tx_chan;
175 struct dma_chan *rx_chan;
183 struct omap_hsmmc_next next_data;
185 struct omap_mmc_platform_data *pdata;
188 static int omap_hsmmc_card_detect(struct device *dev, int slot)
190 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
191 struct omap_mmc_platform_data *mmc = host->pdata;
193 /* NOTE: assumes card detect signal is active-low */
194 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
197 static int omap_hsmmc_get_wp(struct device *dev, int slot)
199 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
200 struct omap_mmc_platform_data *mmc = host->pdata;
202 /* NOTE: assumes write protect signal is active-high */
203 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
206 static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
208 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
209 struct omap_mmc_platform_data *mmc = host->pdata;
211 /* NOTE: assumes card detect signal is active-low */
212 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
217 static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
219 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
220 struct omap_mmc_platform_data *mmc = host->pdata;
222 disable_irq(mmc->slots[0].card_detect_irq);
226 static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
228 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
229 struct omap_mmc_platform_data *mmc = host->pdata;
231 enable_irq(mmc->slots[0].card_detect_irq);
237 #define omap_hsmmc_suspend_cdirq NULL
238 #define omap_hsmmc_resume_cdirq NULL
242 #ifdef CONFIG_REGULATOR
244 static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
247 struct omap_hsmmc_host *host =
248 platform_get_drvdata(to_platform_device(dev));
252 * If we don't see a Vcc regulator, assume it's a fixed
253 * voltage always-on regulator.
258 * With DT, never turn OFF the regulator. This is because
259 * the pbias cell programming support is still missing when
260 * booting with Device tree
262 if (dev->of_node && !vdd)
265 if (mmc_slot(host).before_set_reg)
266 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
269 * Assume Vcc regulator is used only to power the card ... OMAP
270 * VDDS is used to power the pins, optionally with a transceiver to
271 * support cards using voltages other than VDDS (1.8V nominal). When a
272 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
274 * In some cases this regulator won't support enable/disable;
275 * e.g. it's a fixed rail for a WLAN chip.
277 * In other cases vcc_aux switches interface power. Example, for
278 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
279 * chips/cards need an interface voltage rail too.
282 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
283 /* Enable interface voltage rail, if needed */
284 if (ret == 0 && host->vcc_aux) {
285 ret = regulator_enable(host->vcc_aux);
287 ret = mmc_regulator_set_ocr(host->mmc,
291 /* Shut down the rail */
293 ret = regulator_disable(host->vcc_aux);
295 /* Then proceed to shut down the local regulator */
296 ret = mmc_regulator_set_ocr(host->mmc,
301 if (mmc_slot(host).after_set_reg)
302 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
307 static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
309 struct regulator *reg;
312 reg = regulator_get(host->dev, "vmmc");
314 dev_err(host->dev, "vmmc regulator missing\n");
317 mmc_slot(host).set_power = omap_hsmmc_set_power;
319 ocr_value = mmc_regulator_get_ocrmask(reg);
320 if (!mmc_slot(host).ocr_mask) {
321 mmc_slot(host).ocr_mask = ocr_value;
323 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
324 dev_err(host->dev, "ocrmask %x is not supported\n",
325 mmc_slot(host).ocr_mask);
326 mmc_slot(host).ocr_mask = 0;
331 /* Allow an aux regulator */
332 reg = regulator_get(host->dev, "vmmc_aux");
333 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
335 /* For eMMC do not power off when not in sleep state */
336 if (mmc_slot(host).no_regulator_off_init)
339 * UGLY HACK: workaround regulator framework bugs.
340 * When the bootloader leaves a supply active, it's
341 * initialized with zero usecount ... and we can't
342 * disable it without first enabling it. Until the
343 * framework is fixed, we need a workaround like this
344 * (which is safe for MMC, but not in general).
346 if (regulator_is_enabled(host->vcc) > 0 ||
347 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
348 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
350 mmc_slot(host).set_power(host->dev, host->slot_id,
352 mmc_slot(host).set_power(host->dev, host->slot_id,
360 static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
362 regulator_put(host->vcc);
363 regulator_put(host->vcc_aux);
364 mmc_slot(host).set_power = NULL;
367 static inline int omap_hsmmc_have_reg(void)
374 static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
379 static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
383 static inline int omap_hsmmc_have_reg(void)
390 static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
394 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
395 if (pdata->slots[0].cover)
396 pdata->slots[0].get_cover_state =
397 omap_hsmmc_get_cover_state;
399 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
400 pdata->slots[0].card_detect_irq =
401 gpio_to_irq(pdata->slots[0].switch_pin);
402 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
405 ret = gpio_direction_input(pdata->slots[0].switch_pin);
409 pdata->slots[0].switch_pin = -EINVAL;
411 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
412 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
413 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
416 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
420 pdata->slots[0].gpio_wp = -EINVAL;
425 gpio_free(pdata->slots[0].gpio_wp);
427 if (gpio_is_valid(pdata->slots[0].switch_pin))
429 gpio_free(pdata->slots[0].switch_pin);
433 static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
435 if (gpio_is_valid(pdata->slots[0].gpio_wp))
436 gpio_free(pdata->slots[0].gpio_wp);
437 if (gpio_is_valid(pdata->slots[0].switch_pin))
438 gpio_free(pdata->slots[0].switch_pin);
442 * Start clock to the card
444 static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
446 OMAP_HSMMC_WRITE(host->base, SYSCTL,
447 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
451 * Stop clock to the card
453 static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
455 OMAP_HSMMC_WRITE(host->base, SYSCTL,
456 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
457 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
458 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
461 static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
462 struct mmc_command *cmd)
464 unsigned int irq_mask;
467 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
469 irq_mask = INT_EN_MASK;
471 /* Disable timeout for erases */
472 if (cmd->opcode == MMC_ERASE)
475 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
476 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
477 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
480 static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
482 OMAP_HSMMC_WRITE(host->base, ISE, 0);
483 OMAP_HSMMC_WRITE(host->base, IE, 0);
484 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
487 /* Calculate divisor for the given clock frequency */
488 static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
493 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
501 static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
503 struct mmc_ios *ios = &host->mmc->ios;
504 unsigned long regval;
505 unsigned long timeout;
506 unsigned long clkdiv;
508 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
510 omap_hsmmc_stop_clock(host);
512 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
513 regval = regval & ~(CLKD_MASK | DTO_MASK);
514 clkdiv = calc_divisor(host, ios);
515 regval = regval | (clkdiv << 6) | (DTO << 16);
516 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
517 OMAP_HSMMC_WRITE(host->base, SYSCTL,
518 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
520 /* Wait till the ICS bit is set */
521 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
522 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
523 && time_before(jiffies, timeout))
527 * Enable High-Speed Support
529 * - Controller should support High-Speed-Enable Bit
530 * - Controller should not be using DDR Mode
531 * - Controller should advertise that it supports High Speed
532 * in capabilities register
533 * - MMC/SD clock coming out of controller > 25MHz
535 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
536 (ios->timing != MMC_TIMING_UHS_DDR50) &&
537 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
538 regval = OMAP_HSMMC_READ(host->base, HCTL);
539 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
544 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
547 omap_hsmmc_start_clock(host);
550 static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
552 struct mmc_ios *ios = &host->mmc->ios;
555 con = OMAP_HSMMC_READ(host->base, CON);
556 if (ios->timing == MMC_TIMING_UHS_DDR50)
557 con |= DDR; /* configure in DDR mode */
560 switch (ios->bus_width) {
561 case MMC_BUS_WIDTH_8:
562 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
564 case MMC_BUS_WIDTH_4:
565 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
566 OMAP_HSMMC_WRITE(host->base, HCTL,
567 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
569 case MMC_BUS_WIDTH_1:
570 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
571 OMAP_HSMMC_WRITE(host->base, HCTL,
572 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
577 static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
579 struct mmc_ios *ios = &host->mmc->ios;
582 con = OMAP_HSMMC_READ(host->base, CON);
583 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
584 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
586 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
592 * Restore the MMC host context, if it was lost as result of a
593 * power state change.
595 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
597 struct mmc_ios *ios = &host->mmc->ios;
598 struct omap_mmc_platform_data *pdata = host->pdata;
599 int context_loss = 0;
601 unsigned long timeout;
603 if (pdata->get_context_loss_count) {
604 context_loss = pdata->get_context_loss_count(host->dev);
605 if (context_loss < 0)
609 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
610 context_loss == host->context_loss ? "not " : "");
611 if (host->context_loss == context_loss)
614 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
617 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
618 if (host->power_mode != MMC_POWER_OFF &&
619 (1 << ios->vdd) <= MMC_VDD_23_24)
629 OMAP_HSMMC_WRITE(host->base, HCTL,
630 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
632 OMAP_HSMMC_WRITE(host->base, CAPA,
633 OMAP_HSMMC_READ(host->base, CAPA) | capa);
635 OMAP_HSMMC_WRITE(host->base, HCTL,
636 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
638 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
639 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
640 && time_before(jiffies, timeout))
643 omap_hsmmc_disable_irq(host);
645 /* Do not initialize card-specific things if the power is off */
646 if (host->power_mode == MMC_POWER_OFF)
649 omap_hsmmc_set_bus_width(host);
651 omap_hsmmc_set_clock(host);
653 omap_hsmmc_set_bus_mode(host);
656 host->context_loss = context_loss;
658 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
663 * Save the MMC host context (store the number of power state changes so far).
665 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
667 struct omap_mmc_platform_data *pdata = host->pdata;
670 if (pdata->get_context_loss_count) {
671 context_loss = pdata->get_context_loss_count(host->dev);
672 if (context_loss < 0)
674 host->context_loss = context_loss;
680 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
685 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
692 * Send init stream sequence to card
693 * before sending IDLE command
695 static void send_init_stream(struct omap_hsmmc_host *host)
698 unsigned long timeout;
700 if (host->protect_card)
703 disable_irq(host->irq);
705 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
706 OMAP_HSMMC_WRITE(host->base, CON,
707 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
708 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
710 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
711 while ((reg != CC_EN) && time_before(jiffies, timeout))
712 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
714 OMAP_HSMMC_WRITE(host->base, CON,
715 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
717 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
718 OMAP_HSMMC_READ(host->base, STAT);
720 enable_irq(host->irq);
724 int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
728 if (mmc_slot(host).get_cover_state)
729 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
734 omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
737 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
738 struct omap_hsmmc_host *host = mmc_priv(mmc);
740 return sprintf(buf, "%s\n",
741 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
744 static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
747 omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
750 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
751 struct omap_hsmmc_host *host = mmc_priv(mmc);
753 return sprintf(buf, "%s\n", mmc_slot(host).name);
756 static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
759 * Configure the response type and send the cmd.
762 omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
763 struct mmc_data *data)
765 int cmdreg = 0, resptype = 0, cmdtype = 0;
767 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
768 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
771 omap_hsmmc_enable_irq(host, cmd);
773 host->response_busy = 0;
774 if (cmd->flags & MMC_RSP_PRESENT) {
775 if (cmd->flags & MMC_RSP_136)
777 else if (cmd->flags & MMC_RSP_BUSY) {
779 host->response_busy = 1;
785 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
786 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
787 * a val of 0x3, rest 0x0.
789 if (cmd == host->mrq->stop)
792 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
795 cmdreg |= DP_SELECT | MSBS | BCE;
796 if (data->flags & MMC_DATA_READ)
805 host->req_in_progress = 1;
807 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
808 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
812 omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
814 if (data->flags & MMC_DATA_WRITE)
815 return DMA_TO_DEVICE;
817 return DMA_FROM_DEVICE;
820 static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
821 struct mmc_data *data)
823 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
826 static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
831 spin_lock_irqsave(&host->irq_lock, flags);
832 host->req_in_progress = 0;
833 dma_ch = host->dma_ch;
834 spin_unlock_irqrestore(&host->irq_lock, flags);
836 omap_hsmmc_disable_irq(host);
837 /* Do not complete the request if DMA is still in progress */
838 if (mrq->data && host->use_dma && dma_ch != -1)
841 mmc_request_done(host->mmc, mrq);
845 * Notify the transfer complete to MMC core
848 omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
851 struct mmc_request *mrq = host->mrq;
853 /* TC before CC from CMD6 - don't know why, but it happens */
854 if (host->cmd && host->cmd->opcode == 6 &&
855 host->response_busy) {
856 host->response_busy = 0;
860 omap_hsmmc_request_done(host, mrq);
867 data->bytes_xfered += data->blocks * (data->blksz);
869 data->bytes_xfered = 0;
872 omap_hsmmc_request_done(host, data->mrq);
875 omap_hsmmc_start_command(host, data->stop, NULL);
879 * Notify the core about command completion
882 omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
886 if (cmd->flags & MMC_RSP_PRESENT) {
887 if (cmd->flags & MMC_RSP_136) {
888 /* response type 2 */
889 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
890 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
891 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
892 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
894 /* response types 1, 1b, 3, 4, 5, 6 */
895 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
898 if ((host->data == NULL && !host->response_busy) || cmd->error)
899 omap_hsmmc_request_done(host, cmd->mrq);
903 * DMA clean up for command errors
905 static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
910 host->data->error = errno;
912 spin_lock_irqsave(&host->irq_lock, flags);
913 dma_ch = host->dma_ch;
915 spin_unlock_irqrestore(&host->irq_lock, flags);
917 if (host->use_dma && dma_ch != -1) {
918 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
920 dmaengine_terminate_all(chan);
921 dma_unmap_sg(chan->device->dev,
922 host->data->sg, host->data->sg_len,
923 omap_hsmmc_get_dma_dir(host, host->data));
925 host->data->host_cookie = 0;
931 * Readable error output
933 #ifdef CONFIG_MMC_DEBUG
934 static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
936 /* --- means reserved bit without definition at documentation */
937 static const char *omap_hsmmc_status_bits[] = {
938 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
939 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
940 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
941 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
947 len = sprintf(buf, "MMC IRQ 0x%x :", status);
950 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
951 if (status & (1 << i)) {
952 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
956 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
959 static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
963 #endif /* CONFIG_MMC_DEBUG */
966 * MMC controller internal state machines reset
968 * Used to reset command or data internal state machines, using respectively
969 * SRC or SRD bit of SYSCTL register
970 * Can be called from interrupt context
972 static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
976 unsigned long limit = (loops_per_jiffy *
977 msecs_to_jiffies(MMC_TIMEOUT_MS));
979 OMAP_HSMMC_WRITE(host->base, SYSCTL,
980 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
983 * OMAP4 ES2 and greater has an updated reset logic.
984 * Monitor a 0->1 transition first
986 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
987 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
993 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
997 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
998 dev_err(mmc_dev(host->mmc),
999 "Timeout waiting on controller reset in %s\n",
1003 static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1004 int err, int end_cmd)
1007 omap_hsmmc_reset_controller_fsm(host, SRC);
1009 host->cmd->error = err;
1013 omap_hsmmc_reset_controller_fsm(host, SRD);
1014 omap_hsmmc_dma_cleanup(host, err);
1015 } else if (host->mrq && host->mrq->cmd)
1016 host->mrq->cmd->error = err;
1019 static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
1021 struct mmc_data *data;
1022 int end_cmd = 0, end_trans = 0;
1025 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
1027 if (status & ERR_EN) {
1028 omap_hsmmc_dbg_report_irq(host, status);
1030 if (status & (CTO_EN | CCRC_EN))
1032 if (status & (CTO_EN | DTO_EN))
1033 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
1034 else if (status & (CCRC_EN | DCRC_EN))
1035 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1037 if (host->data || host->response_busy) {
1038 end_trans = !end_cmd;
1039 host->response_busy = 0;
1043 if (end_cmd || ((status & CC_EN) && host->cmd))
1044 omap_hsmmc_cmd_done(host, host->cmd);
1045 if ((end_trans || (status & TC_EN)) && host->mrq)
1046 omap_hsmmc_xfer_done(host, data);
1050 * MMC controller IRQ handler
1052 static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1054 struct omap_hsmmc_host *host = dev_id;
1057 status = OMAP_HSMMC_READ(host->base, STAT);
1058 while (status & INT_EN_MASK && host->req_in_progress) {
1059 omap_hsmmc_do_irq(host, status);
1061 /* Flush posted write */
1062 OMAP_HSMMC_WRITE(host->base, STAT, status);
1063 status = OMAP_HSMMC_READ(host->base, STAT);
1069 static void set_sd_bus_power(struct omap_hsmmc_host *host)
1073 OMAP_HSMMC_WRITE(host->base, HCTL,
1074 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1075 for (i = 0; i < loops_per_jiffy; i++) {
1076 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1083 * Switch MMC interface voltage ... only relevant for MMC1.
1085 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1086 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1087 * Some chips, like eMMC ones, use internal transceivers.
1089 static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
1094 /* Disable the clocks */
1095 pm_runtime_put_sync(host->dev);
1097 clk_disable_unprepare(host->dbclk);
1099 /* Turn the power off */
1100 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1102 /* Turn the power ON with given VDD 1.8 or 3.0v */
1104 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1106 pm_runtime_get_sync(host->dev);
1108 clk_prepare_enable(host->dbclk);
1113 OMAP_HSMMC_WRITE(host->base, HCTL,
1114 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1115 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
1118 * If a MMC dual voltage card is detected, the set_ios fn calls
1119 * this fn with VDD bit set for 1.8V. Upon card removal from the
1120 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
1122 * Cope with a bit of slop in the range ... per data sheets:
1123 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1124 * but recommended values are 1.71V to 1.89V
1125 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1126 * but recommended values are 2.7V to 3.3V
1128 * Board setup code shouldn't permit anything very out-of-range.
1129 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1130 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
1132 if ((1 << vdd) <= MMC_VDD_23_24)
1137 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
1138 set_sd_bus_power(host);
1142 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1146 /* Protect the card while the cover is open */
1147 static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1149 if (!mmc_slot(host).get_cover_state)
1152 host->reqs_blocked = 0;
1153 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1154 if (host->protect_card) {
1155 dev_info(host->dev, "%s: cover is closed, "
1156 "card is now accessible\n",
1157 mmc_hostname(host->mmc));
1158 host->protect_card = 0;
1161 if (!host->protect_card) {
1162 dev_info(host->dev, "%s: cover is open, "
1163 "card is now inaccessible\n",
1164 mmc_hostname(host->mmc));
1165 host->protect_card = 1;
1171 * irq handler to notify the core about card insertion/removal
1173 static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
1175 struct omap_hsmmc_host *host = dev_id;
1176 struct omap_mmc_slot_data *slot = &mmc_slot(host);
1179 if (host->suspended)
1182 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1184 if (slot->card_detect)
1185 carddetect = slot->card_detect(host->dev, host->slot_id);
1187 omap_hsmmc_protect_card(host);
1188 carddetect = -ENOSYS;
1192 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1194 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1198 static void omap_hsmmc_dma_callback(void *param)
1200 struct omap_hsmmc_host *host = param;
1201 struct dma_chan *chan;
1202 struct mmc_data *data;
1203 int req_in_progress;
1205 spin_lock_irq(&host->irq_lock);
1206 if (host->dma_ch < 0) {
1207 spin_unlock_irq(&host->irq_lock);
1211 data = host->mrq->data;
1212 chan = omap_hsmmc_get_dma_chan(host, data);
1213 if (!data->host_cookie)
1214 dma_unmap_sg(chan->device->dev,
1215 data->sg, data->sg_len,
1216 omap_hsmmc_get_dma_dir(host, data));
1218 req_in_progress = host->req_in_progress;
1220 spin_unlock_irq(&host->irq_lock);
1222 /* If DMA has finished after TC, complete the request */
1223 if (!req_in_progress) {
1224 struct mmc_request *mrq = host->mrq;
1227 mmc_request_done(host->mmc, mrq);
1231 static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1232 struct mmc_data *data,
1233 struct omap_hsmmc_next *next,
1234 struct dma_chan *chan)
1238 if (!next && data->host_cookie &&
1239 data->host_cookie != host->next_data.cookie) {
1240 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
1241 " host->next_data.cookie %d\n",
1242 __func__, data->host_cookie, host->next_data.cookie);
1243 data->host_cookie = 0;
1246 /* Check if next job is already prepared */
1248 (!next && data->host_cookie != host->next_data.cookie)) {
1249 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
1250 omap_hsmmc_get_dma_dir(host, data));
1253 dma_len = host->next_data.dma_len;
1254 host->next_data.dma_len = 0;
1262 next->dma_len = dma_len;
1263 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1265 host->dma_len = dma_len;
1271 * Routine to configure and start DMA for the MMC card
1273 static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1274 struct mmc_request *req)
1276 struct dma_slave_config cfg;
1277 struct dma_async_tx_descriptor *tx;
1279 struct mmc_data *data = req->data;
1280 struct dma_chan *chan;
1282 /* Sanity check: all the SG entries must be aligned by block size. */
1283 for (i = 0; i < data->sg_len; i++) {
1284 struct scatterlist *sgl;
1287 if (sgl->length % data->blksz)
1290 if ((data->blksz % 4) != 0)
1291 /* REVISIT: The MMC buffer increments only when MSB is written.
1292 * Return error for blksz which is non multiple of four.
1296 BUG_ON(host->dma_ch != -1);
1298 chan = omap_hsmmc_get_dma_chan(host, data);
1300 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1301 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1302 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1303 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1304 cfg.src_maxburst = data->blksz / 4;
1305 cfg.dst_maxburst = data->blksz / 4;
1307 ret = dmaengine_slave_config(chan, &cfg);
1311 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1315 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1316 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1317 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1319 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1320 /* FIXME: cleanup */
1324 tx->callback = omap_hsmmc_dma_callback;
1325 tx->callback_param = host;
1328 dmaengine_submit(tx);
1332 dma_async_issue_pending(chan);
1337 static void set_data_timeout(struct omap_hsmmc_host *host,
1338 unsigned int timeout_ns,
1339 unsigned int timeout_clks)
1341 unsigned int timeout, cycle_ns;
1342 uint32_t reg, clkd, dto = 0;
1344 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1345 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1349 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
1350 timeout = timeout_ns / cycle_ns;
1351 timeout += timeout_clks;
1353 while ((timeout & 0x80000000) == 0) {
1370 reg |= dto << DTO_SHIFT;
1371 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1375 * Configure block length for MMC/SD cards and initiate the transfer.
1378 omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
1381 host->data = req->data;
1383 if (req->data == NULL) {
1384 OMAP_HSMMC_WRITE(host->base, BLK, 0);
1386 * Set an arbitrary 100ms data timeout for commands with
1389 if (req->cmd->flags & MMC_RSP_BUSY)
1390 set_data_timeout(host, 100000000U, 0);
1394 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1395 | (req->data->blocks << 16));
1396 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
1398 if (host->use_dma) {
1399 ret = omap_hsmmc_start_dma_transfer(host, req);
1401 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
1408 static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1411 struct omap_hsmmc_host *host = mmc_priv(mmc);
1412 struct mmc_data *data = mrq->data;
1414 if (host->use_dma && data->host_cookie) {
1415 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
1417 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1418 omap_hsmmc_get_dma_dir(host, data));
1419 data->host_cookie = 0;
1423 static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1426 struct omap_hsmmc_host *host = mmc_priv(mmc);
1428 if (mrq->data->host_cookie) {
1429 mrq->data->host_cookie = 0;
1433 if (host->use_dma) {
1434 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
1436 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1437 &host->next_data, c))
1438 mrq->data->host_cookie = 0;
1443 * Request function. for read/write operation
1445 static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
1447 struct omap_hsmmc_host *host = mmc_priv(mmc);
1450 BUG_ON(host->req_in_progress);
1451 BUG_ON(host->dma_ch != -1);
1452 if (host->protect_card) {
1453 if (host->reqs_blocked < 3) {
1455 * Ensure the controller is left in a consistent
1456 * state by resetting the command and data state
1459 omap_hsmmc_reset_controller_fsm(host, SRD);
1460 omap_hsmmc_reset_controller_fsm(host, SRC);
1461 host->reqs_blocked += 1;
1463 req->cmd->error = -EBADF;
1465 req->data->error = -EBADF;
1466 req->cmd->retries = 0;
1467 mmc_request_done(mmc, req);
1469 } else if (host->reqs_blocked)
1470 host->reqs_blocked = 0;
1471 WARN_ON(host->mrq != NULL);
1473 err = omap_hsmmc_prepare_data(host, req);
1475 req->cmd->error = err;
1477 req->data->error = err;
1479 mmc_request_done(mmc, req);
1483 omap_hsmmc_start_command(host, req->cmd, req->data);
1486 /* Routine to configure clock values. Exposed API to core */
1487 static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1489 struct omap_hsmmc_host *host = mmc_priv(mmc);
1490 int do_send_init_stream = 0;
1492 pm_runtime_get_sync(host->dev);
1494 if (ios->power_mode != host->power_mode) {
1495 switch (ios->power_mode) {
1497 mmc_slot(host).set_power(host->dev, host->slot_id,
1501 mmc_slot(host).set_power(host->dev, host->slot_id,
1505 do_send_init_stream = 1;
1508 host->power_mode = ios->power_mode;
1511 /* FIXME: set registers based only on changes to ios */
1513 omap_hsmmc_set_bus_width(host);
1515 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1516 /* Only MMC1 can interface at 3V without some flavor
1517 * of external transceiver; but they all handle 1.8V.
1519 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1520 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1522 * With pbias cell programming missing, this
1523 * can't be allowed when booting with device
1526 !host->dev->of_node) {
1528 * The mmc_select_voltage fn of the core does
1529 * not seem to set the power_mode to
1530 * MMC_POWER_UP upon recalculating the voltage.
1533 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1534 dev_dbg(mmc_dev(host->mmc),
1535 "Switch operation failed\n");
1539 omap_hsmmc_set_clock(host);
1541 if (do_send_init_stream)
1542 send_init_stream(host);
1544 omap_hsmmc_set_bus_mode(host);
1546 pm_runtime_put_autosuspend(host->dev);
1549 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1551 struct omap_hsmmc_host *host = mmc_priv(mmc);
1553 if (!mmc_slot(host).card_detect)
1555 return mmc_slot(host).card_detect(host->dev, host->slot_id);
1558 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1560 struct omap_hsmmc_host *host = mmc_priv(mmc);
1562 if (!mmc_slot(host).get_ro)
1564 return mmc_slot(host).get_ro(host->dev, 0);
1567 static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1569 struct omap_hsmmc_host *host = mmc_priv(mmc);
1571 if (mmc_slot(host).init_card)
1572 mmc_slot(host).init_card(card);
1575 static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
1577 u32 hctl, capa, value;
1579 /* Only MMC1 supports 3.0V */
1580 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1588 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1589 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1591 value = OMAP_HSMMC_READ(host->base, CAPA);
1592 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1594 /* Set SD bus power bit */
1595 set_sd_bus_power(host);
1598 static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
1600 struct omap_hsmmc_host *host = mmc_priv(mmc);
1602 pm_runtime_get_sync(host->dev);
1607 static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
1609 struct omap_hsmmc_host *host = mmc_priv(mmc);
1611 pm_runtime_mark_last_busy(host->dev);
1612 pm_runtime_put_autosuspend(host->dev);
1617 static const struct mmc_host_ops omap_hsmmc_ops = {
1618 .enable = omap_hsmmc_enable_fclk,
1619 .disable = omap_hsmmc_disable_fclk,
1620 .post_req = omap_hsmmc_post_req,
1621 .pre_req = omap_hsmmc_pre_req,
1622 .request = omap_hsmmc_request,
1623 .set_ios = omap_hsmmc_set_ios,
1624 .get_cd = omap_hsmmc_get_cd,
1625 .get_ro = omap_hsmmc_get_ro,
1626 .init_card = omap_hsmmc_init_card,
1627 /* NYET -- enable_sdio_irq */
1630 #ifdef CONFIG_DEBUG_FS
1632 static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
1634 struct mmc_host *mmc = s->private;
1635 struct omap_hsmmc_host *host = mmc_priv(mmc);
1636 int context_loss = 0;
1638 if (host->pdata->get_context_loss_count)
1639 context_loss = host->pdata->get_context_loss_count(host->dev);
1641 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1642 mmc->index, host->context_loss, context_loss);
1644 if (host->suspended) {
1645 seq_printf(s, "host suspended, can't read registers\n");
1649 pm_runtime_get_sync(host->dev);
1651 seq_printf(s, "CON:\t\t0x%08x\n",
1652 OMAP_HSMMC_READ(host->base, CON));
1653 seq_printf(s, "HCTL:\t\t0x%08x\n",
1654 OMAP_HSMMC_READ(host->base, HCTL));
1655 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1656 OMAP_HSMMC_READ(host->base, SYSCTL));
1657 seq_printf(s, "IE:\t\t0x%08x\n",
1658 OMAP_HSMMC_READ(host->base, IE));
1659 seq_printf(s, "ISE:\t\t0x%08x\n",
1660 OMAP_HSMMC_READ(host->base, ISE));
1661 seq_printf(s, "CAPA:\t\t0x%08x\n",
1662 OMAP_HSMMC_READ(host->base, CAPA));
1664 pm_runtime_mark_last_busy(host->dev);
1665 pm_runtime_put_autosuspend(host->dev);
1670 static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
1672 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
1675 static const struct file_operations mmc_regs_fops = {
1676 .open = omap_hsmmc_regs_open,
1678 .llseek = seq_lseek,
1679 .release = single_release,
1682 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1684 if (mmc->debugfs_root)
1685 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1686 mmc, &mmc_regs_fops);
1691 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1698 static u16 omap4_reg_offset = 0x100;
1700 static const struct of_device_id omap_mmc_of_match[] = {
1702 .compatible = "ti,omap2-hsmmc",
1705 .compatible = "ti,omap3-hsmmc",
1708 .compatible = "ti,omap4-hsmmc",
1709 .data = &omap4_reg_offset,
1713 MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1715 static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1717 struct omap_mmc_platform_data *pdata;
1718 struct device_node *np = dev->of_node;
1719 u32 bus_width, max_freq;
1721 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1723 return NULL; /* out of memory */
1725 if (of_find_property(np, "ti,dual-volt", NULL))
1726 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1728 /* This driver only supports 1 slot */
1729 pdata->nr_slots = 1;
1730 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1731 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1733 if (of_find_property(np, "ti,non-removable", NULL)) {
1734 pdata->slots[0].nonremovable = true;
1735 pdata->slots[0].no_regulator_off_init = true;
1737 of_property_read_u32(np, "bus-width", &bus_width);
1739 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1740 else if (bus_width == 8)
1741 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1743 if (of_find_property(np, "ti,needs-special-reset", NULL))
1744 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1746 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1747 pdata->max_freq = max_freq;
1749 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1750 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1755 static inline struct omap_mmc_platform_data
1756 *of_get_hsmmc_pdata(struct device *dev)
1762 static int omap_hsmmc_probe(struct platform_device *pdev)
1764 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1765 struct mmc_host *mmc;
1766 struct omap_hsmmc_host *host = NULL;
1767 struct resource *res;
1769 const struct of_device_id *match;
1770 dma_cap_mask_t mask;
1771 unsigned tx_req, rx_req;
1772 struct pinctrl *pinctrl;
1774 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1776 pdata = of_get_hsmmc_pdata(&pdev->dev);
1778 const u16 *offsetp = match->data;
1779 pdata->reg_offset = *offsetp;
1783 if (pdata == NULL) {
1784 dev_err(&pdev->dev, "Platform Data is missing\n");
1788 if (pdata->nr_slots == 0) {
1789 dev_err(&pdev->dev, "No Slots\n");
1793 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1794 irq = platform_get_irq(pdev, 0);
1795 if (res == NULL || irq < 0)
1798 res = request_mem_region(res->start, resource_size(res), pdev->name);
1802 ret = omap_hsmmc_gpio_init(pdata);
1806 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
1812 host = mmc_priv(mmc);
1814 host->pdata = pdata;
1815 host->dev = &pdev->dev;
1820 host->mapbase = res->start + pdata->reg_offset;
1821 host->base = ioremap(host->mapbase, SZ_4K);
1822 host->power_mode = MMC_POWER_OFF;
1823 host->next_data.cookie = 1;
1825 platform_set_drvdata(pdev, host);
1827 mmc->ops = &omap_hsmmc_ops;
1830 * If regulator_disable can only put vcc_aux to sleep then there is
1833 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1834 mmc_slot(host).no_off = 1;
1836 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1838 if (pdata->max_freq > 0)
1839 mmc->f_max = pdata->max_freq;
1841 mmc->f_max = OMAP_MMC_MAX_CLOCK;
1843 spin_lock_init(&host->irq_lock);
1845 host->fclk = clk_get(&pdev->dev, "fck");
1846 if (IS_ERR(host->fclk)) {
1847 ret = PTR_ERR(host->fclk);
1852 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1853 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1854 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1857 pm_runtime_enable(host->dev);
1858 pm_runtime_get_sync(host->dev);
1859 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1860 pm_runtime_use_autosuspend(host->dev);
1862 omap_hsmmc_context_save(host);
1864 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1866 * MMC can still work without debounce clock.
1868 if (IS_ERR(host->dbclk)) {
1870 } else if (clk_prepare_enable(host->dbclk) != 0) {
1871 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1872 clk_put(host->dbclk);
1876 /* Since we do only SG emulation, we can have as many segs
1878 mmc->max_segs = 1024;
1880 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1881 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1882 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1883 mmc->max_seg_size = mmc->max_req_size;
1885 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1886 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
1888 mmc->caps |= mmc_slot(host).caps;
1889 if (mmc->caps & MMC_CAP_8_BIT_DATA)
1890 mmc->caps |= MMC_CAP_4_BIT_DATA;
1892 if (mmc_slot(host).nonremovable)
1893 mmc->caps |= MMC_CAP_NONREMOVABLE;
1895 mmc->pm_caps = mmc_slot(host).pm_caps;
1897 omap_hsmmc_conf_bus_power(host);
1899 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1901 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1905 tx_req = res->start;
1907 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1909 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1913 rx_req = res->start;
1916 dma_cap_set(DMA_SLAVE, mask);
1918 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1919 if (!host->rx_chan) {
1920 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
1925 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1926 if (!host->tx_chan) {
1927 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
1932 /* Request IRQ for MMC operations */
1933 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
1934 mmc_hostname(mmc), host);
1936 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1940 if (pdata->init != NULL) {
1941 if (pdata->init(&pdev->dev) != 0) {
1942 dev_err(mmc_dev(host->mmc),
1943 "Unable to configure MMC IRQs\n");
1944 goto err_irq_cd_init;
1948 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
1949 ret = omap_hsmmc_reg_get(host);
1955 mmc->ocr_avail = mmc_slot(host).ocr_mask;
1957 /* Request IRQ for card detect */
1958 if ((mmc_slot(host).card_detect_irq)) {
1959 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1962 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1963 mmc_hostname(mmc), host);
1965 dev_err(mmc_dev(host->mmc),
1966 "Unable to grab MMC CD IRQ\n");
1969 pdata->suspend = omap_hsmmc_suspend_cdirq;
1970 pdata->resume = omap_hsmmc_resume_cdirq;
1973 omap_hsmmc_disable_irq(host);
1975 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1976 if (IS_ERR(pinctrl))
1977 dev_warn(&pdev->dev,
1978 "pins are not configured from the driver\n");
1980 omap_hsmmc_protect_card(host);
1984 if (mmc_slot(host).name != NULL) {
1985 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1989 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
1990 ret = device_create_file(&mmc->class_dev,
1991 &dev_attr_cover_switch);
1996 omap_hsmmc_debugfs(mmc);
1997 pm_runtime_mark_last_busy(host->dev);
1998 pm_runtime_put_autosuspend(host->dev);
2003 mmc_remove_host(mmc);
2004 free_irq(mmc_slot(host).card_detect_irq, host);
2007 omap_hsmmc_reg_put(host);
2009 if (host->pdata->cleanup)
2010 host->pdata->cleanup(&pdev->dev);
2012 free_irq(host->irq, host);
2015 dma_release_channel(host->tx_chan);
2017 dma_release_channel(host->rx_chan);
2018 pm_runtime_put_sync(host->dev);
2019 pm_runtime_disable(host->dev);
2020 clk_put(host->fclk);
2022 clk_disable_unprepare(host->dbclk);
2023 clk_put(host->dbclk);
2026 iounmap(host->base);
2027 platform_set_drvdata(pdev, NULL);
2030 omap_hsmmc_gpio_free(pdata);
2032 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2034 release_mem_region(res->start, resource_size(res));
2038 static int omap_hsmmc_remove(struct platform_device *pdev)
2040 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2041 struct resource *res;
2043 pm_runtime_get_sync(host->dev);
2044 mmc_remove_host(host->mmc);
2046 omap_hsmmc_reg_put(host);
2047 if (host->pdata->cleanup)
2048 host->pdata->cleanup(&pdev->dev);
2049 free_irq(host->irq, host);
2050 if (mmc_slot(host).card_detect_irq)
2051 free_irq(mmc_slot(host).card_detect_irq, host);
2054 dma_release_channel(host->tx_chan);
2056 dma_release_channel(host->rx_chan);
2058 pm_runtime_put_sync(host->dev);
2059 pm_runtime_disable(host->dev);
2060 clk_put(host->fclk);
2062 clk_disable_unprepare(host->dbclk);
2063 clk_put(host->dbclk);
2066 omap_hsmmc_gpio_free(host->pdata);
2067 iounmap(host->base);
2068 mmc_free_host(host->mmc);
2070 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2072 release_mem_region(res->start, resource_size(res));
2073 platform_set_drvdata(pdev, NULL);
2079 static int omap_hsmmc_prepare(struct device *dev)
2081 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2083 if (host->pdata->suspend)
2084 return host->pdata->suspend(dev, host->slot_id);
2089 static void omap_hsmmc_complete(struct device *dev)
2091 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2093 if (host->pdata->resume)
2094 host->pdata->resume(dev, host->slot_id);
2098 static int omap_hsmmc_suspend(struct device *dev)
2101 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2106 if (host && host->suspended)
2109 pm_runtime_get_sync(host->dev);
2110 host->suspended = 1;
2111 ret = mmc_suspend_host(host->mmc);
2114 host->suspended = 0;
2118 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2119 omap_hsmmc_disable_irq(host);
2120 OMAP_HSMMC_WRITE(host->base, HCTL,
2121 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2125 clk_disable_unprepare(host->dbclk);
2127 pm_runtime_put_sync(host->dev);
2131 /* Routine to resume the MMC device */
2132 static int omap_hsmmc_resume(struct device *dev)
2135 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2140 if (host && !host->suspended)
2143 pm_runtime_get_sync(host->dev);
2146 clk_prepare_enable(host->dbclk);
2148 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2149 omap_hsmmc_conf_bus_power(host);
2151 omap_hsmmc_protect_card(host);
2153 /* Notify the core to resume the host */
2154 ret = mmc_resume_host(host->mmc);
2156 host->suspended = 0;
2158 pm_runtime_mark_last_busy(host->dev);
2159 pm_runtime_put_autosuspend(host->dev);
2166 #define omap_hsmmc_prepare NULL
2167 #define omap_hsmmc_complete NULL
2168 #define omap_hsmmc_suspend NULL
2169 #define omap_hsmmc_resume NULL
2172 static int omap_hsmmc_runtime_suspend(struct device *dev)
2174 struct omap_hsmmc_host *host;
2176 host = platform_get_drvdata(to_platform_device(dev));
2177 omap_hsmmc_context_save(host);
2178 dev_dbg(dev, "disabled\n");
2183 static int omap_hsmmc_runtime_resume(struct device *dev)
2185 struct omap_hsmmc_host *host;
2187 host = platform_get_drvdata(to_platform_device(dev));
2188 omap_hsmmc_context_restore(host);
2189 dev_dbg(dev, "enabled\n");
2194 static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
2195 .suspend = omap_hsmmc_suspend,
2196 .resume = omap_hsmmc_resume,
2197 .prepare = omap_hsmmc_prepare,
2198 .complete = omap_hsmmc_complete,
2199 .runtime_suspend = omap_hsmmc_runtime_suspend,
2200 .runtime_resume = omap_hsmmc_runtime_resume,
2203 static struct platform_driver omap_hsmmc_driver = {
2204 .probe = omap_hsmmc_probe,
2205 .remove = omap_hsmmc_remove,
2207 .name = DRIVER_NAME,
2208 .owner = THIS_MODULE,
2209 .pm = &omap_hsmmc_dev_pm_ops,
2210 .of_match_table = of_match_ptr(omap_mmc_of_match),
2214 module_platform_driver(omap_hsmmc_driver);
2215 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2216 MODULE_LICENSE("GPL");
2217 MODULE_ALIAS("platform:" DRIVER_NAME);
2218 MODULE_AUTHOR("Texas Instruments Inc");