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/mmc.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/card.h>
27 #include <linux/clk.h>
28 #include <linux/scatterlist.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>
40 #include <asm/arch/tps65010.h>
41 #include <asm/arch/board-sx1.h>
43 #define OMAP_MMC_REG_CMD 0x00
44 #define OMAP_MMC_REG_ARGL 0x04
45 #define OMAP_MMC_REG_ARGH 0x08
46 #define OMAP_MMC_REG_CON 0x0c
47 #define OMAP_MMC_REG_STAT 0x10
48 #define OMAP_MMC_REG_IE 0x14
49 #define OMAP_MMC_REG_CTO 0x18
50 #define OMAP_MMC_REG_DTO 0x1c
51 #define OMAP_MMC_REG_DATA 0x20
52 #define OMAP_MMC_REG_BLEN 0x24
53 #define OMAP_MMC_REG_NBLK 0x28
54 #define OMAP_MMC_REG_BUF 0x2c
55 #define OMAP_MMC_REG_SDIO 0x34
56 #define OMAP_MMC_REG_REV 0x3c
57 #define OMAP_MMC_REG_RSP0 0x40
58 #define OMAP_MMC_REG_RSP1 0x44
59 #define OMAP_MMC_REG_RSP2 0x48
60 #define OMAP_MMC_REG_RSP3 0x4c
61 #define OMAP_MMC_REG_RSP4 0x50
62 #define OMAP_MMC_REG_RSP5 0x54
63 #define OMAP_MMC_REG_RSP6 0x58
64 #define OMAP_MMC_REG_RSP7 0x5c
65 #define OMAP_MMC_REG_IOSR 0x60
66 #define OMAP_MMC_REG_SYSC 0x64
67 #define OMAP_MMC_REG_SYSS 0x68
69 #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
70 #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
71 #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
72 #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
73 #define OMAP_MMC_STAT_A_FULL (1 << 10)
74 #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
75 #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
76 #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
77 #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
78 #define OMAP_MMC_STAT_END_BUSY (1 << 4)
79 #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
80 #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
81 #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
83 #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
84 #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
89 #define OMAP_MMC_CMDTYPE_BC 0
90 #define OMAP_MMC_CMDTYPE_BCR 1
91 #define OMAP_MMC_CMDTYPE_AC 2
92 #define OMAP_MMC_CMDTYPE_ADTC 3
95 #define DRIVER_NAME "mmci-omap"
97 /* Specifies how often in millisecs to poll for card status changes
98 * when the cover switch is open */
99 #define OMAP_MMC_COVER_POLL_DELAY 500
101 struct mmc_omap_host;
103 struct mmc_omap_slot {
108 unsigned int fclk_freq;
111 struct tasklet_struct cover_tasklet;
112 struct timer_list cover_timer;
115 struct mmc_request *mrq;
116 struct mmc_omap_host *host;
117 struct mmc_host *mmc;
118 struct omap_mmc_slot_data *pdata;
121 struct mmc_omap_host {
124 struct mmc_request * mrq;
125 struct mmc_command * cmd;
126 struct mmc_data * data;
127 struct mmc_host * mmc;
129 unsigned char id; /* 16xx chips have 2 MMC blocks */
132 struct resource *mem_res;
133 void __iomem *virt_base;
134 unsigned int phys_base;
136 unsigned char bus_mode;
137 unsigned char hw_bus_mode;
139 struct work_struct cmd_abort_work;
141 struct timer_list cmd_abort_timer;
146 u32 buffer_bytes_left;
147 u32 total_bytes_left;
150 unsigned brs_received:1, dma_done:1;
151 unsigned dma_is_read:1;
152 unsigned dma_in_use:1;
155 struct timer_list dma_timer;
160 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
161 struct mmc_omap_slot *current_slot;
162 spinlock_t slot_lock;
163 wait_queue_head_t slot_wq;
166 struct omap_mmc_platform_data *pdata;
169 static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
171 struct mmc_omap_host *host = slot->host;
176 spin_lock_irqsave(&host->slot_lock, flags);
177 while (host->mmc != NULL) {
178 spin_unlock_irqrestore(&host->slot_lock, flags);
179 wait_event(host->slot_wq, host->mmc == NULL);
180 spin_lock_irqsave(&host->slot_lock, flags);
182 host->mmc = slot->mmc;
183 spin_unlock_irqrestore(&host->slot_lock, flags);
185 clk_enable(host->fclk);
186 if (host->current_slot != slot) {
187 if (host->pdata->switch_slot != NULL)
188 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
189 host->current_slot = slot;
192 /* Doing the dummy read here seems to work around some bug
193 * at least in OMAP24xx silicon where the command would not
194 * start after writing the CMD register. Sigh. */
195 OMAP_MMC_READ(host, CON);
197 OMAP_MMC_WRITE(host, CON, slot->saved_con);
200 static void mmc_omap_start_request(struct mmc_omap_host *host,
201 struct mmc_request *req);
203 static void mmc_omap_release_slot(struct mmc_omap_slot *slot)
205 struct mmc_omap_host *host = slot->host;
209 BUG_ON(slot == NULL || host->mmc == NULL);
210 clk_disable(host->fclk);
212 spin_lock_irqsave(&host->slot_lock, flags);
213 /* Check for any pending requests */
214 for (i = 0; i < host->nr_slots; i++) {
215 struct mmc_omap_slot *new_slot;
216 struct mmc_request *rq;
218 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
221 new_slot = host->slots[i];
222 /* The current slot should not have a request in queue */
223 BUG_ON(new_slot == host->current_slot);
225 host->mmc = new_slot->mmc;
226 spin_unlock_irqrestore(&host->slot_lock, flags);
227 mmc_omap_select_slot(new_slot, 1);
229 new_slot->mrq = NULL;
230 mmc_omap_start_request(host, rq);
235 wake_up(&host->slot_wq);
236 spin_unlock_irqrestore(&host->slot_lock, flags);
240 int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
242 if (slot->pdata->get_cover_state)
243 return slot->pdata->get_cover_state(mmc_dev(slot->mmc), slot->id);
248 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
251 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
252 struct mmc_omap_slot *slot = mmc_priv(mmc);
254 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
258 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
260 /* Access to the R/O switch is required for production testing
263 mmc_omap_show_ro(struct device *dev, struct device_attribute *attr, char *buf)
265 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
266 struct mmc_omap_slot *slot = mmc_priv(mmc);
268 return sprintf(buf, "%d\n", slot->pdata->get_ro(mmc_dev(mmc),
272 static DEVICE_ATTR(ro, S_IRUGO, mmc_omap_show_ro, NULL);
275 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
278 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
279 struct mmc_omap_slot *slot = mmc_priv(mmc);
281 return sprintf(buf, "%s\n", slot->pdata->name);
284 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
287 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
298 /* Our hardware needs to know exact type */
299 switch (mmc_resp_type(cmd)) {
304 /* resp 1, 1b, 6, 7 */
314 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
318 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
319 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
320 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
321 cmdtype = OMAP_MMC_CMDTYPE_BC;
322 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
323 cmdtype = OMAP_MMC_CMDTYPE_BCR;
325 cmdtype = OMAP_MMC_CMDTYPE_AC;
328 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
330 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
333 if (cmd->flags & MMC_RSP_BUSY)
336 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
339 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
341 OMAP_MMC_WRITE(host, CTO, 200);
342 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
343 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
344 OMAP_MMC_WRITE(host, IE,
345 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
346 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
347 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
348 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
349 OMAP_MMC_STAT_END_OF_DATA);
350 OMAP_MMC_WRITE(host, CMD, cmdreg);
354 mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
357 enum dma_data_direction dma_data_dir;
359 BUG_ON(host->dma_ch < 0);
361 omap_stop_dma(host->dma_ch);
362 /* Release DMA channel lazily */
363 mod_timer(&host->dma_timer, jiffies + HZ);
364 if (data->flags & MMC_DATA_WRITE)
365 dma_data_dir = DMA_TO_DEVICE;
367 dma_data_dir = DMA_FROM_DEVICE;
368 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
373 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
375 if (host->dma_in_use)
376 mmc_omap_release_dma(host, data, data->error);
381 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
382 * dozens of requests until the card finishes writing data.
383 * It'd be cheaper to just wait till an EOFB interrupt arrives...
387 struct mmc_host *mmc;
391 mmc_omap_release_slot(host->current_slot);
392 mmc_request_done(mmc, data->mrq);
396 mmc_omap_start_command(host, data->stop);
400 mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
402 struct mmc_omap_slot *slot = host->current_slot;
403 unsigned int restarts, passes, timeout;
406 /* Sending abort takes 80 clocks. Have some extra and round up */
407 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
409 while (restarts < maxloops) {
410 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
411 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
414 while (passes < timeout) {
415 stat = OMAP_MMC_READ(host, STAT);
416 if (stat & OMAP_MMC_STAT_END_OF_CMD)
425 OMAP_MMC_WRITE(host, STAT, stat);
429 mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
431 if (host->dma_in_use)
432 mmc_omap_release_dma(host, data, 1);
437 mmc_omap_send_abort(host, 10000);
441 mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
446 if (!host->dma_in_use) {
447 mmc_omap_xfer_done(host, data);
451 spin_lock_irqsave(&host->dma_lock, flags);
455 host->brs_received = 1;
456 spin_unlock_irqrestore(&host->dma_lock, flags);
458 mmc_omap_xfer_done(host, data);
462 mmc_omap_dma_timer(unsigned long data)
464 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
466 BUG_ON(host->dma_ch < 0);
467 omap_free_dma(host->dma_ch);
472 mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
478 spin_lock_irqsave(&host->dma_lock, flags);
479 if (host->brs_received)
483 spin_unlock_irqrestore(&host->dma_lock, flags);
485 mmc_omap_xfer_done(host, data);
489 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
493 del_timer(&host->cmd_abort_timer);
495 if (cmd->flags & MMC_RSP_PRESENT) {
496 if (cmd->flags & MMC_RSP_136) {
497 /* response type 2 */
499 OMAP_MMC_READ(host, RSP0) |
500 (OMAP_MMC_READ(host, RSP1) << 16);
502 OMAP_MMC_READ(host, RSP2) |
503 (OMAP_MMC_READ(host, RSP3) << 16);
505 OMAP_MMC_READ(host, RSP4) |
506 (OMAP_MMC_READ(host, RSP5) << 16);
508 OMAP_MMC_READ(host, RSP6) |
509 (OMAP_MMC_READ(host, RSP7) << 16);
511 /* response types 1, 1b, 3, 4, 5, 6 */
513 OMAP_MMC_READ(host, RSP6) |
514 (OMAP_MMC_READ(host, RSP7) << 16);
518 if (host->data == NULL || cmd->error) {
519 struct mmc_host *mmc;
521 if (host->data != NULL)
522 mmc_omap_abort_xfer(host, host->data);
525 mmc_omap_release_slot(host->current_slot);
526 mmc_request_done(mmc, cmd->mrq);
531 * Abort stuck command. Can occur when card is removed while it is being
534 static void mmc_omap_abort_command(struct work_struct *work)
536 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
540 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
543 if (host->cmd->error == 0)
544 host->cmd->error = -ETIMEDOUT;
546 if (host->data == NULL) {
547 struct mmc_command *cmd;
548 struct mmc_host *mmc;
552 mmc_omap_send_abort(host, 10000);
556 mmc_omap_release_slot(host->current_slot);
557 mmc_request_done(mmc, cmd->mrq);
559 mmc_omap_cmd_done(host, host->cmd);
562 enable_irq(host->irq);
566 mmc_omap_cmd_timer(unsigned long data)
568 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
571 spin_lock_irqsave(&host->slot_lock, flags);
572 if (host->cmd != NULL && !host->abort) {
573 OMAP_MMC_WRITE(host, IE, 0);
574 disable_irq(host->irq);
576 schedule_work(&host->cmd_abort_work);
578 spin_unlock_irqrestore(&host->slot_lock, flags);
583 mmc_omap_sg_to_buf(struct mmc_omap_host *host)
585 struct scatterlist *sg;
587 sg = host->data->sg + host->sg_idx;
588 host->buffer_bytes_left = sg->length;
589 host->buffer = sg_virt(sg);
590 if (host->buffer_bytes_left > host->total_bytes_left)
591 host->buffer_bytes_left = host->total_bytes_left;
596 mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
600 if (host->buffer_bytes_left == 0) {
602 BUG_ON(host->sg_idx == host->sg_len);
603 mmc_omap_sg_to_buf(host);
606 if (n > host->buffer_bytes_left)
607 n = host->buffer_bytes_left;
608 host->buffer_bytes_left -= n;
609 host->total_bytes_left -= n;
610 host->data->bytes_xfered += n;
613 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
615 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
619 static inline void mmc_omap_report_irq(u16 status)
621 static const char *mmc_omap_status_bits[] = {
622 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
623 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
627 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
628 if (status & (1 << i)) {
631 printk("%s", mmc_omap_status_bits[i]);
636 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
638 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
642 int transfer_error, cmd_error;
644 if (host->cmd == NULL && host->data == NULL) {
645 status = OMAP_MMC_READ(host, STAT);
646 dev_info(mmc_dev(host->slots[0]->mmc),
647 "Spurious IRQ 0x%04x\n", status);
649 OMAP_MMC_WRITE(host, STAT, status);
650 OMAP_MMC_WRITE(host, IE, 0);
660 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
663 OMAP_MMC_WRITE(host, STAT, status);
664 if (host->cmd != NULL)
665 cmd = host->cmd->opcode;
668 #ifdef CONFIG_MMC_DEBUG
669 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
671 mmc_omap_report_irq(status);
674 if (host->total_bytes_left) {
675 if ((status & OMAP_MMC_STAT_A_FULL) ||
676 (status & OMAP_MMC_STAT_END_OF_DATA))
677 mmc_omap_xfer_data(host, 0);
678 if (status & OMAP_MMC_STAT_A_EMPTY)
679 mmc_omap_xfer_data(host, 1);
682 if (status & OMAP_MMC_STAT_END_OF_DATA)
685 if (status & OMAP_MMC_STAT_DATA_TOUT) {
686 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
689 host->data->error = -ETIMEDOUT;
694 if (status & OMAP_MMC_STAT_DATA_CRC) {
696 host->data->error = -EILSEQ;
697 dev_dbg(mmc_dev(host->mmc),
698 "data CRC error, bytes left %d\n",
699 host->total_bytes_left);
702 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
706 if (status & OMAP_MMC_STAT_CMD_TOUT) {
707 /* Timeouts are routine with some commands */
709 struct mmc_omap_slot *slot =
711 if (host->cmd->opcode != MMC_ALL_SEND_CID &&
712 host->cmd->opcode != MMC_SEND_OP_COND &&
713 host->cmd->opcode != MMC_APP_CMD &&
715 !mmc_omap_cover_is_open(slot)))
716 dev_err(mmc_dev(host->mmc),
717 "command timeout (CMD%d)\n",
719 host->cmd->error = -ETIMEDOUT;
725 if (status & OMAP_MMC_STAT_CMD_CRC) {
727 dev_err(mmc_dev(host->mmc),
728 "command CRC error (CMD%d, arg 0x%08x)\n",
729 cmd, host->cmd->arg);
730 host->cmd->error = -EILSEQ;
734 dev_err(mmc_dev(host->mmc),
735 "command CRC error without cmd?\n");
738 if (status & OMAP_MMC_STAT_CARD_ERR) {
739 dev_dbg(mmc_dev(host->mmc),
740 "ignoring card status error (CMD%d)\n",
746 * NOTE: On 1610 the END_OF_CMD may come too early when
749 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
750 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
755 if (cmd_error && host->data) {
756 del_timer(&host->cmd_abort_timer);
758 OMAP_MMC_WRITE(host, IE, 0);
759 disable_irq(host->irq);
760 schedule_work(&host->cmd_abort_work);
765 mmc_omap_cmd_done(host, host->cmd);
766 if (host->data != NULL) {
768 mmc_omap_xfer_done(host, host->data);
769 else if (end_transfer)
770 mmc_omap_end_of_data(host, host->data);
776 void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
779 struct mmc_omap_host *host = dev_get_drvdata(dev);
780 struct mmc_omap_slot *slot = host->slots[num];
782 BUG_ON(num >= host->nr_slots);
784 /* Other subsystems can call in here before we're initialised. */
785 if (host->nr_slots == 0 || !host->slots[num])
788 cover_open = mmc_omap_cover_is_open(slot);
789 if (cover_open != slot->cover_open) {
790 slot->cover_open = cover_open;
791 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
794 tasklet_hi_schedule(&slot->cover_tasklet);
797 static void mmc_omap_cover_timer(unsigned long arg)
799 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
800 tasklet_schedule(&slot->cover_tasklet);
803 static void mmc_omap_cover_handler(unsigned long param)
805 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
806 int cover_open = mmc_omap_cover_is_open(slot);
808 mmc_detect_change(slot->mmc, 0);
813 * If no card is inserted, we postpone polling until
814 * the cover has been closed.
816 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
819 mod_timer(&slot->cover_timer,
820 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
823 /* Prepare to transfer the next segment of a scatterlist */
825 mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
827 int dma_ch = host->dma_ch;
828 unsigned long data_addr;
831 struct scatterlist *sg = &data->sg[host->sg_idx];
836 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
838 count = sg_dma_len(sg);
840 if ((data->blocks == 1) && (count > data->blksz))
843 host->dma_len = count;
845 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
846 * Use 16 or 32 word frames when the blocksize is at least that large.
847 * Blocksize is usually 512 bytes; but not for some SD reads.
849 if (cpu_is_omap15xx() && frame > 32)
856 if (!(data->flags & MMC_DATA_WRITE)) {
857 buf = 0x800f | ((frame - 1) << 8);
859 if (cpu_class_is_omap1()) {
860 src_port = OMAP_DMA_PORT_TIPB;
861 dst_port = OMAP_DMA_PORT_EMIFF;
863 if (cpu_is_omap24xx())
864 sync_dev = OMAP24XX_DMA_MMC1_RX;
866 omap_set_dma_src_params(dma_ch, src_port,
867 OMAP_DMA_AMODE_CONSTANT,
869 omap_set_dma_dest_params(dma_ch, dst_port,
870 OMAP_DMA_AMODE_POST_INC,
871 sg_dma_address(sg), 0, 0);
872 omap_set_dma_dest_data_pack(dma_ch, 1);
873 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
875 buf = 0x0f80 | ((frame - 1) << 0);
877 if (cpu_class_is_omap1()) {
878 src_port = OMAP_DMA_PORT_EMIFF;
879 dst_port = OMAP_DMA_PORT_TIPB;
881 if (cpu_is_omap24xx())
882 sync_dev = OMAP24XX_DMA_MMC1_TX;
884 omap_set_dma_dest_params(dma_ch, dst_port,
885 OMAP_DMA_AMODE_CONSTANT,
887 omap_set_dma_src_params(dma_ch, src_port,
888 OMAP_DMA_AMODE_POST_INC,
889 sg_dma_address(sg), 0, 0);
890 omap_set_dma_src_data_pack(dma_ch, 1);
891 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
894 /* Max limit for DMA frame count is 0xffff */
895 BUG_ON(count > 0xffff);
897 OMAP_MMC_WRITE(host, BUF, buf);
898 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
899 frame, count, OMAP_DMA_SYNC_FRAME,
903 /* A scatterlist segment completed */
904 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
906 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
907 struct mmc_data *mmcdat = host->data;
909 if (unlikely(host->dma_ch < 0)) {
910 dev_err(mmc_dev(host->mmc),
911 "DMA callback while DMA not enabled\n");
914 /* FIXME: We really should do something to _handle_ the errors */
915 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
916 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
919 if (ch_status & OMAP_DMA_DROP_IRQ) {
920 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
923 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
926 mmcdat->bytes_xfered += host->dma_len;
928 if (host->sg_idx < host->sg_len) {
929 mmc_omap_prepare_dma(host, host->data);
930 omap_start_dma(host->dma_ch);
932 mmc_omap_dma_done(host, host->data);
935 static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
937 const char *dev_name;
938 int sync_dev, dma_ch, is_read, r;
940 is_read = !(data->flags & MMC_DATA_WRITE);
941 del_timer_sync(&host->dma_timer);
942 if (host->dma_ch >= 0) {
943 if (is_read == host->dma_is_read)
945 omap_free_dma(host->dma_ch);
951 sync_dev = OMAP_DMA_MMC_RX;
952 dev_name = "MMC1 read";
954 sync_dev = OMAP_DMA_MMC2_RX;
955 dev_name = "MMC2 read";
959 sync_dev = OMAP_DMA_MMC_TX;
960 dev_name = "MMC1 write";
962 sync_dev = OMAP_DMA_MMC2_TX;
963 dev_name = "MMC2 write";
966 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
969 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
972 host->dma_ch = dma_ch;
973 host->dma_is_read = is_read;
978 static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
982 reg = OMAP_MMC_READ(host, SDIO);
984 OMAP_MMC_WRITE(host, SDIO, reg);
985 /* Set maximum timeout */
986 OMAP_MMC_WRITE(host, CTO, 0xff);
989 static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
991 unsigned int timeout, cycle_ns;
994 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
995 timeout = req->data->timeout_ns / cycle_ns;
996 timeout += req->data->timeout_clks;
998 /* Check if we need to use timeout multiplier register */
999 reg = OMAP_MMC_READ(host, SDIO);
1000 if (timeout > 0xffff) {
1005 OMAP_MMC_WRITE(host, SDIO, reg);
1006 OMAP_MMC_WRITE(host, DTO, timeout);
1010 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
1012 struct mmc_data *data = req->data;
1013 int i, use_dma, block_size;
1018 OMAP_MMC_WRITE(host, BLEN, 0);
1019 OMAP_MMC_WRITE(host, NBLK, 0);
1020 OMAP_MMC_WRITE(host, BUF, 0);
1021 host->dma_in_use = 0;
1022 set_cmd_timeout(host, req);
1026 block_size = data->blksz;
1028 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
1029 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
1030 set_data_timeout(host, req);
1032 /* cope with calling layer confusion; it issues "single
1033 * block" writes using multi-block scatterlists.
1035 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
1037 /* Only do DMA for entire blocks */
1038 use_dma = host->use_dma;
1040 for (i = 0; i < sg_len; i++) {
1041 if ((data->sg[i].length % block_size) != 0) {
1050 if (mmc_omap_get_dma_channel(host, data) == 0) {
1051 enum dma_data_direction dma_data_dir;
1053 if (data->flags & MMC_DATA_WRITE)
1054 dma_data_dir = DMA_TO_DEVICE;
1056 dma_data_dir = DMA_FROM_DEVICE;
1058 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1059 sg_len, dma_data_dir);
1060 host->total_bytes_left = 0;
1061 mmc_omap_prepare_dma(host, req->data);
1062 host->brs_received = 0;
1064 host->dma_in_use = 1;
1069 /* Revert to PIO? */
1071 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
1072 host->total_bytes_left = data->blocks * block_size;
1073 host->sg_len = sg_len;
1074 mmc_omap_sg_to_buf(host);
1075 host->dma_in_use = 0;
1079 static void mmc_omap_start_request(struct mmc_omap_host *host,
1080 struct mmc_request *req)
1082 BUG_ON(host->mrq != NULL);
1086 /* only touch fifo AFTER the controller readies it */
1087 mmc_omap_prepare_data(host, req);
1088 mmc_omap_start_command(host, req->cmd);
1089 if (host->dma_in_use)
1090 omap_start_dma(host->dma_ch);
1091 BUG_ON(irqs_disabled());
1094 static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1096 struct mmc_omap_slot *slot = mmc_priv(mmc);
1097 struct mmc_omap_host *host = slot->host;
1098 unsigned long flags;
1100 spin_lock_irqsave(&host->slot_lock, flags);
1101 if (host->mmc != NULL) {
1102 BUG_ON(slot->mrq != NULL);
1104 spin_unlock_irqrestore(&host->slot_lock, flags);
1108 spin_unlock_irqrestore(&host->slot_lock, flags);
1109 mmc_omap_select_slot(slot, 1);
1110 mmc_omap_start_request(host, req);
1113 static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1116 struct mmc_omap_host *host;
1120 if (slot->pdata->set_power != NULL)
1121 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1124 if (cpu_is_omap24xx()) {
1128 w = OMAP_MMC_READ(host, CON);
1129 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1131 w = OMAP_MMC_READ(host, CON);
1132 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1137 static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1139 struct mmc_omap_slot *slot = mmc_priv(mmc);
1140 struct mmc_omap_host *host = slot->host;
1141 int func_clk_rate = clk_get_rate(host->fclk);
1144 if (ios->clock == 0)
1147 dsor = func_clk_rate / ios->clock;
1151 if (func_clk_rate / dsor > ios->clock)
1157 slot->fclk_freq = func_clk_rate / dsor;
1159 if (ios->bus_width == MMC_BUS_WIDTH_4)
1165 static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1167 struct mmc_omap_slot *slot = mmc_priv(mmc);
1168 struct mmc_omap_host *host = slot->host;
1171 dsor = mmc_omap_calc_divisor(mmc, ios);
1173 mmc_omap_select_slot(slot, 0);
1175 if (ios->vdd != slot->vdd)
1176 slot->vdd = ios->vdd;
1178 switch (ios->power_mode) {
1180 mmc_omap_set_power(slot, 0, ios->vdd);
1183 /* Cannot touch dsor yet, just power up MMC */
1184 mmc_omap_set_power(slot, 1, ios->vdd);
1191 if (slot->bus_mode != ios->bus_mode) {
1192 if (slot->pdata->set_bus_mode != NULL)
1193 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1195 slot->bus_mode = ios->bus_mode;
1198 /* On insanely high arm_per frequencies something sometimes
1199 * goes somehow out of sync, and the POW bit is not being set,
1200 * which results in the while loop below getting stuck.
1201 * Writing to the CON register twice seems to do the trick. */
1202 for (i = 0; i < 2; i++)
1203 OMAP_MMC_WRITE(host, CON, dsor);
1204 slot->saved_con = dsor;
1205 if (ios->power_mode == MMC_POWER_ON) {
1206 /* Send clock cycles, poll completion */
1207 OMAP_MMC_WRITE(host, IE, 0);
1208 OMAP_MMC_WRITE(host, STAT, 0xffff);
1209 OMAP_MMC_WRITE(host, CMD, 1 << 7);
1210 while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
1211 OMAP_MMC_WRITE(host, STAT, 1);
1215 mmc_omap_release_slot(slot);
1218 static int mmc_omap_get_ro(struct mmc_host *mmc)
1220 struct mmc_omap_slot *slot = mmc_priv(mmc);
1222 if (slot->pdata->get_ro != NULL)
1223 return slot->pdata->get_ro(mmc_dev(mmc), slot->id);
1227 static const struct mmc_host_ops mmc_omap_ops = {
1228 .request = mmc_omap_request,
1229 .set_ios = mmc_omap_set_ios,
1230 .get_ro = mmc_omap_get_ro,
1233 static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1235 struct mmc_omap_slot *slot = NULL;
1236 struct mmc_host *mmc;
1239 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1243 slot = mmc_priv(mmc);
1247 slot->pdata = &host->pdata->slots[id];
1249 host->slots[id] = slot;
1251 mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_MMC_HIGHSPEED |
1252 MMC_CAP_SD_HIGHSPEED;
1253 if (host->pdata->conf.wire4)
1254 mmc->caps |= MMC_CAP_4_BIT_DATA;
1256 mmc->ops = &mmc_omap_ops;
1257 mmc->f_min = 400000;
1259 if (cpu_class_is_omap2())
1260 mmc->f_max = 48000000;
1262 mmc->f_max = 24000000;
1263 if (host->pdata->max_freq)
1264 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1265 mmc->ocr_avail = slot->pdata->ocr_mask;
1267 /* Use scatterlist DMA to reduce per-transfer costs.
1268 * NOTE max_seg_size assumption that small blocks aren't
1269 * normally used (except e.g. for reading SD registers).
1271 mmc->max_phys_segs = 32;
1272 mmc->max_hw_segs = 32;
1273 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1274 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1275 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1276 mmc->max_seg_size = mmc->max_req_size;
1278 r = mmc_add_host(mmc);
1282 if (slot->pdata->name != NULL) {
1283 r = device_create_file(&mmc->class_dev,
1284 &dev_attr_slot_name);
1286 goto err_remove_host;
1289 if (slot->pdata->get_cover_state != NULL) {
1290 r = device_create_file(&mmc->class_dev,
1291 &dev_attr_cover_switch);
1293 goto err_remove_slot_name;
1295 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1296 (unsigned long)slot);
1297 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1298 (unsigned long)slot);
1299 tasklet_schedule(&slot->cover_tasklet);
1302 if (slot->pdata->get_ro != NULL) {
1303 r = device_create_file(&mmc->class_dev,
1306 goto err_remove_cover_attr;
1311 err_remove_cover_attr:
1312 if (slot->pdata->get_cover_state != NULL)
1313 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1314 err_remove_slot_name:
1315 if (slot->pdata->name != NULL)
1316 device_remove_file(&mmc->class_dev, &dev_attr_ro);
1318 mmc_remove_host(mmc);
1322 static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1324 struct mmc_host *mmc = slot->mmc;
1326 if (slot->pdata->name != NULL)
1327 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1328 if (slot->pdata->get_cover_state != NULL)
1329 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1330 if (slot->pdata->get_ro != NULL)
1331 device_remove_file(&mmc->class_dev, &dev_attr_ro);
1333 tasklet_kill(&slot->cover_tasklet);
1334 del_timer_sync(&slot->cover_timer);
1335 flush_scheduled_work();
1337 mmc_remove_host(mmc);
1341 static int __init mmc_omap_probe(struct platform_device *pdev)
1343 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1344 struct mmc_omap_host *host = NULL;
1345 struct resource *res;
1349 if (pdata == NULL) {
1350 dev_err(&pdev->dev, "platform data missing\n");
1353 if (pdata->nr_slots == 0) {
1354 dev_err(&pdev->dev, "no slots\n");
1358 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1359 irq = platform_get_irq(pdev, 0);
1360 if (res == NULL || irq < 0)
1363 res = request_mem_region(res->start, res->end - res->start + 1,
1368 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1371 goto err_free_mem_region;
1374 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1375 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1376 (unsigned long) host);
1378 spin_lock_init(&host->dma_lock);
1379 setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
1380 spin_lock_init(&host->slot_lock);
1381 init_waitqueue_head(&host->slot_wq);
1383 host->pdata = pdata;
1384 host->dev = &pdev->dev;
1385 platform_set_drvdata(pdev, host);
1387 host->id = pdev->id;
1388 host->mem_res = res;
1395 host->phys_base = host->mem_res->start;
1396 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1398 if (cpu_is_omap24xx()) {
1399 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1400 if (IS_ERR(host->iclk))
1401 goto err_free_mmc_host;
1402 clk_enable(host->iclk);
1405 if (!cpu_is_omap24xx())
1406 host->fclk = clk_get(&pdev->dev, "mmc_ck");
1408 host->fclk = clk_get(&pdev->dev, "mmc_fck");
1410 if (IS_ERR(host->fclk)) {
1411 ret = PTR_ERR(host->fclk);
1415 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1419 if (pdata->init != NULL) {
1420 ret = pdata->init(&pdev->dev);
1425 host->nr_slots = pdata->nr_slots;
1426 for (i = 0; i < pdata->nr_slots; i++) {
1427 ret = mmc_omap_new_slot(host, i);
1430 mmc_omap_remove_slot(host->slots[i]);
1432 goto err_plat_cleanup;
1440 pdata->cleanup(&pdev->dev);
1442 free_irq(host->irq, host);
1444 clk_put(host->fclk);
1446 if (host->iclk != NULL) {
1447 clk_disable(host->iclk);
1448 clk_put(host->iclk);
1452 err_free_mem_region:
1453 release_mem_region(res->start, res->end - res->start + 1);
1457 static int mmc_omap_remove(struct platform_device *pdev)
1459 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1462 platform_set_drvdata(pdev, NULL);
1464 BUG_ON(host == NULL);
1466 for (i = 0; i < host->nr_slots; i++)
1467 mmc_omap_remove_slot(host->slots[i]);
1469 if (host->pdata->cleanup)
1470 host->pdata->cleanup(&pdev->dev);
1472 if (host->iclk && !IS_ERR(host->iclk))
1473 clk_put(host->iclk);
1474 if (host->fclk && !IS_ERR(host->fclk))
1475 clk_put(host->fclk);
1477 release_mem_region(pdev->resource[0].start,
1478 pdev->resource[0].end - pdev->resource[0].start + 1);
1486 static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1489 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1491 if (host == NULL || host->suspended)
1494 for (i = 0; i < host->nr_slots; i++) {
1495 struct mmc_omap_slot *slot;
1497 slot = host->slots[i];
1498 ret = mmc_suspend_host(slot->mmc, mesg);
1501 slot = host->slots[i];
1502 mmc_resume_host(slot->mmc);
1507 host->suspended = 1;
1511 static int mmc_omap_resume(struct platform_device *pdev)
1514 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1516 if (host == NULL || !host->suspended)
1519 for (i = 0; i < host->nr_slots; i++) {
1520 struct mmc_omap_slot *slot;
1521 slot = host->slots[i];
1522 ret = mmc_resume_host(slot->mmc);
1526 host->suspended = 0;
1531 #define mmc_omap_suspend NULL
1532 #define mmc_omap_resume NULL
1535 static struct platform_driver mmc_omap_driver = {
1536 .probe = mmc_omap_probe,
1537 .remove = mmc_omap_remove,
1538 .suspend = mmc_omap_suspend,
1539 .resume = mmc_omap_resume,
1541 .name = DRIVER_NAME,
1545 static int __init mmc_omap_init(void)
1547 return platform_driver_register(&mmc_omap_driver);
1550 static void __exit mmc_omap_exit(void)
1552 platform_driver_unregister(&mmc_omap_driver);
1555 module_init(mmc_omap_init);
1556 module_exit(mmc_omap_exit);
1558 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1559 MODULE_LICENSE("GPL");
1560 MODULE_ALIAS(DRIVER_NAME);
1561 MODULE_AUTHOR("Juha Yrjölä");