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/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/platform_device.h>
24 #include <linux/workqueue.h>
25 #include <linux/timer.h>
26 #include <linux/clk.h>
27 #include <linux/mmc/host.h>
29 #include <linux/semaphore.h>
31 #include <mach/hardware.h>
32 #include <mach/board.h>
36 /* OMAP HSMMC Host Controller Registers */
37 #define OMAP_HSMMC_SYSCONFIG 0x0010
38 #define OMAP_HSMMC_CON 0x002C
39 #define OMAP_HSMMC_BLK 0x0104
40 #define OMAP_HSMMC_ARG 0x0108
41 #define OMAP_HSMMC_CMD 0x010C
42 #define OMAP_HSMMC_RSP10 0x0110
43 #define OMAP_HSMMC_RSP32 0x0114
44 #define OMAP_HSMMC_RSP54 0x0118
45 #define OMAP_HSMMC_RSP76 0x011C
46 #define OMAP_HSMMC_DATA 0x0120
47 #define OMAP_HSMMC_HCTL 0x0128
48 #define OMAP_HSMMC_SYSCTL 0x012C
49 #define OMAP_HSMMC_STAT 0x0130
50 #define OMAP_HSMMC_IE 0x0134
51 #define OMAP_HSMMC_ISE 0x0138
52 #define OMAP_HSMMC_CAPA 0x0140
54 #define VS18 (1 << 26)
55 #define VS30 (1 << 25)
56 #define SDVS18 (0x5 << 9)
57 #define SDVS30 (0x6 << 9)
58 #define SDVSCLR 0xFFFFF1FF
59 #define SDVSDET 0x00000400
66 #define CLKD_MASK 0x0000FFC0
68 #define DTO_MASK 0x000F0000
70 #define INT_EN_MASK 0x307F0033
71 #define INIT_STREAM (1 << 1)
72 #define DP_SELECT (1 << 21)
77 #define FOUR_BIT (1 << 1)
82 #define CMD_TIMEOUT (1 << 16)
83 #define DATA_TIMEOUT (1 << 20)
84 #define CMD_CRC (1 << 17)
85 #define DATA_CRC (1 << 21)
86 #define CARD_ERR (1 << 28)
87 #define STAT_CLEAR 0xFFFFFFFF
88 #define INIT_STREAM_CMD 0x00000000
89 #define DUAL_VOLT_OCR_BIT 7
94 * FIXME: Most likely all the data using these _DEVID defines should come
95 * from the platform_data, or implemented in controller and slot specific
98 #define OMAP_MMC1_DEVID 0
99 #define OMAP_MMC2_DEVID 1
101 #define MMC_TIMEOUT_MS 20
102 #define OMAP_MMC_MASTER_CLOCK 96000000
103 #define DRIVER_NAME "mmci-omap"
106 * One controller can have multiple slots, like on some omap boards using
107 * omap.c controller driver. Luckily this is not currently done on any known
108 * omap_hsmmc.c device.
110 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
113 * MMC Host controller read/write API's
115 #define OMAP_HSMMC_READ(base, reg) \
116 __raw_readl((base) + OMAP_HSMMC_##reg)
118 #define OMAP_HSMMC_WRITE(base, reg, val) \
119 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
122 #define IDLE_TIMEOUT (jiffies_to_msecs(10))
124 struct mmc_omap_host {
126 struct mmc_host *mmc;
127 struct mmc_request *mrq;
128 struct mmc_command *cmd;
129 struct mmc_data *data;
133 struct semaphore sem;
134 struct work_struct mmc_carddetect_work;
136 resource_size_t mapbase;
138 unsigned int dma_len;
139 unsigned int dma_sg_idx;
140 unsigned char bus_mode;
150 struct timer_list idle_timer;
151 spinlock_t clk_lock; /* for changing enabled state */
152 unsigned int fclk_enabled:1;
154 struct omap_mmc_platform_data *pdata;
157 static int mmc_omap_fclk_state(struct mmc_omap_host *host, unsigned int state)
162 spin_lock_irqsave(&host->clk_lock, flags);
163 del_timer(&host->idle_timer);
164 if (host->fclk_enabled != state) {
166 ret = clk_enable(host->fclk);
170 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
172 clk_disable(host->fclk);
173 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
175 host->fclk_enabled = state;
179 spin_unlock_irqrestore(&host->clk_lock, flags);
183 static void mmc_omap_idle_timer(unsigned long data)
185 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
187 mmc_omap_fclk_state(host, OFF);
190 static void mmc_omap_fclk_lazy_disable(struct mmc_omap_host *host)
192 mod_timer(&host->idle_timer, jiffies + IDLE_TIMEOUT);
196 * Stop clock to the card
198 static void omap_mmc_stop_clock(struct mmc_omap_host *host)
200 OMAP_HSMMC_WRITE(host->base, SYSCTL,
201 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
202 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
203 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
207 * Send init stream sequence to card
208 * before sending IDLE command
210 static void send_init_stream(struct mmc_omap_host *host)
213 unsigned long timeout;
215 disable_irq(host->irq);
216 OMAP_HSMMC_WRITE(host->base, CON,
217 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
218 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
220 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
221 while ((reg != CC) && time_before(jiffies, timeout))
222 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
224 OMAP_HSMMC_WRITE(host->base, CON,
225 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
226 enable_irq(host->irq);
230 int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
232 if (host->pdata->slots[host->slot_id].get_cover_state)
233 return host->pdata->slots[host->slot_id].get_cover_state(host->dev, host->slot_id);
238 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
241 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
242 struct mmc_omap_host *host = mmc_priv(mmc);
244 return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
248 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
251 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
254 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
255 struct mmc_omap_host *host = mmc_priv(mmc);
256 struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
258 return sprintf(buf, "slot:%s\n", slot.name);
261 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
264 * Configure the response type and send the cmd.
267 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
268 struct mmc_data *data)
270 int cmdreg = 0, resptype = 0, cmdtype = 0;
272 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
273 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
277 * Clear status bits and enable interrupts
279 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
280 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
281 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
283 if (cmd->flags & MMC_RSP_PRESENT) {
284 if (cmd->flags & MMC_RSP_136)
291 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
292 * ac, bc, adtc, bcr. Only CMD12 needs a val of 0x3, rest 0x0.
294 if (cmd->opcode == 12)
297 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
300 cmdreg |= DP_SELECT | MSBS | BCE;
301 if (data->flags & MMC_DATA_READ)
310 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
311 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
315 mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
317 if (data->flags & MMC_DATA_WRITE)
318 return DMA_TO_DEVICE;
320 return DMA_FROM_DEVICE;
324 * Notify the transfer complete to MMC core
327 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
331 if (host->use_dma && host->dma_ch != -1)
332 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
333 mmc_omap_get_dma_dir(host, data));
336 data->bytes_xfered += data->blocks * (data->blksz);
338 data->bytes_xfered = 0;
342 mmc_omap_fclk_lazy_disable(host);
343 mmc_request_done(host->mmc, data->mrq);
346 mmc_omap_start_command(host, data->stop, NULL);
350 * Notify the core about command completion
353 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
357 if (cmd->flags & MMC_RSP_PRESENT) {
358 if (cmd->flags & MMC_RSP_136) {
359 /* response type 2 */
360 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
361 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
362 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
363 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
365 /* response types 1, 1b, 3, 4, 5, 6 */
366 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
369 if (host->data == NULL || cmd->error) {
371 mmc_omap_fclk_lazy_disable(host);
372 mmc_request_done(host->mmc, cmd->mrq);
377 * DMA clean up for command errors
379 static void mmc_dma_cleanup(struct mmc_omap_host *host)
381 host->data->error = -ETIMEDOUT;
383 if (host->use_dma && host->dma_ch != -1) {
384 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
385 mmc_omap_get_dma_dir(host, host->data));
386 omap_free_dma(host->dma_ch);
394 * Readable error output
396 #ifdef CONFIG_MMC_DEBUG
397 static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
399 /* --- means reserved bit without definition at documentation */
400 static const char *mmc_omap_status_bits[] = {
401 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
402 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
403 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
404 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
408 dev_dbg(mmc_dev(host->mmc), "MMC IRQ 0x%x :", status);
410 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
411 if (status & (1 << i))
413 * KERN_* facility is not used here because this should
414 * print a single line.
416 printk(" %s", mmc_omap_status_bits[i]);
421 #endif /* CONFIG_MMC_DEBUG */
425 * MMC controller IRQ handler
427 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
429 struct mmc_omap_host *host = dev_id;
430 struct mmc_data *data;
431 int end_cmd = 0, end_trans = 0, status;
433 if (host->cmd == NULL && host->data == NULL) {
434 OMAP_HSMMC_WRITE(host->base, STAT,
435 OMAP_HSMMC_READ(host->base, STAT));
440 status = OMAP_HSMMC_READ(host->base, STAT);
441 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
444 #ifdef CONFIG_MMC_DEBUG
445 mmc_omap_report_irq(host, status);
447 if ((status & CMD_TIMEOUT) ||
448 (status & CMD_CRC)) {
450 if (status & CMD_TIMEOUT) {
451 OMAP_HSMMC_WRITE(host->base, SYSCTL,
452 OMAP_HSMMC_READ(host->base,
454 while (OMAP_HSMMC_READ(host->base,
456 host->cmd->error = -ETIMEDOUT;
458 host->cmd->error = -EILSEQ;
463 mmc_dma_cleanup(host);
465 if ((status & DATA_TIMEOUT) ||
466 (status & DATA_CRC)) {
468 if (status & DATA_TIMEOUT)
469 mmc_dma_cleanup(host);
471 host->data->error = -EILSEQ;
472 OMAP_HSMMC_WRITE(host->base, SYSCTL,
473 OMAP_HSMMC_READ(host->base,
475 while (OMAP_HSMMC_READ(host->base,
480 if (status & CARD_ERR) {
481 dev_dbg(mmc_dev(host->mmc),
482 "Ignoring card err CMD%d\n", host->cmd->opcode);
490 OMAP_HSMMC_WRITE(host->base, STAT, status);
492 if (end_cmd || (status & CC))
493 mmc_omap_cmd_done(host, host->cmd);
494 if (end_trans || (status & TC))
495 mmc_omap_xfer_done(host, data);
501 * Switch MMC operating voltage
503 static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
508 /* Disable the clocks */
509 mmc_omap_fclk_state(host, OFF);
510 clk_disable(host->iclk);
511 clk_disable(host->dbclk);
513 /* Turn the power off */
514 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
518 /* Turn the power ON with given VDD 1.8 or 3.0v */
519 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
523 mmc_omap_fclk_state(host, ON);
524 clk_enable(host->iclk);
525 clk_enable(host->dbclk);
527 OMAP_HSMMC_WRITE(host->base, HCTL,
528 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
529 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
531 * If a MMC dual voltage card is detected, the set_ios fn calls
532 * this fn with VDD bit set for 1.8V. Upon card removal from the
533 * slot, mmc_omap_detect fn sets the VDD back to 3V.
535 * Only MMC1 supports 3.0V. MMC2 will not function if SDVS30 is
538 if (host->id == OMAP_MMC1_DEVID) {
539 if (((1 << vdd) == MMC_VDD_32_33) ||
540 ((1 << vdd) == MMC_VDD_33_34))
542 else if ((1 << vdd) == MMC_VDD_165_195)
547 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
549 OMAP_HSMMC_WRITE(host->base, HCTL,
550 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
554 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
559 * Work Item to notify the core about card insertion/removal
561 static void mmc_omap_detect(struct work_struct *work)
564 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
565 mmc_carddetect_work);
567 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
568 mmc_omap_fclk_state(host, ON);
569 if (host->carddetect) {
570 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
572 * Set the VDD back to 3V when the card is removed
573 * before the set_ios fn turns off the power.
575 vdd = fls(host->mmc->ocr_avail) - 1;
576 if (omap_mmc_switch_opcond(host, vdd) != 0)
577 host->mmc->ios.vdd = vdd;
579 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
581 OMAP_HSMMC_WRITE(host->base, SYSCTL,
582 OMAP_HSMMC_READ(host->base, SYSCTL) | SRD);
583 while (OMAP_HSMMC_READ(host->base, SYSCTL) & SRD) ;
584 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
586 mmc_omap_fclk_lazy_disable(host);
590 * ISR for handling card insertion and removal
592 static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
594 struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
596 host->carddetect = mmc_slot(host).card_detect(irq);
597 schedule_work(&host->mmc_carddetect_work);
602 static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
603 struct mmc_data *data)
607 if (data->flags & MMC_DATA_WRITE) {
608 if (host->id == OMAP_MMC1_DEVID)
609 sync_dev = OMAP24XX_DMA_MMC1_TX;
611 sync_dev = OMAP24XX_DMA_MMC2_TX;
613 if (host->id == OMAP_MMC1_DEVID)
614 sync_dev = OMAP24XX_DMA_MMC1_RX;
616 sync_dev = OMAP24XX_DMA_MMC2_RX;
621 static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
622 struct mmc_data *data,
623 struct scatterlist *sgl)
625 int blksz, nblk, dma_ch;
627 dma_ch = host->dma_ch;
628 if (data->flags & MMC_DATA_WRITE) {
629 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
630 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
631 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
632 sg_dma_address(sgl), 0, 0);
634 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
635 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
636 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
637 sg_dma_address(sgl), 0, 0);
640 blksz = host->data->blksz;
641 nblk = sg_dma_len(sgl) / blksz;
643 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
644 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
645 mmc_omap_get_dma_sync_dev(host, data),
646 !(data->flags & MMC_DATA_WRITE));
648 omap_start_dma(dma_ch);
652 * DMA call back function
654 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
656 struct mmc_omap_host *host = data;
658 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
659 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
661 if (host->dma_ch < 0)
665 if (host->dma_sg_idx < host->dma_len) {
666 /* Fire up the next transfer. */
667 mmc_omap_config_dma_params(host, host->data,
668 host->data->sg + host->dma_sg_idx);
672 omap_free_dma(host->dma_ch);
675 * DMA Callback: run in interrupt context.
676 * mutex_unlock will through a kernel warning if used.
682 * Routine to configure and start DMA for the MMC card
685 mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
687 int dma_ch = 0, ret = 0, err = 1, i;
688 struct mmc_data *data = req->data;
690 /* Sanity check: all the SG entries must be aligned by block size. */
691 for (i = 0; i < host->dma_len; i++) {
692 struct scatterlist *sgl;
695 if (sgl->length % data->blksz)
698 if ((data->blksz % 4) != 0)
699 /* REVISIT: The MMC buffer increments only when MSB is written.
700 * Return error for blksz which is non multiple of four.
705 * If for some reason the DMA transfer is still active,
706 * we wait for timeout period and free the dma
708 if (host->dma_ch != -1) {
709 set_current_state(TASK_UNINTERRUPTIBLE);
710 schedule_timeout(100);
711 if (down_trylock(&host->sem)) {
712 omap_free_dma(host->dma_ch);
718 if (down_trylock(&host->sem))
722 ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
723 mmc_omap_dma_cb,host, &dma_ch);
725 dev_err(mmc_dev(host->mmc),
726 "%s: omap_request_dma() failed with %d\n",
727 mmc_hostname(host->mmc), ret);
731 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
732 data->sg_len, mmc_omap_get_dma_dir(host, data));
733 host->dma_ch = dma_ch;
734 host->dma_sg_idx = 0;
736 mmc_omap_config_dma_params(host, data, data->sg);
741 static void set_data_timeout(struct mmc_omap_host *host,
742 struct mmc_request *req)
744 unsigned int timeout, cycle_ns;
745 uint32_t reg, clkd, dto = 0;
747 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
748 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
752 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
753 timeout = req->data->timeout_ns / cycle_ns;
754 timeout += req->data->timeout_clks;
756 while ((timeout & 0x80000000) == 0) {
773 reg |= dto << DTO_SHIFT;
774 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
778 * Configure block length for MMC/SD cards and initiate the transfer.
781 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
784 host->data = req->data;
786 if (req->data == NULL) {
787 OMAP_HSMMC_WRITE(host->base, BLK, 0);
791 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
792 | (req->data->blocks << 16));
793 set_data_timeout(host, req);
796 ret = mmc_omap_start_dma_transfer(host, req);
798 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
806 * Request function. for read/write operation
808 static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
810 struct mmc_omap_host *host = mmc_priv(mmc);
812 WARN_ON(host->mrq != NULL);
814 mmc_omap_fclk_state(host, ON);
815 mmc_omap_prepare_data(host, req);
816 mmc_omap_start_command(host, req->cmd, req->data);
819 /* Routine to configure clock values. Exposed API to core */
820 static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
822 struct mmc_omap_host *host = mmc_priv(mmc);
824 unsigned long regval;
825 unsigned long timeout;
827 mmc_omap_fclk_state(host, ON);
829 switch (ios->power_mode) {
831 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
834 mmc_slot(host).set_power(host->dev, host->slot_id, 1, ios->vdd);
838 switch (mmc->ios.bus_width) {
839 case MMC_BUS_WIDTH_4:
840 OMAP_HSMMC_WRITE(host->base, HCTL,
841 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
843 case MMC_BUS_WIDTH_1:
844 OMAP_HSMMC_WRITE(host->base, HCTL,
845 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
849 if (host->id == OMAP_MMC1_DEVID) {
850 /* Only MMC1 can operate at 3V/1.8V */
851 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
852 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
854 * The mmc_select_voltage fn of the core does
855 * not seem to set the power_mode to
856 * MMC_POWER_UP upon recalculating the voltage.
859 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
860 dev_dbg(mmc_dev(host->mmc),
861 "Switch operation failed\n");
866 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
870 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
876 omap_mmc_stop_clock(host);
877 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
878 regval = regval & ~(CLKD_MASK);
879 regval = regval | (dsor << 6) | (DTO << 16);
880 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
881 OMAP_HSMMC_WRITE(host->base, SYSCTL,
882 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
884 /* Wait till the ICS bit is set */
885 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
886 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != 0x2
887 && time_before(jiffies, timeout))
890 OMAP_HSMMC_WRITE(host->base, SYSCTL,
891 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
893 if (ios->power_mode == MMC_POWER_ON)
894 send_init_stream(host);
896 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
897 OMAP_HSMMC_WRITE(host->base, CON,
898 OMAP_HSMMC_READ(host->base, CON) | OD);
900 if (ios->power_mode == MMC_POWER_OFF)
901 mmc_omap_fclk_state(host, OFF);
903 mmc_omap_fclk_lazy_disable(host);
906 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
908 struct mmc_omap_host *host = mmc_priv(mmc);
909 struct omap_mmc_platform_data *pdata = host->pdata;
911 if (!pdata->slots[0].card_detect)
913 return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
916 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
918 struct mmc_omap_host *host = mmc_priv(mmc);
919 struct omap_mmc_platform_data *pdata = host->pdata;
921 if (!pdata->slots[0].get_ro)
923 return pdata->slots[0].get_ro(host->dev, 0);
926 static struct mmc_host_ops mmc_omap_ops = {
927 .request = omap_mmc_request,
928 .set_ios = omap_mmc_set_ios,
929 .get_cd = omap_hsmmc_get_cd,
930 .get_ro = omap_hsmmc_get_ro,
931 /* NYET -- enable_sdio_irq */
934 static int __init omap_mmc_probe(struct platform_device *pdev)
936 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
937 struct mmc_host *mmc;
938 struct mmc_omap_host *host = NULL;
939 struct resource *res;
944 dev_err(&pdev->dev, "Platform Data is missing\n");
948 if (pdata->nr_slots == 0) {
949 dev_err(&pdev->dev, "No Slots\n");
953 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
954 irq = platform_get_irq(pdev, 0);
955 if (res == NULL || irq < 0)
958 res = request_mem_region(res->start, res->end - res->start + 1,
963 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
969 host = mmc_priv(mmc);
972 host->dev = &pdev->dev;
974 host->dev->dma_mask = &pdata->dma_mask;
979 host->mapbase = res->start;
980 host->base = ioremap(host->mapbase, SZ_4K);
982 platform_set_drvdata(pdev, host);
983 INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
985 mmc->ops = &mmc_omap_ops;
987 mmc->f_max = 52000000;
989 sema_init(&host->sem, 1);
991 host->iclk = clk_get(&pdev->dev, "mmchs_ick");
992 if (IS_ERR(host->iclk)) {
993 ret = PTR_ERR(host->iclk);
997 host->fclk = clk_get(&pdev->dev, "mmchs_fck");
998 if (IS_ERR(host->fclk)) {
999 ret = PTR_ERR(host->fclk);
1001 clk_put(host->iclk);
1005 spin_lock_init(&host->clk_lock);
1006 setup_timer(&host->idle_timer, mmc_omap_idle_timer,
1007 (unsigned long) host);
1009 if (mmc_omap_fclk_state(host, ON) != 0) {
1010 clk_put(host->iclk);
1011 clk_put(host->fclk);
1014 if (clk_enable(host->iclk) != 0) {
1015 mmc_omap_fclk_state(host, OFF);
1016 clk_put(host->iclk);
1017 clk_put(host->fclk);
1021 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1023 * MMC can still work without debounce clock.
1025 if (IS_ERR(host->dbclk))
1026 dev_dbg(mmc_dev(host->mmc), "Failed to get debounce clock\n");
1028 if (clk_enable(host->dbclk) != 0)
1029 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1032 host->dbclk_enabled = 1;
1034 /* Since we do only SG emulation, we can have as many segs
1036 mmc->max_phys_segs = 1024;
1037 mmc->max_hw_segs = 1024;
1039 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1040 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1041 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1042 mmc->max_seg_size = mmc->max_req_size;
1044 mmc->ocr_avail = mmc_slot(host).ocr_mask;
1045 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1047 if (pdata->slots[host->slot_id].wires >= 4)
1048 mmc->caps |= MMC_CAP_4_BIT_DATA;
1050 /* Only MMC1 supports 3.0V */
1051 if (host->id == OMAP_MMC1_DEVID) {
1059 OMAP_HSMMC_WRITE(host->base, HCTL,
1060 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
1062 OMAP_HSMMC_WRITE(host->base, CAPA,
1063 OMAP_HSMMC_READ(host->base, CAPA) | capa);
1065 /* Set the controller to AUTO IDLE mode */
1066 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
1067 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
1069 /* Set SD bus power bit */
1070 OMAP_HSMMC_WRITE(host->base, HCTL,
1071 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1073 /* Request IRQ for MMC operations */
1074 ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
1075 mmc_hostname(mmc), host);
1077 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1081 if (pdata->init != NULL) {
1082 if (pdata->init(&pdev->dev) != 0) {
1083 dev_dbg(mmc_dev(host->mmc),
1084 "Unable to configure MMC IRQs\n");
1085 goto err_irq_cd_init;
1089 /* Request IRQ for card detect */
1090 if ((mmc_slot(host).card_detect_irq) && (mmc_slot(host).card_detect)) {
1091 ret = request_irq(mmc_slot(host).card_detect_irq,
1092 omap_mmc_cd_handler,
1093 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1095 mmc_hostname(mmc), host);
1097 dev_dbg(mmc_dev(host->mmc),
1098 "Unable to grab MMC CD IRQ\n");
1103 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
1104 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
1108 if (host->pdata->slots[host->slot_id].name != NULL) {
1109 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1113 if (mmc_slot(host).card_detect_irq && mmc_slot(host).card_detect &&
1114 host->pdata->slots[host->slot_id].get_cover_state) {
1115 ret = device_create_file(&mmc->class_dev, &dev_attr_cover_switch);
1117 goto err_cover_switch;
1119 mmc_omap_fclk_lazy_disable(host);
1124 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1126 mmc_remove_host(mmc);
1128 free_irq(mmc_slot(host).card_detect_irq, host);
1130 free_irq(host->irq, host);
1132 mmc_omap_fclk_state(host, OFF);
1133 clk_disable(host->iclk);
1134 clk_put(host->fclk);
1135 clk_put(host->iclk);
1136 if (host->dbclk_enabled) {
1137 clk_disable(host->dbclk);
1138 clk_put(host->dbclk);
1142 iounmap(host->base);
1144 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
1145 release_mem_region(res->start, res->end - res->start + 1);
1151 static int omap_mmc_remove(struct platform_device *pdev)
1153 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1154 struct resource *res;
1158 mmc_omap_fclk_state(host, ON);
1159 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
1161 * Set the vdd back to 3V,
1162 * applicable for dual volt support.
1164 vdd = fls(host->mmc->ocr_avail) - 1;
1165 if (omap_mmc_switch_opcond(host, vdd) != 0)
1166 host->mmc->ios.vdd = vdd;
1170 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1172 release_mem_region(res->start, res->end - res->start + 1);
1174 platform_set_drvdata(pdev, NULL);
1176 mmc_remove_host(host->mmc);
1177 if (host->pdata->cleanup)
1178 host->pdata->cleanup(&pdev->dev);
1179 free_irq(host->irq, host);
1180 if (mmc_slot(host).card_detect_irq)
1181 free_irq(mmc_slot(host).card_detect_irq, host);
1182 flush_scheduled_work();
1184 mmc_omap_fclk_state(host, OFF);
1185 clk_disable(host->iclk);
1186 clk_put(host->fclk);
1187 clk_put(host->iclk);
1188 if (host->dbclk_enabled) {
1189 clk_disable(host->dbclk);
1190 clk_put(host->dbclk);
1193 mmc_free_host(host->mmc);
1194 iounmap(host->base);
1201 static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1204 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1206 if (host && host->suspended)
1210 mmc_omap_fclk_state(host, ON);
1212 ret = mmc_suspend_host(host->mmc, state);
1214 host->suspended = 1;
1216 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1217 OMAP_HSMMC_WRITE(host->base, IE, 0);
1219 if (host->pdata->suspend) {
1220 ret = host->pdata->suspend(&pdev->dev, host->slot_id);
1222 dev_dbg(mmc_dev(host->mmc),
1223 "Unable to handle MMC board"
1224 " level suspend\n");
1227 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
1228 u32 hctl = OMAP_HSMMC_READ(host->base, HCTL) &
1231 if (host->id == OMAP_MMC1_DEVID)
1236 OMAP_HSMMC_WRITE(host->base, HCTL, hctl);
1237 OMAP_HSMMC_WRITE(host->base, HCTL, hctl | SDBP);
1240 mmc_omap_fclk_state(host, OFF);
1241 clk_disable(host->iclk);
1242 clk_disable(host->dbclk);
1249 /* Routine to resume the MMC device */
1250 static int omap_mmc_resume(struct platform_device *pdev)
1253 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1255 if (host && !host->suspended)
1259 if (mmc_omap_fclk_state(host, ON) != 0)
1262 ret = clk_enable(host->iclk);
1264 mmc_omap_fclk_state(host, OFF);
1265 clk_put(host->fclk);
1269 if (clk_enable(host->dbclk) != 0)
1270 dev_dbg(mmc_dev(host->mmc),
1271 "Enabling debounce clk failed\n");
1273 if (host->pdata->resume) {
1274 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1276 dev_dbg(mmc_dev(host->mmc),
1277 "Unmask interrupt failed\n");
1280 /* Notify the core to resume the host */
1281 ret = mmc_resume_host(host->mmc);
1283 host->suspended = 0;
1285 mmc_omap_fclk_lazy_disable(host);
1291 dev_dbg(mmc_dev(host->mmc),
1292 "Failed to enable MMC clocks during resume\n");
1297 #define omap_mmc_suspend NULL
1298 #define omap_mmc_resume NULL
1301 static struct platform_driver omap_mmc_driver = {
1302 .probe = omap_mmc_probe,
1303 .remove = omap_mmc_remove,
1304 .suspend = omap_mmc_suspend,
1305 .resume = omap_mmc_resume,
1307 .name = DRIVER_NAME,
1308 .owner = THIS_MODULE,
1312 static int __init omap_mmc_init(void)
1314 /* Register the MMC driver */
1315 return platform_driver_register(&omap_mmc_driver);
1318 static void __exit omap_mmc_cleanup(void)
1320 /* Unregister MMC driver */
1321 platform_driver_unregister(&omap_mmc_driver);
1324 module_init(omap_mmc_init);
1325 module_exit(omap_mmc_cleanup);
1327 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1328 MODULE_LICENSE("GPL");
1329 MODULE_ALIAS("platform:" DRIVER_NAME);
1330 MODULE_AUTHOR("Texas Instruments Inc");