2 * linux/drivers/media/mmc/omap.c
4 * Copyright (C) 2004 Nokia Corporation
5 * Written by Tuukka Tikkanen and Juha Yrjölä <juha.yrjola@nokia.com>
6 * Pin multiplexing and Innovator support by Tony Lindgren <tony@atomide.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/ioport.h>
18 #include <linux/device.h>
19 #include <linux/interrupt.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/delay.h>
22 #include <linux/spinlock.h>
23 #include <linux/timer.h>
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/protocol.h>
26 #include <linux/mmc/card.h>
30 #include <asm/scatterlist.h>
31 #include <asm/mach-types.h>
33 #include <asm/arch/board.h>
34 #include <asm/arch/gpio.h>
35 #include <asm/arch/dma.h>
36 #include <asm/arch/mux.h>
37 #include <asm/arch/fpga.h>
38 #include <asm/arch/tps65010.h>
40 #include <asm/hardware/clock.h>
44 #define DRIVER_NAME "mmci-omap"
46 #ifdef CONFIG_MMC_DEBUG
47 //#define DBG(x...) printk(KERN_DEBUG x)
48 #define DBG(x...) printk(x)
50 #define DBG(x...) do { } while (0)
53 /* Specifies how often in millisecs to poll for card status changes
54 * when the cover switch is open */
55 #define OMAP_MMC_SWITCH_POLL_DELAY 500
57 static s16 mmc1_power_pin = -1,
59 static s16 mmc1_switch_pin = -1,
62 static int mmc_omap_enable_poll = 1;
64 struct mmc_omap_host {
67 struct mmc_request * mrq;
68 struct mmc_command * cmd;
69 struct mmc_data * data;
70 struct mmc_host * mmc;
72 unsigned char id; /* 16xx chips have 2 MMC blocks */
76 unsigned char bus_mode;
78 #define OMAP_MMC_DATADIR_NONE 0
79 #define OMAP_MMC_DATADIR_READ 1
80 #define OMAP_MMC_DATADIR_WRITE 2
81 unsigned char datadir;
87 struct completion dma_completion;
90 struct work_struct switch_work;
91 struct timer_list switch_timer;
92 int switch_last_state;
94 unsigned char sd_support;
98 mmc_omap_cover_is_open(struct mmc_omap_host *host)
100 if (host->switch_pin < 0)
102 return omap_get_gpio_datain(host->switch_pin);
106 mmc_omap_show_cover_switch(struct device *dev, char *buf)
108 struct mmc_omap_host *host = dev_get_drvdata(dev);
110 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(host) ? "open" : "closed");
113 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
116 mmc_omap_show_enable_poll(struct device *dev, char *buf)
118 return snprintf(buf, PAGE_SIZE, "%d\n", mmc_omap_enable_poll);
122 mmc_omap_store_enable_poll(struct device *dev, const char *buf, size_t size)
126 if (sscanf(buf, "%10d", &enable_poll) != 1)
129 if (enable_poll != mmc_omap_enable_poll) {
130 struct mmc_omap_host *host = dev_get_drvdata(dev);
132 mmc_omap_enable_poll = enable_poll;
133 if (enable_poll && host->switch_pin >= 0)
134 schedule_work(&host->switch_work);
139 static DEVICE_ATTR(enable_poll, 0664,
140 mmc_omap_show_enable_poll, mmc_omap_store_enable_poll);
143 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
149 DBG("MMC%d: CMD%d, argument 0x%08x", host->id, cmd->opcode, cmd->arg);
150 if (cmd->flags & MMC_RSP_SHORT)
151 DBG(", 32-bit response");
152 if (cmd->flags & MMC_RSP_LONG)
153 DBG(", 128-bit response");
154 if (cmd->flags & MMC_RSP_CRC)
156 if (cmd->flags & MMC_RSP_BUSY)
157 DBG(", busy notification");
165 /* Protocol layer does not provide response type,
166 * but our hardware needs to know exact type, not just size!
168 switch (cmd->flags & MMC_RSP_MASK) {
173 /* resp 1, resp 1b */
174 /* OR resp 3!! (assume this if bus is set opendrain) */
175 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
186 /* Protocol layer does not provide command type, but our hardware
188 * any data transfer means adtc type (but that information is not
189 * in command structure, so we flagged it into host struct.)
190 * However, telling bc, bcr and ac apart based on response is
192 * CMD0 = bc = resp0 CMD15 = ac = resp0
193 * CMD2 = bcr = resp2 CMD10 = ac = resp2
195 * Resolve to best guess with some exception testing:
196 * resp0 -> bc, except CMD15 = ac
197 * rest are ac, except if opendrain
200 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
201 } else if (resptype == 0 && cmd->opcode != 15) {
202 cmdtype = OMAP_MMC_CMDTYPE_BC;
203 } else if (host->bus_mode == MMC_BUSMODE_OPENDRAIN) {
204 cmdtype = OMAP_MMC_CMDTYPE_BCR;
206 cmdtype = OMAP_MMC_CMDTYPE_AC;
209 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
211 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
214 if (cmd->flags & MMC_RSP_BUSY)
217 if (host->datadir == OMAP_MMC_DATADIR_READ)
222 OMAP_MMC_WRITE(host->base, CTO, 200);
223 OMAP_MMC_WRITE(host->base, ARGL, cmd->arg & 0xffff);
224 OMAP_MMC_WRITE(host->base, ARGH, cmd->arg >> 16);
225 OMAP_MMC_WRITE(host->base, IE,
226 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
227 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
228 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
229 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
230 OMAP_MMC_STAT_END_OF_DATA);
231 OMAP_MMC_WRITE(host->base, CMD, cmdreg);
235 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
238 host->datadir = OMAP_MMC_DATADIR_NONE;
240 if (data->error == MMC_ERR_NONE)
241 data->bytes_xfered += data->blocks * (1<<data->blksz_bits);
243 int dma_ch = host->dma_ch;
245 /* We got an error, let's free the DMA channel if it's
246 * still allocated. */
249 omap_free_dma(dma_ch);
253 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
257 clk_unuse(host->clk);
261 mmc_request_done(host->mmc, data->mrq);
265 mmc_omap_start_command(host, data->stop);
270 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
274 switch (cmd->flags & MMC_RSP_MASK) {
279 /* response types 1, 1b, 3, 4, 5, 6 */
281 OMAP_MMC_READ(host->base, RSP6) |
282 (OMAP_MMC_READ(host->base, RSP7) << 16);
283 DBG("MMC%d: Response %08x\n", host->id, cmd->resp[0]);
286 /* response type 2 */
288 OMAP_MMC_READ(host->base, RSP0) |
289 (OMAP_MMC_READ(host->base, RSP1) << 16);
291 OMAP_MMC_READ(host->base, RSP2) |
292 (OMAP_MMC_READ(host->base, RSP3) << 16);
294 OMAP_MMC_READ(host->base, RSP4) |
295 (OMAP_MMC_READ(host->base, RSP5) << 16);
297 OMAP_MMC_READ(host->base, RSP6) |
298 (OMAP_MMC_READ(host->base, RSP7) << 16);
299 DBG("MMC%d: Response %08x %08x %08x %08x\n", host->id,
300 cmd->resp[0], cmd->resp[1],
301 cmd->resp[2], cmd->resp[3]);
305 if (host->data == NULL || cmd->error != MMC_ERR_NONE) {
306 DBG("MMC%d: End request, err %x\n", host->id, cmd->error);
308 clk_unuse(host->clk);
309 mmc_request_done(host->mmc, cmd->mrq);
313 static irqreturn_t mmc_omap_irq(int irq, void *dev_id, struct pt_regs *regs)
315 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
321 if (host->cmd == NULL && host->data == NULL) {
322 status = OMAP_MMC_READ(host->base, STAT);
323 printk(KERN_INFO "MMC%d: Spurious interrupt 0x%04x\n", host->id, status);
325 OMAP_MMC_WRITE(host->base, STAT, status);
326 OMAP_MMC_WRITE(host->base, IE, 0);
334 while ((status = OMAP_MMC_READ(host->base, STAT)) != 0) {
335 OMAP_MMC_WRITE(host->base, STAT, status); // Reset status bits
336 DBG("\tMMC IRQ %04x (cmd %d)\n", status,
337 host->cmd != NULL ? host->cmd->opcode : -1);
339 if ((status & OMAP_MMC_STAT_A_FULL) ||
340 ((status & OMAP_MMC_STAT_END_OF_DATA) &&
341 (host->bytesleft > 0))) {
342 // Buffer almost full
343 ii = host->bytesleft / 2;
346 host->bytesleft -= ii * 2;
348 *host->buffer++ = OMAP_MMC_READ(host->base, DATA);
351 if (status & OMAP_MMC_STAT_A_EMPTY) {
352 // Buffer almost empty
353 ii = host->bytesleft / 2;
356 host->bytesleft -= ii * 2;
358 OMAP_MMC_WRITE(host->base, DATA, *host->buffer++);
361 if (status & OMAP_MMC_STAT_END_OF_DATA) {
362 // Block sent/received
366 if (status & OMAP_MMC_STAT_DATA_TOUT) {
368 printk(KERN_DEBUG "MMC%d: Data timeout\n", host->id);
370 host->data->error |= MMC_ERR_TIMEOUT;
375 if (status & OMAP_MMC_STAT_DATA_CRC) {
378 host->data->error |= MMC_ERR_BADCRC;
379 printk(KERN_DEBUG "MMC%d: Data CRC error, bytes left %d\n",
380 host->id, host->bytesleft);
383 printk(KERN_DEBUG "MMC%d: Data CRC error\n",
388 if (status & OMAP_MMC_STAT_CMD_TOUT) {
391 /* Timeouts are normal in case of MMC_SEND_STATUS */
392 if (host->cmd->opcode != MMC_ALL_SEND_CID &&
393 host->cmd->opcode != MMC_SEND_OP_COND &&
394 !mmc_omap_cover_is_open(host))
395 printk(KERN_ERR "MMC%d: Command timeout, CMD%d\n",
396 host->id, host->cmd->opcode);
397 host->cmd->error |= MMC_ERR_TIMEOUT;
402 if (status & OMAP_MMC_STAT_CMD_CRC) {
404 printk(KERN_ERR "MMC%d: Command CRC error\n", host->id);
406 host->cmd->error |= MMC_ERR_BADCRC;
411 if (status & OMAP_MMC_STAT_OCR_BUSY) {
413 if (host->cmd && host->cmd->opcode != MMC_SEND_OP_COND &&
414 host->cmd->opcode != MMC_SET_RELATIVE_ADDR) {
415 printk(KERN_DEBUG "MMC%d: OCR busy error, CMD%d\n",
416 host->id, host->cmd->opcode);
420 if (status & OMAP_MMC_STAT_CARD_ERR) {
422 printk(KERN_DEBUG "MMC%d: Card status error (CMD%d)\n",
423 host->id, host->cmd->opcode);
425 host->cmd->error |= MMC_ERR_FAILED;
429 host->data->error |= MMC_ERR_FAILED;
435 * NOTE: On 1610 the END_OF_CMD may come too early when
438 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
439 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
440 // End of command phase
446 mmc_omap_cmd_done(host, host->cmd);
449 mmc_omap_xfer_done(host, host->data);
455 static irqreturn_t mmc_omap_switch_irq(int irq, void *dev_id, struct pt_regs *regs)
457 struct mmc_omap_host *host = (struct mmc_omap_host *) dev_id;
459 DBG("MMC%d cover is now %s\n", host->id,
460 omap_get_gpio_datain(host->switch_pin) ? "open" : "closed");
461 schedule_work(&host->switch_work);
466 static void mmc_omap_switch_timer(unsigned long arg)
468 struct mmc_omap_host *host = (struct mmc_omap_host *) arg;
470 schedule_work(&host->switch_work);
473 static void mmc_omap_switch_handler(void *data)
475 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
476 struct mmc_card *card;
477 static int complained = 0;
478 int cards = 0, cover_open;
480 if (host->switch_pin == -1)
482 cover_open = mmc_omap_cover_is_open(host);
483 if (cover_open != host->switch_last_state) {
484 kobject_uevent(&host->dev->kobj, KOBJ_CHANGE, &dev_attr_cover_switch.attr);
485 host->switch_last_state = cover_open;
487 DBG("MMC cover switch handler started\n");
488 mmc_detect_change(host->mmc);
489 list_for_each_entry(card, &host->mmc->cards, node) {
490 if (mmc_card_present(card))
493 DBG("MMC%d: %d card(s) present\n", host->id, cards);
494 if (mmc_omap_cover_is_open(host)) {
496 printk(KERN_INFO "MMC%d: cover is open\n", host->id);
499 if (mmc_omap_enable_poll)
500 mod_timer(&host->switch_timer, jiffies +
501 msecs_to_jiffies(OMAP_MMC_SWITCH_POLL_DELAY));
507 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
509 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
512 /* FIXME: We ignore the possible errors for now. */
513 if (host->dma_ch < 0) {
514 printk(KERN_ERR "MMC%d: DMA callback while DMA not enabled?\n",
518 dma_ch = host->dma_ch;
521 omap_free_dma(dma_ch);
522 complete(&host->dma_completion);
526 mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
528 int sync_dev, dma_ch, r;
529 const char *dev_name;
530 struct mmc_data *data = req->data;
534 /* If for some reason the DMA transfer is still active,
535 * we wait for it to complete. This shouldn't normally happen. */
536 if (host->dma_ch != -1)
537 wait_for_completion(&host->dma_completion);
539 init_completion(&host->dma_completion);
541 if (!(data->flags & MMC_DATA_WRITE)) {
543 sync_dev = OMAP_DMA_MMC_RX;
544 dev_name = "MMC1 read";
546 sync_dev = OMAP_DMA_MMC2_RX;
547 dev_name = "MMC2 read";
551 sync_dev = OMAP_DMA_MMC_TX;
552 dev_name = "MMC1 write";
554 sync_dev = OMAP_DMA_MMC2_TX;
555 dev_name = "MMC2 write";
558 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
561 printk("MMC%d: omap_request_dma() failed with %d\n",
566 /* FIFO is 32x2 bytes; use 32 word frames when possible */
567 frame = 1 << (data->blksz_bits - 1);
568 words = data->blocks * frame;
572 if (!(data->flags & MMC_DATA_WRITE)) {
573 buf = 0x800f | ((frame - 1) << 8);
574 omap_set_dma_src_params(dma_ch, OMAP_DMA_PORT_TIPB,
575 OMAP_DMA_AMODE_CONSTANT,
576 virt_to_phys((void __force *)host->base)
577 + OMAP_MMC_REG_DATA);
578 omap_set_dma_dest_params(dma_ch, OMAP_DMA_PORT_EMIFF,
579 OMAP_DMA_AMODE_POST_INC,
580 data->sg->dma_address);
581 omap_set_dma_dest_data_pack(dma_ch, 1);
582 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
584 buf = 0x0f80 | ((frame - 1) << 0);
585 omap_set_dma_dest_params(dma_ch, OMAP_DMA_PORT_TIPB,
586 OMAP_DMA_AMODE_CONSTANT,
587 virt_to_phys((void __force *)host->base)
588 + OMAP_MMC_REG_DATA);
589 omap_set_dma_src_params(dma_ch, OMAP_DMA_PORT_EMIFF,
590 OMAP_DMA_AMODE_POST_INC,
591 data->sg->dma_address);
592 omap_set_dma_src_data_pack(dma_ch, 1);
593 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
596 /* blocksize is usually 512 bytes; but not for some SD reads */
597 OMAP_MMC_WRITE(host->base, BUF, buf);
598 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
599 frame, words / frame, OMAP_DMA_SYNC_FRAME);
601 host->dma_ch = dma_ch;
602 omap_start_dma(dma_ch);
608 set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
612 reg = OMAP_MMC_READ(host->base, SDIO);
614 OMAP_MMC_WRITE(host->base, SDIO, reg);
615 /* Set maximum timeout */
616 OMAP_MMC_WRITE(host->base, CTO, 0xff);
619 static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
624 /* Convert ns to clock cycles by assuming 20MHz frequency
625 * 1 cycle at 20MHz = 500 ns
627 timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
629 /* Some cards require more time to do at least the first read operation */
630 timeout = timeout << 4;
632 /* Check if we need to use timeout multiplier register */
633 reg = OMAP_MMC_READ(host->base, SDIO);
634 if (timeout > 0xffff) {
639 OMAP_MMC_WRITE(host->base, SDIO, reg);
640 OMAP_MMC_WRITE(host->base, DTO, timeout);
643 static void mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
645 struct mmc_data *data = req->data;
646 enum dma_data_direction dma_data_dir;
650 host->datadir = OMAP_MMC_DATADIR_NONE;
651 OMAP_MMC_WRITE(host->base, BLEN, 0);
652 OMAP_MMC_WRITE(host->base, NBLK, 0);
653 OMAP_MMC_WRITE(host->base, BUF, 0);
654 set_cmd_timeout(host, req);
658 DBG("MMC%d: Data xfer (%s %s), DTO %d cycles + %d ns, %d blocks of %d bytes\n",
659 host->id, (data->flags & MMC_DATA_STREAM) ? "stream" : "block",
660 (data->flags & MMC_DATA_WRITE) ? "write" : "read",
661 data->timeout_clks, data->timeout_ns, data->blocks,
662 1 << data->blksz_bits);
664 OMAP_MMC_WRITE(host->base, NBLK, data->blocks - 1);
665 OMAP_MMC_WRITE(host->base, BLEN, (1 << data->blksz_bits) - 1);
666 set_data_timeout(host, req);
668 if (data->flags & MMC_DATA_WRITE) {
669 host->datadir = OMAP_MMC_DATADIR_WRITE;
670 dma_data_dir = DMA_TO_DEVICE;
672 host->datadir = OMAP_MMC_DATADIR_READ;
673 dma_data_dir = DMA_FROM_DEVICE;
676 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
680 if (unlikely(host->dma_len > 1))
684 if (mmc_omap_start_dma_transfer(host, req) == 0) {
689 /* Revert to CPU copy */
690 OMAP_MMC_WRITE(host->base, BUF, 0x1f1f);
691 host->buffer = page_address(data->sg->page) + data->sg->offset;
692 host->bytesleft = data->blocks * (1 << data->blksz_bits);
697 static inline int is_broken_card(struct mmc_card *card)
700 struct mmc_cid *c = &card->cid;
701 static const struct broken_card_cid {
707 { 0x00150000, "\x30\x30\x30\x30\x30\x30\x15\x00", 0x06, 0x03 },
710 for (i = 0; i < sizeof(broken_cards)/sizeof(broken_cards[0]); i++) {
711 const struct broken_card_cid *b = broken_cards + i;
713 if (b->manfid != c->manfid)
715 if (memcmp(b->prod_name, c->prod_name, sizeof(b->prod_name)) != 0)
717 if (b->hwrev != c->hwrev || b->fwrev != c->fwrev)
724 static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
726 struct mmc_omap_host *host = mmc_priv(mmc);
728 WARN_ON(host->mrq != NULL);
732 /* Some cards (vendor left unnamed to protect the guilty) seem to
733 * require this delay after power-up. Otherwise we'll get mysterious
735 if (req->cmd->opcode == MMC_SEND_CSD) {
736 struct mmc_card *card;
737 int broken_present = 0;
739 list_for_each_entry(card, &mmc->cards, node) {
740 if (is_broken_card(card)) {
745 if (broken_present) {
746 static int complained = 0;
749 printk(KERN_WARNING "MMC%d: Broken card workaround enabled\n",
753 if (in_interrupt()) {
755 printk(KERN_ERR "Sleeping in IRQ handler, FIXME please!\n");
759 set_current_state(TASK_UNINTERRUPTIBLE);
760 schedule_timeout(100 * HZ / 1000);
765 mmc_omap_prepare_data(host, req);
766 mmc_omap_start_command(host, req->cmd);
769 static void innovator_fpga_socket_power(int on)
771 #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP1510)
774 fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
775 OMAP1510_FPGA_POWER);
777 fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
778 OMAP1510_FPGA_POWER);
784 * Turn the socket power on/off. Innovator uses FPGA, most boards
787 static void mmc_omap_power(struct mmc_omap_host *host, int on)
790 if (machine_is_omap_innovator())
791 innovator_fpga_socket_power(1);
792 else if (machine_is_omap_h2())
793 tps65010_set_gpio_out_value(GPIO3, HIGH);
794 else if (machine_is_omap_h3())
795 /* GPIO 4 of TPS65010 sends SD_EN signal */
796 tps65010_set_gpio_out_value(GPIO4, HIGH);
798 if (host->power_pin >= 0)
799 omap_set_gpio_dataout(host->power_pin, 1);
801 if (machine_is_omap_innovator())
802 innovator_fpga_socket_power(0);
803 else if (machine_is_omap_h2())
804 tps65010_set_gpio_out_value(GPIO3, LOW);
805 else if (machine_is_omap_h3())
806 tps65010_set_gpio_out_value(GPIO4, LOW);
808 if (host->power_pin >= 0)
809 omap_set_gpio_dataout(host->power_pin, 0);
813 static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
815 struct mmc_omap_host *host = mmc_priv(mmc);
819 DBG("MMC%d: set_ios: clock %dHz busmode %d powermode %d Vdd %d.%02d\n",
820 host->id, ios->clock, ios->bus_mode, ios->power_mode,
821 ios->vdd / 100, ios->vdd % 100);
823 if (ios->power_mode == MMC_POWER_UP && ios->clock < 400000) {
824 /* Fix for broken stack */
827 realclock = ios->clock;
830 if (ios->clock == 0) {
833 dsor = 48000000 / realclock;
837 if (48000000 / dsor > realclock)
844 switch (ios->power_mode) {
846 mmc_omap_power(host, 0);
850 mmc_omap_power(host, 1);
855 host->bus_mode = ios->bus_mode;
857 /* On insanely high arm_per frequencies something sometimes
858 * goes somehow out of sync, and the POW bit is not being set,
859 * which results in the while loop below getting stuck.
860 * Writing to the CON register twice seems to do the trick. */
861 for (i = 0; i < 2; i++)
862 OMAP_MMC_WRITE(host->base, CON, dsor);
863 if (ios->power_mode == MMC_POWER_UP) {
864 /* Send clock cycles, poll completion */
865 OMAP_MMC_WRITE(host->base, IE, 0);
866 OMAP_MMC_WRITE(host->base, STAT, 0xffff);
867 OMAP_MMC_WRITE(host->base, CMD, 1<<7);
868 while (0 == (OMAP_MMC_READ(host->base, STAT) & 1));
869 OMAP_MMC_WRITE(host->base, STAT, 1);
871 clk_unuse(host->clk);
874 static struct mmc_host_ops mmc_omap_ops = {
875 .request = mmc_omap_request,
876 .set_ios = mmc_omap_set_ios,
879 static int __init mmc_omap_probe(struct device *dev)
881 struct platform_device *pdev = to_platform_device(dev);
882 struct mmc_host *mmc;
883 struct mmc_omap_host *host = NULL;
886 if (pdev->resource[0].flags != IORESOURCE_MEM
887 || pdev->resource[1].flags != IORESOURCE_IRQ) {
888 printk(KERN_ERR "mmc_omap_probe: invalid resource type\n");
892 if (!request_mem_region(pdev->resource[0].start,
893 pdev->resource[0].end - pdev->resource[0].start + 1,
895 dev_dbg(&pdev->dev, "request_mem_region failed\n");
899 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), dev);
905 host = mmc_priv(mmc);
908 host->id = (pdev->resource[0].start == IO_ADDRESS(OMAP_MMC1_BASE)) ? 1 : 2;
910 host->clk = clk_get(dev, (host->id == 1) ? "mmc1_ck" : "mmc2_ck");
911 if (IS_ERR(host->clk)) {
912 ret = PTR_ERR(host->clk);
917 omap_cfg_reg(MMC_CMD);
918 omap_cfg_reg(MMC_CLK);
919 omap_cfg_reg(MMC_DAT0);
920 if (cpu_is_omap1710()) {
921 omap_cfg_reg(M15_1710_MMC_CLKI);
922 omap_cfg_reg(P19_1710_MMC_CMDDIR);
923 omap_cfg_reg(P20_1710_MMC_DATDIR0);
925 if (host->sd_support) {
926 omap_cfg_reg(MMC_DAT1);
927 omap_cfg_reg(MMC_DAT2);
928 omap_cfg_reg(MMC_DAT3);
930 host->power_pin = mmc1_power_pin;
931 host->switch_pin = mmc1_switch_pin;
933 omap_cfg_reg(Y8_1610_MMC2_CMD);
934 omap_cfg_reg(Y10_1610_MMC2_CLK);
935 omap_cfg_reg(R18_1610_MMC2_CLKIN);
936 omap_cfg_reg(W8_1610_MMC2_DAT0);
937 if (host->sd_support) {
938 omap_cfg_reg(V8_1610_MMC2_DAT1);
939 omap_cfg_reg(W15_1610_MMC2_DAT2);
940 omap_cfg_reg(R10_1610_MMC2_DAT3);
943 /* These are needed for the level shifter */
944 omap_cfg_reg(V9_1610_MMC2_CMDDIR);
945 omap_cfg_reg(V5_1610_MMC2_DATDIR0);
946 omap_cfg_reg(W19_1610_MMC2_DATDIR1);
948 host->power_pin = mmc2_power_pin;
949 host->switch_pin = mmc2_switch_pin;
951 /* Feedback clock must be set on OMAP-1710 MMC2 */
952 if (cpu_is_omap1710())
953 omap_writel(omap_readl(MOD_CONF_CTRL_1) | (1 << 24),
960 host->irq = pdev->resource[1].start;
961 host->base = (void __iomem *)pdev->resource[0].start;
963 mmc->ops = &mmc_omap_ops;
965 mmc->f_max = 24000000;
966 mmc->ocr_avail = MMC_VDD_33_34;
969 mmc->max_phys_segs = 1;
970 mmc->max_seg_size = PAGE_SIZE;
972 if (host->power_pin >= 0) {
973 if ((ret = omap_request_gpio(host->power_pin)) != 0) {
974 printk(KERN_ERR "MMC%d: Unable to get GPIO pin for MMC power\n",
978 omap_set_gpio_direction(host->power_pin, 0);
981 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
986 dev_set_drvdata(dev, host);
990 if (host->switch_pin >= 0) {
991 INIT_WORK(&host->switch_work, mmc_omap_switch_handler, host);
992 init_timer(&host->switch_timer);
993 host->switch_timer.function = mmc_omap_switch_timer;
994 host->switch_timer.data = (unsigned long) host;
995 if (omap_request_gpio(host->switch_pin) != 0) {
996 printk(KERN_WARNING "MMC%d: Unable to get GPIO pin for MMC cover switch\n",
998 host->switch_pin = -1;
1002 omap_set_gpio_direction(host->switch_pin, 1);
1003 set_irq_type(OMAP_GPIO_IRQ(host->switch_pin), IRQT_RISING);
1004 ret = request_irq(OMAP_GPIO_IRQ(host->switch_pin),
1005 mmc_omap_switch_irq, 0, DRIVER_NAME, host);
1007 printk(KERN_WARNING "MMC%d: Unable to get IRQ for MMC cover switch\n",
1009 omap_free_gpio(host->switch_pin);
1010 host->switch_pin = -1;
1013 ret = device_create_file(dev, &dev_attr_cover_switch);
1015 ret = device_create_file(dev, &dev_attr_enable_poll);
1017 device_remove_file(dev, &dev_attr_cover_switch);
1020 printk(KERN_WARNING "MMC%d: Unable to create sysfs attributes\n",
1022 free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
1023 omap_free_gpio(host->switch_pin);
1024 host->switch_pin = -1;
1027 if (mmc_omap_enable_poll && mmc_omap_cover_is_open(host))
1028 schedule_work(&host->switch_work);
1033 /* FIXME: Free other resources too. */
1035 if (host->clk && !IS_ERR(host->clk))
1037 mmc_free_host(host->mmc);
1042 static int __exit mmc_omap_remove(struct device *dev)
1044 struct platform_device *pdev = to_platform_device(dev);
1045 struct mmc_omap_host *host = dev_get_drvdata(dev);
1047 dev_set_drvdata(dev, NULL);
1050 mmc_remove_host(host->mmc);
1051 free_irq(host->irq, host);
1052 mmc_omap_power(host, 0);
1054 if (host->power_pin >= 0)
1055 omap_free_gpio(host->power_pin);
1056 if (host->switch_pin >= 0) {
1057 device_remove_file(dev, &dev_attr_enable_poll);
1058 device_remove_file(dev, &dev_attr_cover_switch);
1059 free_irq(OMAP_GPIO_IRQ(host->switch_pin), host);
1060 omap_free_gpio(host->switch_pin);
1061 host->switch_pin = -1;
1062 del_timer_sync(&host->switch_timer);
1063 flush_scheduled_work();
1065 if (host->clk && !IS_ERR(host->clk))
1067 mmc_free_host(host->mmc);
1070 release_mem_region(pdev->resource[0].start,
1071 pdev->resource[0].end - pdev->resource[0].start + 1);
1077 static int mmc_omap_suspend(struct device *dev, pm_message_t mesg, u32 level)
1080 struct mmc_omap_host *host = dev_get_drvdata(dev);
1082 if (host && host->suspended)
1085 if (!irqs_disabled())
1089 ret = mmc_suspend_host(host->mmc, mesg);
1091 host->suspended = 1;
1096 static int mmc_omap_resume(struct device *dev, u32 level)
1099 struct mmc_omap_host *host = dev_get_drvdata(dev);
1101 if (host && !host->suspended)
1105 ret = mmc_resume_host(host->mmc);
1107 host->suspended = 0;
1113 #define mmc_omap_suspend NULL
1114 #define mmc_omap_resume NULL
1117 static void mmc_release(struct device *dev)
1119 /* Nothing to release? */
1122 static struct resource mmc1_resources[] = {
1124 .start = IO_ADDRESS(OMAP_MMC1_BASE),
1125 .end = IO_ADDRESS(OMAP_MMC1_BASE) + 0x7f,
1126 .flags = IORESOURCE_MEM,
1130 .flags = IORESOURCE_IRQ,
1134 static struct resource mmc2_resources[] = {
1136 .start = IO_ADDRESS(OMAP_MMC2_BASE),
1137 .end = IO_ADDRESS(OMAP_MMC2_BASE) + 0x7f,
1138 .flags = IORESOURCE_MEM,
1141 .start = INT_1610_MMC2,
1142 .flags = IORESOURCE_IRQ,
1147 static u64 mmc_dmamask = 0xffffffff;
1149 static struct platform_device mmc_omap_device1 = {
1150 .name = "mmci-omap",
1153 .release = mmc_release,
1154 .dma_mask = &mmc_dmamask,
1156 .num_resources = ARRAY_SIZE(mmc1_resources),
1157 .resource = mmc1_resources,
1160 static struct platform_device mmc_omap_device2 = {
1161 .name = "mmci-omap",
1164 .release = mmc_release,
1165 .dma_mask = &mmc_dmamask,
1167 .num_resources = ARRAY_SIZE(mmc2_resources),
1168 .resource = mmc2_resources,
1171 static struct device_driver mmc_omap_driver = {
1172 .name = "mmci-omap",
1173 .bus = &platform_bus_type,
1174 .probe = mmc_omap_probe,
1175 .remove = __exit_p(mmc_omap_remove),
1176 .suspend = mmc_omap_suspend,
1177 .resume = mmc_omap_resume,
1180 static int enable_blocks = 0;
1182 static int __init mmc_omap_init(void)
1185 const struct omap_mmc_config *minfo;
1187 minfo = omap_get_config(OMAP_TAG_MMC, struct omap_mmc_config);
1188 if (minfo != NULL) {
1189 enable_blocks = minfo->mmc_blocks;
1190 if (enable_blocks & 1) {
1191 mmc1_power_pin = minfo->mmc1_power_pin;
1192 mmc1_switch_pin = minfo->mmc1_switch_pin;
1194 if (enable_blocks & 2) {
1195 mmc2_power_pin = minfo->mmc2_power_pin;
1196 mmc2_switch_pin = minfo->mmc2_switch_pin;
1199 #if defined(CONFIG_ARCH_OMAP1510) || defined(CONFIG_MMC_OMAP16XX_BLOCK1)
1202 #if defined(CONFIG_MMC_OMAP16XX_BLOCK2)
1206 if (enable_blocks == 0) {
1207 printk(KERN_INFO "OMAP MMC driver not loaded\n");
1211 if (enable_blocks & 1) {
1212 ret = platform_device_register(&mmc_omap_device1);
1217 if (enable_blocks & 2) {
1218 ret = platform_device_register(&mmc_omap_device2);
1223 ret = driver_register(&mmc_omap_driver);
1227 if (enable_blocks & 2)
1228 platform_device_unregister(&mmc_omap_device2);
1231 if (enable_blocks & 1)
1232 platform_device_unregister(&mmc_omap_device1);
1237 static void __exit mmc_omap_exit(void)
1239 driver_unregister(&mmc_omap_driver);
1241 if (enable_blocks & 2)
1242 platform_device_unregister(&mmc_omap_device2);
1244 if (enable_blocks & 1)
1245 platform_device_unregister(&mmc_omap_device1);
1248 module_init(mmc_omap_init);
1249 module_exit(mmc_omap_exit);
1251 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1252 MODULE_LICENSE("GPL");
1253 MODULE_AUTHOR("Juha Yrjölä");