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 SDVS33 (0x7 << 9)
59 #define SDVS_MASK 0x00000E00
60 #define SDVSCLR 0xFFFFF1FF
61 #define SDVSDET 0x00000400
68 #define CLKD_MASK 0x0000FFC0
70 #define DTO_MASK 0x000F0000
72 #define INT_EN_MASK 0x307F0033
73 #define INIT_STREAM (1 << 1)
74 #define DP_SELECT (1 << 21)
79 #define FOUR_BIT (1 << 1)
85 #define CMD_TIMEOUT (1 << 16)
86 #define DATA_TIMEOUT (1 << 20)
87 #define CMD_CRC (1 << 17)
88 #define DATA_CRC (1 << 21)
89 #define CARD_ERR (1 << 28)
90 #define STAT_CLEAR 0xFFFFFFFF
91 #define INIT_STREAM_CMD 0x00000000
92 #define DUAL_VOLT_OCR_BIT 7
97 * FIXME: Most likely all the data using these _DEVID defines should come
98 * from the platform_data, or implemented in controller and slot specific
101 #define OMAP_MMC1_DEVID 0
102 #define OMAP_MMC2_DEVID 1
103 #define OMAP_MMC3_DEVID 2
105 #define MMC_TIMEOUT_MS 20
106 #define OMAP_MMC_MASTER_CLOCK 96000000
107 #define DRIVER_NAME "mmci-omap-hs"
110 * One controller can have multiple slots, like on some omap boards using
111 * omap.c controller driver. Luckily this is not currently done on any known
112 * omap_hsmmc.c device.
114 #define mmc_slot(host) (host->pdata->slots[host->slot_id])
117 * MMC Host controller read/write API's
119 #define OMAP_HSMMC_READ(base, reg) \
120 __raw_readl((base) + OMAP_HSMMC_##reg)
122 #define OMAP_HSMMC_WRITE(base, reg, val) \
123 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
125 struct mmc_omap_host {
127 struct mmc_host *mmc;
128 struct mmc_request *mrq;
129 struct mmc_command *cmd;
130 struct mmc_data *data;
134 struct semaphore sem;
135 struct work_struct mmc_carddetect_work;
137 resource_size_t mapbase;
139 unsigned int dma_len;
140 unsigned int dma_sg_idx;
141 unsigned char bus_mode;
148 int dma_line_tx, dma_line_rx;
152 struct omap_mmc_platform_data *pdata;
156 * Stop clock to the card
158 static void omap_mmc_stop_clock(struct mmc_omap_host *host)
160 OMAP_HSMMC_WRITE(host->base, SYSCTL,
161 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
162 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
163 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
167 * Send init stream sequence to card
168 * before sending IDLE command
170 static void send_init_stream(struct mmc_omap_host *host)
173 unsigned long timeout;
175 disable_irq(host->irq);
176 OMAP_HSMMC_WRITE(host->base, CON,
177 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
178 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
180 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
181 while ((reg != CC) && time_before(jiffies, timeout))
182 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
184 OMAP_HSMMC_WRITE(host->base, CON,
185 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
186 enable_irq(host->irq);
190 int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
194 if (host->pdata->slots[host->slot_id].get_cover_state)
195 r = host->pdata->slots[host->slot_id].get_cover_state(host->dev,
201 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
204 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
205 struct mmc_omap_host *host = mmc_priv(mmc);
207 return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
211 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
214 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
217 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
218 struct mmc_omap_host *host = mmc_priv(mmc);
219 struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
221 return sprintf(buf, "%s\n", slot.name);
224 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
227 * Configure the response type and send the cmd.
230 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
231 struct mmc_data *data)
233 int cmdreg = 0, resptype = 0, cmdtype = 0;
235 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
236 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
240 * Clear status bits and enable interrupts
242 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
243 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
244 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
246 host->response_busy = 0;
247 if (cmd->flags & MMC_RSP_PRESENT) {
248 if (cmd->flags & MMC_RSP_136)
250 else if (cmd->flags & MMC_RSP_BUSY) {
252 host->response_busy = 1;
258 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
259 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
260 * a val of 0x3, rest 0x0.
262 if (cmd == host->mrq->stop)
265 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
268 cmdreg |= DP_SELECT | MSBS | BCE;
269 if (data->flags & MMC_DATA_READ)
278 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
279 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
283 mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
285 if (data->flags & MMC_DATA_WRITE)
286 return DMA_TO_DEVICE;
288 return DMA_FROM_DEVICE;
292 * Notify the transfer complete to MMC core
295 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
298 struct mmc_request *mrq = host->mrq;
301 mmc_request_done(host->mmc, mrq);
307 if (host->use_dma && host->dma_ch != -1)
308 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
309 mmc_omap_get_dma_dir(host, data));
312 data->bytes_xfered += data->blocks * (data->blksz);
314 data->bytes_xfered = 0;
318 mmc_request_done(host->mmc, data->mrq);
321 mmc_omap_start_command(host, data->stop, NULL);
325 * Notify the core about command completion
328 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
332 if (cmd->flags & MMC_RSP_PRESENT) {
333 if (cmd->flags & MMC_RSP_136) {
334 /* response type 2 */
335 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
336 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
337 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
338 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
340 /* response types 1, 1b, 3, 4, 5, 6 */
341 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
344 if ((host->data == NULL && !host->response_busy) || cmd->error) {
346 mmc_request_done(host->mmc, cmd->mrq);
351 * DMA clean up for command errors
353 static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
355 host->data->error = errno;
357 if (host->use_dma && host->dma_ch != -1) {
358 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
359 mmc_omap_get_dma_dir(host, host->data));
360 omap_free_dma(host->dma_ch);
368 * Readable error output
370 #ifdef CONFIG_MMC_DEBUG
371 static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
373 /* --- means reserved bit without definition at documentation */
374 static const char *mmc_omap_status_bits[] = {
375 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
376 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
377 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
378 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
384 len = sprintf(buf, "MMC IRQ 0x%x :", status);
387 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
388 if (status & (1 << i)) {
389 len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
393 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
395 #endif /* CONFIG_MMC_DEBUG */
398 * MMC controller internal state machines reset
400 * Used to reset command or data internal state machines, using respectively
401 * SRC or SRD bit of SYSCTL register
402 * Can be called from interrupt context
404 static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
408 unsigned long limit = (loops_per_jiffy *
409 msecs_to_jiffies(MMC_TIMEOUT_MS));
411 OMAP_HSMMC_WRITE(host->base, SYSCTL,
412 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
414 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
418 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
419 dev_err(mmc_dev(host->mmc),
420 "Timeout waiting on controller reset in %s\n",
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->mrq == NULL) {
434 OMAP_HSMMC_WRITE(host->base, STAT,
435 OMAP_HSMMC_READ(host->base, STAT));
436 /* Flush posted write */
437 OMAP_HSMMC_READ(host->base, STAT);
442 status = OMAP_HSMMC_READ(host->base, STAT);
443 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
446 #ifdef CONFIG_MMC_DEBUG
447 mmc_omap_report_irq(host, status);
449 if ((status & CMD_TIMEOUT) ||
450 (status & CMD_CRC)) {
452 if (status & CMD_TIMEOUT) {
453 mmc_omap_reset_controller_fsm(host, SRC);
454 host->cmd->error = -ETIMEDOUT;
456 host->cmd->error = -EILSEQ;
460 if (host->data || host->response_busy) {
462 mmc_dma_cleanup(host, -ETIMEDOUT);
463 host->response_busy = 0;
464 mmc_omap_reset_controller_fsm(host, SRD);
467 if ((status & DATA_TIMEOUT) ||
468 (status & DATA_CRC)) {
469 if (host->data || host->response_busy) {
470 int err = (status & DATA_TIMEOUT) ?
471 -ETIMEDOUT : -EILSEQ;
474 mmc_dma_cleanup(host, err);
476 host->mrq->cmd->error = err;
477 host->response_busy = 0;
478 mmc_omap_reset_controller_fsm(host, SRD);
482 if (status & CARD_ERR) {
483 dev_dbg(mmc_dev(host->mmc),
484 "Ignoring card err CMD%d\n", host->cmd->opcode);
492 OMAP_HSMMC_WRITE(host->base, STAT, status);
493 /* Flush posted write */
494 OMAP_HSMMC_READ(host->base, STAT);
496 if (end_cmd || ((status & CC) && host->cmd))
497 mmc_omap_cmd_done(host, host->cmd);
498 if (end_trans || (status & TC))
499 mmc_omap_xfer_done(host, data);
504 static void set_sd_bus_power(struct mmc_omap_host *host)
508 OMAP_HSMMC_WRITE(host->base, HCTL,
509 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
510 for (i = 0; i < loops_per_jiffy; i++) {
511 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
518 * Switch MMC interface voltage ... only relevant for MMC1.
520 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
521 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
522 * Some chips, like eMMC ones, use internal transceivers.
524 static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
529 /* Disable the clocks */
530 clk_disable(host->fclk);
531 clk_disable(host->iclk);
532 clk_disable(host->dbclk);
534 /* Turn the power off */
535 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
539 /* Turn the power ON with given VDD 1.8 or 3.0v */
540 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
544 clk_enable(host->fclk);
545 clk_enable(host->iclk);
546 clk_enable(host->dbclk);
548 OMAP_HSMMC_WRITE(host->base, HCTL,
549 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
550 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
553 * If a MMC dual voltage card is detected, the set_ios fn calls
554 * this fn with VDD bit set for 1.8V. Upon card removal from the
555 * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
557 * Cope with a bit of slop in the range ... per data sheets:
558 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
559 * but recommended values are 1.71V to 1.89V
560 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
561 * but recommended values are 2.7V to 3.3V
563 * Board setup code shouldn't permit anything very out-of-range.
564 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
565 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
567 if ((1 << vdd) <= MMC_VDD_23_24)
572 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
573 set_sd_bus_power(host);
577 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
582 * Work Item to notify the core about card insertion/removal
584 static void mmc_omap_detect(struct work_struct *work)
586 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
587 mmc_carddetect_work);
588 struct omap_mmc_slot_data *slot = &mmc_slot(host);
590 if (mmc_slot(host).card_detect)
591 host->carddetect = slot->card_detect(slot->card_detect_irq);
593 host->carddetect = -ENOSYS;
595 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
596 if (host->carddetect) {
597 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
599 mmc_omap_reset_controller_fsm(host, SRD);
600 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
605 * ISR for handling card insertion and removal
607 static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
609 struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
611 schedule_work(&host->mmc_carddetect_work);
616 static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
617 struct mmc_data *data)
621 if (data->flags & MMC_DATA_WRITE)
622 sync_dev = host->dma_line_tx;
624 sync_dev = host->dma_line_rx;
628 static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
629 struct mmc_data *data,
630 struct scatterlist *sgl)
632 int blksz, nblk, dma_ch;
634 dma_ch = host->dma_ch;
635 if (data->flags & MMC_DATA_WRITE) {
636 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
637 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
638 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
639 sg_dma_address(sgl), 0, 0);
641 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
642 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
643 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
644 sg_dma_address(sgl), 0, 0);
647 blksz = host->data->blksz;
648 nblk = sg_dma_len(sgl) / blksz;
650 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
651 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
652 mmc_omap_get_dma_sync_dev(host, data),
653 !(data->flags & MMC_DATA_WRITE));
655 omap_start_dma(dma_ch);
659 * DMA call back function
661 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
663 struct mmc_omap_host *host = data;
665 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
666 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
668 if (host->dma_ch < 0)
672 if (host->dma_sg_idx < host->dma_len) {
673 /* Fire up the next transfer. */
674 mmc_omap_config_dma_params(host, host->data,
675 host->data->sg + host->dma_sg_idx);
679 omap_free_dma(host->dma_ch);
682 * DMA Callback: run in interrupt context.
683 * mutex_unlock will through a kernel warning if used.
689 * Routine to configure and start DMA for the MMC card
692 mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
694 int dma_ch = 0, ret = 0, err = 1, i;
695 struct mmc_data *data = req->data;
697 /* Sanity check: all the SG entries must be aligned by block size. */
698 for (i = 0; i < host->dma_len; i++) {
699 struct scatterlist *sgl;
702 if (sgl->length % data->blksz)
705 if ((data->blksz % 4) != 0)
706 /* REVISIT: The MMC buffer increments only when MSB is written.
707 * Return error for blksz which is non multiple of four.
712 * If for some reason the DMA transfer is still active,
713 * we wait for timeout period and free the dma
715 if (host->dma_ch != -1) {
716 set_current_state(TASK_UNINTERRUPTIBLE);
717 schedule_timeout(100);
718 if (down_trylock(&host->sem)) {
719 omap_free_dma(host->dma_ch);
725 if (down_trylock(&host->sem))
729 ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
730 mmc_omap_dma_cb,host, &dma_ch);
732 dev_err(mmc_dev(host->mmc),
733 "%s: omap_request_dma() failed with %d\n",
734 mmc_hostname(host->mmc), ret);
738 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
739 data->sg_len, mmc_omap_get_dma_dir(host, data));
740 host->dma_ch = dma_ch;
741 host->dma_sg_idx = 0;
743 mmc_omap_config_dma_params(host, data, data->sg);
748 static void set_data_timeout(struct mmc_omap_host *host,
749 struct mmc_request *req)
751 unsigned int timeout, cycle_ns;
752 uint32_t reg, clkd, dto = 0;
754 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
755 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
759 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
760 timeout = req->data->timeout_ns / cycle_ns;
761 timeout += req->data->timeout_clks;
763 while ((timeout & 0x80000000) == 0) {
780 reg |= dto << DTO_SHIFT;
781 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
785 * Configure block length for MMC/SD cards and initiate the transfer.
788 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
791 host->data = req->data;
793 if (req->data == NULL) {
794 OMAP_HSMMC_WRITE(host->base, BLK, 0);
798 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
799 | (req->data->blocks << 16));
800 set_data_timeout(host, req);
803 ret = mmc_omap_start_dma_transfer(host, req);
805 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
813 * Request function. for read/write operation
815 static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
817 struct mmc_omap_host *host = mmc_priv(mmc);
819 WARN_ON(host->mrq != NULL);
821 mmc_omap_prepare_data(host, req);
822 mmc_omap_start_command(host, req->cmd, req->data);
826 /* Routine to configure clock values. Exposed API to core */
827 static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
829 struct mmc_omap_host *host = mmc_priv(mmc);
831 unsigned long regval;
832 unsigned long timeout;
835 switch (ios->power_mode) {
837 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
840 mmc_slot(host).set_power(host->dev, host->slot_id, 1, ios->vdd);
844 con = OMAP_HSMMC_READ(host->base, CON);
845 switch (mmc->ios.bus_width) {
846 case MMC_BUS_WIDTH_8:
847 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
849 case MMC_BUS_WIDTH_4:
850 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
851 OMAP_HSMMC_WRITE(host->base, HCTL,
852 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
854 case MMC_BUS_WIDTH_1:
855 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
856 OMAP_HSMMC_WRITE(host->base, HCTL,
857 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
861 if (host->id == OMAP_MMC1_DEVID) {
862 /* Only MMC1 can interface at 3V without some flavor
863 * of external transceiver; but they all handle 1.8V.
865 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
866 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
868 * The mmc_select_voltage fn of the core does
869 * not seem to set the power_mode to
870 * MMC_POWER_UP upon recalculating the voltage.
873 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
874 dev_dbg(mmc_dev(host->mmc),
875 "Switch operation failed\n");
880 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
884 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
890 omap_mmc_stop_clock(host);
891 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
892 regval = regval & ~(CLKD_MASK);
893 regval = regval | (dsor << 6) | (DTO << 16);
894 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
895 OMAP_HSMMC_WRITE(host->base, SYSCTL,
896 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
898 /* Wait till the ICS bit is set */
899 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
900 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != 0x2
901 && time_before(jiffies, timeout))
904 OMAP_HSMMC_WRITE(host->base, SYSCTL,
905 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
907 if (ios->power_mode == MMC_POWER_ON)
908 send_init_stream(host);
910 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
911 OMAP_HSMMC_WRITE(host->base, CON,
912 OMAP_HSMMC_READ(host->base, CON) | OD);
915 static int omap_hsmmc_get_cd(struct mmc_host *mmc)
917 struct mmc_omap_host *host = mmc_priv(mmc);
918 struct omap_mmc_platform_data *pdata = host->pdata;
920 if (!pdata->slots[0].card_detect)
922 return pdata->slots[0].card_detect(pdata->slots[0].card_detect_irq);
925 static int omap_hsmmc_get_ro(struct mmc_host *mmc)
927 struct mmc_omap_host *host = mmc_priv(mmc);
928 struct omap_mmc_platform_data *pdata = host->pdata;
930 if (!pdata->slots[0].get_ro)
932 return pdata->slots[0].get_ro(host->dev, 0);
935 static void omap_hsmmc_init(struct mmc_omap_host *host)
937 u32 hctl, capa, value;
939 /* Only MMC1 supports 3.0V */
940 if (host->id == OMAP_MMC1_DEVID) {
948 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
949 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
951 value = OMAP_HSMMC_READ(host->base, CAPA);
952 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
954 /* Set the controller to AUTO IDLE mode */
955 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
956 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
958 /* Set SD bus power bit */
959 set_sd_bus_power(host);
962 static struct mmc_host_ops mmc_omap_ops = {
963 .request = omap_mmc_request,
964 .set_ios = omap_mmc_set_ios,
965 .get_cd = omap_hsmmc_get_cd,
966 .get_ro = omap_hsmmc_get_ro,
967 /* NYET -- enable_sdio_irq */
970 static int __init omap_mmc_probe(struct platform_device *pdev)
972 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
973 struct mmc_host *mmc;
974 struct mmc_omap_host *host = NULL;
975 struct resource *res;
979 dev_err(&pdev->dev, "Platform Data is missing\n");
983 if (pdata->nr_slots == 0) {
984 dev_err(&pdev->dev, "No Slots\n");
988 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
989 irq = platform_get_irq(pdev, 0);
990 if (res == NULL || irq < 0)
993 res = request_mem_region(res->start, res->end - res->start + 1,
998 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
1004 host = mmc_priv(mmc);
1006 host->pdata = pdata;
1007 host->dev = &pdev->dev;
1009 host->dev->dma_mask = &pdata->dma_mask;
1012 host->id = pdev->id;
1014 host->mapbase = res->start;
1015 host->base = ioremap(host->mapbase, SZ_4K);
1017 platform_set_drvdata(pdev, host);
1018 INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
1020 mmc->ops = &mmc_omap_ops;
1021 mmc->f_min = 400000;
1022 mmc->f_max = 52000000;
1024 sema_init(&host->sem, 1);
1026 host->iclk = clk_get(&pdev->dev, "ick");
1027 if (IS_ERR(host->iclk)) {
1028 ret = PTR_ERR(host->iclk);
1032 host->fclk = clk_get(&pdev->dev, "fck");
1033 if (IS_ERR(host->fclk)) {
1034 ret = PTR_ERR(host->fclk);
1036 clk_put(host->iclk);
1040 if (clk_enable(host->fclk) != 0) {
1041 clk_put(host->iclk);
1042 clk_put(host->fclk);
1046 if (clk_enable(host->iclk) != 0) {
1047 clk_disable(host->fclk);
1048 clk_put(host->iclk);
1049 clk_put(host->fclk);
1053 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1055 * MMC can still work without debounce clock.
1057 if (IS_ERR(host->dbclk))
1058 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
1060 if (clk_enable(host->dbclk) != 0)
1061 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1064 host->dbclk_enabled = 1;
1066 /* Since we do only SG emulation, we can have as many segs
1068 mmc->max_phys_segs = 1024;
1069 mmc->max_hw_segs = 1024;
1071 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1072 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1073 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1074 mmc->max_seg_size = mmc->max_req_size;
1076 mmc->ocr_avail = mmc_slot(host).ocr_mask;
1077 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1079 if (pdata->slots[host->slot_id].wires >= 8)
1080 mmc->caps |= MMC_CAP_8_BIT_DATA;
1081 else if (pdata->slots[host->slot_id].wires >= 4)
1082 mmc->caps |= MMC_CAP_4_BIT_DATA;
1084 omap_hsmmc_init(host);
1086 /* Select DMA lines */
1088 case OMAP_MMC1_DEVID:
1089 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1090 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1092 case OMAP_MMC2_DEVID:
1093 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
1094 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
1096 case OMAP_MMC3_DEVID:
1097 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
1098 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
1101 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
1105 /* Request IRQ for MMC operations */
1106 ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
1107 mmc_hostname(mmc), host);
1109 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1113 if (pdata->init != NULL) {
1114 if (pdata->init(&pdev->dev) != 0) {
1115 dev_dbg(mmc_dev(host->mmc),
1116 "Unable to configure MMC IRQs\n");
1117 goto err_irq_cd_init;
1121 /* Request IRQ for card detect */
1122 if ((mmc_slot(host).card_detect_irq)) {
1123 ret = request_irq(mmc_slot(host).card_detect_irq,
1124 omap_mmc_cd_handler,
1125 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1127 mmc_hostname(mmc), host);
1129 dev_dbg(mmc_dev(host->mmc),
1130 "Unable to grab MMC CD IRQ\n");
1135 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
1136 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
1140 if (host->pdata->slots[host->slot_id].name != NULL) {
1141 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1145 if (mmc_slot(host).card_detect_irq &&
1146 host->pdata->slots[host->slot_id].get_cover_state) {
1147 ret = device_create_file(&mmc->class_dev,
1148 &dev_attr_cover_switch);
1150 goto err_cover_switch;
1156 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1158 mmc_remove_host(mmc);
1160 free_irq(mmc_slot(host).card_detect_irq, host);
1162 free_irq(host->irq, host);
1164 clk_disable(host->fclk);
1165 clk_disable(host->iclk);
1166 clk_put(host->fclk);
1167 clk_put(host->iclk);
1168 if (host->dbclk_enabled) {
1169 clk_disable(host->dbclk);
1170 clk_put(host->dbclk);
1174 iounmap(host->base);
1176 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
1177 release_mem_region(res->start, res->end - res->start + 1);
1183 static int omap_mmc_remove(struct platform_device *pdev)
1185 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1186 struct resource *res;
1189 mmc_remove_host(host->mmc);
1190 if (host->pdata->cleanup)
1191 host->pdata->cleanup(&pdev->dev);
1192 free_irq(host->irq, host);
1193 if (mmc_slot(host).card_detect_irq)
1194 free_irq(mmc_slot(host).card_detect_irq, host);
1195 flush_scheduled_work();
1197 clk_disable(host->fclk);
1198 clk_disable(host->iclk);
1199 clk_put(host->fclk);
1200 clk_put(host->iclk);
1201 if (host->dbclk_enabled) {
1202 clk_disable(host->dbclk);
1203 clk_put(host->dbclk);
1206 mmc_free_host(host->mmc);
1207 iounmap(host->base);
1210 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1212 release_mem_region(res->start, res->end - res->start + 1);
1213 platform_set_drvdata(pdev, NULL);
1219 static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1222 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1224 if (host && host->suspended)
1228 ret = mmc_suspend_host(host->mmc, state);
1230 host->suspended = 1;
1232 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1233 OMAP_HSMMC_WRITE(host->base, IE, 0);
1235 if (host->pdata->suspend) {
1236 ret = host->pdata->suspend(&pdev->dev,
1239 dev_dbg(mmc_dev(host->mmc),
1240 "Unable to handle MMC board"
1241 " level suspend\n");
1244 OMAP_HSMMC_WRITE(host->base, HCTL,
1245 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
1246 clk_disable(host->fclk);
1247 clk_disable(host->iclk);
1248 clk_disable(host->dbclk);
1255 /* Routine to resume the MMC device */
1256 static int omap_mmc_resume(struct platform_device *pdev)
1259 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1261 if (host && !host->suspended)
1266 ret = clk_enable(host->fclk);
1270 ret = clk_enable(host->iclk);
1272 clk_disable(host->fclk);
1273 clk_put(host->fclk);
1277 if (clk_enable(host->dbclk) != 0)
1278 dev_dbg(mmc_dev(host->mmc),
1279 "Enabling debounce clk failed\n");
1281 omap_hsmmc_init(host);
1283 if (host->pdata->resume) {
1284 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1286 dev_dbg(mmc_dev(host->mmc),
1287 "Unmask interrupt failed\n");
1290 /* Notify the core to resume the host */
1291 ret = mmc_resume_host(host->mmc);
1293 host->suspended = 0;
1299 dev_dbg(mmc_dev(host->mmc),
1300 "Failed to enable MMC clocks during resume\n");
1305 #define omap_mmc_suspend NULL
1306 #define omap_mmc_resume NULL
1309 static struct platform_driver omap_mmc_driver = {
1310 .probe = omap_mmc_probe,
1311 .remove = omap_mmc_remove,
1312 .suspend = omap_mmc_suspend,
1313 .resume = omap_mmc_resume,
1315 .name = DRIVER_NAME,
1316 .owner = THIS_MODULE,
1320 static int __init omap_mmc_init(void)
1322 /* Register the MMC driver */
1323 return platform_driver_register(&omap_mmc_driver);
1326 static void __exit omap_mmc_cleanup(void)
1328 /* Unregister MMC driver */
1329 platform_driver_unregister(&omap_mmc_driver);
1332 module_init(omap_mmc_init);
1333 module_exit(omap_mmc_cleanup);
1335 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1336 MODULE_LICENSE("GPL");
1337 MODULE_ALIAS("platform:" DRIVER_NAME);
1338 MODULE_AUTHOR("Texas Instruments Inc");