2 * linux/drivers/mmc/host/omap.c
4 * Copyright (C) 2004 Nokia Corporation
5 * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
6 * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
7 * Other hacks (DMA, SD, etc) by David Brownell
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/ioport.h>
18 #include <linux/platform_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/card.h>
26 #include <linux/clk.h>
27 #include <linux/scatterlist.h>
28 #include <linux/i2c/tps65010.h>
32 #include <asm/mach-types.h>
34 #include <asm/arch/board.h>
35 #include <asm/arch/mmc.h>
36 #include <asm/arch/gpio.h>
37 #include <asm/arch/dma.h>
38 #include <asm/arch/mux.h>
39 #include <asm/arch/fpga.h>
41 #define OMAP_MMC_REG_CMD 0x00
42 #define OMAP_MMC_REG_ARGL 0x04
43 #define OMAP_MMC_REG_ARGH 0x08
44 #define OMAP_MMC_REG_CON 0x0c
45 #define OMAP_MMC_REG_STAT 0x10
46 #define OMAP_MMC_REG_IE 0x14
47 #define OMAP_MMC_REG_CTO 0x18
48 #define OMAP_MMC_REG_DTO 0x1c
49 #define OMAP_MMC_REG_DATA 0x20
50 #define OMAP_MMC_REG_BLEN 0x24
51 #define OMAP_MMC_REG_NBLK 0x28
52 #define OMAP_MMC_REG_BUF 0x2c
53 #define OMAP_MMC_REG_SDIO 0x34
54 #define OMAP_MMC_REG_REV 0x3c
55 #define OMAP_MMC_REG_RSP0 0x40
56 #define OMAP_MMC_REG_RSP1 0x44
57 #define OMAP_MMC_REG_RSP2 0x48
58 #define OMAP_MMC_REG_RSP3 0x4c
59 #define OMAP_MMC_REG_RSP4 0x50
60 #define OMAP_MMC_REG_RSP5 0x54
61 #define OMAP_MMC_REG_RSP6 0x58
62 #define OMAP_MMC_REG_RSP7 0x5c
63 #define OMAP_MMC_REG_IOSR 0x60
64 #define OMAP_MMC_REG_SYSC 0x64
65 #define OMAP_MMC_REG_SYSS 0x68
67 #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
68 #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
69 #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
70 #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
71 #define OMAP_MMC_STAT_A_FULL (1 << 10)
72 #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
73 #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
74 #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
75 #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
76 #define OMAP_MMC_STAT_END_BUSY (1 << 4)
77 #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
78 #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
79 #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
81 #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
82 #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
87 #define OMAP_MMC_CMDTYPE_BC 0
88 #define OMAP_MMC_CMDTYPE_BCR 1
89 #define OMAP_MMC_CMDTYPE_AC 2
90 #define OMAP_MMC_CMDTYPE_ADTC 3
93 #define DRIVER_NAME "mmci-omap"
95 /* Specifies how often in millisecs to poll for card status changes
96 * when the cover switch is open */
97 #define OMAP_MMC_SWITCH_POLL_DELAY 500
101 struct mmc_omap_slot {
106 unsigned int fclk_freq;
109 struct mmc_request *mrq;
110 struct mmc_omap_host *host;
111 struct mmc_host *mmc;
112 struct omap_mmc_slot_data *pdata;
115 struct mmc_omap_host {
118 struct mmc_request * mrq;
119 struct mmc_command * cmd;
120 struct mmc_data * data;
121 struct mmc_host * mmc;
123 unsigned char id; /* 16xx chips have 2 MMC blocks */
126 struct resource *mem_res;
127 void __iomem *virt_base;
128 unsigned int phys_base;
130 unsigned char bus_mode;
131 unsigned char hw_bus_mode;
136 u32 buffer_bytes_left;
137 u32 total_bytes_left;
140 unsigned brs_received:1, dma_done:1;
141 unsigned dma_is_read:1;
142 unsigned dma_in_use:1;
145 struct timer_list dma_timer;
150 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
151 struct mmc_omap_slot *current_slot;
152 spinlock_t slot_lock;
153 wait_queue_head_t slot_wq;
156 struct omap_mmc_platform_data *pdata;
159 static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
161 struct mmc_omap_host *host = slot->host;
166 spin_lock_irqsave(&host->slot_lock, flags);
167 while (host->mmc != NULL) {
168 spin_unlock_irqrestore(&host->slot_lock, flags);
169 wait_event(host->slot_wq, host->mmc == NULL);
170 spin_lock_irqsave(&host->slot_lock, flags);
172 host->mmc = slot->mmc;
173 spin_unlock_irqrestore(&host->slot_lock, flags);
175 clk_enable(host->fclk);
176 if (host->current_slot != slot) {
177 if (host->pdata->switch_slot != NULL)
178 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
179 host->current_slot = slot;
182 /* Doing the dummy read here seems to work around some bug
183 * at least in OMAP24xx silicon where the command would not
184 * start after writing the CMD register. Sigh. */
185 OMAP_MMC_READ(host, CON);
187 OMAP_MMC_WRITE(host, CON, slot->saved_con);
190 static void mmc_omap_start_request(struct mmc_omap_host *host,
191 struct mmc_request *req);
193 static void mmc_omap_release_slot(struct mmc_omap_slot *slot)
195 struct mmc_omap_host *host = slot->host;
199 BUG_ON(slot == NULL || host->mmc == NULL);
200 clk_disable(host->fclk);
202 spin_lock_irqsave(&host->slot_lock, flags);
203 /* Check for any pending requests */
204 for (i = 0; i < host->nr_slots; i++) {
205 struct mmc_omap_slot *new_slot;
206 struct mmc_request *rq;
208 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
211 new_slot = host->slots[i];
212 /* The current slot should not have a request in queue */
213 BUG_ON(new_slot == host->current_slot);
215 host->mmc = new_slot->mmc;
216 spin_unlock_irqrestore(&host->slot_lock, flags);
217 mmc_omap_select_slot(new_slot, 1);
219 new_slot->mrq = NULL;
220 mmc_omap_start_request(host, rq);
225 wake_up(&host->slot_wq);
226 spin_unlock_irqrestore(&host->slot_lock, flags);
230 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
233 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
234 struct mmc_omap_slot *slot = mmc_priv(mmc);
236 return sprintf(buf, "%s\n", slot->pdata->name);
239 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
242 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
253 /* Our hardware needs to know exact type */
254 switch (mmc_resp_type(cmd)) {
259 /* resp 1, 1b, 6, 7 */
269 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
273 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
274 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
275 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
276 cmdtype = OMAP_MMC_CMDTYPE_BC;
277 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
278 cmdtype = OMAP_MMC_CMDTYPE_BCR;
280 cmdtype = OMAP_MMC_CMDTYPE_AC;
283 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
285 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
288 if (cmd->flags & MMC_RSP_BUSY)
291 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
294 OMAP_MMC_WRITE(host, CTO, 200);
295 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
296 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
297 OMAP_MMC_WRITE(host, IE,
298 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
299 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
300 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
301 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
302 OMAP_MMC_STAT_END_OF_DATA);
303 OMAP_MMC_WRITE(host, CMD, cmdreg);
307 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
309 if (host->dma_in_use) {
310 enum dma_data_direction dma_data_dir;
312 BUG_ON(host->dma_ch < 0);
314 omap_stop_dma(host->dma_ch);
315 /* Release DMA channel lazily */
316 mod_timer(&host->dma_timer, jiffies + HZ);
317 if (data->flags & MMC_DATA_WRITE)
318 dma_data_dir = DMA_TO_DEVICE;
320 dma_data_dir = DMA_FROM_DEVICE;
321 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
326 clk_disable(host->fclk);
328 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
329 * dozens of requests until the card finishes writing data.
330 * It'd be cheaper to just wait till an EOFB interrupt arrives...
335 mmc_request_done(host->mmc, data->mrq);
339 mmc_omap_start_command(host, data->stop);
343 mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
348 if (!host->dma_in_use) {
349 mmc_omap_xfer_done(host, data);
353 spin_lock_irqsave(&host->dma_lock, flags);
357 host->brs_received = 1;
358 spin_unlock_irqrestore(&host->dma_lock, flags);
360 mmc_omap_xfer_done(host, data);
364 mmc_omap_dma_timer(unsigned long data)
366 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
368 BUG_ON(host->dma_ch < 0);
369 omap_free_dma(host->dma_ch);
374 mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
380 spin_lock_irqsave(&host->dma_lock, flags);
381 if (host->brs_received)
385 spin_unlock_irqrestore(&host->dma_lock, flags);
387 mmc_omap_xfer_done(host, data);
391 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
395 if (cmd->flags & MMC_RSP_PRESENT) {
396 if (cmd->flags & MMC_RSP_136) {
397 /* response type 2 */
399 OMAP_MMC_READ(host, RSP0) |
400 (OMAP_MMC_READ(host, RSP1) << 16);
402 OMAP_MMC_READ(host, RSP2) |
403 (OMAP_MMC_READ(host, RSP3) << 16);
405 OMAP_MMC_READ(host, RSP4) |
406 (OMAP_MMC_READ(host, RSP5) << 16);
408 OMAP_MMC_READ(host, RSP6) |
409 (OMAP_MMC_READ(host, RSP7) << 16);
411 /* response types 1, 1b, 3, 4, 5, 6 */
413 OMAP_MMC_READ(host, RSP6) |
414 (OMAP_MMC_READ(host, RSP7) << 16);
418 if (host->data == NULL || cmd->error) {
420 clk_disable(host->fclk);
421 mmc_request_done(host->mmc, cmd->mrq);
427 mmc_omap_sg_to_buf(struct mmc_omap_host *host)
429 struct scatterlist *sg;
431 sg = host->data->sg + host->sg_idx;
432 host->buffer_bytes_left = sg->length;
433 host->buffer = sg_virt(sg);
434 if (host->buffer_bytes_left > host->total_bytes_left)
435 host->buffer_bytes_left = host->total_bytes_left;
440 mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
444 if (host->buffer_bytes_left == 0) {
446 BUG_ON(host->sg_idx == host->sg_len);
447 mmc_omap_sg_to_buf(host);
450 if (n > host->buffer_bytes_left)
451 n = host->buffer_bytes_left;
452 host->buffer_bytes_left -= n;
453 host->total_bytes_left -= n;
454 host->data->bytes_xfered += n;
457 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
459 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
463 static inline void mmc_omap_report_irq(u16 status)
465 static const char *mmc_omap_status_bits[] = {
466 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
467 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
471 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
472 if (status & (1 << i)) {
475 printk("%s", mmc_omap_status_bits[i]);
480 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
482 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
488 if (host->cmd == NULL && host->data == NULL) {
489 status = OMAP_MMC_READ(host, STAT);
490 dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
492 OMAP_MMC_WRITE(host, STAT, status);
493 OMAP_MMC_WRITE(host, IE, 0);
502 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
503 OMAP_MMC_WRITE(host, STAT, status);
504 #ifdef CONFIG_MMC_DEBUG
505 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
506 status, host->cmd != NULL ? host->cmd->opcode : -1);
507 mmc_omap_report_irq(status);
510 if (host->total_bytes_left) {
511 if ((status & OMAP_MMC_STAT_A_FULL) ||
512 (status & OMAP_MMC_STAT_END_OF_DATA))
513 mmc_omap_xfer_data(host, 0);
514 if (status & OMAP_MMC_STAT_A_EMPTY)
515 mmc_omap_xfer_data(host, 1);
518 if (status & OMAP_MMC_STAT_END_OF_DATA) {
522 if (status & OMAP_MMC_STAT_DATA_TOUT) {
523 dev_dbg(mmc_dev(host->mmc), "data timeout\n");
525 host->data->error = -ETIMEDOUT;
530 if (status & OMAP_MMC_STAT_DATA_CRC) {
532 host->data->error = -EILSEQ;
533 dev_dbg(mmc_dev(host->mmc),
534 "data CRC error, bytes left %d\n",
535 host->total_bytes_left);
538 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
542 if (status & OMAP_MMC_STAT_CMD_TOUT) {
543 /* Timeouts are routine with some commands */
545 struct mmc_omap_slot *slot =
547 dev_err(mmc_dev(host->mmc),
548 "command timeout, CMD %d\n",
550 host->cmd->error = -ETIMEDOUT;
555 if (status & OMAP_MMC_STAT_CMD_CRC) {
557 dev_err(mmc_dev(host->mmc),
558 "command CRC error (CMD%d, arg 0x%08x)\n",
559 host->cmd->opcode, host->cmd->arg);
560 host->cmd->error = -EILSEQ;
563 dev_err(mmc_dev(host->mmc),
564 "command CRC error without cmd?\n");
567 if (status & OMAP_MMC_STAT_CARD_ERR) {
568 dev_dbg(mmc_dev(host->mmc),
569 "ignoring card status error (CMD%d)\n",
575 * NOTE: On 1610 the END_OF_CMD may come too early when
578 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
579 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
585 mmc_omap_cmd_done(host, host->cmd);
588 mmc_omap_xfer_done(host, host->data);
589 else if (end_transfer)
590 mmc_omap_end_of_data(host, host->data);
595 /* Prepare to transfer the next segment of a scatterlist */
597 mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
599 int dma_ch = host->dma_ch;
600 unsigned long data_addr;
603 struct scatterlist *sg = &data->sg[host->sg_idx];
608 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
610 count = sg_dma_len(sg);
612 if ((data->blocks == 1) && (count > data->blksz))
615 host->dma_len = count;
617 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
618 * Use 16 or 32 word frames when the blocksize is at least that large.
619 * Blocksize is usually 512 bytes; but not for some SD reads.
621 if (cpu_is_omap15xx() && frame > 32)
628 if (!(data->flags & MMC_DATA_WRITE)) {
629 buf = 0x800f | ((frame - 1) << 8);
631 if (cpu_class_is_omap1()) {
632 src_port = OMAP_DMA_PORT_TIPB;
633 dst_port = OMAP_DMA_PORT_EMIFF;
635 if (cpu_is_omap24xx())
636 sync_dev = OMAP24XX_DMA_MMC1_RX;
638 omap_set_dma_src_params(dma_ch, src_port,
639 OMAP_DMA_AMODE_CONSTANT,
641 omap_set_dma_dest_params(dma_ch, dst_port,
642 OMAP_DMA_AMODE_POST_INC,
643 sg_dma_address(sg), 0, 0);
644 omap_set_dma_dest_data_pack(dma_ch, 1);
645 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
647 buf = 0x0f80 | ((frame - 1) << 0);
649 if (cpu_class_is_omap1()) {
650 src_port = OMAP_DMA_PORT_EMIFF;
651 dst_port = OMAP_DMA_PORT_TIPB;
653 if (cpu_is_omap24xx())
654 sync_dev = OMAP24XX_DMA_MMC1_TX;
656 omap_set_dma_dest_params(dma_ch, dst_port,
657 OMAP_DMA_AMODE_CONSTANT,
659 omap_set_dma_src_params(dma_ch, src_port,
660 OMAP_DMA_AMODE_POST_INC,
661 sg_dma_address(sg), 0, 0);
662 omap_set_dma_src_data_pack(dma_ch, 1);
663 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
666 /* Max limit for DMA frame count is 0xffff */
667 BUG_ON(count > 0xffff);
669 OMAP_MMC_WRITE(host, BUF, buf);
670 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
671 frame, count, OMAP_DMA_SYNC_FRAME,
675 /* A scatterlist segment completed */
676 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
678 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
679 struct mmc_data *mmcdat = host->data;
681 if (unlikely(host->dma_ch < 0)) {
682 dev_err(mmc_dev(host->mmc),
683 "DMA callback while DMA not enabled\n");
686 /* FIXME: We really should do something to _handle_ the errors */
687 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
688 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
691 if (ch_status & OMAP_DMA_DROP_IRQ) {
692 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
695 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
698 mmcdat->bytes_xfered += host->dma_len;
700 if (host->sg_idx < host->sg_len) {
701 mmc_omap_prepare_dma(host, host->data);
702 omap_start_dma(host->dma_ch);
704 mmc_omap_dma_done(host, host->data);
707 static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
709 const char *dev_name;
710 int sync_dev, dma_ch, is_read, r;
712 is_read = !(data->flags & MMC_DATA_WRITE);
713 del_timer_sync(&host->dma_timer);
714 if (host->dma_ch >= 0) {
715 if (is_read == host->dma_is_read)
717 omap_free_dma(host->dma_ch);
723 sync_dev = OMAP_DMA_MMC_RX;
724 dev_name = "MMC1 read";
726 sync_dev = OMAP_DMA_MMC2_RX;
727 dev_name = "MMC2 read";
731 sync_dev = OMAP_DMA_MMC_TX;
732 dev_name = "MMC1 write";
734 sync_dev = OMAP_DMA_MMC2_TX;
735 dev_name = "MMC2 write";
738 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
741 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
744 host->dma_ch = dma_ch;
745 host->dma_is_read = is_read;
750 static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
754 reg = OMAP_MMC_READ(host, SDIO);
756 OMAP_MMC_WRITE(host, SDIO, reg);
757 /* Set maximum timeout */
758 OMAP_MMC_WRITE(host, CTO, 0xff);
761 static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
766 /* Convert ns to clock cycles by assuming 20MHz frequency
767 * 1 cycle at 20MHz = 500 ns
769 timeout = req->data->timeout_clks + req->data->timeout_ns / 500;
771 /* Check if we need to use timeout multiplier register */
772 reg = OMAP_MMC_READ(host, SDIO);
773 if (timeout > 0xffff) {
778 OMAP_MMC_WRITE(host, SDIO, reg);
779 OMAP_MMC_WRITE(host, DTO, timeout);
783 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
785 struct mmc_data *data = req->data;
786 int i, use_dma, block_size;
791 OMAP_MMC_WRITE(host, BLEN, 0);
792 OMAP_MMC_WRITE(host, NBLK, 0);
793 OMAP_MMC_WRITE(host, BUF, 0);
794 host->dma_in_use = 0;
795 set_cmd_timeout(host, req);
799 block_size = data->blksz;
801 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
802 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
803 set_data_timeout(host, req);
805 /* cope with calling layer confusion; it issues "single
806 * block" writes using multi-block scatterlists.
808 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
810 /* Only do DMA for entire blocks */
811 use_dma = host->use_dma;
813 for (i = 0; i < sg_len; i++) {
814 if ((data->sg[i].length % block_size) != 0) {
823 if (mmc_omap_get_dma_channel(host, data) == 0) {
824 enum dma_data_direction dma_data_dir;
826 if (data->flags & MMC_DATA_WRITE)
827 dma_data_dir = DMA_TO_DEVICE;
829 dma_data_dir = DMA_FROM_DEVICE;
831 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
832 sg_len, dma_data_dir);
833 host->total_bytes_left = 0;
834 mmc_omap_prepare_dma(host, req->data);
835 host->brs_received = 0;
837 host->dma_in_use = 1;
844 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
845 host->total_bytes_left = data->blocks * block_size;
846 host->sg_len = sg_len;
847 mmc_omap_sg_to_buf(host);
848 host->dma_in_use = 0;
852 static void mmc_omap_start_request(struct mmc_omap_host *host,
853 struct mmc_request *req)
855 BUG_ON(host->mrq != NULL);
859 /* only touch fifo AFTER the controller readies it */
860 mmc_omap_prepare_data(host, req);
861 mmc_omap_start_command(host, req->cmd);
862 if (host->dma_in_use)
863 omap_start_dma(host->dma_ch);
864 BUG_ON(irqs_disabled());
867 static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
869 struct mmc_omap_slot *slot = mmc_priv(mmc);
870 struct mmc_omap_host *host = slot->host;
873 spin_lock_irqsave(&host->slot_lock, flags);
874 if (host->mmc != NULL) {
875 BUG_ON(slot->mrq != NULL);
877 spin_unlock_irqrestore(&host->slot_lock, flags);
881 spin_unlock_irqrestore(&host->slot_lock, flags);
882 mmc_omap_select_slot(slot, 1);
883 mmc_omap_start_request(host, req);
886 static void innovator_fpga_socket_power(int on)
888 #if defined(CONFIG_MACH_OMAP_INNOVATOR) && defined(CONFIG_ARCH_OMAP15XX)
890 fpga_write(fpga_read(OMAP1510_FPGA_POWER) | (1 << 3),
891 OMAP1510_FPGA_POWER);
893 fpga_write(fpga_read(OMAP1510_FPGA_POWER) & ~(1 << 3),
894 OMAP1510_FPGA_POWER);
900 * Turn the socket power on/off. Innovator uses FPGA, most boards
903 static void mmc_omap_power(struct mmc_omap_host *host, int on)
906 if (machine_is_omap_innovator())
907 innovator_fpga_socket_power(1);
908 else if (machine_is_omap_h2())
909 tps65010_set_gpio_out_value(GPIO3, HIGH);
910 else if (machine_is_omap_h3())
911 /* GPIO 4 of TPS65010 sends SD_EN signal */
912 tps65010_set_gpio_out_value(GPIO4, HIGH);
913 else if (cpu_is_omap24xx()) {
914 u16 reg = OMAP_MMC_READ(host, CON);
915 OMAP_MMC_WRITE(host, CON, reg | (1 << 11));
917 if (host->power_pin >= 0)
918 omap_set_gpio_dataout(host->power_pin, 1);
920 if (machine_is_omap_innovator())
921 innovator_fpga_socket_power(0);
922 else if (machine_is_omap_h2())
923 tps65010_set_gpio_out_value(GPIO3, LOW);
924 else if (machine_is_omap_h3())
925 tps65010_set_gpio_out_value(GPIO4, LOW);
926 else if (cpu_is_omap24xx()) {
927 u16 reg = OMAP_MMC_READ(host, CON);
928 OMAP_MMC_WRITE(host, CON, reg & ~(1 << 11));
930 if (host->power_pin >= 0)
931 omap_set_gpio_dataout(host->power_pin, 0);
935 static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
937 struct mmc_omap_slot *slot = mmc_priv(mmc);
938 struct mmc_omap_host *host = slot->host;
939 int func_clk_rate = clk_get_rate(host->fclk);
945 dsor = func_clk_rate / ios->clock;
949 if (func_clk_rate / dsor > ios->clock)
955 slot->fclk_freq = func_clk_rate / dsor;
957 if (ios->bus_width == MMC_BUS_WIDTH_4)
963 static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
965 struct mmc_omap_slot *slot = mmc_priv(mmc);
966 struct mmc_omap_host *host = slot->host;
969 dsor = mmc_omap_calc_divisor(mmc, ios);
970 host->bus_mode = ios->bus_mode;
971 host->hw_bus_mode = host->bus_mode;
973 switch (ios->power_mode) {
975 mmc_omap_power(host, 0);
978 /* Cannot touch dsor yet, just power up MMC */
979 mmc_omap_power(host, 1);
986 clk_enable(host->fclk);
988 /* On insanely high arm_per frequencies something sometimes
989 * goes somehow out of sync, and the POW bit is not being set,
990 * which results in the while loop below getting stuck.
991 * Writing to the CON register twice seems to do the trick. */
992 for (i = 0; i < 2; i++)
993 OMAP_MMC_WRITE(host, CON, dsor);
994 if (ios->power_mode == MMC_POWER_ON) {
995 /* Send clock cycles, poll completion */
996 OMAP_MMC_WRITE(host, IE, 0);
997 OMAP_MMC_WRITE(host, STAT, 0xffff);
998 OMAP_MMC_WRITE(host, CMD, 1 << 7);
999 while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
1000 OMAP_MMC_WRITE(host, STAT, 1);
1002 clk_disable(host->fclk);
1005 static const struct mmc_host_ops mmc_omap_ops = {
1006 .request = mmc_omap_request,
1007 .set_ios = mmc_omap_set_ios,
1010 static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1012 struct mmc_omap_slot *slot = NULL;
1013 struct mmc_host *mmc;
1016 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1020 slot = mmc_priv(mmc);
1024 slot->pdata = &host->pdata->slots[id];
1026 host->slots[id] = slot;
1028 mmc->caps = MMC_CAP_MULTIWRITE;
1029 if (host->pdata->conf.wire4)
1030 mmc->caps |= MMC_CAP_4_BIT_DATA;
1032 mmc->ops = &mmc_omap_ops;
1033 mmc->f_min = 400000;
1035 if (cpu_class_is_omap2())
1036 mmc->f_max = 48000000;
1038 mmc->f_max = 24000000;
1039 if (host->pdata->max_freq)
1040 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1041 mmc->ocr_avail = slot->pdata->ocr_mask;
1043 /* Use scatterlist DMA to reduce per-transfer costs.
1044 * NOTE max_seg_size assumption that small blocks aren't
1045 * normally used (except e.g. for reading SD registers).
1047 mmc->max_phys_segs = 32;
1048 mmc->max_hw_segs = 32;
1049 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1050 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1051 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1052 mmc->max_seg_size = mmc->max_req_size;
1054 r = mmc_add_host(mmc);
1056 goto err_remove_host;
1058 if (slot->pdata->name != NULL) {
1059 r = device_create_file(&mmc->class_dev,
1060 &dev_attr_slot_name);
1062 goto err_remove_host;
1068 mmc_remove_host(mmc);
1073 static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1075 struct mmc_host *mmc = slot->mmc;
1077 if (slot->pdata->name != NULL)
1078 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1080 mmc_remove_host(mmc);
1084 static int __init mmc_omap_probe(struct platform_device *pdev)
1086 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1087 struct mmc_omap_host *host = NULL;
1088 struct resource *res;
1092 if (pdata == NULL) {
1093 dev_err(&pdev->dev, "platform data missing\n");
1096 if (pdata->nr_slots == 0) {
1097 dev_err(&pdev->dev, "no slots\n");
1101 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1102 irq = platform_get_irq(pdev, 0);
1103 if (res == NULL || irq < 0)
1106 res = request_mem_region(res->start, res->end - res->start + 1,
1111 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1114 goto err_free_mem_region;
1117 spin_lock_init(&host->dma_lock);
1118 init_timer(&host->dma_timer);
1119 spin_lock_init(&host->slot_lock);
1120 init_waitqueue_head(&host->slot_wq);
1122 host->dma_timer.function = mmc_omap_dma_timer;
1123 host->dma_timer.data = (unsigned long) host;
1125 host->pdata = pdata;
1126 host->dev = &pdev->dev;
1127 platform_set_drvdata(pdev, host);
1129 host->id = pdev->id;
1130 host->mem_res = res;
1137 host->phys_base = host->mem_res->start;
1138 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1140 if (cpu_is_omap24xx()) {
1141 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1142 if (IS_ERR(host->iclk))
1143 goto err_free_mmc_host;
1144 clk_enable(host->iclk);
1147 if (!cpu_is_omap24xx())
1148 host->fclk = clk_get(&pdev->dev, "mmc_ck");
1150 host->fclk = clk_get(&pdev->dev, "mmc_fck");
1152 if (IS_ERR(host->fclk)) {
1153 ret = PTR_ERR(host->fclk);
1157 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1161 if (pdata->init != NULL) {
1162 ret = pdata->init(&pdev->dev);
1167 host->nr_slots = pdata->nr_slots;
1168 for (i = 0; i < pdata->nr_slots; i++) {
1169 ret = mmc_omap_new_slot(host, i);
1172 mmc_omap_remove_slot(host->slots[i]);
1174 goto err_plat_cleanup;
1182 pdata->cleanup(&pdev->dev);
1184 free_irq(host->irq, host);
1186 clk_put(host->fclk);
1188 if (host->iclk != NULL) {
1189 clk_disable(host->iclk);
1190 clk_put(host->iclk);
1194 err_free_mem_region:
1195 release_mem_region(res->start, res->end - res->start + 1);
1199 static int mmc_omap_remove(struct platform_device *pdev)
1201 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1204 platform_set_drvdata(pdev, NULL);
1206 BUG_ON(host == NULL);
1208 for (i = 0; i < host->nr_slots; i++)
1209 mmc_omap_remove_slot(host->slots[i]);
1211 if (host->pdata->cleanup)
1212 host->pdata->cleanup(&pdev->dev);
1214 if (host->iclk && !IS_ERR(host->iclk))
1215 clk_put(host->iclk);
1216 if (host->fclk && !IS_ERR(host->fclk))
1217 clk_put(host->fclk);
1219 release_mem_region(pdev->resource[0].start,
1220 pdev->resource[0].end - pdev->resource[0].start + 1);
1228 static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1231 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1233 if (host == NULL || host->suspended)
1236 for (i = 0; i < host->nr_slots; i++) {
1237 struct mmc_omap_slot *slot;
1239 slot = host->slots[i];
1240 ret = mmc_suspend_host(slot->mmc, mesg);
1243 slot = host->slots[i];
1244 mmc_resume_host(slot->mmc);
1249 host->suspended = 1;
1253 static int mmc_omap_resume(struct platform_device *pdev)
1256 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1258 if (host == NULL || !host->suspended)
1261 for (i = 0; i < host->nr_slots; i++) {
1262 struct mmc_omap_slot *slot;
1263 slot = host->slots[i];
1264 ret = mmc_resume_host(slot->mmc);
1268 host->suspended = 0;
1273 #define mmc_omap_suspend NULL
1274 #define mmc_omap_resume NULL
1277 static struct platform_driver mmc_omap_driver = {
1278 .probe = mmc_omap_probe,
1279 .remove = mmc_omap_remove,
1280 .suspend = mmc_omap_suspend,
1281 .resume = mmc_omap_resume,
1283 .name = DRIVER_NAME,
1284 .owner = THIS_MODULE,
1288 static int __init mmc_omap_init(void)
1290 return platform_driver_register(&mmc_omap_driver);
1293 static void __exit mmc_omap_exit(void)
1295 platform_driver_unregister(&mmc_omap_driver);
1298 module_init(mmc_omap_init);
1299 module_exit(mmc_omap_exit);
1301 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1302 MODULE_LICENSE("GPL");
1303 MODULE_ALIAS("platform:" DRIVER_NAME);
1304 MODULE_AUTHOR("Juha Yrjölä");