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/seq_file.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_device.h>
27 #include <linux/timer.h>
28 #include <linux/clk.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/core.h>
31 #include <linux/mmc/mmc.h>
33 #include <linux/semaphore.h>
34 #include <linux/gpio.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/pm_runtime.h>
38 #include <mach/hardware.h>
39 #include <plat/board.h>
43 /* OMAP HSMMC Host Controller Registers */
44 #define OMAP_HSMMC_SYSCONFIG 0x0010
45 #define OMAP_HSMMC_SYSSTATUS 0x0014
46 #define OMAP_HSMMC_CON 0x002C
47 #define OMAP_HSMMC_BLK 0x0104
48 #define OMAP_HSMMC_ARG 0x0108
49 #define OMAP_HSMMC_CMD 0x010C
50 #define OMAP_HSMMC_RSP10 0x0110
51 #define OMAP_HSMMC_RSP32 0x0114
52 #define OMAP_HSMMC_RSP54 0x0118
53 #define OMAP_HSMMC_RSP76 0x011C
54 #define OMAP_HSMMC_DATA 0x0120
55 #define OMAP_HSMMC_HCTL 0x0128
56 #define OMAP_HSMMC_SYSCTL 0x012C
57 #define OMAP_HSMMC_STAT 0x0130
58 #define OMAP_HSMMC_IE 0x0134
59 #define OMAP_HSMMC_ISE 0x0138
60 #define OMAP_HSMMC_CAPA 0x0140
62 #define VS18 (1 << 26)
63 #define VS30 (1 << 25)
64 #define SDVS18 (0x5 << 9)
65 #define SDVS30 (0x6 << 9)
66 #define SDVS33 (0x7 << 9)
67 #define SDVS_MASK 0x00000E00
68 #define SDVSCLR 0xFFFFF1FF
69 #define SDVSDET 0x00000400
76 #define CLKD_MASK 0x0000FFC0
78 #define DTO_MASK 0x000F0000
80 #define INT_EN_MASK 0x307F0033
81 #define BWR_ENABLE (1 << 4)
82 #define BRR_ENABLE (1 << 5)
83 #define DTO_ENABLE (1 << 20)
84 #define INIT_STREAM (1 << 1)
85 #define DP_SELECT (1 << 21)
90 #define FOUR_BIT (1 << 1)
96 #define CMD_TIMEOUT (1 << 16)
97 #define DATA_TIMEOUT (1 << 20)
98 #define CMD_CRC (1 << 17)
99 #define DATA_CRC (1 << 21)
100 #define CARD_ERR (1 << 28)
101 #define STAT_CLEAR 0xFFFFFFFF
102 #define INIT_STREAM_CMD 0x00000000
103 #define DUAL_VOLT_OCR_BIT 7
104 #define SRC (1 << 25)
105 #define SRD (1 << 26)
106 #define SOFTRESET (1 << 1)
107 #define RESETDONE (1 << 0)
109 #define MMC_AUTOSUSPEND_DELAY 100
110 #define MMC_TIMEOUT_MS 20
111 #define OMAP_MMC_MIN_CLOCK 400000
112 #define OMAP_MMC_MAX_CLOCK 52000000
113 #define DRIVER_NAME "omap_hsmmc"
116 * One controller can have multiple slots, like on some omap boards using
117 * omap.c controller driver. Luckily this is not currently done on any known
118 * omap_hsmmc.c device.
120 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
123 * MMC Host controller read/write API's
125 #define OMAP_HSMMC_READ(base, reg) \
126 __raw_readl((base) + OMAP_HSMMC_##reg)
128 #define OMAP_HSMMC_WRITE(base, reg, val) \
129 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
131 struct omap_hsmmc_next {
132 unsigned int dma_len;
136 struct omap_hsmmc_host {
138 struct mmc_host *mmc;
139 struct mmc_request *mrq;
140 struct mmc_command *cmd;
141 struct mmc_data *data;
145 * vcc == configured supply
146 * vcc_aux == optional
147 * - MMC1, supply for DAT4..DAT7
148 * - MMC2/MMC2, external level shifter voltage supply, for
149 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
151 struct regulator *vcc;
152 struct regulator *vcc_aux;
154 resource_size_t mapbase;
155 spinlock_t irq_lock; /* Prevent races with irq handler */
156 unsigned int dma_len;
157 unsigned int dma_sg_idx;
158 unsigned char bus_mode;
159 unsigned char power_mode;
165 int dma_line_tx, dma_line_rx;
175 struct omap_hsmmc_next next_data;
177 struct omap_mmc_platform_data *pdata;
180 static int omap_hsmmc_card_detect(struct device *dev, int slot)
182 struct omap_mmc_platform_data *mmc = dev->platform_data;
184 /* NOTE: assumes card detect signal is active-low */
185 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
188 static int omap_hsmmc_get_wp(struct device *dev, int slot)
190 struct omap_mmc_platform_data *mmc = dev->platform_data;
192 /* NOTE: assumes write protect signal is active-high */
193 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
196 static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
198 struct omap_mmc_platform_data *mmc = dev->platform_data;
200 /* NOTE: assumes card detect signal is active-low */
201 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
206 static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
208 struct omap_mmc_platform_data *mmc = dev->platform_data;
210 disable_irq(mmc->slots[0].card_detect_irq);
214 static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
216 struct omap_mmc_platform_data *mmc = dev->platform_data;
218 enable_irq(mmc->slots[0].card_detect_irq);
224 #define omap_hsmmc_suspend_cdirq NULL
225 #define omap_hsmmc_resume_cdirq NULL
229 #ifdef CONFIG_REGULATOR
231 static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
234 struct omap_hsmmc_host *host =
235 platform_get_drvdata(to_platform_device(dev));
239 * If we don't see a Vcc regulator, assume it's a fixed
240 * voltage always-on regulator.
245 if (mmc_slot(host).before_set_reg)
246 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
249 * Assume Vcc regulator is used only to power the card ... OMAP
250 * VDDS is used to power the pins, optionally with a transceiver to
251 * support cards using voltages other than VDDS (1.8V nominal). When a
252 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
254 * In some cases this regulator won't support enable/disable;
255 * e.g. it's a fixed rail for a WLAN chip.
257 * In other cases vcc_aux switches interface power. Example, for
258 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
259 * chips/cards need an interface voltage rail too.
262 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
263 /* Enable interface voltage rail, if needed */
264 if (ret == 0 && host->vcc_aux) {
265 ret = regulator_enable(host->vcc_aux);
267 ret = mmc_regulator_set_ocr(host->mmc,
271 /* Shut down the rail */
273 ret = regulator_disable(host->vcc_aux);
275 /* Then proceed to shut down the local regulator */
276 ret = mmc_regulator_set_ocr(host->mmc,
281 if (mmc_slot(host).after_set_reg)
282 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
287 static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
289 struct regulator *reg;
292 mmc_slot(host).set_power = omap_hsmmc_set_power;
294 reg = regulator_get(host->dev, "vmmc");
296 dev_dbg(host->dev, "vmmc regulator missing\n");
299 ocr_value = mmc_regulator_get_ocrmask(reg);
300 if (!mmc_slot(host).ocr_mask) {
301 mmc_slot(host).ocr_mask = ocr_value;
303 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
304 dev_err(host->dev, "ocrmask %x is not supported\n",
305 mmc_slot(host).ocr_mask);
306 mmc_slot(host).ocr_mask = 0;
311 /* Allow an aux regulator */
312 reg = regulator_get(host->dev, "vmmc_aux");
313 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
315 /* For eMMC do not power off when not in sleep state */
316 if (mmc_slot(host).no_regulator_off_init)
319 * UGLY HACK: workaround regulator framework bugs.
320 * When the bootloader leaves a supply active, it's
321 * initialized with zero usecount ... and we can't
322 * disable it without first enabling it. Until the
323 * framework is fixed, we need a workaround like this
324 * (which is safe for MMC, but not in general).
326 if (regulator_is_enabled(host->vcc) > 0 ||
327 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
328 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
330 mmc_slot(host).set_power(host->dev, host->slot_id,
332 mmc_slot(host).set_power(host->dev, host->slot_id,
340 static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
342 regulator_put(host->vcc);
343 regulator_put(host->vcc_aux);
344 mmc_slot(host).set_power = NULL;
347 static inline int omap_hsmmc_have_reg(void)
354 static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
359 static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
363 static inline int omap_hsmmc_have_reg(void)
370 static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
374 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
375 if (pdata->slots[0].cover)
376 pdata->slots[0].get_cover_state =
377 omap_hsmmc_get_cover_state;
379 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
380 pdata->slots[0].card_detect_irq =
381 gpio_to_irq(pdata->slots[0].switch_pin);
382 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
385 ret = gpio_direction_input(pdata->slots[0].switch_pin);
389 pdata->slots[0].switch_pin = -EINVAL;
391 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
392 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
393 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
396 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
400 pdata->slots[0].gpio_wp = -EINVAL;
405 gpio_free(pdata->slots[0].gpio_wp);
407 if (gpio_is_valid(pdata->slots[0].switch_pin))
409 gpio_free(pdata->slots[0].switch_pin);
413 static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
415 if (gpio_is_valid(pdata->slots[0].gpio_wp))
416 gpio_free(pdata->slots[0].gpio_wp);
417 if (gpio_is_valid(pdata->slots[0].switch_pin))
418 gpio_free(pdata->slots[0].switch_pin);
422 * Start clock to the card
424 static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
426 OMAP_HSMMC_WRITE(host->base, SYSCTL,
427 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
431 * Stop clock to the card
433 static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
435 OMAP_HSMMC_WRITE(host->base, SYSCTL,
436 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
437 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
438 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
441 static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
442 struct mmc_command *cmd)
444 unsigned int irq_mask;
447 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
449 irq_mask = INT_EN_MASK;
451 /* Disable timeout for erases */
452 if (cmd->opcode == MMC_ERASE)
453 irq_mask &= ~DTO_ENABLE;
455 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
456 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
457 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
460 static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
462 OMAP_HSMMC_WRITE(host->base, ISE, 0);
463 OMAP_HSMMC_WRITE(host->base, IE, 0);
464 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
467 /* Calculate divisor for the given clock frequency */
468 static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
473 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
481 static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
483 struct mmc_ios *ios = &host->mmc->ios;
484 unsigned long regval;
485 unsigned long timeout;
487 dev_dbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
489 omap_hsmmc_stop_clock(host);
491 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
492 regval = regval & ~(CLKD_MASK | DTO_MASK);
493 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
494 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
495 OMAP_HSMMC_WRITE(host->base, SYSCTL,
496 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
498 /* Wait till the ICS bit is set */
499 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
500 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
501 && time_before(jiffies, timeout))
504 omap_hsmmc_start_clock(host);
507 static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
509 struct mmc_ios *ios = &host->mmc->ios;
512 con = OMAP_HSMMC_READ(host->base, CON);
513 switch (ios->bus_width) {
514 case MMC_BUS_WIDTH_8:
515 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
517 case MMC_BUS_WIDTH_4:
518 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
519 OMAP_HSMMC_WRITE(host->base, HCTL,
520 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
522 case MMC_BUS_WIDTH_1:
523 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
524 OMAP_HSMMC_WRITE(host->base, HCTL,
525 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
530 static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
532 struct mmc_ios *ios = &host->mmc->ios;
535 con = OMAP_HSMMC_READ(host->base, CON);
536 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
537 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
539 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
545 * Restore the MMC host context, if it was lost as result of a
546 * power state change.
548 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
550 struct mmc_ios *ios = &host->mmc->ios;
551 struct omap_mmc_platform_data *pdata = host->pdata;
552 int context_loss = 0;
554 unsigned long timeout;
556 if (pdata->get_context_loss_count) {
557 context_loss = pdata->get_context_loss_count(host->dev);
558 if (context_loss < 0)
562 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
563 context_loss == host->context_loss ? "not " : "");
564 if (host->context_loss == context_loss)
567 /* Wait for hardware reset */
568 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
569 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
570 && time_before(jiffies, timeout))
573 /* Do software reset */
574 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
575 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
576 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
577 && time_before(jiffies, timeout))
580 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
581 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
583 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
584 if (host->power_mode != MMC_POWER_OFF &&
585 (1 << ios->vdd) <= MMC_VDD_23_24)
595 OMAP_HSMMC_WRITE(host->base, HCTL,
596 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
598 OMAP_HSMMC_WRITE(host->base, CAPA,
599 OMAP_HSMMC_READ(host->base, CAPA) | capa);
601 OMAP_HSMMC_WRITE(host->base, HCTL,
602 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
604 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
605 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
606 && time_before(jiffies, timeout))
609 omap_hsmmc_disable_irq(host);
611 /* Do not initialize card-specific things if the power is off */
612 if (host->power_mode == MMC_POWER_OFF)
615 omap_hsmmc_set_bus_width(host);
617 omap_hsmmc_set_clock(host);
619 omap_hsmmc_set_bus_mode(host);
622 host->context_loss = context_loss;
624 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
629 * Save the MMC host context (store the number of power state changes so far).
631 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
633 struct omap_mmc_platform_data *pdata = host->pdata;
636 if (pdata->get_context_loss_count) {
637 context_loss = pdata->get_context_loss_count(host->dev);
638 if (context_loss < 0)
640 host->context_loss = context_loss;
646 static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
651 static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
658 * Send init stream sequence to card
659 * before sending IDLE command
661 static void send_init_stream(struct omap_hsmmc_host *host)
664 unsigned long timeout;
666 if (host->protect_card)
669 disable_irq(host->irq);
671 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
672 OMAP_HSMMC_WRITE(host->base, CON,
673 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
674 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
676 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
677 while ((reg != CC) && time_before(jiffies, timeout))
678 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
680 OMAP_HSMMC_WRITE(host->base, CON,
681 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
683 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
684 OMAP_HSMMC_READ(host->base, STAT);
686 enable_irq(host->irq);
690 int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
694 if (mmc_slot(host).get_cover_state)
695 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
700 omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
703 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
704 struct omap_hsmmc_host *host = mmc_priv(mmc);
706 return sprintf(buf, "%s\n",
707 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
710 static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
713 omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
716 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
717 struct omap_hsmmc_host *host = mmc_priv(mmc);
719 return sprintf(buf, "%s\n", mmc_slot(host).name);
722 static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
725 * Configure the response type and send the cmd.
728 omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
729 struct mmc_data *data)
731 int cmdreg = 0, resptype = 0, cmdtype = 0;
733 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
734 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
737 omap_hsmmc_enable_irq(host, cmd);
739 host->response_busy = 0;
740 if (cmd->flags & MMC_RSP_PRESENT) {
741 if (cmd->flags & MMC_RSP_136)
743 else if (cmd->flags & MMC_RSP_BUSY) {
745 host->response_busy = 1;
751 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
752 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
753 * a val of 0x3, rest 0x0.
755 if (cmd == host->mrq->stop)
758 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
761 cmdreg |= DP_SELECT | MSBS | BCE;
762 if (data->flags & MMC_DATA_READ)
771 host->req_in_progress = 1;
773 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
774 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
778 omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
780 if (data->flags & MMC_DATA_WRITE)
781 return DMA_TO_DEVICE;
783 return DMA_FROM_DEVICE;
786 static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
790 spin_lock(&host->irq_lock);
791 host->req_in_progress = 0;
792 dma_ch = host->dma_ch;
793 spin_unlock(&host->irq_lock);
795 omap_hsmmc_disable_irq(host);
796 /* Do not complete the request if DMA is still in progress */
797 if (mrq->data && host->use_dma && dma_ch != -1)
800 mmc_request_done(host->mmc, mrq);
804 * Notify the transfer complete to MMC core
807 omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
810 struct mmc_request *mrq = host->mrq;
812 /* TC before CC from CMD6 - don't know why, but it happens */
813 if (host->cmd && host->cmd->opcode == 6 &&
814 host->response_busy) {
815 host->response_busy = 0;
819 omap_hsmmc_request_done(host, mrq);
826 data->bytes_xfered += data->blocks * (data->blksz);
828 data->bytes_xfered = 0;
831 omap_hsmmc_request_done(host, data->mrq);
834 omap_hsmmc_start_command(host, data->stop, NULL);
838 * Notify the core about command completion
841 omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
845 if (cmd->flags & MMC_RSP_PRESENT) {
846 if (cmd->flags & MMC_RSP_136) {
847 /* response type 2 */
848 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
849 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
850 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
851 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
853 /* response types 1, 1b, 3, 4, 5, 6 */
854 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
857 if ((host->data == NULL && !host->response_busy) || cmd->error)
858 omap_hsmmc_request_done(host, cmd->mrq);
862 * DMA clean up for command errors
864 static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
868 host->data->error = errno;
870 spin_lock(&host->irq_lock);
871 dma_ch = host->dma_ch;
873 spin_unlock(&host->irq_lock);
875 if (host->use_dma && dma_ch != -1) {
876 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
878 omap_hsmmc_get_dma_dir(host, host->data));
879 omap_free_dma(dma_ch);
880 host->data->host_cookie = 0;
886 * Readable error output
888 #ifdef CONFIG_MMC_DEBUG
889 static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
891 /* --- means reserved bit without definition at documentation */
892 static const char *omap_hsmmc_status_bits[] = {
893 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
894 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
895 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
896 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
902 len = sprintf(buf, "MMC IRQ 0x%x :", status);
905 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
906 if (status & (1 << i)) {
907 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
911 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
914 static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
918 #endif /* CONFIG_MMC_DEBUG */
921 * MMC controller internal state machines reset
923 * Used to reset command or data internal state machines, using respectively
924 * SRC or SRD bit of SYSCTL register
925 * Can be called from interrupt context
927 static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
931 unsigned long limit = (loops_per_jiffy *
932 msecs_to_jiffies(MMC_TIMEOUT_MS));
934 OMAP_HSMMC_WRITE(host->base, SYSCTL,
935 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
938 * OMAP4 ES2 and greater has an updated reset logic.
939 * Monitor a 0->1 transition first
941 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
942 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
948 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
952 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
953 dev_err(mmc_dev(host->mmc),
954 "Timeout waiting on controller reset in %s\n",
958 static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
960 struct mmc_data *data;
961 int end_cmd = 0, end_trans = 0;
963 if (!host->req_in_progress) {
965 OMAP_HSMMC_WRITE(host->base, STAT, status);
966 /* Flush posted write */
967 status = OMAP_HSMMC_READ(host->base, STAT);
968 } while (status & INT_EN_MASK);
973 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
976 omap_hsmmc_dbg_report_irq(host, status);
977 if ((status & CMD_TIMEOUT) ||
978 (status & CMD_CRC)) {
980 if (status & CMD_TIMEOUT) {
981 omap_hsmmc_reset_controller_fsm(host,
983 host->cmd->error = -ETIMEDOUT;
985 host->cmd->error = -EILSEQ;
989 if (host->data || host->response_busy) {
991 omap_hsmmc_dma_cleanup(host,
993 host->response_busy = 0;
994 omap_hsmmc_reset_controller_fsm(host, SRD);
997 if ((status & DATA_TIMEOUT) ||
998 (status & DATA_CRC)) {
999 if (host->data || host->response_busy) {
1000 int err = (status & DATA_TIMEOUT) ?
1001 -ETIMEDOUT : -EILSEQ;
1004 omap_hsmmc_dma_cleanup(host, err);
1006 host->mrq->cmd->error = err;
1007 host->response_busy = 0;
1008 omap_hsmmc_reset_controller_fsm(host, SRD);
1012 if (status & CARD_ERR) {
1013 dev_dbg(mmc_dev(host->mmc),
1014 "Ignoring card err CMD%d\n", host->cmd->opcode);
1022 OMAP_HSMMC_WRITE(host->base, STAT, status);
1024 if (end_cmd || ((status & CC) && host->cmd))
1025 omap_hsmmc_cmd_done(host, host->cmd);
1026 if ((end_trans || (status & TC)) && host->mrq)
1027 omap_hsmmc_xfer_done(host, data);
1031 * MMC controller IRQ handler
1033 static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1035 struct omap_hsmmc_host *host = dev_id;
1038 status = OMAP_HSMMC_READ(host->base, STAT);
1040 omap_hsmmc_do_irq(host, status);
1041 /* Flush posted write */
1042 status = OMAP_HSMMC_READ(host->base, STAT);
1043 } while (status & INT_EN_MASK);
1048 static void set_sd_bus_power(struct omap_hsmmc_host *host)
1052 OMAP_HSMMC_WRITE(host->base, HCTL,
1053 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1054 for (i = 0; i < loops_per_jiffy; i++) {
1055 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1062 * Switch MMC interface voltage ... only relevant for MMC1.
1064 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1065 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1066 * Some chips, like eMMC ones, use internal transceivers.
1068 static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
1073 /* Disable the clocks */
1074 pm_runtime_put_sync(host->dev);
1075 if (host->got_dbclk)
1076 clk_disable(host->dbclk);
1078 /* Turn the power off */
1079 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1081 /* Turn the power ON with given VDD 1.8 or 3.0v */
1083 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1085 pm_runtime_get_sync(host->dev);
1086 if (host->got_dbclk)
1087 clk_enable(host->dbclk);
1092 OMAP_HSMMC_WRITE(host->base, HCTL,
1093 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1094 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
1097 * If a MMC dual voltage card is detected, the set_ios fn calls
1098 * this fn with VDD bit set for 1.8V. Upon card removal from the
1099 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
1101 * Cope with a bit of slop in the range ... per data sheets:
1102 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1103 * but recommended values are 1.71V to 1.89V
1104 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1105 * but recommended values are 2.7V to 3.3V
1107 * Board setup code shouldn't permit anything very out-of-range.
1108 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1109 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
1111 if ((1 << vdd) <= MMC_VDD_23_24)
1116 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
1117 set_sd_bus_power(host);
1121 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1125 /* Protect the card while the cover is open */
1126 static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1128 if (!mmc_slot(host).get_cover_state)
1131 host->reqs_blocked = 0;
1132 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1133 if (host->protect_card) {
1134 dev_info(host->dev, "%s: cover is closed, "
1135 "card is now accessible\n",
1136 mmc_hostname(host->mmc));
1137 host->protect_card = 0;
1140 if (!host->protect_card) {
1141 dev_info(host->dev, "%s: cover is open, "
1142 "card is now inaccessible\n",
1143 mmc_hostname(host->mmc));
1144 host->protect_card = 1;
1150 * irq handler to notify the core about card insertion/removal
1152 static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
1154 struct omap_hsmmc_host *host = dev_id;
1155 struct omap_mmc_slot_data *slot = &mmc_slot(host);
1158 if (host->suspended)
1161 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1163 if (slot->card_detect)
1164 carddetect = slot->card_detect(host->dev, host->slot_id);
1166 omap_hsmmc_protect_card(host);
1167 carddetect = -ENOSYS;
1171 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1173 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1177 static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
1178 struct mmc_data *data)
1182 if (data->flags & MMC_DATA_WRITE)
1183 sync_dev = host->dma_line_tx;
1185 sync_dev = host->dma_line_rx;
1189 static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
1190 struct mmc_data *data,
1191 struct scatterlist *sgl)
1193 int blksz, nblk, dma_ch;
1195 dma_ch = host->dma_ch;
1196 if (data->flags & MMC_DATA_WRITE) {
1197 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1198 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1199 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1200 sg_dma_address(sgl), 0, 0);
1202 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1203 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1204 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1205 sg_dma_address(sgl), 0, 0);
1208 blksz = host->data->blksz;
1209 nblk = sg_dma_len(sgl) / blksz;
1211 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1212 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
1213 omap_hsmmc_get_dma_sync_dev(host, data),
1214 !(data->flags & MMC_DATA_WRITE));
1216 omap_start_dma(dma_ch);
1220 * DMA call back function
1222 static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
1224 struct omap_hsmmc_host *host = cb_data;
1225 struct mmc_data *data;
1226 int dma_ch, req_in_progress;
1228 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1229 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1234 spin_lock(&host->irq_lock);
1235 if (host->dma_ch < 0) {
1236 spin_unlock(&host->irq_lock);
1240 data = host->mrq->data;
1242 if (host->dma_sg_idx < host->dma_len) {
1243 /* Fire up the next transfer. */
1244 omap_hsmmc_config_dma_params(host, data,
1245 data->sg + host->dma_sg_idx);
1246 spin_unlock(&host->irq_lock);
1250 if (!data->host_cookie)
1251 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1252 omap_hsmmc_get_dma_dir(host, data));
1254 req_in_progress = host->req_in_progress;
1255 dma_ch = host->dma_ch;
1257 spin_unlock(&host->irq_lock);
1259 omap_free_dma(dma_ch);
1261 /* If DMA has finished after TC, complete the request */
1262 if (!req_in_progress) {
1263 struct mmc_request *mrq = host->mrq;
1266 mmc_request_done(host->mmc, mrq);
1270 static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1271 struct mmc_data *data,
1272 struct omap_hsmmc_next *next)
1276 if (!next && data->host_cookie &&
1277 data->host_cookie != host->next_data.cookie) {
1278 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
1279 " host->next_data.cookie %d\n",
1280 __func__, data->host_cookie, host->next_data.cookie);
1281 data->host_cookie = 0;
1284 /* Check if next job is already prepared */
1286 (!next && data->host_cookie != host->next_data.cookie)) {
1287 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1289 omap_hsmmc_get_dma_dir(host, data));
1292 dma_len = host->next_data.dma_len;
1293 host->next_data.dma_len = 0;
1301 next->dma_len = dma_len;
1302 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1304 host->dma_len = dma_len;
1310 * Routine to configure and start DMA for the MMC card
1312 static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1313 struct mmc_request *req)
1315 int dma_ch = 0, ret = 0, i;
1316 struct mmc_data *data = req->data;
1318 /* Sanity check: all the SG entries must be aligned by block size. */
1319 for (i = 0; i < data->sg_len; i++) {
1320 struct scatterlist *sgl;
1323 if (sgl->length % data->blksz)
1326 if ((data->blksz % 4) != 0)
1327 /* REVISIT: The MMC buffer increments only when MSB is written.
1328 * Return error for blksz which is non multiple of four.
1332 BUG_ON(host->dma_ch != -1);
1334 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1335 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
1337 dev_err(mmc_dev(host->mmc),
1338 "%s: omap_request_dma() failed with %d\n",
1339 mmc_hostname(host->mmc), ret);
1342 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1346 host->dma_ch = dma_ch;
1347 host->dma_sg_idx = 0;
1349 omap_hsmmc_config_dma_params(host, data, data->sg);
1354 static void set_data_timeout(struct omap_hsmmc_host *host,
1355 unsigned int timeout_ns,
1356 unsigned int timeout_clks)
1358 unsigned int timeout, cycle_ns;
1359 uint32_t reg, clkd, dto = 0;
1361 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1362 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1366 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
1367 timeout = timeout_ns / cycle_ns;
1368 timeout += timeout_clks;
1370 while ((timeout & 0x80000000) == 0) {
1387 reg |= dto << DTO_SHIFT;
1388 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1392 * Configure block length for MMC/SD cards and initiate the transfer.
1395 omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
1398 host->data = req->data;
1400 if (req->data == NULL) {
1401 OMAP_HSMMC_WRITE(host->base, BLK, 0);
1403 * Set an arbitrary 100ms data timeout for commands with
1406 if (req->cmd->flags & MMC_RSP_BUSY)
1407 set_data_timeout(host, 100000000U, 0);
1411 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1412 | (req->data->blocks << 16));
1413 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
1415 if (host->use_dma) {
1416 ret = omap_hsmmc_start_dma_transfer(host, req);
1418 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1425 static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1428 struct omap_hsmmc_host *host = mmc_priv(mmc);
1429 struct mmc_data *data = mrq->data;
1431 if (host->use_dma) {
1432 if (data->host_cookie)
1433 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1435 omap_hsmmc_get_dma_dir(host, data));
1436 data->host_cookie = 0;
1440 static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1443 struct omap_hsmmc_host *host = mmc_priv(mmc);
1445 if (mrq->data->host_cookie) {
1446 mrq->data->host_cookie = 0;
1451 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1453 mrq->data->host_cookie = 0;
1457 * Request function. for read/write operation
1459 static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
1461 struct omap_hsmmc_host *host = mmc_priv(mmc);
1464 BUG_ON(host->req_in_progress);
1465 BUG_ON(host->dma_ch != -1);
1466 if (host->protect_card) {
1467 if (host->reqs_blocked < 3) {
1469 * Ensure the controller is left in a consistent
1470 * state by resetting the command and data state
1473 omap_hsmmc_reset_controller_fsm(host, SRD);
1474 omap_hsmmc_reset_controller_fsm(host, SRC);
1475 host->reqs_blocked += 1;
1477 req->cmd->error = -EBADF;
1479 req->data->error = -EBADF;
1480 req->cmd->retries = 0;
1481 mmc_request_done(mmc, req);
1483 } else if (host->reqs_blocked)
1484 host->reqs_blocked = 0;
1485 WARN_ON(host->mrq != NULL);
1487 err = omap_hsmmc_prepare_data(host, req);
1489 req->cmd->error = err;
1491 req->data->error = err;
1493 mmc_request_done(mmc, req);
1497 omap_hsmmc_start_command(host, req->cmd, req->data);
1500 /* Routine to configure clock values. Exposed API to core */
1501 static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1503 struct omap_hsmmc_host *host = mmc_priv(mmc);
1504 int do_send_init_stream = 0;
1506 pm_runtime_get_sync(host->dev);
1508 if (ios->power_mode != host->power_mode) {
1509 switch (ios->power_mode) {
1511 mmc_slot(host).set_power(host->dev, host->slot_id,
1516 mmc_slot(host).set_power(host->dev, host->slot_id,
1518 host->vdd = ios->vdd;
1521 do_send_init_stream = 1;
1524 host->power_mode = ios->power_mode;
1527 /* FIXME: set registers based only on changes to ios */
1529 omap_hsmmc_set_bus_width(host);
1531 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1532 /* Only MMC1 can interface at 3V without some flavor
1533 * of external transceiver; but they all handle 1.8V.
1535 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1536 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1538 * The mmc_select_voltage fn of the core does
1539 * not seem to set the power_mode to
1540 * MMC_POWER_UP upon recalculating the voltage.
1543 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1544 dev_dbg(mmc_dev(host->mmc),
1545 "Switch operation failed\n");
1549 omap_hsmmc_set_clock(host);
1551 if (do_send_init_stream)
1552 send_init_stream(host);
1554 omap_hsmmc_set_bus_mode(host);
1556 pm_runtime_put_autosuspend(host->dev);
1559 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1561 struct omap_hsmmc_host *host = mmc_priv(mmc);
1563 if (!mmc_slot(host).card_detect)
1565 return mmc_slot(host).card_detect(host->dev, host->slot_id);
1568 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1570 struct omap_hsmmc_host *host = mmc_priv(mmc);
1572 if (!mmc_slot(host).get_ro)
1574 return mmc_slot(host).get_ro(host->dev, 0);
1577 static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1579 struct omap_hsmmc_host *host = mmc_priv(mmc);
1581 if (mmc_slot(host).init_card)
1582 mmc_slot(host).init_card(card);
1585 static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
1587 u32 hctl, capa, value;
1589 /* Only MMC1 supports 3.0V */
1590 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
1598 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1599 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1601 value = OMAP_HSMMC_READ(host->base, CAPA);
1602 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1604 /* Set the controller to AUTO IDLE mode */
1605 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1606 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1608 /* Set SD bus power bit */
1609 set_sd_bus_power(host);
1612 static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
1614 struct omap_hsmmc_host *host = mmc_priv(mmc);
1616 pm_runtime_get_sync(host->dev);
1621 static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
1623 struct omap_hsmmc_host *host = mmc_priv(mmc);
1625 pm_runtime_mark_last_busy(host->dev);
1626 pm_runtime_put_autosuspend(host->dev);
1631 static const struct mmc_host_ops omap_hsmmc_ops = {
1632 .enable = omap_hsmmc_enable_fclk,
1633 .disable = omap_hsmmc_disable_fclk,
1634 .post_req = omap_hsmmc_post_req,
1635 .pre_req = omap_hsmmc_pre_req,
1636 .request = omap_hsmmc_request,
1637 .set_ios = omap_hsmmc_set_ios,
1638 .get_cd = omap_hsmmc_get_cd,
1639 .get_ro = omap_hsmmc_get_ro,
1640 .init_card = omap_hsmmc_init_card,
1641 /* NYET -- enable_sdio_irq */
1644 #ifdef CONFIG_DEBUG_FS
1646 static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
1648 struct mmc_host *mmc = s->private;
1649 struct omap_hsmmc_host *host = mmc_priv(mmc);
1650 int context_loss = 0;
1652 if (host->pdata->get_context_loss_count)
1653 context_loss = host->pdata->get_context_loss_count(host->dev);
1655 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1656 mmc->index, host->context_loss, context_loss);
1658 if (host->suspended) {
1659 seq_printf(s, "host suspended, can't read registers\n");
1663 pm_runtime_get_sync(host->dev);
1665 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1666 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1667 seq_printf(s, "CON:\t\t0x%08x\n",
1668 OMAP_HSMMC_READ(host->base, CON));
1669 seq_printf(s, "HCTL:\t\t0x%08x\n",
1670 OMAP_HSMMC_READ(host->base, HCTL));
1671 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1672 OMAP_HSMMC_READ(host->base, SYSCTL));
1673 seq_printf(s, "IE:\t\t0x%08x\n",
1674 OMAP_HSMMC_READ(host->base, IE));
1675 seq_printf(s, "ISE:\t\t0x%08x\n",
1676 OMAP_HSMMC_READ(host->base, ISE));
1677 seq_printf(s, "CAPA:\t\t0x%08x\n",
1678 OMAP_HSMMC_READ(host->base, CAPA));
1680 pm_runtime_mark_last_busy(host->dev);
1681 pm_runtime_put_autosuspend(host->dev);
1686 static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
1688 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
1691 static const struct file_operations mmc_regs_fops = {
1692 .open = omap_hsmmc_regs_open,
1694 .llseek = seq_lseek,
1695 .release = single_release,
1698 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1700 if (mmc->debugfs_root)
1701 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1702 mmc, &mmc_regs_fops);
1707 static void omap_hsmmc_debugfs(struct mmc_host *mmc)
1713 static int __init omap_hsmmc_probe(struct platform_device *pdev)
1715 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1716 struct mmc_host *mmc;
1717 struct omap_hsmmc_host *host = NULL;
1718 struct resource *res;
1721 if (pdata == NULL) {
1722 dev_err(&pdev->dev, "Platform Data is missing\n");
1726 if (pdata->nr_slots == 0) {
1727 dev_err(&pdev->dev, "No Slots\n");
1731 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1732 irq = platform_get_irq(pdev, 0);
1733 if (res == NULL || irq < 0)
1736 res->start += pdata->reg_offset;
1737 res->end += pdata->reg_offset;
1738 res = request_mem_region(res->start, resource_size(res), pdev->name);
1742 ret = omap_hsmmc_gpio_init(pdata);
1746 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
1752 host = mmc_priv(mmc);
1754 host->pdata = pdata;
1755 host->dev = &pdev->dev;
1757 host->dev->dma_mask = &pdata->dma_mask;
1761 host->mapbase = res->start;
1762 host->base = ioremap(host->mapbase, SZ_4K);
1763 host->power_mode = MMC_POWER_OFF;
1764 host->next_data.cookie = 1;
1766 platform_set_drvdata(pdev, host);
1768 mmc->ops = &omap_hsmmc_ops;
1771 * If regulator_disable can only put vcc_aux to sleep then there is
1774 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1775 mmc_slot(host).no_off = 1;
1777 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1779 if (pdata->max_freq > 0)
1780 mmc->f_max = pdata->max_freq;
1782 mmc->f_max = OMAP_MMC_MAX_CLOCK;
1784 spin_lock_init(&host->irq_lock);
1786 host->fclk = clk_get(&pdev->dev, "fck");
1787 if (IS_ERR(host->fclk)) {
1788 ret = PTR_ERR(host->fclk);
1793 omap_hsmmc_context_save(host);
1795 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1796 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1797 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1800 pm_runtime_enable(host->dev);
1801 pm_runtime_get_sync(host->dev);
1802 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1803 pm_runtime_use_autosuspend(host->dev);
1805 if (cpu_is_omap2430()) {
1806 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1808 * MMC can still work without debounce clock.
1810 if (IS_ERR(host->dbclk))
1811 dev_warn(mmc_dev(host->mmc),
1812 "Failed to get debounce clock\n");
1814 host->got_dbclk = 1;
1816 if (host->got_dbclk)
1817 if (clk_enable(host->dbclk) != 0)
1818 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1822 /* Since we do only SG emulation, we can have as many segs
1824 mmc->max_segs = 1024;
1826 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1827 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1828 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1829 mmc->max_seg_size = mmc->max_req_size;
1831 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1832 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
1834 mmc->caps |= mmc_slot(host).caps;
1835 if (mmc->caps & MMC_CAP_8_BIT_DATA)
1836 mmc->caps |= MMC_CAP_4_BIT_DATA;
1838 if (mmc_slot(host).nonremovable)
1839 mmc->caps |= MMC_CAP_NONREMOVABLE;
1841 mmc->pm_caps = mmc_slot(host).pm_caps;
1843 omap_hsmmc_conf_bus_power(host);
1845 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1847 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1850 host->dma_line_tx = res->start;
1852 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1854 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1857 host->dma_line_rx = res->start;
1859 /* Request IRQ for MMC operations */
1860 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
1861 mmc_hostname(mmc), host);
1863 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1867 if (pdata->init != NULL) {
1868 if (pdata->init(&pdev->dev) != 0) {
1869 dev_dbg(mmc_dev(host->mmc),
1870 "Unable to configure MMC IRQs\n");
1871 goto err_irq_cd_init;
1875 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
1876 ret = omap_hsmmc_reg_get(host);
1882 mmc->ocr_avail = mmc_slot(host).ocr_mask;
1884 /* Request IRQ for card detect */
1885 if ((mmc_slot(host).card_detect_irq)) {
1886 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1889 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1890 mmc_hostname(mmc), host);
1892 dev_dbg(mmc_dev(host->mmc),
1893 "Unable to grab MMC CD IRQ\n");
1896 pdata->suspend = omap_hsmmc_suspend_cdirq;
1897 pdata->resume = omap_hsmmc_resume_cdirq;
1900 omap_hsmmc_disable_irq(host);
1902 omap_hsmmc_protect_card(host);
1906 if (mmc_slot(host).name != NULL) {
1907 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1911 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
1912 ret = device_create_file(&mmc->class_dev,
1913 &dev_attr_cover_switch);
1918 omap_hsmmc_debugfs(mmc);
1919 pm_runtime_mark_last_busy(host->dev);
1920 pm_runtime_put_autosuspend(host->dev);
1925 mmc_remove_host(mmc);
1926 free_irq(mmc_slot(host).card_detect_irq, host);
1929 omap_hsmmc_reg_put(host);
1931 if (host->pdata->cleanup)
1932 host->pdata->cleanup(&pdev->dev);
1934 free_irq(host->irq, host);
1936 pm_runtime_mark_last_busy(host->dev);
1937 pm_runtime_put_autosuspend(host->dev);
1938 pm_runtime_disable(host->dev);
1939 clk_put(host->fclk);
1940 if (host->got_dbclk) {
1941 clk_disable(host->dbclk);
1942 clk_put(host->dbclk);
1945 iounmap(host->base);
1946 platform_set_drvdata(pdev, NULL);
1949 omap_hsmmc_gpio_free(pdata);
1951 release_mem_region(res->start, resource_size(res));
1955 static int omap_hsmmc_remove(struct platform_device *pdev)
1957 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
1958 struct resource *res;
1961 pm_runtime_get_sync(host->dev);
1962 mmc_remove_host(host->mmc);
1964 omap_hsmmc_reg_put(host);
1965 if (host->pdata->cleanup)
1966 host->pdata->cleanup(&pdev->dev);
1967 free_irq(host->irq, host);
1968 if (mmc_slot(host).card_detect_irq)
1969 free_irq(mmc_slot(host).card_detect_irq, host);
1971 pm_runtime_put_sync(host->dev);
1972 pm_runtime_disable(host->dev);
1973 clk_put(host->fclk);
1974 if (host->got_dbclk) {
1975 clk_disable(host->dbclk);
1976 clk_put(host->dbclk);
1979 mmc_free_host(host->mmc);
1980 iounmap(host->base);
1981 omap_hsmmc_gpio_free(pdev->dev.platform_data);
1984 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1986 release_mem_region(res->start, resource_size(res));
1987 platform_set_drvdata(pdev, NULL);
1993 static int omap_hsmmc_suspend(struct device *dev)
1996 struct platform_device *pdev = to_platform_device(dev);
1997 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
1999 if (host && host->suspended)
2003 pm_runtime_get_sync(host->dev);
2004 host->suspended = 1;
2005 if (host->pdata->suspend) {
2006 ret = host->pdata->suspend(&pdev->dev,
2009 dev_dbg(mmc_dev(host->mmc),
2010 "Unable to handle MMC board"
2011 " level suspend\n");
2012 host->suspended = 0;
2016 ret = mmc_suspend_host(host->mmc);
2019 host->suspended = 0;
2020 if (host->pdata->resume) {
2021 ret = host->pdata->resume(&pdev->dev,
2024 dev_dbg(mmc_dev(host->mmc),
2025 "Unmask interrupt failed\n");
2030 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2031 omap_hsmmc_disable_irq(host);
2032 OMAP_HSMMC_WRITE(host->base, HCTL,
2033 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2035 if (host->got_dbclk)
2036 clk_disable(host->dbclk);
2040 pm_runtime_put_sync(host->dev);
2044 /* Routine to resume the MMC device */
2045 static int omap_hsmmc_resume(struct device *dev)
2048 struct platform_device *pdev = to_platform_device(dev);
2049 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
2051 if (host && !host->suspended)
2055 pm_runtime_get_sync(host->dev);
2057 if (host->got_dbclk)
2058 clk_enable(host->dbclk);
2060 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2061 omap_hsmmc_conf_bus_power(host);
2063 if (host->pdata->resume) {
2064 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2066 dev_dbg(mmc_dev(host->mmc),
2067 "Unmask interrupt failed\n");
2070 omap_hsmmc_protect_card(host);
2072 /* Notify the core to resume the host */
2073 ret = mmc_resume_host(host->mmc);
2075 host->suspended = 0;
2077 pm_runtime_mark_last_busy(host->dev);
2078 pm_runtime_put_autosuspend(host->dev);
2086 #define omap_hsmmc_suspend NULL
2087 #define omap_hsmmc_resume NULL
2090 static int omap_hsmmc_runtime_suspend(struct device *dev)
2092 struct omap_hsmmc_host *host;
2094 host = platform_get_drvdata(to_platform_device(dev));
2095 omap_hsmmc_context_save(host);
2096 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2101 static int omap_hsmmc_runtime_resume(struct device *dev)
2103 struct omap_hsmmc_host *host;
2105 host = platform_get_drvdata(to_platform_device(dev));
2106 omap_hsmmc_context_restore(host);
2107 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2112 static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
2113 .suspend = omap_hsmmc_suspend,
2114 .resume = omap_hsmmc_resume,
2115 .runtime_suspend = omap_hsmmc_runtime_suspend,
2116 .runtime_resume = omap_hsmmc_runtime_resume,
2119 static struct platform_driver omap_hsmmc_driver = {
2120 .remove = omap_hsmmc_remove,
2122 .name = DRIVER_NAME,
2123 .owner = THIS_MODULE,
2124 .pm = &omap_hsmmc_dev_pm_ops,
2128 static int __init omap_hsmmc_init(void)
2130 /* Register the MMC driver */
2131 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
2134 static void __exit omap_hsmmc_cleanup(void)
2136 /* Unregister MMC driver */
2137 platform_driver_unregister(&omap_hsmmc_driver);
2140 module_init(omap_hsmmc_init);
2141 module_exit(omap_hsmmc_cleanup);
2143 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2144 MODULE_LICENSE("GPL");
2145 MODULE_ALIAS("platform:" DRIVER_NAME);
2146 MODULE_AUTHOR("Texas Instruments Inc");