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>
33 #include <mach/board.h>
35 #include <mach/gpio.h>
38 #include <mach/fpga.h>
40 #define OMAP_MMC_REG_CMD 0x00
41 #define OMAP_MMC_REG_ARGL 0x04
42 #define OMAP_MMC_REG_ARGH 0x08
43 #define OMAP_MMC_REG_CON 0x0c
44 #define OMAP_MMC_REG_STAT 0x10
45 #define OMAP_MMC_REG_IE 0x14
46 #define OMAP_MMC_REG_CTO 0x18
47 #define OMAP_MMC_REG_DTO 0x1c
48 #define OMAP_MMC_REG_DATA 0x20
49 #define OMAP_MMC_REG_BLEN 0x24
50 #define OMAP_MMC_REG_NBLK 0x28
51 #define OMAP_MMC_REG_BUF 0x2c
52 #define OMAP_MMC_REG_SDIO 0x34
53 #define OMAP_MMC_REG_REV 0x3c
54 #define OMAP_MMC_REG_RSP0 0x40
55 #define OMAP_MMC_REG_RSP1 0x44
56 #define OMAP_MMC_REG_RSP2 0x48
57 #define OMAP_MMC_REG_RSP3 0x4c
58 #define OMAP_MMC_REG_RSP4 0x50
59 #define OMAP_MMC_REG_RSP5 0x54
60 #define OMAP_MMC_REG_RSP6 0x58
61 #define OMAP_MMC_REG_RSP7 0x5c
62 #define OMAP_MMC_REG_IOSR 0x60
63 #define OMAP_MMC_REG_SYSC 0x64
64 #define OMAP_MMC_REG_SYSS 0x68
66 #define OMAP_MMC_STAT_CARD_ERR (1 << 14)
67 #define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
68 #define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
69 #define OMAP_MMC_STAT_A_EMPTY (1 << 11)
70 #define OMAP_MMC_STAT_A_FULL (1 << 10)
71 #define OMAP_MMC_STAT_CMD_CRC (1 << 8)
72 #define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
73 #define OMAP_MMC_STAT_DATA_CRC (1 << 6)
74 #define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
75 #define OMAP_MMC_STAT_END_BUSY (1 << 4)
76 #define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
77 #define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
78 #define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
80 #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG_##reg)
81 #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG_##reg)
86 #define OMAP_MMC_CMDTYPE_BC 0
87 #define OMAP_MMC_CMDTYPE_BCR 1
88 #define OMAP_MMC_CMDTYPE_AC 2
89 #define OMAP_MMC_CMDTYPE_ADTC 3
92 #define DRIVER_NAME "mmci-omap"
94 /* Specifies how often in millisecs to poll for card status changes
95 * when the cover switch is open */
96 #define OMAP_MMC_COVER_POLL_DELAY 500
100 struct mmc_omap_slot {
105 unsigned int fclk_freq;
108 struct tasklet_struct cover_tasklet;
109 struct timer_list cover_timer;
112 struct mmc_request *mrq;
113 struct mmc_omap_host *host;
114 struct mmc_host *mmc;
115 struct omap_mmc_slot_data *pdata;
118 struct mmc_omap_host {
121 struct mmc_request * mrq;
122 struct mmc_command * cmd;
123 struct mmc_data * data;
124 struct mmc_host * mmc;
126 unsigned char id; /* 16xx chips have 2 MMC blocks */
129 struct resource *mem_res;
130 void __iomem *virt_base;
131 unsigned int phys_base;
133 unsigned char bus_mode;
134 unsigned char hw_bus_mode;
136 struct work_struct cmd_abort_work;
138 struct timer_list cmd_abort_timer;
140 struct work_struct slot_release_work;
141 struct mmc_omap_slot *next_slot;
142 struct work_struct send_stop_work;
143 struct mmc_data *stop_data;
148 u32 buffer_bytes_left;
149 u32 total_bytes_left;
152 unsigned brs_received:1, dma_done:1;
153 unsigned dma_is_read:1;
154 unsigned dma_in_use:1;
157 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 timer_list clk_timer;
167 spinlock_t clk_lock; /* for changing enabled state */
168 unsigned int fclk_enabled:1;
170 struct omap_mmc_platform_data *pdata;
173 static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
175 unsigned long tick_ns;
177 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
178 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
183 static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
187 spin_lock_irqsave(&host->clk_lock, flags);
188 if (host->fclk_enabled != enable) {
189 host->fclk_enabled = enable;
191 clk_enable(host->fclk);
193 clk_disable(host->fclk);
195 spin_unlock_irqrestore(&host->clk_lock, flags);
198 static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
200 struct mmc_omap_host *host = slot->host;
205 spin_lock_irqsave(&host->slot_lock, flags);
206 while (host->mmc != NULL) {
207 spin_unlock_irqrestore(&host->slot_lock, flags);
208 wait_event(host->slot_wq, host->mmc == NULL);
209 spin_lock_irqsave(&host->slot_lock, flags);
211 host->mmc = slot->mmc;
212 spin_unlock_irqrestore(&host->slot_lock, flags);
214 del_timer(&host->clk_timer);
215 if (host->current_slot != slot || !claimed)
216 mmc_omap_fclk_offdelay(host->current_slot);
218 if (host->current_slot != slot) {
219 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
220 if (host->pdata->switch_slot != NULL)
221 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
222 host->current_slot = slot;
226 mmc_omap_fclk_enable(host, 1);
228 /* Doing the dummy read here seems to work around some bug
229 * at least in OMAP24xx silicon where the command would not
230 * start after writing the CMD register. Sigh. */
231 OMAP_MMC_READ(host, CON);
233 OMAP_MMC_WRITE(host, CON, slot->saved_con);
235 mmc_omap_fclk_enable(host, 0);
238 static void mmc_omap_start_request(struct mmc_omap_host *host,
239 struct mmc_request *req);
241 static void mmc_omap_slot_release_work(struct work_struct *work)
243 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
245 struct mmc_omap_slot *next_slot = host->next_slot;
246 struct mmc_request *rq;
248 host->next_slot = NULL;
249 mmc_omap_select_slot(next_slot, 1);
252 next_slot->mrq = NULL;
253 mmc_omap_start_request(host, rq);
256 static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
258 struct mmc_omap_host *host = slot->host;
262 BUG_ON(slot == NULL || host->mmc == NULL);
265 /* Keeps clock running for at least 8 cycles on valid freq */
266 mod_timer(&host->clk_timer, jiffies + HZ/10);
268 del_timer(&host->clk_timer);
269 mmc_omap_fclk_offdelay(slot);
270 mmc_omap_fclk_enable(host, 0);
273 spin_lock_irqsave(&host->slot_lock, flags);
274 /* Check for any pending requests */
275 for (i = 0; i < host->nr_slots; i++) {
276 struct mmc_omap_slot *new_slot;
278 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
281 BUG_ON(host->next_slot != NULL);
282 new_slot = host->slots[i];
283 /* The current slot should not have a request in queue */
284 BUG_ON(new_slot == host->current_slot);
286 host->next_slot = new_slot;
287 host->mmc = new_slot->mmc;
288 spin_unlock_irqrestore(&host->slot_lock, flags);
289 schedule_work(&host->slot_release_work);
294 wake_up(&host->slot_wq);
295 spin_unlock_irqrestore(&host->slot_lock, flags);
299 int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
301 if (slot->pdata->get_cover_state)
302 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
308 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
311 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
312 struct mmc_omap_slot *slot = mmc_priv(mmc);
314 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
318 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
321 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
324 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
325 struct mmc_omap_slot *slot = mmc_priv(mmc);
327 return sprintf(buf, "%s\n", slot->pdata->name);
330 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
333 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
344 /* Our hardware needs to know exact type */
345 switch (mmc_resp_type(cmd)) {
350 /* resp 1, 1b, 6, 7 */
360 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
364 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
365 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
366 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
367 cmdtype = OMAP_MMC_CMDTYPE_BC;
368 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
369 cmdtype = OMAP_MMC_CMDTYPE_BCR;
371 cmdtype = OMAP_MMC_CMDTYPE_AC;
374 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
376 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
379 if (cmd->flags & MMC_RSP_BUSY)
382 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
385 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
387 OMAP_MMC_WRITE(host, CTO, 200);
388 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
389 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
390 OMAP_MMC_WRITE(host, IE,
391 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
392 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
393 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
394 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
395 OMAP_MMC_STAT_END_OF_DATA);
396 OMAP_MMC_WRITE(host, CMD, cmdreg);
400 mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
403 enum dma_data_direction dma_data_dir;
405 BUG_ON(host->dma_ch < 0);
407 omap_stop_dma(host->dma_ch);
408 /* Release DMA channel lazily */
409 mod_timer(&host->dma_timer, jiffies + HZ);
410 if (data->flags & MMC_DATA_WRITE)
411 dma_data_dir = DMA_TO_DEVICE;
413 dma_data_dir = DMA_FROM_DEVICE;
414 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
418 static void mmc_omap_send_stop_work(struct work_struct *work)
420 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
422 struct mmc_omap_slot *slot = host->current_slot;
423 struct mmc_data *data = host->stop_data;
424 unsigned long tick_ns;
426 tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
429 mmc_omap_start_command(host, data->stop);
433 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
435 if (host->dma_in_use)
436 mmc_omap_release_dma(host, data, data->error);
441 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
442 * dozens of requests until the card finishes writing data.
443 * It'd be cheaper to just wait till an EOFB interrupt arrives...
447 struct mmc_host *mmc;
451 mmc_omap_release_slot(host->current_slot, 1);
452 mmc_request_done(mmc, data->mrq);
456 host->stop_data = data;
457 schedule_work(&host->send_stop_work);
461 mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
463 struct mmc_omap_slot *slot = host->current_slot;
464 unsigned int restarts, passes, timeout;
467 /* Sending abort takes 80 clocks. Have some extra and round up */
468 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
470 while (restarts < maxloops) {
471 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
472 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
475 while (passes < timeout) {
476 stat = OMAP_MMC_READ(host, STAT);
477 if (stat & OMAP_MMC_STAT_END_OF_CMD)
486 OMAP_MMC_WRITE(host, STAT, stat);
490 mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
492 if (host->dma_in_use)
493 mmc_omap_release_dma(host, data, 1);
498 mmc_omap_send_abort(host, 10000);
502 mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
507 if (!host->dma_in_use) {
508 mmc_omap_xfer_done(host, data);
512 spin_lock_irqsave(&host->dma_lock, flags);
516 host->brs_received = 1;
517 spin_unlock_irqrestore(&host->dma_lock, flags);
519 mmc_omap_xfer_done(host, data);
523 mmc_omap_dma_timer(unsigned long data)
525 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
527 BUG_ON(host->dma_ch < 0);
528 omap_free_dma(host->dma_ch);
533 mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
539 spin_lock_irqsave(&host->dma_lock, flags);
540 if (host->brs_received)
544 spin_unlock_irqrestore(&host->dma_lock, flags);
546 mmc_omap_xfer_done(host, data);
550 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
554 del_timer(&host->cmd_abort_timer);
556 if (cmd->flags & MMC_RSP_PRESENT) {
557 if (cmd->flags & MMC_RSP_136) {
558 /* response type 2 */
560 OMAP_MMC_READ(host, RSP0) |
561 (OMAP_MMC_READ(host, RSP1) << 16);
563 OMAP_MMC_READ(host, RSP2) |
564 (OMAP_MMC_READ(host, RSP3) << 16);
566 OMAP_MMC_READ(host, RSP4) |
567 (OMAP_MMC_READ(host, RSP5) << 16);
569 OMAP_MMC_READ(host, RSP6) |
570 (OMAP_MMC_READ(host, RSP7) << 16);
572 /* response types 1, 1b, 3, 4, 5, 6 */
574 OMAP_MMC_READ(host, RSP6) |
575 (OMAP_MMC_READ(host, RSP7) << 16);
579 if (host->data == NULL || cmd->error) {
580 struct mmc_host *mmc;
582 if (host->data != NULL)
583 mmc_omap_abort_xfer(host, host->data);
586 mmc_omap_release_slot(host->current_slot, 1);
587 mmc_request_done(mmc, cmd->mrq);
592 * Abort stuck command. Can occur when card is removed while it is being
595 static void mmc_omap_abort_command(struct work_struct *work)
597 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
601 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
604 if (host->cmd->error == 0)
605 host->cmd->error = -ETIMEDOUT;
607 if (host->data == NULL) {
608 struct mmc_command *cmd;
609 struct mmc_host *mmc;
613 mmc_omap_send_abort(host, 10000);
617 mmc_omap_release_slot(host->current_slot, 1);
618 mmc_request_done(mmc, cmd->mrq);
620 mmc_omap_cmd_done(host, host->cmd);
623 enable_irq(host->irq);
627 mmc_omap_cmd_timer(unsigned long data)
629 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
632 spin_lock_irqsave(&host->slot_lock, flags);
633 if (host->cmd != NULL && !host->abort) {
634 OMAP_MMC_WRITE(host, IE, 0);
635 disable_irq(host->irq);
637 schedule_work(&host->cmd_abort_work);
639 spin_unlock_irqrestore(&host->slot_lock, flags);
644 mmc_omap_sg_to_buf(struct mmc_omap_host *host)
646 struct scatterlist *sg;
648 sg = host->data->sg + host->sg_idx;
649 host->buffer_bytes_left = sg->length;
650 host->buffer = sg_virt(sg);
651 if (host->buffer_bytes_left > host->total_bytes_left)
652 host->buffer_bytes_left = host->total_bytes_left;
656 mmc_omap_clk_timer(unsigned long data)
658 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
660 mmc_omap_fclk_enable(host, 0);
665 mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
669 if (host->buffer_bytes_left == 0) {
671 BUG_ON(host->sg_idx == host->sg_len);
672 mmc_omap_sg_to_buf(host);
675 if (n > host->buffer_bytes_left)
676 n = host->buffer_bytes_left;
677 host->buffer_bytes_left -= n;
678 host->total_bytes_left -= n;
679 host->data->bytes_xfered += n;
682 __raw_writesw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
684 __raw_readsw(host->virt_base + OMAP_MMC_REG_DATA, host->buffer, n);
688 static inline void mmc_omap_report_irq(u16 status)
690 static const char *mmc_omap_status_bits[] = {
691 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
692 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
696 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
697 if (status & (1 << i)) {
700 printk("%s", mmc_omap_status_bits[i]);
705 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
707 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
711 int transfer_error, cmd_error;
713 if (host->cmd == NULL && host->data == NULL) {
714 status = OMAP_MMC_READ(host, STAT);
715 dev_info(mmc_dev(host->slots[0]->mmc),
716 "Spurious IRQ 0x%04x\n", status);
718 OMAP_MMC_WRITE(host, STAT, status);
719 OMAP_MMC_WRITE(host, IE, 0);
729 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
732 OMAP_MMC_WRITE(host, STAT, status);
733 if (host->cmd != NULL)
734 cmd = host->cmd->opcode;
737 #ifdef CONFIG_MMC_DEBUG
738 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
740 mmc_omap_report_irq(status);
743 if (host->total_bytes_left) {
744 if ((status & OMAP_MMC_STAT_A_FULL) ||
745 (status & OMAP_MMC_STAT_END_OF_DATA))
746 mmc_omap_xfer_data(host, 0);
747 if (status & OMAP_MMC_STAT_A_EMPTY)
748 mmc_omap_xfer_data(host, 1);
751 if (status & OMAP_MMC_STAT_END_OF_DATA)
754 if (status & OMAP_MMC_STAT_DATA_TOUT) {
755 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
758 host->data->error = -ETIMEDOUT;
763 if (status & OMAP_MMC_STAT_DATA_CRC) {
765 host->data->error = -EILSEQ;
766 dev_dbg(mmc_dev(host->mmc),
767 "data CRC error, bytes left %d\n",
768 host->total_bytes_left);
771 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
775 if (status & OMAP_MMC_STAT_CMD_TOUT) {
776 /* Timeouts are routine with some commands */
778 struct mmc_omap_slot *slot =
781 !mmc_omap_cover_is_open(slot))
782 dev_err(mmc_dev(host->mmc),
783 "command timeout (CMD%d)\n",
785 host->cmd->error = -ETIMEDOUT;
791 if (status & OMAP_MMC_STAT_CMD_CRC) {
793 dev_err(mmc_dev(host->mmc),
794 "command CRC error (CMD%d, arg 0x%08x)\n",
795 cmd, host->cmd->arg);
796 host->cmd->error = -EILSEQ;
800 dev_err(mmc_dev(host->mmc),
801 "command CRC error without cmd?\n");
804 if (status & OMAP_MMC_STAT_CARD_ERR) {
805 dev_dbg(mmc_dev(host->mmc),
806 "ignoring card status error (CMD%d)\n",
812 * NOTE: On 1610 the END_OF_CMD may come too early when
815 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
816 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
821 if (cmd_error && host->data) {
822 del_timer(&host->cmd_abort_timer);
824 OMAP_MMC_WRITE(host, IE, 0);
825 disable_irq_nosync(host->irq);
826 schedule_work(&host->cmd_abort_work);
831 mmc_omap_cmd_done(host, host->cmd);
832 if (host->data != NULL) {
834 mmc_omap_xfer_done(host, host->data);
835 else if (end_transfer)
836 mmc_omap_end_of_data(host, host->data);
842 void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
845 struct mmc_omap_host *host = dev_get_drvdata(dev);
846 struct mmc_omap_slot *slot = host->slots[num];
848 BUG_ON(num >= host->nr_slots);
850 /* Other subsystems can call in here before we're initialised. */
851 if (host->nr_slots == 0 || !host->slots[num])
854 cover_open = mmc_omap_cover_is_open(slot);
855 if (cover_open != slot->cover_open) {
856 slot->cover_open = cover_open;
857 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
860 tasklet_hi_schedule(&slot->cover_tasklet);
863 static void mmc_omap_cover_timer(unsigned long arg)
865 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
866 tasklet_schedule(&slot->cover_tasklet);
869 static void mmc_omap_cover_handler(unsigned long param)
871 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
872 int cover_open = mmc_omap_cover_is_open(slot);
874 mmc_detect_change(slot->mmc, 0);
879 * If no card is inserted, we postpone polling until
880 * the cover has been closed.
882 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
885 mod_timer(&slot->cover_timer,
886 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
889 /* Prepare to transfer the next segment of a scatterlist */
891 mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
893 int dma_ch = host->dma_ch;
894 unsigned long data_addr;
897 struct scatterlist *sg = &data->sg[host->sg_idx];
902 data_addr = host->phys_base + OMAP_MMC_REG_DATA;
904 count = sg_dma_len(sg);
906 if ((data->blocks == 1) && (count > data->blksz))
909 host->dma_len = count;
911 /* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
912 * Use 16 or 32 word frames when the blocksize is at least that large.
913 * Blocksize is usually 512 bytes; but not for some SD reads.
915 if (cpu_is_omap15xx() && frame > 32)
922 if (!(data->flags & MMC_DATA_WRITE)) {
923 buf = 0x800f | ((frame - 1) << 8);
925 if (cpu_class_is_omap1()) {
926 src_port = OMAP_DMA_PORT_TIPB;
927 dst_port = OMAP_DMA_PORT_EMIFF;
929 if (cpu_is_omap24xx())
930 sync_dev = OMAP24XX_DMA_MMC1_RX;
932 omap_set_dma_src_params(dma_ch, src_port,
933 OMAP_DMA_AMODE_CONSTANT,
935 omap_set_dma_dest_params(dma_ch, dst_port,
936 OMAP_DMA_AMODE_POST_INC,
937 sg_dma_address(sg), 0, 0);
938 omap_set_dma_dest_data_pack(dma_ch, 1);
939 omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
941 buf = 0x0f80 | ((frame - 1) << 0);
943 if (cpu_class_is_omap1()) {
944 src_port = OMAP_DMA_PORT_EMIFF;
945 dst_port = OMAP_DMA_PORT_TIPB;
947 if (cpu_is_omap24xx())
948 sync_dev = OMAP24XX_DMA_MMC1_TX;
950 omap_set_dma_dest_params(dma_ch, dst_port,
951 OMAP_DMA_AMODE_CONSTANT,
953 omap_set_dma_src_params(dma_ch, src_port,
954 OMAP_DMA_AMODE_POST_INC,
955 sg_dma_address(sg), 0, 0);
956 omap_set_dma_src_data_pack(dma_ch, 1);
957 omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
960 /* Max limit for DMA frame count is 0xffff */
961 BUG_ON(count > 0xffff);
963 OMAP_MMC_WRITE(host, BUF, buf);
964 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
965 frame, count, OMAP_DMA_SYNC_FRAME,
969 /* A scatterlist segment completed */
970 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
972 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
973 struct mmc_data *mmcdat = host->data;
975 if (unlikely(host->dma_ch < 0)) {
976 dev_err(mmc_dev(host->mmc),
977 "DMA callback while DMA not enabled\n");
980 /* FIXME: We really should do something to _handle_ the errors */
981 if (ch_status & OMAP1_DMA_TOUT_IRQ) {
982 dev_err(mmc_dev(host->mmc),"DMA timeout\n");
985 if (ch_status & OMAP_DMA_DROP_IRQ) {
986 dev_err(mmc_dev(host->mmc), "DMA sync error\n");
989 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
992 mmcdat->bytes_xfered += host->dma_len;
994 if (host->sg_idx < host->sg_len) {
995 mmc_omap_prepare_dma(host, host->data);
996 omap_start_dma(host->dma_ch);
998 mmc_omap_dma_done(host, host->data);
1001 static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
1003 const char *dma_dev_name;
1004 int sync_dev, dma_ch, is_read, r;
1006 is_read = !(data->flags & MMC_DATA_WRITE);
1007 del_timer_sync(&host->dma_timer);
1008 if (host->dma_ch >= 0) {
1009 if (is_read == host->dma_is_read)
1011 omap_free_dma(host->dma_ch);
1016 if (host->id == 0) {
1017 sync_dev = OMAP_DMA_MMC_RX;
1018 dma_dev_name = "MMC1 read";
1020 sync_dev = OMAP_DMA_MMC2_RX;
1021 dma_dev_name = "MMC2 read";
1024 if (host->id == 0) {
1025 sync_dev = OMAP_DMA_MMC_TX;
1026 dma_dev_name = "MMC1 write";
1028 sync_dev = OMAP_DMA_MMC2_TX;
1029 dma_dev_name = "MMC2 write";
1032 r = omap_request_dma(sync_dev, dma_dev_name, mmc_omap_dma_cb,
1035 dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
1038 host->dma_ch = dma_ch;
1039 host->dma_is_read = is_read;
1044 static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
1048 reg = OMAP_MMC_READ(host, SDIO);
1050 OMAP_MMC_WRITE(host, SDIO, reg);
1051 /* Set maximum timeout */
1052 OMAP_MMC_WRITE(host, CTO, 0xff);
1055 static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
1057 unsigned int timeout, cycle_ns;
1060 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
1061 timeout = req->data->timeout_ns / cycle_ns;
1062 timeout += req->data->timeout_clks;
1064 /* Check if we need to use timeout multiplier register */
1065 reg = OMAP_MMC_READ(host, SDIO);
1066 if (timeout > 0xffff) {
1071 OMAP_MMC_WRITE(host, SDIO, reg);
1072 OMAP_MMC_WRITE(host, DTO, timeout);
1076 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
1078 struct mmc_data *data = req->data;
1079 int i, use_dma, block_size;
1084 OMAP_MMC_WRITE(host, BLEN, 0);
1085 OMAP_MMC_WRITE(host, NBLK, 0);
1086 OMAP_MMC_WRITE(host, BUF, 0);
1087 host->dma_in_use = 0;
1088 set_cmd_timeout(host, req);
1092 block_size = data->blksz;
1094 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
1095 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
1096 set_data_timeout(host, req);
1098 /* cope with calling layer confusion; it issues "single
1099 * block" writes using multi-block scatterlists.
1101 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
1103 /* Only do DMA for entire blocks */
1104 use_dma = host->use_dma;
1106 for (i = 0; i < sg_len; i++) {
1107 if ((data->sg[i].length % block_size) != 0) {
1116 if (mmc_omap_get_dma_channel(host, data) == 0) {
1117 enum dma_data_direction dma_data_dir;
1119 if (data->flags & MMC_DATA_WRITE)
1120 dma_data_dir = DMA_TO_DEVICE;
1122 dma_data_dir = DMA_FROM_DEVICE;
1124 host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1125 sg_len, dma_data_dir);
1126 host->total_bytes_left = 0;
1127 mmc_omap_prepare_dma(host, req->data);
1128 host->brs_received = 0;
1130 host->dma_in_use = 1;
1135 /* Revert to PIO? */
1137 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
1138 host->total_bytes_left = data->blocks * block_size;
1139 host->sg_len = sg_len;
1140 mmc_omap_sg_to_buf(host);
1141 host->dma_in_use = 0;
1145 static void mmc_omap_start_request(struct mmc_omap_host *host,
1146 struct mmc_request *req)
1148 BUG_ON(host->mrq != NULL);
1152 /* only touch fifo AFTER the controller readies it */
1153 mmc_omap_prepare_data(host, req);
1154 mmc_omap_start_command(host, req->cmd);
1155 if (host->dma_in_use)
1156 omap_start_dma(host->dma_ch);
1157 BUG_ON(irqs_disabled());
1160 static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1162 struct mmc_omap_slot *slot = mmc_priv(mmc);
1163 struct mmc_omap_host *host = slot->host;
1164 unsigned long flags;
1166 spin_lock_irqsave(&host->slot_lock, flags);
1167 if (host->mmc != NULL) {
1168 BUG_ON(slot->mrq != NULL);
1170 spin_unlock_irqrestore(&host->slot_lock, flags);
1174 spin_unlock_irqrestore(&host->slot_lock, flags);
1175 mmc_omap_select_slot(slot, 1);
1176 mmc_omap_start_request(host, req);
1179 static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1182 struct mmc_omap_host *host;
1186 if (slot->pdata->set_power != NULL)
1187 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1190 if (cpu_is_omap24xx()) {
1194 w = OMAP_MMC_READ(host, CON);
1195 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1197 w = OMAP_MMC_READ(host, CON);
1198 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1203 static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1205 struct mmc_omap_slot *slot = mmc_priv(mmc);
1206 struct mmc_omap_host *host = slot->host;
1207 int func_clk_rate = clk_get_rate(host->fclk);
1210 if (ios->clock == 0)
1213 dsor = func_clk_rate / ios->clock;
1217 if (func_clk_rate / dsor > ios->clock)
1223 slot->fclk_freq = func_clk_rate / dsor;
1225 if (ios->bus_width == MMC_BUS_WIDTH_4)
1231 static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1233 struct mmc_omap_slot *slot = mmc_priv(mmc);
1234 struct mmc_omap_host *host = slot->host;
1238 mmc_omap_select_slot(slot, 0);
1240 dsor = mmc_omap_calc_divisor(mmc, ios);
1242 if (ios->vdd != slot->vdd)
1243 slot->vdd = ios->vdd;
1246 switch (ios->power_mode) {
1248 mmc_omap_set_power(slot, 0, ios->vdd);
1251 /* Cannot touch dsor yet, just power up MMC */
1252 mmc_omap_set_power(slot, 1, ios->vdd);
1255 mmc_omap_fclk_enable(host, 1);
1261 if (slot->bus_mode != ios->bus_mode) {
1262 if (slot->pdata->set_bus_mode != NULL)
1263 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1265 slot->bus_mode = ios->bus_mode;
1268 /* On insanely high arm_per frequencies something sometimes
1269 * goes somehow out of sync, and the POW bit is not being set,
1270 * which results in the while loop below getting stuck.
1271 * Writing to the CON register twice seems to do the trick. */
1272 for (i = 0; i < 2; i++)
1273 OMAP_MMC_WRITE(host, CON, dsor);
1274 slot->saved_con = dsor;
1275 if (ios->power_mode == MMC_POWER_ON) {
1276 /* worst case at 400kHz, 80 cycles makes 200 microsecs */
1279 /* Send clock cycles, poll completion */
1280 OMAP_MMC_WRITE(host, IE, 0);
1281 OMAP_MMC_WRITE(host, STAT, 0xffff);
1282 OMAP_MMC_WRITE(host, CMD, 1 << 7);
1283 while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
1287 OMAP_MMC_WRITE(host, STAT, 1);
1291 mmc_omap_release_slot(slot, clk_enabled);
1294 static const struct mmc_host_ops mmc_omap_ops = {
1295 .request = mmc_omap_request,
1296 .set_ios = mmc_omap_set_ios,
1299 static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1301 struct mmc_omap_slot *slot = NULL;
1302 struct mmc_host *mmc;
1305 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1309 slot = mmc_priv(mmc);
1313 slot->pdata = &host->pdata->slots[id];
1315 host->slots[id] = slot;
1318 if (host->pdata->slots[id].wires >= 4)
1319 mmc->caps |= MMC_CAP_4_BIT_DATA;
1321 mmc->ops = &mmc_omap_ops;
1322 mmc->f_min = 400000;
1324 if (cpu_class_is_omap2())
1325 mmc->f_max = 48000000;
1327 mmc->f_max = 24000000;
1328 if (host->pdata->max_freq)
1329 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1330 mmc->ocr_avail = slot->pdata->ocr_mask;
1332 /* Use scatterlist DMA to reduce per-transfer costs.
1333 * NOTE max_seg_size assumption that small blocks aren't
1334 * normally used (except e.g. for reading SD registers).
1336 mmc->max_phys_segs = 32;
1337 mmc->max_hw_segs = 32;
1338 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1339 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1340 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1341 mmc->max_seg_size = mmc->max_req_size;
1343 r = mmc_add_host(mmc);
1345 goto err_remove_host;
1347 if (slot->pdata->name != NULL) {
1348 r = device_create_file(&mmc->class_dev,
1349 &dev_attr_slot_name);
1351 goto err_remove_host;
1354 if (slot->pdata->get_cover_state != NULL) {
1355 r = device_create_file(&mmc->class_dev,
1356 &dev_attr_cover_switch);
1358 goto err_remove_slot_name;
1360 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1361 (unsigned long)slot);
1362 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1363 (unsigned long)slot);
1364 tasklet_schedule(&slot->cover_tasklet);
1369 err_remove_slot_name:
1370 if (slot->pdata->name != NULL)
1371 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1373 mmc_remove_host(mmc);
1378 static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1380 struct mmc_host *mmc = slot->mmc;
1382 if (slot->pdata->name != NULL)
1383 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1384 if (slot->pdata->get_cover_state != NULL)
1385 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1387 tasklet_kill(&slot->cover_tasklet);
1388 del_timer_sync(&slot->cover_timer);
1389 flush_scheduled_work();
1391 mmc_remove_host(mmc);
1395 static int __init mmc_omap_probe(struct platform_device *pdev)
1397 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1398 struct mmc_omap_host *host = NULL;
1399 struct resource *res;
1403 if (pdata == NULL) {
1404 dev_err(&pdev->dev, "platform data missing\n");
1407 if (pdata->nr_slots == 0) {
1408 dev_err(&pdev->dev, "no slots\n");
1412 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1413 irq = platform_get_irq(pdev, 0);
1414 if (res == NULL || irq < 0)
1417 res = request_mem_region(res->start, res->end - res->start + 1,
1422 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1425 goto err_free_mem_region;
1428 INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
1429 INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
1431 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1432 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1433 (unsigned long) host);
1435 spin_lock_init(&host->clk_lock);
1436 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1438 spin_lock_init(&host->dma_lock);
1439 setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
1440 spin_lock_init(&host->slot_lock);
1441 init_waitqueue_head(&host->slot_wq);
1443 host->pdata = pdata;
1444 host->dev = &pdev->dev;
1445 platform_set_drvdata(pdev, host);
1447 host->id = pdev->id;
1448 host->mem_res = res;
1452 host->dev->dma_mask = &pdata->dma_mask;
1456 host->phys_base = host->mem_res->start;
1457 host->virt_base = ioremap(res->start, res->end - res->start + 1);
1458 if (!host->virt_base)
1461 host->iclk = clk_get(&pdev->dev, "ick");
1462 if (IS_ERR(host->iclk))
1463 goto err_free_mmc_host;
1464 clk_enable(host->iclk);
1466 host->fclk = clk_get(&pdev->dev, "fck");
1467 if (IS_ERR(host->fclk)) {
1468 ret = PTR_ERR(host->fclk);
1472 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1476 if (pdata->init != NULL) {
1477 ret = pdata->init(&pdev->dev);
1482 host->nr_slots = pdata->nr_slots;
1483 for (i = 0; i < pdata->nr_slots; i++) {
1484 ret = mmc_omap_new_slot(host, i);
1487 mmc_omap_remove_slot(host->slots[i]);
1489 goto err_plat_cleanup;
1497 pdata->cleanup(&pdev->dev);
1499 free_irq(host->irq, host);
1501 clk_put(host->fclk);
1503 if (host->iclk != NULL) {
1504 clk_disable(host->iclk);
1505 clk_put(host->iclk);
1508 iounmap(host->virt_base);
1511 err_free_mem_region:
1512 release_mem_region(res->start, res->end - res->start + 1);
1516 static int mmc_omap_remove(struct platform_device *pdev)
1518 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1521 platform_set_drvdata(pdev, NULL);
1523 BUG_ON(host == NULL);
1525 for (i = 0; i < host->nr_slots; i++)
1526 mmc_omap_remove_slot(host->slots[i]);
1528 if (host->pdata->cleanup)
1529 host->pdata->cleanup(&pdev->dev);
1531 mmc_omap_fclk_enable(host, 0);
1532 clk_put(host->fclk);
1533 clk_disable(host->iclk);
1534 clk_put(host->iclk);
1536 iounmap(host->virt_base);
1537 release_mem_region(pdev->resource[0].start,
1538 pdev->resource[0].end - pdev->resource[0].start + 1);
1546 static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1549 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1551 if (host == NULL || host->suspended)
1554 for (i = 0; i < host->nr_slots; i++) {
1555 struct mmc_omap_slot *slot;
1557 slot = host->slots[i];
1558 ret = mmc_suspend_host(slot->mmc, mesg);
1561 slot = host->slots[i];
1562 mmc_resume_host(slot->mmc);
1567 host->suspended = 1;
1571 static int mmc_omap_resume(struct platform_device *pdev)
1574 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1576 if (host == NULL || !host->suspended)
1579 for (i = 0; i < host->nr_slots; i++) {
1580 struct mmc_omap_slot *slot;
1581 slot = host->slots[i];
1582 ret = mmc_resume_host(slot->mmc);
1586 host->suspended = 0;
1591 #define mmc_omap_suspend NULL
1592 #define mmc_omap_resume NULL
1595 static struct platform_driver mmc_omap_driver = {
1596 .probe = mmc_omap_probe,
1597 .remove = mmc_omap_remove,
1598 .suspend = mmc_omap_suspend,
1599 .resume = mmc_omap_resume,
1601 .name = DRIVER_NAME,
1602 .owner = THIS_MODULE,
1606 static int __init mmc_omap_init(void)
1608 return platform_driver_register(&mmc_omap_driver);
1611 static void __exit mmc_omap_exit(void)
1613 platform_driver_unregister(&mmc_omap_driver);
1616 module_init(mmc_omap_init);
1617 module_exit(mmc_omap_exit);
1619 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1620 MODULE_LICENSE("GPL");
1621 MODULE_ALIAS("platform:" DRIVER_NAME);
1622 MODULE_AUTHOR("Juha Yrjölä");