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
56 #define SDVS18 (0x5<<9)
57 #define SDVS30 (0x6<<9)
58 #define SDVSCLR 0xFFFFF1FF
59 #define SDVSDET 0x00000400
66 #define CLKD_MASK 0x0000FFC0
67 #define INT_EN_MASK 0x307F0033
68 #define INIT_STREAM (1<<1)
69 #define DP_SELECT (1<<21)
74 #define FOUR_BIT 1 << 1
79 #define CMD_TIMEOUT (1 << 16)
80 #define DATA_TIMEOUT (1 << 20)
81 #define CMD_CRC (1 << 17)
82 #define DATA_CRC (1 << 21)
83 #define CARD_ERR (1 << 28)
84 #define STAT_CLEAR 0xFFFFFFFF
85 #define INIT_STREAM_CMD 0x00000000
86 #define DUAL_VOLT_OCR_BIT 7
90 #define OMAP_MMC1_DEVID 1
91 #define OMAP_MMC2_DEVID 2
92 #define OMAP_MMC_DATADIR_NONE 0
93 #define OMAP_MMC_DATADIR_READ 1
94 #define OMAP_MMC_DATADIR_WRITE 2
95 #define MMC_TIMEOUT_MS 20
96 #define OMAP_MMC_MASTER_CLOCK 96000000
97 #define DRIVER_NAME "mmci-omap"
99 * slot_id is device id - 1, device id is a static value
100 * of 1 to represent device 1 etc..
102 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
105 * MMC Host controller read/write API's
107 #define OMAP_HSMMC_READ(base, reg) \
108 __raw_readl((base) + OMAP_HSMMC_##reg)
110 #define OMAP_HSMMC_WRITE(base, reg, val) \
111 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
113 struct mmc_omap_host {
115 struct mmc_host *mmc;
116 struct mmc_request *mrq;
117 struct mmc_command *cmd;
118 struct mmc_data *data;
122 struct semaphore sem;
123 struct work_struct mmc_carddetect_work;
125 resource_size_t mapbase;
127 unsigned int dma_len;
128 unsigned int dma_dir;
129 unsigned char bus_mode;
130 unsigned char datadir;
140 struct omap_mmc_platform_data *pdata;
144 * Stop clock to the card
146 static void omap_mmc_stop_clock(struct mmc_omap_host *host)
148 OMAP_HSMMC_WRITE(host->base, SYSCTL,
149 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
150 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
151 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
155 * Send init stream sequence to card
156 * before sending IDLE command
158 static void send_init_stream(struct mmc_omap_host *host)
161 unsigned long timeout;
163 disable_irq(host->irq);
164 OMAP_HSMMC_WRITE(host->base, CON,
165 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
166 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
168 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
169 while ((reg != CC) && time_before(jiffies, timeout))
170 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
172 OMAP_HSMMC_WRITE(host->base, CON,
173 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
174 enable_irq(host->irq);
178 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
181 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
182 struct mmc_omap_host *host = mmc_priv(mmc);
183 struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
185 return sprintf(buf, "slot:%s\n", slot.name);
188 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
191 * Configure the response type and send the cmd.
194 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
195 struct mmc_data *data)
197 int cmdreg = 0, resptype = 0, cmdtype = 0;
199 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
200 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
204 * Clear status bits and enable interrupts
206 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
207 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
208 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
210 if (cmd->flags & MMC_RSP_PRESENT) {
211 if (cmd->flags & MMC_RSP_136)
218 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
219 * ac, bc, adtc, bcr. Only CMD12 needs a val of 0x3, rest 0x0.
221 if (cmd->opcode == 12)
224 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
227 cmdreg |= DP_SELECT | MSBS | BCE;
228 if (data->flags & MMC_DATA_READ)
237 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
238 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
242 * Notify the transfer complete to MMC core
245 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
249 if (host->use_dma && host->dma_ch != -1)
250 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
253 host->datadir = OMAP_MMC_DATADIR_NONE;
256 data->bytes_xfered += data->blocks * (data->blksz);
258 data->bytes_xfered = 0;
262 mmc_request_done(host->mmc, data->mrq);
265 mmc_omap_start_command(host, data->stop, NULL);
269 * Notify the core about command completion
272 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
276 if (cmd->flags & MMC_RSP_PRESENT) {
277 if (cmd->flags & MMC_RSP_136) {
278 /* response type 2 */
279 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
280 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
281 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
282 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
284 /* response types 1, 1b, 3, 4, 5, 6 */
285 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
288 if (host->data == NULL || cmd->error) {
290 mmc_request_done(host->mmc, cmd->mrq);
295 * DMA clean up for command errors
297 static void mmc_dma_cleanup(struct mmc_omap_host *host)
299 host->data->error = -ETIMEDOUT;
301 if (host->use_dma && host->dma_ch != -1) {
302 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
304 omap_free_dma(host->dma_ch);
309 host->datadir = OMAP_MMC_DATADIR_NONE;
313 * Readable error output
315 #ifdef CONFIG_MMC_DEBUG
316 static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
318 /* --- means reserved bit without definition at documentation */
319 static const char *mmc_omap_status_bits[] = {
320 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
321 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
322 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
323 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
327 dev_dbg(mmc_dev(host->mmc), "MMC IRQ 0x%x :", status);
329 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
330 if (status & (1 << i))
332 * KERN_* facility is not used here because this should
333 * print a single line.
335 printk(" %s", mmc_omap_status_bits[i]);
340 #endif /* CONFIG_MMC_DEBUG */
344 * MMC controller IRQ handler
346 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
348 struct mmc_omap_host *host = dev_id;
349 struct mmc_data *data;
350 int end_cmd = 0, end_trans = 0, status;
352 if (host->cmd == NULL && host->data == NULL) {
353 OMAP_HSMMC_WRITE(host->base, STAT,
354 OMAP_HSMMC_READ(host->base, STAT));
359 status = OMAP_HSMMC_READ(host->base, STAT);
360 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
363 #ifdef CONFIG_MMC_DEBUG
364 mmc_omap_report_irq(host, status);
366 if ((status & CMD_TIMEOUT) ||
367 (status & CMD_CRC)) {
369 if (status & CMD_TIMEOUT) {
370 OMAP_HSMMC_WRITE(host->base, SYSCTL,
371 OMAP_HSMMC_READ(host->base,
373 while (OMAP_HSMMC_READ(host->base,
375 host->cmd->error = -ETIMEDOUT;
377 host->cmd->error = -EILSEQ;
382 mmc_dma_cleanup(host);
384 if ((status & DATA_TIMEOUT) ||
385 (status & DATA_CRC)) {
387 if (status & DATA_TIMEOUT)
388 mmc_dma_cleanup(host);
390 host->data->error = -EILSEQ;
394 if (status & CARD_ERR) {
395 dev_dbg(mmc_dev(host->mmc),
396 "Ignoring card err CMD%d\n", host->cmd->opcode);
404 OMAP_HSMMC_WRITE(host->base, STAT, status);
406 if (end_cmd || (status & CC))
407 mmc_omap_cmd_done(host, host->cmd);
408 if (end_trans || (status & TC))
409 mmc_omap_xfer_done(host, data);
415 * Switch MMC operating voltage
417 static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
422 /* Disable the clocks */
423 clk_disable(host->fclk);
424 clk_disable(host->iclk);
425 clk_disable(host->dbclk);
427 /* Turn the power off */
428 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
432 /* Turn the power ON with given VDD 1.8 or 3.0v */
433 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
437 clk_enable(host->fclk);
438 clk_enable(host->iclk);
439 clk_enable(host->dbclk);
441 OMAP_HSMMC_WRITE(host->base, HCTL,
442 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
443 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
445 * If a MMC dual voltage card is detected, the set_ios fn calls
446 * this fn with VDD bit set for 1.8V. Upon card removal from the
447 * slot, mmc_omap_detect fn sets the VDD back to 3V.
449 * Only MMC1 supports 3.0V. MMC2 will not function if SDVS30 is
452 if (host->id == OMAP_MMC1_DEVID && (((1 << vdd) == MMC_VDD_32_33) ||
453 ((1 << vdd) == MMC_VDD_33_34)))
455 if ((1 << vdd) == MMC_VDD_165_195)
458 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
460 OMAP_HSMMC_WRITE(host->base, HCTL,
461 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
465 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
470 * Work Item to notify the core about card insertion/removal
472 static void mmc_omap_detect(struct work_struct *work)
475 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
476 mmc_carddetect_work);
478 if (host->carddetect) {
479 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
481 * Set the VDD back to 3V when the card is removed
482 * before the set_ios fn turns off the power.
484 vdd = fls(host->mmc->ocr_avail) - 1;
485 if (omap_mmc_switch_opcond(host, vdd) != 0)
486 host->mmc->ios.vdd = vdd;
488 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
490 OMAP_HSMMC_WRITE(host->base, SYSCTL,
491 OMAP_HSMMC_READ(host->base, SYSCTL) | SRD);
492 while (OMAP_HSMMC_READ(host->base, SYSCTL) & SRD) ;
493 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
498 * ISR for handling card insertion and removal
500 static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
502 struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
504 host->carddetect = mmc_slot(host).card_detect(irq);
505 schedule_work(&host->mmc_carddetect_work);
511 * DMA call back function
513 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
515 struct mmc_omap_host *host = data;
517 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
518 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
520 if (host->dma_ch < 0)
523 omap_free_dma(host->dma_ch);
526 * DMA Callback: run in interrupt context.
527 * mutex_unlock will through a kernel warning if used.
533 * Configure dma src and destination parameters
535 static int mmc_omap_config_dma_param(int sync_dir, struct mmc_omap_host *host,
536 struct mmc_data *data)
539 omap_set_dma_dest_params(host->dma_ch, 0,
540 OMAP_DMA_AMODE_CONSTANT,
541 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
542 omap_set_dma_src_params(host->dma_ch, 0,
543 OMAP_DMA_AMODE_POST_INC,
544 sg_dma_address(&data->sg[0]), 0, 0);
546 omap_set_dma_src_params(host->dma_ch, 0,
547 OMAP_DMA_AMODE_CONSTANT,
548 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
549 omap_set_dma_dest_params(host->dma_ch, 0,
550 OMAP_DMA_AMODE_POST_INC,
551 sg_dma_address(&data->sg[0]), 0, 0);
556 * Routine to configure and start DMA for the MMC card
559 mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
561 int sync_dev, sync_dir = 0;
562 int dma_ch = 0, ret = 0, err = 1;
563 struct mmc_data *data = req->data;
566 * If for some reason the DMA transfer is still active,
567 * we wait for timeout period and free the dma
569 if (host->dma_ch != -1) {
570 set_current_state(TASK_UNINTERRUPTIBLE);
571 schedule_timeout(100);
572 if (down_trylock(&host->sem)) {
573 omap_free_dma(host->dma_ch);
579 if (down_trylock(&host->sem))
583 if (!(data->flags & MMC_DATA_WRITE)) {
584 host->dma_dir = DMA_FROM_DEVICE;
585 if (host->id == OMAP_MMC1_DEVID)
586 sync_dev = OMAP24XX_DMA_MMC1_RX;
588 sync_dev = OMAP24XX_DMA_MMC2_RX;
590 host->dma_dir = DMA_TO_DEVICE;
591 if (host->id == OMAP_MMC1_DEVID)
592 sync_dev = OMAP24XX_DMA_MMC1_TX;
594 sync_dev = OMAP24XX_DMA_MMC2_TX;
597 ret = omap_request_dma(sync_dev, "MMC/SD", mmc_omap_dma_cb,
600 dev_dbg(mmc_dev(host->mmc),
601 "%s: omap_request_dma() failed with %d\n",
602 mmc_hostname(host->mmc), ret);
606 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
607 data->sg_len, host->dma_dir);
608 host->dma_ch = dma_ch;
610 if (!(data->flags & MMC_DATA_WRITE))
611 mmc_omap_config_dma_param(1, host, data);
613 mmc_omap_config_dma_param(0, host, data);
615 if ((data->blksz % 4) == 0)
616 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
617 (data->blksz / 4), data->blocks, OMAP_DMA_SYNC_FRAME,
620 /* REVISIT: The MMC buffer increments only when MSB is written.
621 * Return error for blksz which is non multiple of four.
625 omap_start_dma(dma_ch);
630 * Configure block length for MMC/SD cards and initiate the transfer.
633 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
636 host->data = req->data;
638 if (req->data == NULL) {
639 host->datadir = OMAP_MMC_DATADIR_NONE;
640 OMAP_HSMMC_WRITE(host->base, BLK, 0);
644 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
645 | (req->data->blocks << 16));
647 host->datadir = (req->data->flags & MMC_DATA_WRITE) ?
648 OMAP_MMC_DATADIR_WRITE : OMAP_MMC_DATADIR_READ;
651 ret = mmc_omap_start_dma_transfer(host, req);
653 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
661 * Request function. for read/write operation
663 static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
665 struct mmc_omap_host *host = mmc_priv(mmc);
667 WARN_ON(host->mrq != NULL);
669 mmc_omap_prepare_data(host, req);
670 mmc_omap_start_command(host, req->cmd, req->data);
674 /* Routine to configure clock values. Exposed API to core */
675 static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
677 struct mmc_omap_host *host = mmc_priv(mmc);
679 unsigned long regval;
680 unsigned long timeout;
682 switch (ios->power_mode) {
684 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
687 mmc_slot(host).set_power(host->dev, host->slot_id, 1, ios->vdd);
691 switch (mmc->ios.bus_width) {
692 case MMC_BUS_WIDTH_4:
693 OMAP_HSMMC_WRITE(host->base, HCTL,
694 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
696 case MMC_BUS_WIDTH_1:
697 OMAP_HSMMC_WRITE(host->base, HCTL,
698 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
702 if (host->id == OMAP_MMC1_DEVID) {
703 /* Only MMC1 can operate at 3V/1.8V */
704 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
705 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
707 * The mmc_select_voltage fn of the core does
708 * not seem to set the power_mode to
709 * MMC_POWER_UP upon recalculating the voltage.
712 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
713 dev_dbg(mmc_dev(host->mmc),
714 "Switch operation failed\n");
719 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
723 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
729 omap_mmc_stop_clock(host);
730 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
731 regval = regval & ~(CLKD_MASK);
732 regval = regval | (dsor << 6) | (DTO << 16);
733 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
734 OMAP_HSMMC_WRITE(host->base, SYSCTL,
735 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
737 /* Wait till the ICS bit is set */
738 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
739 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != 0x2
740 && time_before(jiffies, timeout))
743 OMAP_HSMMC_WRITE(host->base, SYSCTL,
744 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
746 if (ios->power_mode == MMC_POWER_ON)
747 send_init_stream(host);
749 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
750 OMAP_HSMMC_WRITE(host->base, CON,
751 OMAP_HSMMC_READ(host->base, CON) | OD);
753 /* NOTE: Read only switch not supported yet */
754 static struct mmc_host_ops mmc_omap_ops = {
755 .request = omap_mmc_request,
756 .set_ios = omap_mmc_set_ios,
759 static int __init omap_mmc_probe(struct platform_device *pdev)
761 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
762 struct mmc_host *mmc;
763 struct mmc_omap_host *host = NULL;
764 struct resource *res;
769 dev_err(&pdev->dev, "Platform Data is missing\n");
773 if (pdata->nr_slots == 0) {
774 dev_err(&pdev->dev, "No Slots\n");
778 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
779 irq = platform_get_irq(pdev, 0);
780 if (res == NULL || irq < 0)
783 res = request_mem_region(res->start, res->end - res->start + 1,
788 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
794 host = mmc_priv(mmc);
802 host->mapbase = res->start;
803 host->base = ioremap(host->mapbase, SZ_4K);
804 mmc->ops = &mmc_omap_ops;
806 mmc->f_max = 52000000;
808 sema_init(&host->sem, 1);
810 host->iclk = clk_get(&pdev->dev, "mmchs_ick");
811 if (IS_ERR(host->iclk)) {
812 ret = PTR_ERR(host->iclk);
816 host->fclk = clk_get(&pdev->dev, "mmchs_fck");
817 if (IS_ERR(host->fclk)) {
818 ret = PTR_ERR(host->fclk);
824 if (clk_enable(host->fclk) != 0) {
830 if (clk_enable(host->iclk) != 0) {
831 clk_disable(host->fclk);
837 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
839 * MMC can still work without debounce clock.
841 if (IS_ERR(host->dbclk))
842 dev_dbg(mmc_dev(host->mmc), "Failed to get debounce clock\n");
844 if (clk_enable(host->dbclk) != 0)
845 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
848 host->dbclk_enabled = 1;
850 #ifdef CONFIG_MMC_BLOCK_BOUNCE
851 mmc->max_phys_segs = 1;
852 mmc->max_hw_segs = 1;
854 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
855 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
856 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
857 mmc->max_seg_size = mmc->max_req_size;
859 mmc->ocr_avail = mmc_slot(host).ocr_mask;
860 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
862 if (pdata->conf.wire4)
863 mmc->caps |= MMC_CAP_4_BIT_DATA;
865 /* Only MMC1 supports 3.0V */
866 if (host->id == OMAP_MMC1_DEVID) {
874 OMAP_HSMMC_WRITE(host->base, HCTL,
875 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
877 OMAP_HSMMC_WRITE(host->base, CAPA,
878 OMAP_HSMMC_READ(host->base, CAPA) | capa);
880 /* Set the controller to AUTO IDLE mode */
881 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
882 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
884 /* Set SD bus power bit */
885 OMAP_HSMMC_WRITE(host->base, HCTL,
886 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
888 /* Request IRQ for MMC operations */
889 ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED, pdev->name,
892 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
896 /* Request IRQ for card detect */
897 if ((mmc_slot(host).card_detect_irq) && (mmc_slot(host).card_detect)) {
898 ret = request_irq(mmc_slot(host).card_detect_irq,
899 omap_mmc_cd_handler, IRQF_DISABLED, "MMC CD",
902 dev_dbg(mmc_dev(host->mmc),
903 "Unable to grab MMC CD IRQ\n");
908 INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
909 if (pdata->init != NULL) {
910 if (pdata->init(&pdev->dev) != 0) {
911 dev_dbg(mmc_dev(host->mmc),
912 "Unable to configure MMC IRQs\n");
913 goto err_irq_cd_init;
917 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
918 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
920 platform_set_drvdata(pdev, host);
923 if (host->pdata->slots[host->slot_id].name != NULL) {
924 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
932 mmc_remove_host(mmc);
934 free_irq(mmc_slot(host).card_detect_irq, host);
936 free_irq(host->irq, host);
938 clk_disable(host->fclk);
939 clk_disable(host->iclk);
942 if (host->dbclk_enabled) {
943 clk_disable(host->dbclk);
944 clk_put(host->dbclk);
950 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
951 release_mem_region(res->start, res->end - res->start + 1);
957 static int omap_mmc_remove(struct platform_device *pdev)
959 struct mmc_omap_host *host = platform_get_drvdata(pdev);
960 struct resource *res;
963 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
965 * Set the vdd back to 3V,
966 * applicable for dual volt support.
968 vdd = fls(host->mmc->ocr_avail) - 1;
969 if (omap_mmc_switch_opcond(host, vdd) != 0)
970 host->mmc->ios.vdd = vdd;
973 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
975 release_mem_region(res->start, res->end - res->start + 1);
977 platform_set_drvdata(pdev, NULL);
979 mmc_remove_host(host->mmc);
980 if (host->pdata->cleanup)
981 host->pdata->cleanup(&pdev->dev);
982 free_irq(host->irq, host);
983 if (mmc_slot(host).card_detect_irq)
984 free_irq(mmc_slot(host).card_detect_irq, host);
985 flush_scheduled_work();
987 clk_disable(host->fclk);
988 clk_disable(host->iclk);
991 if (host->dbclk_enabled) {
992 clk_disable(host->dbclk);
993 clk_put(host->dbclk);
996 mmc_free_host(host->mmc);
1004 static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1007 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1009 if (host && host->suspended)
1013 ret = mmc_suspend_host(host->mmc, state);
1015 host->suspended = 1;
1017 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1018 OMAP_HSMMC_WRITE(host->base, IE, 0);
1020 if (host->pdata->suspend) {
1021 ret = host->pdata->suspend(&pdev->dev, host->slot_id);
1023 dev_dbg(mmc_dev(host->mmc),
1024 "Unable to handle MMC board"
1025 " level suspend\n");
1028 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
1029 OMAP_HSMMC_WRITE(host->base, HCTL,
1030 OMAP_HSMMC_READ(host->base, HCTL)
1032 OMAP_HSMMC_WRITE(host->base, HCTL,
1033 OMAP_HSMMC_READ(host->base, HCTL)
1035 OMAP_HSMMC_WRITE(host->base, HCTL,
1036 OMAP_HSMMC_READ(host->base, HCTL)
1040 clk_disable(host->fclk);
1041 clk_disable(host->iclk);
1042 clk_disable(host->dbclk);
1049 /* Routine to resume the MMC device */
1050 static int omap_mmc_resume(struct platform_device *pdev)
1053 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1055 if (host && !host->suspended)
1060 ret = clk_enable(host->fclk);
1064 ret = clk_enable(host->iclk);
1066 clk_disable(host->fclk);
1067 clk_put(host->fclk);
1071 if (clk_enable(host->dbclk) != 0)
1072 dev_dbg(mmc_dev(host->mmc),
1073 "Enabling debounce clk failed\n");
1075 if (host->pdata->resume) {
1076 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1078 dev_dbg(mmc_dev(host->mmc),
1079 "Unmask interrupt failed\n");
1082 /* Notify the core to resume the host */
1083 ret = mmc_resume_host(host->mmc);
1085 host->suspended = 0;
1091 dev_dbg(mmc_dev(host->mmc),
1092 "Failed to enable MMC clocks during resume\n");
1097 #define omap_mmc_suspend NULL
1098 #define omap_mmc_resume NULL
1101 static struct platform_driver omap_mmc_driver = {
1102 .probe = omap_mmc_probe,
1103 .remove = omap_mmc_remove,
1104 .suspend = omap_mmc_suspend,
1105 .resume = omap_mmc_resume,
1107 .name = DRIVER_NAME,
1108 .owner = THIS_MODULE,
1112 static int __init omap_mmc_init(void)
1114 /* Register the MMC driver */
1115 return platform_driver_register(&omap_mmc_driver);
1118 static void __exit omap_mmc_cleanup(void)
1120 /* Unregister MMC driver */
1121 platform_driver_unregister(&omap_mmc_driver);
1124 module_init(omap_mmc_init);
1125 module_exit(omap_mmc_cleanup);
1127 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1128 MODULE_LICENSE("GPL");
1129 MODULE_ALIAS("platform:" DRIVER_NAME);
1130 MODULE_AUTHOR("Texas Instruments Inc");