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_COVER_POLL_DELAY 500
101 struct mmc_omap_slot {
106 unsigned int fclk_freq;
109 struct tasklet_struct cover_tasklet;
110 struct timer_list cover_timer;
113 struct mmc_request *mrq;
114 struct mmc_omap_host *host;
115 struct mmc_host *mmc;
116 struct omap_mmc_slot_data *pdata;
119 struct mmc_omap_host {
122 struct mmc_request * mrq;
123 struct mmc_command * cmd;
124 struct mmc_data * data;
125 struct mmc_host * mmc;
127 unsigned char id; /* 16xx chips have 2 MMC blocks */
130 struct resource *mem_res;
131 void __iomem *virt_base;
132 unsigned int phys_base;
134 unsigned char bus_mode;
135 unsigned char hw_bus_mode;
137 struct work_struct cmd_abort_work;
139 struct timer_list cmd_abort_timer;
144 u32 buffer_bytes_left;
145 u32 total_bytes_left;
148 unsigned brs_received:1, dma_done:1;
149 unsigned dma_is_read:1;
150 unsigned dma_in_use:1;
153 struct timer_list dma_timer;
158 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
159 struct mmc_omap_slot *current_slot;
160 spinlock_t slot_lock;
161 wait_queue_head_t slot_wq;
164 struct timer_list clk_timer;
165 spinlock_t clk_lock; /* for changing enabled state */
166 unsigned int fclk_enabled:1;
168 struct omap_mmc_platform_data *pdata;
171 void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
173 unsigned long tick_ns;
175 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
176 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
181 void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
185 spin_lock_irqsave(&host->clk_lock, flags);
186 if (host->fclk_enabled != enable) {
187 host->fclk_enabled = enable;
189 clk_enable(host->fclk);
191 clk_disable(host->fclk);
193 spin_unlock_irqrestore(&host->clk_lock, flags);
196 static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
198 struct mmc_omap_host *host = slot->host;
203 spin_lock_irqsave(&host->slot_lock, flags);
204 while (host->mmc != NULL) {
205 spin_unlock_irqrestore(&host->slot_lock, flags);
206 wait_event(host->slot_wq, host->mmc == NULL);
207 spin_lock_irqsave(&host->slot_lock, flags);
209 host->mmc = slot->mmc;
210 spin_unlock_irqrestore(&host->slot_lock, flags);
212 del_timer(&host->clk_timer);
213 if (host->current_slot != slot || !claimed)
214 mmc_omap_fclk_offdelay(host->current_slot);
216 if (host->current_slot != slot) {
217 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
218 if (host->pdata->switch_slot != NULL)
219 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
220 host->current_slot = slot;
224 mmc_omap_fclk_enable(host, 1);
226 /* Doing the dummy read here seems to work around some bug
227 * at least in OMAP24xx silicon where the command would not
228 * start after writing the CMD register. Sigh. */
229 OMAP_MMC_READ(host, CON);
231 OMAP_MMC_WRITE(host, CON, slot->saved_con);
233 mmc_omap_fclk_enable(host, 0);
236 static void mmc_omap_start_request(struct mmc_omap_host *host,
237 struct mmc_request *req);
239 static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
241 struct mmc_omap_host *host = slot->host;
245 BUG_ON(slot == NULL || host->mmc == NULL);
248 /* Keeps clock running for at least 8 cycles on valid freq */
249 mod_timer(&host->clk_timer, jiffies + HZ/10);
251 del_timer(&host->clk_timer);
252 mmc_omap_fclk_offdelay(slot);
253 mmc_omap_fclk_enable(host, 0);
256 spin_lock_irqsave(&host->slot_lock, flags);
257 /* Check for any pending requests */
258 for (i = 0; i < host->nr_slots; i++) {
259 struct mmc_omap_slot *new_slot;
260 struct mmc_request *rq;
262 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
265 new_slot = host->slots[i];
266 /* The current slot should not have a request in queue */
267 BUG_ON(new_slot == host->current_slot);
269 host->mmc = new_slot->mmc;
270 spin_unlock_irqrestore(&host->slot_lock, flags);
271 mmc_omap_select_slot(new_slot, 1);
273 new_slot->mrq = NULL;
274 mmc_omap_start_request(host, rq);
279 wake_up(&host->slot_wq);
280 spin_unlock_irqrestore(&host->slot_lock, flags);
284 int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
286 if (slot->pdata->get_cover_state)
287 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
293 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
296 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
297 struct mmc_omap_slot *slot = mmc_priv(mmc);
299 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
303 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
306 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
309 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
310 struct mmc_omap_slot *slot = mmc_priv(mmc);
312 return sprintf(buf, "%s\n", slot->pdata->name);
315 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
318 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
329 /* Our hardware needs to know exact type */
330 switch (mmc_resp_type(cmd)) {
335 /* resp 1, 1b, 6, 7 */
345 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
349 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
350 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
351 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
352 cmdtype = OMAP_MMC_CMDTYPE_BC;
353 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
354 cmdtype = OMAP_MMC_CMDTYPE_BCR;
356 cmdtype = OMAP_MMC_CMDTYPE_AC;
359 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
361 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
364 if (cmd->flags & MMC_RSP_BUSY)
367 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
370 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
372 OMAP_MMC_WRITE(host, CTO, 200);
373 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
374 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
375 OMAP_MMC_WRITE(host, IE,
376 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
377 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
378 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
379 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
380 OMAP_MMC_STAT_END_OF_DATA);
381 OMAP_MMC_WRITE(host, CMD, cmdreg);
385 mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
388 enum dma_data_direction dma_data_dir;
390 BUG_ON(host->dma_ch < 0);
392 omap_stop_dma(host->dma_ch);
393 /* Release DMA channel lazily */
394 mod_timer(&host->dma_timer, jiffies + HZ);
395 if (data->flags & MMC_DATA_WRITE)
396 dma_data_dir = DMA_TO_DEVICE;
398 dma_data_dir = DMA_FROM_DEVICE;
399 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
404 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
406 if (host->dma_in_use)
407 mmc_omap_release_dma(host, data, data->error);
412 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
413 * dozens of requests until the card finishes writing data.
414 * It'd be cheaper to just wait till an EOFB interrupt arrives...
418 struct mmc_host *mmc;
422 mmc_omap_release_slot(host->current_slot, 1);
423 mmc_request_done(mmc, data->mrq);
427 mmc_omap_start_command(host, data->stop);
431 mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
433 struct mmc_omap_slot *slot = host->current_slot;
434 unsigned int restarts, passes, timeout;
437 /* Sending abort takes 80 clocks. Have some extra and round up */
438 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
440 while (restarts < maxloops) {
441 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
442 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
445 while (passes < timeout) {
446 stat = OMAP_MMC_READ(host, STAT);
447 if (stat & OMAP_MMC_STAT_END_OF_CMD)
456 OMAP_MMC_WRITE(host, STAT, stat);
460 mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
462 if (host->dma_in_use)
463 mmc_omap_release_dma(host, data, 1);
468 mmc_omap_send_abort(host, 10000);
472 mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
477 if (!host->dma_in_use) {
478 mmc_omap_xfer_done(host, data);
482 spin_lock_irqsave(&host->dma_lock, flags);
486 host->brs_received = 1;
487 spin_unlock_irqrestore(&host->dma_lock, flags);
489 mmc_omap_xfer_done(host, data);
493 mmc_omap_dma_timer(unsigned long data)
495 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
497 BUG_ON(host->dma_ch < 0);
498 omap_free_dma(host->dma_ch);
503 mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
509 spin_lock_irqsave(&host->dma_lock, flags);
510 if (host->brs_received)
514 spin_unlock_irqrestore(&host->dma_lock, flags);
516 mmc_omap_xfer_done(host, data);
520 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
524 del_timer(&host->cmd_abort_timer);
526 if (cmd->flags & MMC_RSP_PRESENT) {
527 if (cmd->flags & MMC_RSP_136) {
528 /* response type 2 */
530 OMAP_MMC_READ(host, RSP0) |
531 (OMAP_MMC_READ(host, RSP1) << 16);
533 OMAP_MMC_READ(host, RSP2) |
534 (OMAP_MMC_READ(host, RSP3) << 16);
536 OMAP_MMC_READ(host, RSP4) |
537 (OMAP_MMC_READ(host, RSP5) << 16);
539 OMAP_MMC_READ(host, RSP6) |
540 (OMAP_MMC_READ(host, RSP7) << 16);
542 /* response types 1, 1b, 3, 4, 5, 6 */
544 OMAP_MMC_READ(host, RSP6) |
545 (OMAP_MMC_READ(host, RSP7) << 16);
549 if (host->data == NULL || cmd->error) {
550 struct mmc_host *mmc;
552 if (host->data != NULL)
553 mmc_omap_abort_xfer(host, host->data);
556 mmc_omap_release_slot(host->current_slot, 1);
557 mmc_request_done(mmc, cmd->mrq);
562 * Abort stuck command. Can occur when card is removed while it is being
565 static void mmc_omap_abort_command(struct work_struct *work)
567 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
571 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
574 if (host->cmd->error == 0)
575 host->cmd->error = -ETIMEDOUT;
577 if (host->data == NULL) {
578 struct mmc_command *cmd;
579 struct mmc_host *mmc;
583 mmc_omap_send_abort(host, 10000);
587 mmc_omap_release_slot(host->current_slot, 1);
588 mmc_request_done(mmc, cmd->mrq);
590 mmc_omap_cmd_done(host, host->cmd);
593 enable_irq(host->irq);
597 mmc_omap_cmd_timer(unsigned long data)
599 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
602 spin_lock_irqsave(&host->slot_lock, flags);
603 if (host->cmd != NULL && !host->abort) {
604 OMAP_MMC_WRITE(host, IE, 0);
605 disable_irq(host->irq);
607 schedule_work(&host->cmd_abort_work);
609 spin_unlock_irqrestore(&host->slot_lock, flags);
614 mmc_omap_sg_to_buf(struct mmc_omap_host *host)
616 struct scatterlist *sg;
618 sg = host->data->sg + host->sg_idx;
619 host->buffer_bytes_left = sg->length;
620 host->buffer = sg_virt(sg);
621 if (host->buffer_bytes_left > host->total_bytes_left)
622 host->buffer_bytes_left = host->total_bytes_left;
626 mmc_omap_clk_timer(unsigned long data)
628 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
630 mmc_omap_fclk_enable(host, 0);
635 mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
639 if (host->buffer_bytes_left == 0) {
641 BUG_ON(host->sg_idx == host->sg_len);
642 mmc_omap_sg_to_buf(host);
645 if (n > host->buffer_bytes_left)
646 n = host->buffer_bytes_left;
647 host->buffer_bytes_left -= n;
648 host->total_bytes_left -= n;
649 host->data->bytes_xfered += n;
652 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
654 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
658 static inline void mmc_omap_report_irq(u16 status)
660 static const char *mmc_omap_status_bits[] = {
661 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
662 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
666 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
667 if (status & (1 << i)) {
670 printk("%s", mmc_omap_status_bits[i]);
675 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
677 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
681 int transfer_error, cmd_error;
683 if (host->cmd == NULL && host->data == NULL) {
684 status = OMAP_MMC_READ(host, STAT);
685 dev_info(mmc_dev(host->slots[0]->mmc),
686 "Spurious IRQ 0x%04x\n", status);
688 OMAP_MMC_WRITE(host, STAT, status);
689 OMAP_MMC_WRITE(host, IE, 0);
699 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
702 OMAP_MMC_WRITE(host, STAT, status);
703 if (host->cmd != NULL)
704 cmd = host->cmd->opcode;
707 #ifdef CONFIG_MMC_DEBUG
708 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
710 mmc_omap_report_irq(status);
713 if (host->total_bytes_left) {
714 if ((status & OMAP_MMC_STAT_A_FULL) ||
715 (status & OMAP_MMC_STAT_END_OF_DATA))
716 mmc_omap_xfer_data(host, 0);
717 if (status & OMAP_MMC_STAT_A_EMPTY)
718 mmc_omap_xfer_data(host, 1);
721 if (status & OMAP_MMC_STAT_END_OF_DATA)
724 if (status & OMAP_MMC_STAT_DATA_TOUT) {
725 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
728 host->data->error = -ETIMEDOUT;
733 if (status & OMAP_MMC_STAT_DATA_CRC) {
735 host->data->error = -EILSEQ;
736 dev_dbg(mmc_dev(host->mmc),
737 "data CRC error, bytes left %d\n",
738 host->total_bytes_left);
741 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
745 if (status & OMAP_MMC_STAT_CMD_TOUT) {
746 /* Timeouts are routine with some commands */
748 struct mmc_omap_slot *slot =
751 !mmc_omap_cover_is_open(slot))
752 dev_err(mmc_dev(host->mmc),
753 "command timeout (CMD%d)\n",
755 host->cmd->error = -ETIMEDOUT;
761 if (status & OMAP_MMC_STAT_CMD_CRC) {
763 dev_err(mmc_dev(host->mmc),
764 "command CRC error (CMD%d, arg 0x%08x)\n",
765 cmd, host->cmd->arg);
766 host->cmd->error = -EILSEQ;
770 dev_err(mmc_dev(host->mmc),
771 "command CRC error without cmd?\n");
774 if (status & OMAP_MMC_STAT_CARD_ERR) {
775 dev_dbg(mmc_dev(host->mmc),
776 "ignoring card status error (CMD%d)\n",
782 * NOTE: On 1610 the END_OF_CMD may come too early when
785 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
786 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
791 if (cmd_error && host->data) {
792 del_timer(&host->cmd_abort_timer);
794 OMAP_MMC_WRITE(host, IE, 0);
795 disable_irq(host->irq);
796 schedule_work(&host->cmd_abort_work);
801 mmc_omap_cmd_done(host, host->cmd);
802 if (host->data != NULL) {
804 mmc_omap_xfer_done(host, host->data);
805 else if (end_transfer)
806 mmc_omap_end_of_data(host, host->data);
812 void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
815 struct mmc_omap_host *host = dev_get_drvdata(dev);
816 struct mmc_omap_slot *slot = host->slots[num];
818 BUG_ON(num >= host->nr_slots);
820 /* Other subsystems can call in here before we're initialised. */
821 if (host->nr_slots == 0 || !host->slots[num])
824 cover_open = mmc_omap_cover_is_open(slot);
825 if (cover_open != slot->cover_open) {
826 slot->cover_open = cover_open;
827 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
830 tasklet_hi_schedule(&slot->cover_tasklet);
833 static void mmc_omap_cover_timer(unsigned long arg)
835 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
836 tasklet_schedule(&slot->cover_tasklet);
839 static void mmc_omap_cover_handler(unsigned long param)
841 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
842 int cover_open = mmc_omap_cover_is_open(slot);
844 mmc_detect_change(slot->mmc, 0);
849 * If no card is inserted, we postpone polling until
850 * the cover has been closed.
852 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
855 mod_timer(&slot->cover_timer,
856 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
859 /* Prepare to transfer the next segment of a scatterlist */
861 mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
863 int dma_ch = host->dma_ch;
864 unsigned long data_addr;
867 struct scatterlist *sg = &data->sg[host->sg_idx];
872 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
874 count = sg_dma_len(sg);
876 if ((data->blocks == 1) && (count > data->blksz))
879 host->dma_len = count;
881 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
882 * Use 16 or 32 word frames when the blocksize is at least that large.
883 * Blocksize is usually 512 bytes; but not for some SD reads.
885 if (cpu_is_omap15xx() && frame > 32)
892 if (!(data->flags & MMC_DATA_WRITE)) {
893 buf = 0x800f | ((frame - 1) << 8);
895 if (cpu_class_is_omap1()) {
896 src_port = OMAP_DMA_PORT_TIPB;
897 dst_port = OMAP_DMA_PORT_EMIFF;
899 if (cpu_is_omap24xx())
900 sync_dev = OMAP24XX_DMA_MMC1_RX;
902 omap_set_dma_src_params(dma_ch, src_port,
903 OMAP_DMA_AMODE_CONSTANT,
905 omap_set_dma_dest_params(dma_ch, dst_port,
906 OMAP_DMA_AMODE_POST_INC,
907 sg_dma_address(sg), 0, 0);
908 omap_set_dma_dest_data_pack(dma_ch, 1);
909 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
911 buf = 0x0f80 | ((frame - 1) << 0);
913 if (cpu_class_is_omap1()) {
914 src_port = OMAP_DMA_PORT_EMIFF;
915 dst_port = OMAP_DMA_PORT_TIPB;
917 if (cpu_is_omap24xx())
918 sync_dev = OMAP24XX_DMA_MMC1_TX;
920 omap_set_dma_dest_params(dma_ch, dst_port,
921 OMAP_DMA_AMODE_CONSTANT,
923 omap_set_dma_src_params(dma_ch, src_port,
924 OMAP_DMA_AMODE_POST_INC,
925 sg_dma_address(sg), 0, 0);
926 omap_set_dma_src_data_pack(dma_ch, 1);
927 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
930 /* Max limit for DMA frame count is 0xffff */
931 BUG_ON(count > 0xffff);
933 OMAP_MMC_WRITE(host, BUF, buf);
934 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
935 frame, count, OMAP_DMA_SYNC_FRAME,
939 /* A scatterlist segment completed */
940 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
942 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
943 struct mmc_data *mmcdat = host->data;
945 if (unlikely(host->dma_ch < 0)) {
946 dev_err(mmc_dev(host->mmc),
947 "DMA callback while DMA not enabled\n");
950 /* FIXME: We really should do something to _handle_ the errors */
951 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
952 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
955 if (ch_status & OMAP_DMA_DROP_IRQ) {
956 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
959 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
962 mmcdat->bytes_xfered += host->dma_len;
964 if (host->sg_idx < host->sg_len) {
965 mmc_omap_prepare_dma(host, host->data);
966 omap_start_dma(host->dma_ch);
968 mmc_omap_dma_done(host, host->data);
971 static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
973 const char *dev_name;
974 int sync_dev, dma_ch, is_read, r;
976 is_read = !(data->flags & MMC_DATA_WRITE);
977 del_timer_sync(&host->dma_timer);
978 if (host->dma_ch >= 0) {
979 if (is_read == host->dma_is_read)
981 omap_free_dma(host->dma_ch);
987 sync_dev = OMAP_DMA_MMC_RX;
988 dev_name = "MMC1 read";
990 sync_dev = OMAP_DMA_MMC2_RX;
991 dev_name = "MMC2 read";
995 sync_dev = OMAP_DMA_MMC_TX;
996 dev_name = "MMC1 write";
998 sync_dev = OMAP_DMA_MMC2_TX;
999 dev_name = "MMC2 write";
1002 r = omap_request_dma(sync_dev, dev_name, mmc_omap_dma_cb,
1005 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
1008 host->dma_ch = dma_ch;
1009 host->dma_is_read = is_read;
1014 static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
1018 reg = OMAP_MMC_READ(host, SDIO);
1020 OMAP_MMC_WRITE(host, SDIO, reg);
1021 /* Set maximum timeout */
1022 OMAP_MMC_WRITE(host, CTO, 0xff);
1025 static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
1027 unsigned int timeout, cycle_ns;
1030 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
1031 timeout = req->data->timeout_ns / cycle_ns;
1032 timeout += req->data->timeout_clks;
1034 /* Check if we need to use timeout multiplier register */
1035 reg = OMAP_MMC_READ(host, SDIO);
1036 if (timeout > 0xffff) {
1041 OMAP_MMC_WRITE(host, SDIO, reg);
1042 OMAP_MMC_WRITE(host, DTO, timeout);
1046 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
1048 struct mmc_data *data = req->data;
1049 int i, use_dma, block_size;
1054 OMAP_MMC_WRITE(host, BLEN, 0);
1055 OMAP_MMC_WRITE(host, NBLK, 0);
1056 OMAP_MMC_WRITE(host, BUF, 0);
1057 host->dma_in_use = 0;
1058 set_cmd_timeout(host, req);
1062 block_size = data->blksz;
1064 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
1065 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
1066 set_data_timeout(host, req);
1068 /* cope with calling layer confusion; it issues "single
1069 * block" writes using multi-block scatterlists.
1071 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
1073 /* Only do DMA for entire blocks */
1074 use_dma = host->use_dma;
1076 for (i = 0; i < sg_len; i++) {
1077 if ((data->sg[i].length % block_size) != 0) {
1086 if (mmc_omap_get_dma_channel(host, data) == 0) {
1087 enum dma_data_direction dma_data_dir;
1089 if (data->flags & MMC_DATA_WRITE)
1090 dma_data_dir = DMA_TO_DEVICE;
1092 dma_data_dir = DMA_FROM_DEVICE;
1094 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1095 sg_len, dma_data_dir);
1096 host->total_bytes_left = 0;
1097 mmc_omap_prepare_dma(host, req->data);
1098 host->brs_received = 0;
1100 host->dma_in_use = 1;
1105 /* Revert to PIO? */
1107 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
1108 host->total_bytes_left = data->blocks * block_size;
1109 host->sg_len = sg_len;
1110 mmc_omap_sg_to_buf(host);
1111 host->dma_in_use = 0;
1115 static void mmc_omap_start_request(struct mmc_omap_host *host,
1116 struct mmc_request *req)
1118 BUG_ON(host->mrq != NULL);
1122 /* only touch fifo AFTER the controller readies it */
1123 mmc_omap_prepare_data(host, req);
1124 mmc_omap_start_command(host, req->cmd);
1125 if (host->dma_in_use)
1126 omap_start_dma(host->dma_ch);
1127 BUG_ON(irqs_disabled());
1130 static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1132 struct mmc_omap_slot *slot = mmc_priv(mmc);
1133 struct mmc_omap_host *host = slot->host;
1134 unsigned long flags;
1136 spin_lock_irqsave(&host->slot_lock, flags);
1137 if (host->mmc != NULL) {
1138 BUG_ON(slot->mrq != NULL);
1140 spin_unlock_irqrestore(&host->slot_lock, flags);
1144 spin_unlock_irqrestore(&host->slot_lock, flags);
1145 mmc_omap_select_slot(slot, 1);
1146 mmc_omap_start_request(host, req);
1149 static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1152 struct mmc_omap_host *host;
1156 if (slot->pdata->set_power != NULL)
1157 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1160 if (cpu_is_omap24xx()) {
1164 w = OMAP_MMC_READ(host, CON);
1165 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1167 w = OMAP_MMC_READ(host, CON);
1168 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1173 static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1175 struct mmc_omap_slot *slot = mmc_priv(mmc);
1176 struct mmc_omap_host *host = slot->host;
1177 int func_clk_rate = clk_get_rate(host->fclk);
1180 if (ios->clock == 0)
1183 dsor = func_clk_rate / ios->clock;
1187 if (func_clk_rate / dsor > ios->clock)
1193 slot->fclk_freq = func_clk_rate / dsor;
1195 if (ios->bus_width == MMC_BUS_WIDTH_4)
1201 static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1203 struct mmc_omap_slot *slot = mmc_priv(mmc);
1204 struct mmc_omap_host *host = slot->host;
1208 mmc_omap_select_slot(slot, 0);
1210 dsor = mmc_omap_calc_divisor(mmc, ios);
1212 if (ios->vdd != slot->vdd)
1213 slot->vdd = ios->vdd;
1216 switch (ios->power_mode) {
1218 mmc_omap_set_power(slot, 0, ios->vdd);
1221 /* Cannot touch dsor yet, just power up MMC */
1222 mmc_omap_set_power(slot, 1, ios->vdd);
1225 mmc_omap_fclk_enable(host, 1);
1231 if (slot->bus_mode != ios->bus_mode) {
1232 if (slot->pdata->set_bus_mode != NULL)
1233 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1235 slot->bus_mode = ios->bus_mode;
1238 /* On insanely high arm_per frequencies something sometimes
1239 * goes somehow out of sync, and the POW bit is not being set,
1240 * which results in the while loop below getting stuck.
1241 * Writing to the CON register twice seems to do the trick. */
1242 for (i = 0; i < 2; i++)
1243 OMAP_MMC_WRITE(host, CON, dsor);
1244 slot->saved_con = dsor;
1245 if (ios->power_mode == MMC_POWER_ON) {
1246 /* Send clock cycles, poll completion */
1247 OMAP_MMC_WRITE(host, IE, 0);
1248 OMAP_MMC_WRITE(host, STAT, 0xffff);
1249 OMAP_MMC_WRITE(host, CMD, 1 << 7);
1250 while ((OMAP_MMC_READ(host, STAT) & 1) == 0);
1251 OMAP_MMC_WRITE(host, STAT, 1);
1255 mmc_omap_release_slot(slot, clk_enabled);
1258 static const struct mmc_host_ops mmc_omap_ops = {
1259 .request = mmc_omap_request,
1260 .set_ios = mmc_omap_set_ios,
1263 static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1265 struct mmc_omap_slot *slot = NULL;
1266 struct mmc_host *mmc;
1269 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1273 slot = mmc_priv(mmc);
1277 slot->pdata = &host->pdata->slots[id];
1279 host->slots[id] = slot;
1281 mmc->caps = MMC_CAP_MULTIWRITE;
1282 if (host->pdata->conf.wire4)
1283 mmc->caps |= MMC_CAP_4_BIT_DATA;
1285 mmc->ops = &mmc_omap_ops;
1286 mmc->f_min = 400000;
1288 if (cpu_class_is_omap2())
1289 mmc->f_max = 48000000;
1291 mmc->f_max = 24000000;
1292 if (host->pdata->max_freq)
1293 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1294 mmc->ocr_avail = slot->pdata->ocr_mask;
1296 /* Use scatterlist DMA to reduce per-transfer costs.
1297 * NOTE max_seg_size assumption that small blocks aren't
1298 * normally used (except e.g. for reading SD registers).
1300 mmc->max_phys_segs = 32;
1301 mmc->max_hw_segs = 32;
1302 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1303 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1304 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1305 mmc->max_seg_size = mmc->max_req_size;
1307 r = mmc_add_host(mmc);
1309 goto err_remove_host;
1311 if (slot->pdata->name != NULL) {
1312 r = device_create_file(&mmc->class_dev,
1313 &dev_attr_slot_name);
1315 goto err_remove_host;
1318 if (slot->pdata->get_cover_state != NULL) {
1319 r = device_create_file(&mmc->class_dev,
1320 &dev_attr_cover_switch);
1322 goto err_remove_slot_name;
1324 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1325 (unsigned long)slot);
1326 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1327 (unsigned long)slot);
1328 tasklet_schedule(&slot->cover_tasklet);
1333 err_remove_slot_name:
1334 if (slot->pdata->name != NULL)
1335 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1337 mmc_remove_host(mmc);
1342 static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1344 struct mmc_host *mmc = slot->mmc;
1346 if (slot->pdata->name != NULL)
1347 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1348 if (slot->pdata->get_cover_state != NULL)
1349 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1351 tasklet_kill(&slot->cover_tasklet);
1352 del_timer_sync(&slot->cover_timer);
1353 flush_scheduled_work();
1355 mmc_remove_host(mmc);
1359 static int __init mmc_omap_probe(struct platform_device *pdev)
1361 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1362 struct mmc_omap_host *host = NULL;
1363 struct resource *res;
1367 if (pdata == NULL) {
1368 dev_err(&pdev->dev, "platform data missing\n");
1371 if (pdata->nr_slots == 0) {
1372 dev_err(&pdev->dev, "no slots\n");
1376 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1377 irq = platform_get_irq(pdev, 0);
1378 if (res == NULL || irq < 0)
1381 res = request_mem_region(res->start, res->end - res->start + 1,
1386 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1389 goto err_free_mem_region;
1392 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1393 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1394 (unsigned long) host);
1396 spin_lock_init(&host->clk_lock);
1397 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1399 spin_lock_init(&host->dma_lock);
1400 setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
1401 spin_lock_init(&host->slot_lock);
1402 init_waitqueue_head(&host->slot_wq);
1404 host->pdata = pdata;
1405 host->dev = &pdev->dev;
1406 platform_set_drvdata(pdev, host);
1408 host->id = pdev->id;
1409 host->mem_res = res;
1416 host->phys_base = host->mem_res->start;
1417 host->virt_base = (void __iomem *) IO_ADDRESS(host->phys_base);
1419 if (cpu_is_omap24xx()) {
1420 host->iclk = clk_get(&pdev->dev, "mmc_ick");
1421 if (IS_ERR(host->iclk))
1422 goto err_free_mmc_host;
1423 clk_enable(host->iclk);
1426 if (!cpu_is_omap24xx())
1427 host->fclk = clk_get(&pdev->dev, "mmc_ck");
1429 host->fclk = clk_get(&pdev->dev, "mmc_fck");
1431 if (IS_ERR(host->fclk)) {
1432 ret = PTR_ERR(host->fclk);
1436 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1440 if (pdata->init != NULL) {
1441 ret = pdata->init(&pdev->dev);
1446 host->nr_slots = pdata->nr_slots;
1447 for (i = 0; i < pdata->nr_slots; i++) {
1448 ret = mmc_omap_new_slot(host, i);
1451 mmc_omap_remove_slot(host->slots[i]);
1453 goto err_plat_cleanup;
1461 pdata->cleanup(&pdev->dev);
1463 free_irq(host->irq, host);
1465 clk_put(host->fclk);
1467 if (host->iclk != NULL) {
1468 clk_disable(host->iclk);
1469 clk_put(host->iclk);
1473 err_free_mem_region:
1474 release_mem_region(res->start, res->end - res->start + 1);
1478 static int mmc_omap_remove(struct platform_device *pdev)
1480 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1483 platform_set_drvdata(pdev, NULL);
1485 BUG_ON(host == NULL);
1487 for (i = 0; i < host->nr_slots; i++)
1488 mmc_omap_remove_slot(host->slots[i]);
1490 if (host->pdata->cleanup)
1491 host->pdata->cleanup(&pdev->dev);
1493 if (host->iclk && !IS_ERR(host->iclk))
1494 clk_put(host->iclk);
1495 if (host->fclk && !IS_ERR(host->fclk))
1496 clk_put(host->fclk);
1498 release_mem_region(pdev->resource[0].start,
1499 pdev->resource[0].end - pdev->resource[0].start + 1);
1507 static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1510 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1512 if (host == NULL || host->suspended)
1515 for (i = 0; i < host->nr_slots; i++) {
1516 struct mmc_omap_slot *slot;
1518 slot = host->slots[i];
1519 ret = mmc_suspend_host(slot->mmc, mesg);
1522 slot = host->slots[i];
1523 mmc_resume_host(slot->mmc);
1528 host->suspended = 1;
1532 static int mmc_omap_resume(struct platform_device *pdev)
1535 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1537 if (host == NULL || !host->suspended)
1540 for (i = 0; i < host->nr_slots; i++) {
1541 struct mmc_omap_slot *slot;
1542 slot = host->slots[i];
1543 ret = mmc_resume_host(slot->mmc);
1547 host->suspended = 0;
1552 #define mmc_omap_suspend NULL
1553 #define mmc_omap_resume NULL
1556 static struct platform_driver mmc_omap_driver = {
1557 .probe = mmc_omap_probe,
1558 .remove = mmc_omap_remove,
1559 .suspend = mmc_omap_suspend,
1560 .resume = mmc_omap_resume,
1562 .name = DRIVER_NAME,
1563 .owner = THIS_MODULE,
1567 static int __init mmc_omap_init(void)
1569 return platform_driver_register(&mmc_omap_driver);
1572 static void __exit mmc_omap_exit(void)
1574 platform_driver_unregister(&mmc_omap_driver);
1577 module_init(mmc_omap_init);
1578 module_exit(mmc_omap_exit);
1580 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1581 MODULE_LICENSE("GPL");
1582 MODULE_ALIAS("platform:" DRIVER_NAME);
1583 MODULE_AUTHOR("Juha Yrjölä");