2 * drivers/mmc/host/omap_hsmmc.c
4 * Driver for OMAP2430/3430 MMC controller.
6 * Copyright (C) 2007 Texas Instruments.
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/seq_file.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/platform_device.h>
26 #include <linux/workqueue.h>
27 #include <linux/timer.h>
28 #include <linux/clk.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/core.h>
32 #include <linux/semaphore.h>
34 #include <mach/hardware.h>
35 #include <mach/board.h>
39 /* OMAP HSMMC Host Controller Registers */
40 #define OMAP_HSMMC_SYSCONFIG 0x0010
41 #define OMAP_HSMMC_SYSSTATUS 0x0014
42 #define OMAP_HSMMC_CON 0x002C
43 #define OMAP_HSMMC_BLK 0x0104
44 #define OMAP_HSMMC_ARG 0x0108
45 #define OMAP_HSMMC_CMD 0x010C
46 #define OMAP_HSMMC_RSP10 0x0110
47 #define OMAP_HSMMC_RSP32 0x0114
48 #define OMAP_HSMMC_RSP54 0x0118
49 #define OMAP_HSMMC_RSP76 0x011C
50 #define OMAP_HSMMC_DATA 0x0120
51 #define OMAP_HSMMC_HCTL 0x0128
52 #define OMAP_HSMMC_SYSCTL 0x012C
53 #define OMAP_HSMMC_STAT 0x0130
54 #define OMAP_HSMMC_IE 0x0134
55 #define OMAP_HSMMC_ISE 0x0138
56 #define OMAP_HSMMC_CAPA 0x0140
58 #define VS18 (1 << 26)
59 #define VS30 (1 << 25)
60 #define SDVS18 (0x5 << 9)
61 #define SDVS30 (0x6 << 9)
62 #define SDVS33 (0x7 << 9)
63 #define SDVS_MASK 0x00000E00
64 #define SDVSCLR 0xFFFFF1FF
65 #define SDVSDET 0x00000400
72 #define CLKD_MASK 0x0000FFC0
74 #define DTO_MASK 0x000F0000
76 #define INT_EN_MASK 0x307F0033
77 #define BWR_ENABLE (1 << 4)
78 #define BRR_ENABLE (1 << 5)
79 #define INIT_STREAM (1 << 1)
80 #define DP_SELECT (1 << 21)
85 #define FOUR_BIT (1 << 1)
91 #define CMD_TIMEOUT (1 << 16)
92 #define DATA_TIMEOUT (1 << 20)
93 #define CMD_CRC (1 << 17)
94 #define DATA_CRC (1 << 21)
95 #define CARD_ERR (1 << 28)
96 #define STAT_CLEAR 0xFFFFFFFF
97 #define INIT_STREAM_CMD 0x00000000
98 #define DUAL_VOLT_OCR_BIT 7
100 #define SRD (1 << 26)
101 #define SOFTRESET (1 << 1)
102 #define RESETDONE (1 << 0)
105 * FIXME: Most likely all the data using these _DEVID defines should come
106 * from the platform_data, or implemented in controller and slot specific
109 #define OMAP_MMC1_DEVID 0
110 #define OMAP_MMC2_DEVID 1
111 #define OMAP_MMC3_DEVID 2
113 #define MMC_TIMEOUT_MS 20
114 #define OMAP_MMC_MASTER_CLOCK 96000000
115 #define DRIVER_NAME "mmci-omap-hs"
117 /* Timeouts for entering power saving states on inactivity, msec */
118 #define OMAP_MMC_DISABLED_TIMEOUT 100
119 #define OMAP_MMC_SLEEP_TIMEOUT 1000
120 #define OMAP_MMC_OFF_TIMEOUT 8000
123 * One controller can have multiple slots, like on some omap boards using
124 * omap.c controller driver. Luckily this is not currently done on any known
125 * omap_hsmmc.c device.
127 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
130 * MMC Host controller read/write API's
132 #define OMAP_HSMMC_READ(base, reg) \
133 __raw_readl((base) + OMAP_HSMMC_##reg)
135 #define OMAP_HSMMC_WRITE(base, reg, val) \
136 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
138 struct mmc_omap_host {
140 struct mmc_host *mmc;
141 struct mmc_request *mrq;
142 struct mmc_command *cmd;
143 struct mmc_data *data;
147 struct semaphore sem;
148 struct work_struct mmc_carddetect_work;
150 resource_size_t mapbase;
152 unsigned int dma_len;
153 unsigned int dma_sg_idx;
154 unsigned char bus_mode;
155 unsigned char power_mode;
161 int dma_line_tx, dma_line_rx;
169 struct omap_mmc_platform_data *pdata;
173 * Stop clock to the card
175 static void omap_mmc_stop_clock(struct mmc_omap_host *host)
177 OMAP_HSMMC_WRITE(host->base, SYSCTL,
178 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
179 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
180 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
186 * Restore the MMC host context, if it was lost as result of a
187 * power state change.
189 static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
191 struct mmc_ios *ios = &host->mmc->ios;
192 struct omap_mmc_platform_data *pdata = host->pdata;
193 int context_loss = 0;
196 unsigned long timeout;
198 if (pdata->get_context_loss_count) {
199 context_loss = pdata->get_context_loss_count(host->dev);
200 if (context_loss < 0)
204 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
205 context_loss == host->context_loss ? "not " : "");
206 if (host->context_loss == context_loss)
209 /* Wait for hardware reset */
210 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
211 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
212 && time_before(jiffies, timeout))
215 /* Do software reset */
216 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
217 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
218 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
219 && time_before(jiffies, timeout))
222 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
223 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
225 if (host->id == OMAP_MMC1_DEVID) {
226 if (host->power_mode != MMC_POWER_OFF &&
227 (1 << ios->vdd) <= MMC_VDD_23_24)
237 OMAP_HSMMC_WRITE(host->base, HCTL,
238 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
240 OMAP_HSMMC_WRITE(host->base, CAPA,
241 OMAP_HSMMC_READ(host->base, CAPA) | capa);
243 OMAP_HSMMC_WRITE(host->base, HCTL,
244 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
246 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
247 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
248 && time_before(jiffies, timeout))
251 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
252 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
253 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
255 /* Do not initialize card-specific things if the power is off */
256 if (host->power_mode == MMC_POWER_OFF)
259 con = OMAP_HSMMC_READ(host->base, CON);
260 switch (ios->bus_width) {
261 case MMC_BUS_WIDTH_8:
262 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
264 case MMC_BUS_WIDTH_4:
265 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
266 OMAP_HSMMC_WRITE(host->base, HCTL,
267 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
269 case MMC_BUS_WIDTH_1:
270 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
271 OMAP_HSMMC_WRITE(host->base, HCTL,
272 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
277 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
281 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
288 OMAP_HSMMC_WRITE(host->base, SYSCTL,
289 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
290 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
291 OMAP_HSMMC_WRITE(host->base, SYSCTL,
292 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
294 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
295 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
296 && time_before(jiffies, timeout))
299 OMAP_HSMMC_WRITE(host->base, SYSCTL,
300 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
302 con = OMAP_HSMMC_READ(host->base, CON);
303 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
304 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
306 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
308 host->context_loss = context_loss;
310 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
315 * Save the MMC host context (store the number of power state changes so far).
317 static void omap_mmc_save_ctx(struct mmc_omap_host *host)
319 struct omap_mmc_platform_data *pdata = host->pdata;
322 if (pdata->get_context_loss_count) {
323 context_loss = pdata->get_context_loss_count(host->dev);
324 if (context_loss < 0)
326 host->context_loss = context_loss;
332 static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
337 static void omap_mmc_save_ctx(struct mmc_omap_host *host)
344 * Send init stream sequence to card
345 * before sending IDLE command
347 static void send_init_stream(struct mmc_omap_host *host)
350 unsigned long timeout;
352 disable_irq(host->irq);
353 OMAP_HSMMC_WRITE(host->base, CON,
354 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
355 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
357 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
358 while ((reg != CC) && time_before(jiffies, timeout))
359 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
361 OMAP_HSMMC_WRITE(host->base, CON,
362 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
364 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
365 OMAP_HSMMC_READ(host->base, STAT);
367 enable_irq(host->irq);
371 int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
375 if (mmc_slot(host).get_cover_state)
376 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
381 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
384 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
385 struct mmc_omap_host *host = mmc_priv(mmc);
387 return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
391 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
394 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
397 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
398 struct mmc_omap_host *host = mmc_priv(mmc);
400 return sprintf(buf, "%s\n", mmc_slot(host).name);
403 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
406 * Configure the response type and send the cmd.
409 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
410 struct mmc_data *data)
412 int cmdreg = 0, resptype = 0, cmdtype = 0;
414 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
415 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
419 * Clear status bits and enable interrupts
421 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
422 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
425 OMAP_HSMMC_WRITE(host->base, IE,
426 INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
428 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
430 host->response_busy = 0;
431 if (cmd->flags & MMC_RSP_PRESENT) {
432 if (cmd->flags & MMC_RSP_136)
434 else if (cmd->flags & MMC_RSP_BUSY) {
436 host->response_busy = 1;
442 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
443 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
444 * a val of 0x3, rest 0x0.
446 if (cmd == host->mrq->stop)
449 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
452 cmdreg |= DP_SELECT | MSBS | BCE;
453 if (data->flags & MMC_DATA_READ)
462 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
463 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
467 mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
469 if (data->flags & MMC_DATA_WRITE)
470 return DMA_TO_DEVICE;
472 return DMA_FROM_DEVICE;
476 * Notify the transfer complete to MMC core
479 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
482 struct mmc_request *mrq = host->mrq;
484 /* TC before CC from CMD6 - don't know why, but it happens */
485 if (host->cmd && host->cmd->opcode == 6 &&
486 host->response_busy) {
487 host->response_busy = 0;
492 mmc_request_done(host->mmc, mrq);
498 if (host->use_dma && host->dma_ch != -1)
499 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
500 mmc_omap_get_dma_dir(host, data));
503 data->bytes_xfered += data->blocks * (data->blksz);
505 data->bytes_xfered = 0;
509 mmc_request_done(host->mmc, data->mrq);
512 mmc_omap_start_command(host, data->stop, NULL);
516 * Notify the core about command completion
519 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
523 if (cmd->flags & MMC_RSP_PRESENT) {
524 if (cmd->flags & MMC_RSP_136) {
525 /* response type 2 */
526 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
527 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
528 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
529 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
531 /* response types 1, 1b, 3, 4, 5, 6 */
532 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
535 if ((host->data == NULL && !host->response_busy) || cmd->error) {
537 mmc_request_done(host->mmc, cmd->mrq);
542 * DMA clean up for command errors
544 static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
546 host->data->error = errno;
548 if (host->use_dma && host->dma_ch != -1) {
549 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
550 mmc_omap_get_dma_dir(host, host->data));
551 omap_free_dma(host->dma_ch);
559 * Readable error output
561 #ifdef CONFIG_MMC_DEBUG
562 static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
564 /* --- means reserved bit without definition at documentation */
565 static const char *mmc_omap_status_bits[] = {
566 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
567 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
568 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
569 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
575 len = sprintf(buf, "MMC IRQ 0x%x :", status);
578 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
579 if (status & (1 << i)) {
580 len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
584 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
586 #endif /* CONFIG_MMC_DEBUG */
589 * MMC controller internal state machines reset
591 * Used to reset command or data internal state machines, using respectively
592 * SRC or SRD bit of SYSCTL register
593 * Can be called from interrupt context
595 static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
599 unsigned long limit = (loops_per_jiffy *
600 msecs_to_jiffies(MMC_TIMEOUT_MS));
602 OMAP_HSMMC_WRITE(host->base, SYSCTL,
603 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
605 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
609 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
610 dev_err(mmc_dev(host->mmc),
611 "Timeout waiting on controller reset in %s\n",
616 * MMC controller IRQ handler
618 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
620 struct mmc_omap_host *host = dev_id;
621 struct mmc_data *data;
622 int end_cmd = 0, end_trans = 0, status;
624 if (host->mrq == NULL) {
625 OMAP_HSMMC_WRITE(host->base, STAT,
626 OMAP_HSMMC_READ(host->base, STAT));
627 /* Flush posted write */
628 OMAP_HSMMC_READ(host->base, STAT);
633 status = OMAP_HSMMC_READ(host->base, STAT);
634 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
637 #ifdef CONFIG_MMC_DEBUG
638 mmc_omap_report_irq(host, status);
640 if ((status & CMD_TIMEOUT) ||
641 (status & CMD_CRC)) {
643 if (status & CMD_TIMEOUT) {
644 mmc_omap_reset_controller_fsm(host,
646 host->cmd->error = -ETIMEDOUT;
648 host->cmd->error = -EILSEQ;
652 if (host->data || host->response_busy) {
654 mmc_dma_cleanup(host, -ETIMEDOUT);
655 host->response_busy = 0;
656 mmc_omap_reset_controller_fsm(host, SRD);
659 if ((status & DATA_TIMEOUT) ||
660 (status & DATA_CRC)) {
661 if (host->data || host->response_busy) {
662 int err = (status & DATA_TIMEOUT) ?
663 -ETIMEDOUT : -EILSEQ;
666 mmc_dma_cleanup(host, err);
668 host->mrq->cmd->error = err;
669 host->response_busy = 0;
670 mmc_omap_reset_controller_fsm(host, SRD);
674 if (status & CARD_ERR) {
675 dev_dbg(mmc_dev(host->mmc),
676 "Ignoring card err CMD%d\n", host->cmd->opcode);
684 OMAP_HSMMC_WRITE(host->base, STAT, status);
685 /* Flush posted write */
686 OMAP_HSMMC_READ(host->base, STAT);
688 if (end_cmd || ((status & CC) && host->cmd))
689 mmc_omap_cmd_done(host, host->cmd);
690 if ((end_trans || (status & TC)) && host->mrq)
691 mmc_omap_xfer_done(host, data);
696 static void set_sd_bus_power(struct mmc_omap_host *host)
700 OMAP_HSMMC_WRITE(host->base, HCTL,
701 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
702 for (i = 0; i < loops_per_jiffy; i++) {
703 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
710 * Switch MMC interface voltage ... only relevant for MMC1.
712 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
713 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
714 * Some chips, like eMMC ones, use internal transceivers.
716 static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
721 /* Disable the clocks */
722 clk_disable(host->fclk);
723 clk_disable(host->iclk);
724 clk_disable(host->dbclk);
726 /* Turn the power off */
727 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
731 /* Turn the power ON with given VDD 1.8 or 3.0v */
732 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
736 clk_enable(host->fclk);
737 clk_enable(host->iclk);
738 clk_enable(host->dbclk);
740 OMAP_HSMMC_WRITE(host->base, HCTL,
741 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
742 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
745 * If a MMC dual voltage card is detected, the set_ios fn calls
746 * this fn with VDD bit set for 1.8V. Upon card removal from the
747 * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
749 * Cope with a bit of slop in the range ... per data sheets:
750 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
751 * but recommended values are 1.71V to 1.89V
752 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
753 * but recommended values are 2.7V to 3.3V
755 * Board setup code shouldn't permit anything very out-of-range.
756 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
757 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
759 if ((1 << vdd) <= MMC_VDD_23_24)
764 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
765 set_sd_bus_power(host);
769 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
774 * Work Item to notify the core about card insertion/removal
776 static void mmc_omap_detect(struct work_struct *work)
778 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
779 mmc_carddetect_work);
780 struct omap_mmc_slot_data *slot = &mmc_slot(host);
786 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
788 if (slot->card_detect)
789 carddetect = slot->card_detect(slot->card_detect_irq);
791 carddetect = -ENOSYS;
794 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
796 mmc_host_enable(host->mmc);
797 mmc_omap_reset_controller_fsm(host, SRD);
798 mmc_host_lazy_disable(host->mmc);
799 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
804 * ISR for handling card insertion and removal
806 static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
808 struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
812 schedule_work(&host->mmc_carddetect_work);
817 static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
818 struct mmc_data *data)
822 if (data->flags & MMC_DATA_WRITE)
823 sync_dev = host->dma_line_tx;
825 sync_dev = host->dma_line_rx;
829 static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
830 struct mmc_data *data,
831 struct scatterlist *sgl)
833 int blksz, nblk, dma_ch;
835 dma_ch = host->dma_ch;
836 if (data->flags & MMC_DATA_WRITE) {
837 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
838 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
839 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
840 sg_dma_address(sgl), 0, 0);
842 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
843 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
844 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
845 sg_dma_address(sgl), 0, 0);
848 blksz = host->data->blksz;
849 nblk = sg_dma_len(sgl) / blksz;
851 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
852 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
853 mmc_omap_get_dma_sync_dev(host, data),
854 !(data->flags & MMC_DATA_WRITE));
856 omap_start_dma(dma_ch);
860 * DMA call back function
862 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
864 struct mmc_omap_host *host = data;
866 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
867 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
869 if (host->dma_ch < 0)
873 if (host->dma_sg_idx < host->dma_len) {
874 /* Fire up the next transfer. */
875 mmc_omap_config_dma_params(host, host->data,
876 host->data->sg + host->dma_sg_idx);
880 omap_free_dma(host->dma_ch);
883 * DMA Callback: run in interrupt context.
884 * mutex_unlock will throw a kernel warning if used.
890 * Routine to configure and start DMA for the MMC card
893 mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
895 int dma_ch = 0, ret = 0, err = 1, i;
896 struct mmc_data *data = req->data;
898 /* Sanity check: all the SG entries must be aligned by block size. */
899 for (i = 0; i < data->sg_len; i++) {
900 struct scatterlist *sgl;
903 if (sgl->length % data->blksz)
906 if ((data->blksz % 4) != 0)
907 /* REVISIT: The MMC buffer increments only when MSB is written.
908 * Return error for blksz which is non multiple of four.
913 * If for some reason the DMA transfer is still active,
914 * we wait for timeout period and free the dma
916 if (host->dma_ch != -1) {
917 set_current_state(TASK_UNINTERRUPTIBLE);
918 schedule_timeout(100);
919 if (down_trylock(&host->sem)) {
920 omap_free_dma(host->dma_ch);
926 if (down_trylock(&host->sem))
930 ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
931 mmc_omap_dma_cb, host, &dma_ch);
933 dev_err(mmc_dev(host->mmc),
934 "%s: omap_request_dma() failed with %d\n",
935 mmc_hostname(host->mmc), ret);
939 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
940 data->sg_len, mmc_omap_get_dma_dir(host, data));
941 host->dma_ch = dma_ch;
942 host->dma_sg_idx = 0;
944 mmc_omap_config_dma_params(host, data, data->sg);
949 static void set_data_timeout(struct mmc_omap_host *host,
950 struct mmc_request *req)
952 unsigned int timeout, cycle_ns;
953 uint32_t reg, clkd, dto = 0;
955 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
956 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
960 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
961 timeout = req->data->timeout_ns / cycle_ns;
962 timeout += req->data->timeout_clks;
964 while ((timeout & 0x80000000) == 0) {
981 reg |= dto << DTO_SHIFT;
982 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
986 * Configure block length for MMC/SD cards and initiate the transfer.
989 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
992 host->data = req->data;
994 if (req->data == NULL) {
995 OMAP_HSMMC_WRITE(host->base, BLK, 0);
999 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1000 | (req->data->blocks << 16));
1001 set_data_timeout(host, req);
1003 if (host->use_dma) {
1004 ret = mmc_omap_start_dma_transfer(host, req);
1006 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1014 * Request function. for read/write operation
1016 static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1018 struct mmc_omap_host *host = mmc_priv(mmc);
1021 WARN_ON(host->mrq != NULL);
1023 err = mmc_omap_prepare_data(host, req);
1025 req->cmd->error = err;
1027 req->data->error = err;
1029 mmc_request_done(mmc, req);
1033 mmc_omap_start_command(host, req->cmd, req->data);
1037 /* Routine to configure clock values. Exposed API to core */
1038 static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1040 struct mmc_omap_host *host = mmc_priv(mmc);
1042 unsigned long regval;
1043 unsigned long timeout;
1045 int do_send_init_stream = 0;
1047 mmc_host_enable(host->mmc);
1049 if (ios->power_mode != host->power_mode) {
1050 switch (ios->power_mode) {
1052 mmc_slot(host).set_power(host->dev, host->slot_id,
1057 mmc_slot(host).set_power(host->dev, host->slot_id,
1059 host->vdd = ios->vdd;
1062 do_send_init_stream = 1;
1065 host->power_mode = ios->power_mode;
1068 /* FIXME: set registers based only on changes to ios */
1070 con = OMAP_HSMMC_READ(host->base, CON);
1071 switch (mmc->ios.bus_width) {
1072 case MMC_BUS_WIDTH_8:
1073 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1075 case MMC_BUS_WIDTH_4:
1076 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
1077 OMAP_HSMMC_WRITE(host->base, HCTL,
1078 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1080 case MMC_BUS_WIDTH_1:
1081 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
1082 OMAP_HSMMC_WRITE(host->base, HCTL,
1083 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1087 if (host->id == OMAP_MMC1_DEVID) {
1088 /* Only MMC1 can interface at 3V without some flavor
1089 * of external transceiver; but they all handle 1.8V.
1091 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1092 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1094 * The mmc_select_voltage fn of the core does
1095 * not seem to set the power_mode to
1096 * MMC_POWER_UP upon recalculating the voltage.
1099 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
1100 dev_dbg(mmc_dev(host->mmc),
1101 "Switch operation failed\n");
1106 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1110 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1116 omap_mmc_stop_clock(host);
1117 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1118 regval = regval & ~(CLKD_MASK);
1119 regval = regval | (dsor << 6) | (DTO << 16);
1120 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1121 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1122 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1124 /* Wait till the ICS bit is set */
1125 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
1126 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
1127 && time_before(jiffies, timeout))
1130 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1131 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1133 if (do_send_init_stream)
1134 send_init_stream(host);
1136 con = OMAP_HSMMC_READ(host->base, CON);
1137 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1138 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1140 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
1142 if (host->power_mode == MMC_POWER_OFF)
1143 mmc_host_disable(host->mmc);
1145 mmc_host_lazy_disable(host->mmc);
1148 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1150 struct mmc_omap_host *host = mmc_priv(mmc);
1152 if (!mmc_slot(host).card_detect)
1154 return mmc_slot(host).card_detect(mmc_slot(host).card_detect_irq);
1157 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1159 struct mmc_omap_host *host = mmc_priv(mmc);
1161 if (!mmc_slot(host).get_ro)
1163 return mmc_slot(host).get_ro(host->dev, 0);
1166 static void omap_hsmmc_init(struct mmc_omap_host *host)
1168 u32 hctl, capa, value;
1170 /* Only MMC1 supports 3.0V */
1171 if (host->id == OMAP_MMC1_DEVID) {
1179 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1180 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1182 value = OMAP_HSMMC_READ(host->base, CAPA);
1183 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1185 /* Set the controller to AUTO IDLE mode */
1186 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1187 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1189 /* Set SD bus power bit */
1190 set_sd_bus_power(host);
1194 * Dynamic power saving handling, FSM:
1195 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1197 * |______________________|______________________|
1199 * ENABLED: mmc host is fully functional
1200 * DISABLED: fclk is off
1201 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1202 * REGSLEEP: fclk is off, voltage regulator is asleep
1203 * OFF: fclk is off, voltage regulator is off
1205 * Transition handlers return the timeout for the next state transition
1206 * or negative error.
1209 enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
1211 /* Handler for [ENABLED -> DISABLED] transition */
1212 static int omap_mmc_enabled_to_disabled(struct mmc_omap_host *host)
1214 omap_mmc_save_ctx(host);
1215 clk_disable(host->fclk);
1216 host->dpm_state = DISABLED;
1218 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1220 if (host->power_mode == MMC_POWER_OFF)
1223 return msecs_to_jiffies(OMAP_MMC_SLEEP_TIMEOUT);
1226 /* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
1227 static int omap_mmc_disabled_to_sleep(struct mmc_omap_host *host)
1231 if (!mmc_try_claim_host(host->mmc))
1234 clk_enable(host->fclk);
1235 omap_mmc_restore_ctx(host);
1236 if (mmc_card_can_sleep(host->mmc)) {
1237 err = mmc_card_sleep(host->mmc);
1239 clk_disable(host->fclk);
1240 mmc_release_host(host->mmc);
1243 new_state = CARDSLEEP;
1245 new_state = REGSLEEP;
1246 if (mmc_slot(host).set_sleep)
1247 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1248 new_state == CARDSLEEP);
1249 /* FIXME: turn off bus power and perhaps interrupts too */
1250 clk_disable(host->fclk);
1251 host->dpm_state = new_state;
1253 mmc_release_host(host->mmc);
1255 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1256 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1258 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1259 mmc_slot(host).card_detect ||
1260 (mmc_slot(host).get_cover_state &&
1261 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
1262 return msecs_to_jiffies(OMAP_MMC_OFF_TIMEOUT);
1267 /* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
1268 static int omap_mmc_sleep_to_off(struct mmc_omap_host *host)
1270 if (!mmc_try_claim_host(host->mmc))
1273 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1274 mmc_slot(host).card_detect ||
1275 (mmc_slot(host).get_cover_state &&
1276 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1277 mmc_release_host(host->mmc);
1281 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1283 host->power_mode = MMC_POWER_OFF;
1285 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1286 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1288 host->dpm_state = OFF;
1290 mmc_release_host(host->mmc);
1295 /* Handler for [DISABLED -> ENABLED] transition */
1296 static int omap_mmc_disabled_to_enabled(struct mmc_omap_host *host)
1300 err = clk_enable(host->fclk);
1304 omap_mmc_restore_ctx(host);
1306 host->dpm_state = ENABLED;
1308 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1313 /* Handler for [SLEEP -> ENABLED] transition */
1314 static int omap_mmc_sleep_to_enabled(struct mmc_omap_host *host)
1316 if (!mmc_try_claim_host(host->mmc))
1319 clk_enable(host->fclk);
1320 omap_mmc_restore_ctx(host);
1321 if (mmc_slot(host).set_sleep)
1322 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1323 host->vdd, host->dpm_state == CARDSLEEP);
1324 if (mmc_card_can_sleep(host->mmc))
1325 mmc_card_awake(host->mmc);
1327 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1328 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1330 host->dpm_state = ENABLED;
1332 mmc_release_host(host->mmc);
1337 /* Handler for [OFF -> ENABLED] transition */
1338 static int omap_mmc_off_to_enabled(struct mmc_omap_host *host)
1340 clk_enable(host->fclk);
1342 omap_mmc_restore_ctx(host);
1343 omap_hsmmc_init(host);
1344 mmc_power_restore_host(host->mmc);
1346 host->dpm_state = ENABLED;
1348 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1354 * Bring MMC host to ENABLED from any other PM state.
1356 static int omap_mmc_enable(struct mmc_host *mmc)
1358 struct mmc_omap_host *host = mmc_priv(mmc);
1360 switch (host->dpm_state) {
1362 return omap_mmc_disabled_to_enabled(host);
1365 return omap_mmc_sleep_to_enabled(host);
1367 return omap_mmc_off_to_enabled(host);
1369 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1375 * Bring MMC host in PM state (one level deeper).
1377 static int omap_mmc_disable(struct mmc_host *mmc, int lazy)
1379 struct mmc_omap_host *host = mmc_priv(mmc);
1381 switch (host->dpm_state) {
1385 delay = omap_mmc_enabled_to_disabled(host);
1386 if (lazy || delay < 0)
1391 return omap_mmc_disabled_to_sleep(host);
1394 return omap_mmc_sleep_to_off(host);
1396 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1401 static int omap_mmc_enable_fclk(struct mmc_host *mmc)
1403 struct mmc_omap_host *host = mmc_priv(mmc);
1406 err = clk_enable(host->fclk);
1409 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
1410 omap_mmc_restore_ctx(host);
1414 static int omap_mmc_disable_fclk(struct mmc_host *mmc, int lazy)
1416 struct mmc_omap_host *host = mmc_priv(mmc);
1418 omap_mmc_save_ctx(host);
1419 clk_disable(host->fclk);
1420 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1424 static const struct mmc_host_ops mmc_omap_ops = {
1425 .enable = omap_mmc_enable_fclk,
1426 .disable = omap_mmc_disable_fclk,
1427 .request = omap_mmc_request,
1428 .set_ios = omap_mmc_set_ios,
1429 .get_cd = omap_hsmmc_get_cd,
1430 .get_ro = omap_hsmmc_get_ro,
1431 /* NYET -- enable_sdio_irq */
1434 static const struct mmc_host_ops mmc_omap_ps_ops = {
1435 .enable = omap_mmc_enable,
1436 .disable = omap_mmc_disable,
1437 .request = omap_mmc_request,
1438 .set_ios = omap_mmc_set_ios,
1439 .get_cd = omap_hsmmc_get_cd,
1440 .get_ro = omap_hsmmc_get_ro,
1441 /* NYET -- enable_sdio_irq */
1444 #ifdef CONFIG_DEBUG_FS
1446 static int mmc_regs_show(struct seq_file *s, void *data)
1448 struct mmc_host *mmc = s->private;
1449 struct mmc_omap_host *host = mmc_priv(mmc);
1450 struct omap_mmc_platform_data *pdata = host->pdata;
1451 int context_loss = 0;
1453 if (pdata->get_context_loss_count)
1454 context_loss = pdata->get_context_loss_count(host->dev);
1456 seq_printf(s, "mmc%d:\n"
1459 " nesting_cnt:\t%d\n"
1460 " ctx_loss:\t%d:%d\n"
1462 mmc->index, mmc->enabled ? 1 : 0,
1463 host->dpm_state, mmc->nesting_cnt,
1464 host->context_loss, context_loss);
1466 if (host->suspended || host->dpm_state == OFF) {
1467 seq_printf(s, "host suspended, can't read registers\n");
1471 if (clk_enable(host->fclk) != 0) {
1472 seq_printf(s, "can't read the regs\n");
1476 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1477 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1478 seq_printf(s, "CON:\t\t0x%08x\n",
1479 OMAP_HSMMC_READ(host->base, CON));
1480 seq_printf(s, "HCTL:\t\t0x%08x\n",
1481 OMAP_HSMMC_READ(host->base, HCTL));
1482 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1483 OMAP_HSMMC_READ(host->base, SYSCTL));
1484 seq_printf(s, "IE:\t\t0x%08x\n",
1485 OMAP_HSMMC_READ(host->base, IE));
1486 seq_printf(s, "ISE:\t\t0x%08x\n",
1487 OMAP_HSMMC_READ(host->base, ISE));
1488 seq_printf(s, "CAPA:\t\t0x%08x\n",
1489 OMAP_HSMMC_READ(host->base, CAPA));
1491 clk_disable(host->fclk);
1496 static int mmc_regs_open(struct inode *inode, struct file *file)
1498 return single_open(file, mmc_regs_show, inode->i_private);
1501 static const struct file_operations mmc_regs_fops = {
1502 .open = mmc_regs_open,
1504 .llseek = seq_lseek,
1505 .release = single_release,
1508 static void omap_mmc_debugfs(struct mmc_host *mmc)
1510 if (mmc->debugfs_root)
1511 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1512 mmc, &mmc_regs_fops);
1517 static void omap_mmc_debugfs(struct mmc_host *mmc)
1523 static int __init omap_mmc_probe(struct platform_device *pdev)
1525 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1526 struct mmc_host *mmc;
1527 struct mmc_omap_host *host = NULL;
1528 struct resource *res;
1531 if (pdata == NULL) {
1532 dev_err(&pdev->dev, "Platform Data is missing\n");
1536 if (pdata->nr_slots == 0) {
1537 dev_err(&pdev->dev, "No Slots\n");
1541 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1542 irq = platform_get_irq(pdev, 0);
1543 if (res == NULL || irq < 0)
1546 res = request_mem_region(res->start, res->end - res->start + 1,
1551 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
1557 host = mmc_priv(mmc);
1559 host->pdata = pdata;
1560 host->dev = &pdev->dev;
1562 host->dev->dma_mask = &pdata->dma_mask;
1565 host->id = pdev->id;
1567 host->mapbase = res->start;
1568 host->base = ioremap(host->mapbase, SZ_4K);
1569 host->power_mode = -1;
1571 platform_set_drvdata(pdev, host);
1572 INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
1574 if (mmc_slot(host).power_saving)
1575 mmc->ops = &mmc_omap_ps_ops;
1577 mmc->ops = &mmc_omap_ops;
1579 mmc->f_min = 400000;
1580 mmc->f_max = 52000000;
1582 sema_init(&host->sem, 1);
1584 host->iclk = clk_get(&pdev->dev, "ick");
1585 if (IS_ERR(host->iclk)) {
1586 ret = PTR_ERR(host->iclk);
1590 host->fclk = clk_get(&pdev->dev, "fck");
1591 if (IS_ERR(host->fclk)) {
1592 ret = PTR_ERR(host->fclk);
1594 clk_put(host->iclk);
1598 omap_mmc_save_ctx(host);
1600 mmc->caps |= MMC_CAP_DISABLE;
1601 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
1602 /* we start off in DISABLED state */
1603 host->dpm_state = DISABLED;
1605 if (mmc_host_enable(host->mmc) != 0) {
1606 clk_put(host->iclk);
1607 clk_put(host->fclk);
1611 if (clk_enable(host->iclk) != 0) {
1612 mmc_host_disable(host->mmc);
1613 clk_put(host->iclk);
1614 clk_put(host->fclk);
1618 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1620 * MMC can still work without debounce clock.
1622 if (IS_ERR(host->dbclk))
1623 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
1625 if (clk_enable(host->dbclk) != 0)
1626 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1629 host->dbclk_enabled = 1;
1631 /* Since we do only SG emulation, we can have as many segs
1633 mmc->max_phys_segs = 1024;
1634 mmc->max_hw_segs = 1024;
1636 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1637 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1638 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1639 mmc->max_seg_size = mmc->max_req_size;
1641 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1642 MMC_CAP_WAIT_WHILE_BUSY;
1644 if (mmc_slot(host).wires >= 8)
1645 mmc->caps |= MMC_CAP_8_BIT_DATA;
1646 else if (mmc_slot(host).wires >= 4)
1647 mmc->caps |= MMC_CAP_4_BIT_DATA;
1649 if (mmc_slot(host).nonremovable)
1650 mmc->caps |= MMC_CAP_NONREMOVABLE;
1652 omap_hsmmc_init(host);
1654 /* Select DMA lines */
1656 case OMAP_MMC1_DEVID:
1657 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1658 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1660 case OMAP_MMC2_DEVID:
1661 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
1662 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
1664 case OMAP_MMC3_DEVID:
1665 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
1666 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
1669 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
1673 /* Request IRQ for MMC operations */
1674 ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
1675 mmc_hostname(mmc), host);
1677 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1681 /* initialize power supplies, gpios, etc */
1682 if (pdata->init != NULL) {
1683 if (pdata->init(&pdev->dev) != 0) {
1684 dev_dbg(mmc_dev(host->mmc), "late init error\n");
1685 goto err_irq_cd_init;
1688 mmc->ocr_avail = mmc_slot(host).ocr_mask;
1690 /* Request IRQ for card detect */
1691 if ((mmc_slot(host).card_detect_irq)) {
1692 ret = request_irq(mmc_slot(host).card_detect_irq,
1693 omap_mmc_cd_handler,
1694 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1696 mmc_hostname(mmc), host);
1698 dev_dbg(mmc_dev(host->mmc),
1699 "Unable to grab MMC CD IRQ\n");
1704 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
1705 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
1707 mmc_host_lazy_disable(host->mmc);
1711 if (mmc_slot(host).name != NULL) {
1712 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1716 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
1717 ret = device_create_file(&mmc->class_dev,
1718 &dev_attr_cover_switch);
1720 goto err_cover_switch;
1723 omap_mmc_debugfs(mmc);
1728 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1730 mmc_remove_host(mmc);
1732 free_irq(mmc_slot(host).card_detect_irq, host);
1734 free_irq(host->irq, host);
1736 mmc_host_disable(host->mmc);
1737 clk_disable(host->iclk);
1738 clk_put(host->fclk);
1739 clk_put(host->iclk);
1740 if (host->dbclk_enabled) {
1741 clk_disable(host->dbclk);
1742 clk_put(host->dbclk);
1746 iounmap(host->base);
1748 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
1749 release_mem_region(res->start, res->end - res->start + 1);
1755 static int omap_mmc_remove(struct platform_device *pdev)
1757 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1758 struct resource *res;
1761 mmc_host_enable(host->mmc);
1762 mmc_remove_host(host->mmc);
1763 if (host->pdata->cleanup)
1764 host->pdata->cleanup(&pdev->dev);
1765 free_irq(host->irq, host);
1766 if (mmc_slot(host).card_detect_irq)
1767 free_irq(mmc_slot(host).card_detect_irq, host);
1768 flush_scheduled_work();
1770 mmc_host_disable(host->mmc);
1771 clk_disable(host->iclk);
1772 clk_put(host->fclk);
1773 clk_put(host->iclk);
1774 if (host->dbclk_enabled) {
1775 clk_disable(host->dbclk);
1776 clk_put(host->dbclk);
1779 mmc_free_host(host->mmc);
1780 iounmap(host->base);
1783 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1785 release_mem_region(res->start, res->end - res->start + 1);
1786 platform_set_drvdata(pdev, NULL);
1792 static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1795 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1797 if (host && host->suspended)
1801 host->suspended = 1;
1802 if (host->pdata->suspend) {
1803 ret = host->pdata->suspend(&pdev->dev,
1806 dev_dbg(mmc_dev(host->mmc),
1807 "Unable to handle MMC board"
1808 " level suspend\n");
1809 host->suspended = 0;
1813 cancel_work_sync(&host->mmc_carddetect_work);
1814 mmc_host_enable(host->mmc);
1815 ret = mmc_suspend_host(host->mmc, state);
1817 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1818 OMAP_HSMMC_WRITE(host->base, IE, 0);
1821 OMAP_HSMMC_WRITE(host->base, HCTL,
1822 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
1823 mmc_host_disable(host->mmc);
1824 clk_disable(host->iclk);
1825 clk_disable(host->dbclk);
1827 host->suspended = 0;
1828 if (host->pdata->resume) {
1829 ret = host->pdata->resume(&pdev->dev,
1832 dev_dbg(mmc_dev(host->mmc),
1833 "Unmask interrupt failed\n");
1835 mmc_host_disable(host->mmc);
1842 /* Routine to resume the MMC device */
1843 static int omap_mmc_resume(struct platform_device *pdev)
1846 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1848 if (host && !host->suspended)
1852 ret = clk_enable(host->iclk);
1856 if (clk_enable(host->dbclk) != 0)
1857 dev_dbg(mmc_dev(host->mmc),
1858 "Enabling debounce clk failed\n");
1860 if (mmc_host_enable(host->mmc) != 0) {
1861 clk_disable(host->iclk);
1865 omap_hsmmc_init(host);
1867 if (host->pdata->resume) {
1868 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1870 dev_dbg(mmc_dev(host->mmc),
1871 "Unmask interrupt failed\n");
1874 /* Notify the core to resume the host */
1875 ret = mmc_resume_host(host->mmc);
1877 host->suspended = 0;
1878 mmc_host_lazy_disable(host->mmc);
1884 dev_dbg(mmc_dev(host->mmc),
1885 "Failed to enable MMC clocks during resume\n");
1890 #define omap_mmc_suspend NULL
1891 #define omap_mmc_resume NULL
1894 static struct platform_driver omap_mmc_driver = {
1895 .remove = omap_mmc_remove,
1896 .suspend = omap_mmc_suspend,
1897 .resume = omap_mmc_resume,
1899 .name = DRIVER_NAME,
1900 .owner = THIS_MODULE,
1904 static int __init omap_mmc_init(void)
1906 /* Register the MMC driver */
1907 return platform_driver_probe(&omap_mmc_driver, omap_mmc_probe);
1910 static void __exit omap_mmc_cleanup(void)
1912 /* Unregister MMC driver */
1913 platform_driver_unregister(&omap_mmc_driver);
1916 module_init(omap_mmc_init);
1917 module_exit(omap_mmc_cleanup);
1919 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1920 MODULE_LICENSE("GPL");
1921 MODULE_ALIAS("platform:" DRIVER_NAME);
1922 MODULE_AUTHOR("Texas Instruments Inc");