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/dmaengine.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/timer.h>
25 #include <linux/omap-dma.h>
26 #include <linux/mmc/host.h>
27 #include <linux/mmc/card.h>
28 #include <linux/clk.h>
29 #include <linux/scatterlist.h>
30 #include <linux/i2c/tps65010.h>
31 #include <linux/slab.h>
36 #include <plat/board.h>
41 #include <plat/fpga.h>
43 #define OMAP_MMC_REG_CMD 0x00
44 #define OMAP_MMC_REG_ARGL 0x01
45 #define OMAP_MMC_REG_ARGH 0x02
46 #define OMAP_MMC_REG_CON 0x03
47 #define OMAP_MMC_REG_STAT 0x04
48 #define OMAP_MMC_REG_IE 0x05
49 #define OMAP_MMC_REG_CTO 0x06
50 #define OMAP_MMC_REG_DTO 0x07
51 #define OMAP_MMC_REG_DATA 0x08
52 #define OMAP_MMC_REG_BLEN 0x09
53 #define OMAP_MMC_REG_NBLK 0x0a
54 #define OMAP_MMC_REG_BUF 0x0b
55 #define OMAP_MMC_REG_SDIO 0x0d
56 #define OMAP_MMC_REG_REV 0x0f
57 #define OMAP_MMC_REG_RSP0 0x10
58 #define OMAP_MMC_REG_RSP1 0x11
59 #define OMAP_MMC_REG_RSP2 0x12
60 #define OMAP_MMC_REG_RSP3 0x13
61 #define OMAP_MMC_REG_RSP4 0x14
62 #define OMAP_MMC_REG_RSP5 0x15
63 #define OMAP_MMC_REG_RSP6 0x16
64 #define OMAP_MMC_REG_RSP7 0x17
65 #define OMAP_MMC_REG_IOSR 0x18
66 #define OMAP_MMC_REG_SYSC 0x19
67 #define OMAP_MMC_REG_SYSS 0x1a
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_REG(host, reg) (OMAP_MMC_REG_##reg << (host)->reg_shift)
84 #define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
85 #define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
90 #define OMAP_MMC_CMDTYPE_BC 0
91 #define OMAP_MMC_CMDTYPE_BCR 1
92 #define OMAP_MMC_CMDTYPE_AC 2
93 #define OMAP_MMC_CMDTYPE_ADTC 3
96 #define DRIVER_NAME "mmci-omap"
98 /* Specifies how often in millisecs to poll for card status changes
99 * when the cover switch is open */
100 #define OMAP_MMC_COVER_POLL_DELAY 500
102 struct mmc_omap_host;
104 struct mmc_omap_slot {
109 unsigned int fclk_freq;
112 struct tasklet_struct cover_tasklet;
113 struct timer_list cover_timer;
116 struct mmc_request *mrq;
117 struct mmc_omap_host *host;
118 struct mmc_host *mmc;
119 struct omap_mmc_slot_data *pdata;
122 struct mmc_omap_host {
125 struct mmc_request * mrq;
126 struct mmc_command * cmd;
127 struct mmc_data * data;
128 struct mmc_host * mmc;
130 unsigned char id; /* 16xx chips have 2 MMC blocks */
133 struct dma_chan *dma_rx;
135 struct dma_chan *dma_tx;
137 struct resource *mem_res;
138 void __iomem *virt_base;
139 unsigned int phys_base;
141 unsigned char bus_mode;
142 unsigned char hw_bus_mode;
143 unsigned int reg_shift;
145 struct work_struct cmd_abort_work;
147 struct timer_list cmd_abort_timer;
149 struct work_struct slot_release_work;
150 struct mmc_omap_slot *next_slot;
151 struct work_struct send_stop_work;
152 struct mmc_data *stop_data;
157 u32 buffer_bytes_left;
158 u32 total_bytes_left;
161 unsigned brs_received:1, dma_done:1;
162 unsigned dma_in_use:1;
165 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
166 struct mmc_omap_slot *current_slot;
167 spinlock_t slot_lock;
168 wait_queue_head_t slot_wq;
171 struct timer_list clk_timer;
172 spinlock_t clk_lock; /* for changing enabled state */
173 unsigned int fclk_enabled:1;
174 struct workqueue_struct *mmc_omap_wq;
176 struct omap_mmc_platform_data *pdata;
180 static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
182 unsigned long tick_ns;
184 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
185 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
190 static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
194 spin_lock_irqsave(&host->clk_lock, flags);
195 if (host->fclk_enabled != enable) {
196 host->fclk_enabled = enable;
198 clk_enable(host->fclk);
200 clk_disable(host->fclk);
202 spin_unlock_irqrestore(&host->clk_lock, flags);
205 static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
207 struct mmc_omap_host *host = slot->host;
212 spin_lock_irqsave(&host->slot_lock, flags);
213 while (host->mmc != NULL) {
214 spin_unlock_irqrestore(&host->slot_lock, flags);
215 wait_event(host->slot_wq, host->mmc == NULL);
216 spin_lock_irqsave(&host->slot_lock, flags);
218 host->mmc = slot->mmc;
219 spin_unlock_irqrestore(&host->slot_lock, flags);
221 del_timer(&host->clk_timer);
222 if (host->current_slot != slot || !claimed)
223 mmc_omap_fclk_offdelay(host->current_slot);
225 if (host->current_slot != slot) {
226 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
227 if (host->pdata->switch_slot != NULL)
228 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
229 host->current_slot = slot;
233 mmc_omap_fclk_enable(host, 1);
235 /* Doing the dummy read here seems to work around some bug
236 * at least in OMAP24xx silicon where the command would not
237 * start after writing the CMD register. Sigh. */
238 OMAP_MMC_READ(host, CON);
240 OMAP_MMC_WRITE(host, CON, slot->saved_con);
242 mmc_omap_fclk_enable(host, 0);
245 static void mmc_omap_start_request(struct mmc_omap_host *host,
246 struct mmc_request *req);
248 static void mmc_omap_slot_release_work(struct work_struct *work)
250 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
252 struct mmc_omap_slot *next_slot = host->next_slot;
253 struct mmc_request *rq;
255 host->next_slot = NULL;
256 mmc_omap_select_slot(next_slot, 1);
259 next_slot->mrq = NULL;
260 mmc_omap_start_request(host, rq);
263 static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
265 struct mmc_omap_host *host = slot->host;
269 BUG_ON(slot == NULL || host->mmc == NULL);
272 /* Keeps clock running for at least 8 cycles on valid freq */
273 mod_timer(&host->clk_timer, jiffies + HZ/10);
275 del_timer(&host->clk_timer);
276 mmc_omap_fclk_offdelay(slot);
277 mmc_omap_fclk_enable(host, 0);
280 spin_lock_irqsave(&host->slot_lock, flags);
281 /* Check for any pending requests */
282 for (i = 0; i < host->nr_slots; i++) {
283 struct mmc_omap_slot *new_slot;
285 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
288 BUG_ON(host->next_slot != NULL);
289 new_slot = host->slots[i];
290 /* The current slot should not have a request in queue */
291 BUG_ON(new_slot == host->current_slot);
293 host->next_slot = new_slot;
294 host->mmc = new_slot->mmc;
295 spin_unlock_irqrestore(&host->slot_lock, flags);
296 queue_work(host->mmc_omap_wq, &host->slot_release_work);
301 wake_up(&host->slot_wq);
302 spin_unlock_irqrestore(&host->slot_lock, flags);
306 int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
308 if (slot->pdata->get_cover_state)
309 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
315 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
318 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
319 struct mmc_omap_slot *slot = mmc_priv(mmc);
321 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
325 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
328 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
331 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
332 struct mmc_omap_slot *slot = mmc_priv(mmc);
334 return sprintf(buf, "%s\n", slot->pdata->name);
337 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
340 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
351 /* Our hardware needs to know exact type */
352 switch (mmc_resp_type(cmd)) {
357 /* resp 1, 1b, 6, 7 */
367 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
371 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
372 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
373 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
374 cmdtype = OMAP_MMC_CMDTYPE_BC;
375 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
376 cmdtype = OMAP_MMC_CMDTYPE_BCR;
378 cmdtype = OMAP_MMC_CMDTYPE_AC;
381 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
383 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
386 if (cmd->flags & MMC_RSP_BUSY)
389 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
392 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
394 OMAP_MMC_WRITE(host, CTO, 200);
395 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
396 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
397 OMAP_MMC_WRITE(host, IE,
398 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
399 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
400 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
401 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
402 OMAP_MMC_STAT_END_OF_DATA);
403 OMAP_MMC_WRITE(host, CMD, cmdreg);
407 mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
410 enum dma_data_direction dma_data_dir;
411 struct device *dev = mmc_dev(host->mmc);
414 if (data->flags & MMC_DATA_WRITE) {
415 dma_data_dir = DMA_TO_DEVICE;
418 dma_data_dir = DMA_FROM_DEVICE;
423 dmaengine_terminate_all(c);
424 /* Claim nothing transferred on error... */
425 data->bytes_xfered = 0;
427 dev = c->device->dev;
429 dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
432 static void mmc_omap_send_stop_work(struct work_struct *work)
434 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
436 struct mmc_omap_slot *slot = host->current_slot;
437 struct mmc_data *data = host->stop_data;
438 unsigned long tick_ns;
440 tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
443 mmc_omap_start_command(host, data->stop);
447 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
449 if (host->dma_in_use)
450 mmc_omap_release_dma(host, data, data->error);
455 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
456 * dozens of requests until the card finishes writing data.
457 * It'd be cheaper to just wait till an EOFB interrupt arrives...
461 struct mmc_host *mmc;
465 mmc_omap_release_slot(host->current_slot, 1);
466 mmc_request_done(mmc, data->mrq);
470 host->stop_data = data;
471 queue_work(host->mmc_omap_wq, &host->send_stop_work);
475 mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
477 struct mmc_omap_slot *slot = host->current_slot;
478 unsigned int restarts, passes, timeout;
481 /* Sending abort takes 80 clocks. Have some extra and round up */
482 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
484 while (restarts < maxloops) {
485 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
486 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
489 while (passes < timeout) {
490 stat = OMAP_MMC_READ(host, STAT);
491 if (stat & OMAP_MMC_STAT_END_OF_CMD)
500 OMAP_MMC_WRITE(host, STAT, stat);
504 mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
506 if (host->dma_in_use)
507 mmc_omap_release_dma(host, data, 1);
512 mmc_omap_send_abort(host, 10000);
516 mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
521 if (!host->dma_in_use) {
522 mmc_omap_xfer_done(host, data);
526 spin_lock_irqsave(&host->dma_lock, flags);
530 host->brs_received = 1;
531 spin_unlock_irqrestore(&host->dma_lock, flags);
533 mmc_omap_xfer_done(host, data);
537 mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
543 spin_lock_irqsave(&host->dma_lock, flags);
544 if (host->brs_received)
548 spin_unlock_irqrestore(&host->dma_lock, flags);
550 mmc_omap_xfer_done(host, data);
554 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
558 del_timer(&host->cmd_abort_timer);
560 if (cmd->flags & MMC_RSP_PRESENT) {
561 if (cmd->flags & MMC_RSP_136) {
562 /* response type 2 */
564 OMAP_MMC_READ(host, RSP0) |
565 (OMAP_MMC_READ(host, RSP1) << 16);
567 OMAP_MMC_READ(host, RSP2) |
568 (OMAP_MMC_READ(host, RSP3) << 16);
570 OMAP_MMC_READ(host, RSP4) |
571 (OMAP_MMC_READ(host, RSP5) << 16);
573 OMAP_MMC_READ(host, RSP6) |
574 (OMAP_MMC_READ(host, RSP7) << 16);
576 /* response types 1, 1b, 3, 4, 5, 6 */
578 OMAP_MMC_READ(host, RSP6) |
579 (OMAP_MMC_READ(host, RSP7) << 16);
583 if (host->data == NULL || cmd->error) {
584 struct mmc_host *mmc;
586 if (host->data != NULL)
587 mmc_omap_abort_xfer(host, host->data);
590 mmc_omap_release_slot(host->current_slot, 1);
591 mmc_request_done(mmc, cmd->mrq);
596 * Abort stuck command. Can occur when card is removed while it is being
599 static void mmc_omap_abort_command(struct work_struct *work)
601 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
605 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
608 if (host->cmd->error == 0)
609 host->cmd->error = -ETIMEDOUT;
611 if (host->data == NULL) {
612 struct mmc_command *cmd;
613 struct mmc_host *mmc;
617 mmc_omap_send_abort(host, 10000);
621 mmc_omap_release_slot(host->current_slot, 1);
622 mmc_request_done(mmc, cmd->mrq);
624 mmc_omap_cmd_done(host, host->cmd);
627 enable_irq(host->irq);
631 mmc_omap_cmd_timer(unsigned long data)
633 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
636 spin_lock_irqsave(&host->slot_lock, flags);
637 if (host->cmd != NULL && !host->abort) {
638 OMAP_MMC_WRITE(host, IE, 0);
639 disable_irq(host->irq);
641 queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
643 spin_unlock_irqrestore(&host->slot_lock, flags);
648 mmc_omap_sg_to_buf(struct mmc_omap_host *host)
650 struct scatterlist *sg;
652 sg = host->data->sg + host->sg_idx;
653 host->buffer_bytes_left = sg->length;
654 host->buffer = sg_virt(sg);
655 if (host->buffer_bytes_left > host->total_bytes_left)
656 host->buffer_bytes_left = host->total_bytes_left;
660 mmc_omap_clk_timer(unsigned long data)
662 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
664 mmc_omap_fclk_enable(host, 0);
669 mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
673 if (host->buffer_bytes_left == 0) {
675 BUG_ON(host->sg_idx == host->sg_len);
676 mmc_omap_sg_to_buf(host);
679 if (n > host->buffer_bytes_left)
680 n = host->buffer_bytes_left;
683 nwords += n & 1; /* handle odd number of bytes to transfer */
685 host->buffer_bytes_left -= n;
686 host->total_bytes_left -= n;
687 host->data->bytes_xfered += n;
690 __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
691 host->buffer, nwords);
693 __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
694 host->buffer, nwords);
697 host->buffer += nwords;
700 static inline void mmc_omap_report_irq(u16 status)
702 static const char *mmc_omap_status_bits[] = {
703 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
704 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
708 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
709 if (status & (1 << i)) {
712 printk("%s", mmc_omap_status_bits[i]);
717 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
719 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
723 int transfer_error, cmd_error;
725 if (host->cmd == NULL && host->data == NULL) {
726 status = OMAP_MMC_READ(host, STAT);
727 dev_info(mmc_dev(host->slots[0]->mmc),
728 "Spurious IRQ 0x%04x\n", status);
730 OMAP_MMC_WRITE(host, STAT, status);
731 OMAP_MMC_WRITE(host, IE, 0);
741 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
744 OMAP_MMC_WRITE(host, STAT, status);
745 if (host->cmd != NULL)
746 cmd = host->cmd->opcode;
749 #ifdef CONFIG_MMC_DEBUG
750 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
752 mmc_omap_report_irq(status);
755 if (host->total_bytes_left) {
756 if ((status & OMAP_MMC_STAT_A_FULL) ||
757 (status & OMAP_MMC_STAT_END_OF_DATA))
758 mmc_omap_xfer_data(host, 0);
759 if (status & OMAP_MMC_STAT_A_EMPTY)
760 mmc_omap_xfer_data(host, 1);
763 if (status & OMAP_MMC_STAT_END_OF_DATA)
766 if (status & OMAP_MMC_STAT_DATA_TOUT) {
767 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
770 host->data->error = -ETIMEDOUT;
775 if (status & OMAP_MMC_STAT_DATA_CRC) {
777 host->data->error = -EILSEQ;
778 dev_dbg(mmc_dev(host->mmc),
779 "data CRC error, bytes left %d\n",
780 host->total_bytes_left);
783 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
787 if (status & OMAP_MMC_STAT_CMD_TOUT) {
788 /* Timeouts are routine with some commands */
790 struct mmc_omap_slot *slot =
793 !mmc_omap_cover_is_open(slot))
794 dev_err(mmc_dev(host->mmc),
795 "command timeout (CMD%d)\n",
797 host->cmd->error = -ETIMEDOUT;
803 if (status & OMAP_MMC_STAT_CMD_CRC) {
805 dev_err(mmc_dev(host->mmc),
806 "command CRC error (CMD%d, arg 0x%08x)\n",
807 cmd, host->cmd->arg);
808 host->cmd->error = -EILSEQ;
812 dev_err(mmc_dev(host->mmc),
813 "command CRC error without cmd?\n");
816 if (status & OMAP_MMC_STAT_CARD_ERR) {
817 dev_dbg(mmc_dev(host->mmc),
818 "ignoring card status error (CMD%d)\n",
824 * NOTE: On 1610 the END_OF_CMD may come too early when
827 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
828 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
833 if (cmd_error && host->data) {
834 del_timer(&host->cmd_abort_timer);
836 OMAP_MMC_WRITE(host, IE, 0);
837 disable_irq_nosync(host->irq);
838 queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
842 if (end_command && host->cmd)
843 mmc_omap_cmd_done(host, host->cmd);
844 if (host->data != NULL) {
846 mmc_omap_xfer_done(host, host->data);
847 else if (end_transfer)
848 mmc_omap_end_of_data(host, host->data);
854 void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
857 struct mmc_omap_host *host = dev_get_drvdata(dev);
858 struct mmc_omap_slot *slot = host->slots[num];
860 BUG_ON(num >= host->nr_slots);
862 /* Other subsystems can call in here before we're initialised. */
863 if (host->nr_slots == 0 || !host->slots[num])
866 cover_open = mmc_omap_cover_is_open(slot);
867 if (cover_open != slot->cover_open) {
868 slot->cover_open = cover_open;
869 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
872 tasklet_hi_schedule(&slot->cover_tasklet);
875 static void mmc_omap_cover_timer(unsigned long arg)
877 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
878 tasklet_schedule(&slot->cover_tasklet);
881 static void mmc_omap_cover_handler(unsigned long param)
883 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
884 int cover_open = mmc_omap_cover_is_open(slot);
886 mmc_detect_change(slot->mmc, 0);
891 * If no card is inserted, we postpone polling until
892 * the cover has been closed.
894 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
897 mod_timer(&slot->cover_timer,
898 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
901 static void mmc_omap_dma_callback(void *priv)
903 struct mmc_omap_host *host = priv;
904 struct mmc_data *data = host->data;
906 /* If we got to the end of DMA, assume everything went well */
907 data->bytes_xfered += data->blocks * data->blksz;
909 mmc_omap_dma_done(host, data);
912 static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
916 reg = OMAP_MMC_READ(host, SDIO);
918 OMAP_MMC_WRITE(host, SDIO, reg);
919 /* Set maximum timeout */
920 OMAP_MMC_WRITE(host, CTO, 0xff);
923 static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
925 unsigned int timeout, cycle_ns;
928 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
929 timeout = req->data->timeout_ns / cycle_ns;
930 timeout += req->data->timeout_clks;
932 /* Check if we need to use timeout multiplier register */
933 reg = OMAP_MMC_READ(host, SDIO);
934 if (timeout > 0xffff) {
939 OMAP_MMC_WRITE(host, SDIO, reg);
940 OMAP_MMC_WRITE(host, DTO, timeout);
944 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
946 struct mmc_data *data = req->data;
947 int i, use_dma, block_size;
952 OMAP_MMC_WRITE(host, BLEN, 0);
953 OMAP_MMC_WRITE(host, NBLK, 0);
954 OMAP_MMC_WRITE(host, BUF, 0);
955 host->dma_in_use = 0;
956 set_cmd_timeout(host, req);
960 block_size = data->blksz;
962 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
963 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
964 set_data_timeout(host, req);
966 /* cope with calling layer confusion; it issues "single
967 * block" writes using multi-block scatterlists.
969 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
971 /* Only do DMA for entire blocks */
972 use_dma = host->use_dma;
974 for (i = 0; i < sg_len; i++) {
975 if ((data->sg[i].length % block_size) != 0) {
984 enum dma_data_direction dma_data_dir;
985 struct dma_async_tx_descriptor *tx;
991 * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
992 * and 24xx. Use 16 or 32 word frames when the
993 * blocksize is at least that large. Blocksize is
994 * usually 512 bytes; but not for some SD reads.
996 burst = cpu_is_omap15xx() ? 32 : 64;
997 if (burst > data->blksz)
1002 if (data->flags & MMC_DATA_WRITE) {
1004 bp = &host->dma_tx_burst;
1005 buf = 0x0f80 | (burst - 1) << 0;
1006 dma_data_dir = DMA_TO_DEVICE;
1009 bp = &host->dma_rx_burst;
1010 buf = 0x800f | (burst - 1) << 8;
1011 dma_data_dir = DMA_FROM_DEVICE;
1017 /* Only reconfigure if we have a different burst size */
1019 struct dma_slave_config cfg;
1021 cfg.src_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
1022 cfg.dst_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
1023 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1024 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1025 cfg.src_maxburst = burst;
1026 cfg.dst_maxburst = burst;
1028 if (dmaengine_slave_config(c, &cfg))
1034 host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
1036 if (host->sg_len == 0)
1039 tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
1040 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1041 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1045 OMAP_MMC_WRITE(host, BUF, buf);
1047 tx->callback = mmc_omap_dma_callback;
1048 tx->callback_param = host;
1049 dmaengine_submit(tx);
1050 host->brs_received = 0;
1052 host->dma_in_use = 1;
1057 /* Revert to PIO? */
1058 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
1059 host->total_bytes_left = data->blocks * block_size;
1060 host->sg_len = sg_len;
1061 mmc_omap_sg_to_buf(host);
1062 host->dma_in_use = 0;
1065 static void mmc_omap_start_request(struct mmc_omap_host *host,
1066 struct mmc_request *req)
1068 BUG_ON(host->mrq != NULL);
1072 /* only touch fifo AFTER the controller readies it */
1073 mmc_omap_prepare_data(host, req);
1074 mmc_omap_start_command(host, req->cmd);
1075 if (host->dma_in_use) {
1076 struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
1077 host->dma_tx : host->dma_rx;
1079 dma_async_issue_pending(c);
1083 static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1085 struct mmc_omap_slot *slot = mmc_priv(mmc);
1086 struct mmc_omap_host *host = slot->host;
1087 unsigned long flags;
1089 spin_lock_irqsave(&host->slot_lock, flags);
1090 if (host->mmc != NULL) {
1091 BUG_ON(slot->mrq != NULL);
1093 spin_unlock_irqrestore(&host->slot_lock, flags);
1097 spin_unlock_irqrestore(&host->slot_lock, flags);
1098 mmc_omap_select_slot(slot, 1);
1099 mmc_omap_start_request(host, req);
1102 static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1105 struct mmc_omap_host *host;
1109 if (slot->pdata->set_power != NULL)
1110 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1113 if (cpu_is_omap24xx()) {
1117 w = OMAP_MMC_READ(host, CON);
1118 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1120 w = OMAP_MMC_READ(host, CON);
1121 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1126 static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1128 struct mmc_omap_slot *slot = mmc_priv(mmc);
1129 struct mmc_omap_host *host = slot->host;
1130 int func_clk_rate = clk_get_rate(host->fclk);
1133 if (ios->clock == 0)
1136 dsor = func_clk_rate / ios->clock;
1140 if (func_clk_rate / dsor > ios->clock)
1146 slot->fclk_freq = func_clk_rate / dsor;
1148 if (ios->bus_width == MMC_BUS_WIDTH_4)
1154 static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1156 struct mmc_omap_slot *slot = mmc_priv(mmc);
1157 struct mmc_omap_host *host = slot->host;
1161 mmc_omap_select_slot(slot, 0);
1163 dsor = mmc_omap_calc_divisor(mmc, ios);
1165 if (ios->vdd != slot->vdd)
1166 slot->vdd = ios->vdd;
1169 switch (ios->power_mode) {
1171 mmc_omap_set_power(slot, 0, ios->vdd);
1174 /* Cannot touch dsor yet, just power up MMC */
1175 mmc_omap_set_power(slot, 1, ios->vdd);
1178 mmc_omap_fclk_enable(host, 1);
1184 if (slot->bus_mode != ios->bus_mode) {
1185 if (slot->pdata->set_bus_mode != NULL)
1186 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1188 slot->bus_mode = ios->bus_mode;
1191 /* On insanely high arm_per frequencies something sometimes
1192 * goes somehow out of sync, and the POW bit is not being set,
1193 * which results in the while loop below getting stuck.
1194 * Writing to the CON register twice seems to do the trick. */
1195 for (i = 0; i < 2; i++)
1196 OMAP_MMC_WRITE(host, CON, dsor);
1197 slot->saved_con = dsor;
1198 if (ios->power_mode == MMC_POWER_ON) {
1199 /* worst case at 400kHz, 80 cycles makes 200 microsecs */
1202 /* Send clock cycles, poll completion */
1203 OMAP_MMC_WRITE(host, IE, 0);
1204 OMAP_MMC_WRITE(host, STAT, 0xffff);
1205 OMAP_MMC_WRITE(host, CMD, 1 << 7);
1206 while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
1210 OMAP_MMC_WRITE(host, STAT, 1);
1214 mmc_omap_release_slot(slot, clk_enabled);
1217 static const struct mmc_host_ops mmc_omap_ops = {
1218 .request = mmc_omap_request,
1219 .set_ios = mmc_omap_set_ios,
1222 static int __devinit mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1224 struct mmc_omap_slot *slot = NULL;
1225 struct mmc_host *mmc;
1228 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1232 slot = mmc_priv(mmc);
1236 slot->pdata = &host->pdata->slots[id];
1238 host->slots[id] = slot;
1241 if (host->pdata->slots[id].wires >= 4)
1242 mmc->caps |= MMC_CAP_4_BIT_DATA;
1244 mmc->ops = &mmc_omap_ops;
1245 mmc->f_min = 400000;
1247 if (cpu_class_is_omap2())
1248 mmc->f_max = 48000000;
1250 mmc->f_max = 24000000;
1251 if (host->pdata->max_freq)
1252 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1253 mmc->ocr_avail = slot->pdata->ocr_mask;
1255 /* Use scatterlist DMA to reduce per-transfer costs.
1256 * NOTE max_seg_size assumption that small blocks aren't
1257 * normally used (except e.g. for reading SD registers).
1260 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1261 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1262 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1263 mmc->max_seg_size = mmc->max_req_size;
1265 r = mmc_add_host(mmc);
1267 goto err_remove_host;
1269 if (slot->pdata->name != NULL) {
1270 r = device_create_file(&mmc->class_dev,
1271 &dev_attr_slot_name);
1273 goto err_remove_host;
1276 if (slot->pdata->get_cover_state != NULL) {
1277 r = device_create_file(&mmc->class_dev,
1278 &dev_attr_cover_switch);
1280 goto err_remove_slot_name;
1282 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1283 (unsigned long)slot);
1284 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1285 (unsigned long)slot);
1286 tasklet_schedule(&slot->cover_tasklet);
1291 err_remove_slot_name:
1292 if (slot->pdata->name != NULL)
1293 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1295 mmc_remove_host(mmc);
1300 static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1302 struct mmc_host *mmc = slot->mmc;
1304 if (slot->pdata->name != NULL)
1305 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1306 if (slot->pdata->get_cover_state != NULL)
1307 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1309 tasklet_kill(&slot->cover_tasklet);
1310 del_timer_sync(&slot->cover_timer);
1311 flush_workqueue(slot->host->mmc_omap_wq);
1313 mmc_remove_host(mmc);
1317 static int __devinit mmc_omap_probe(struct platform_device *pdev)
1319 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1320 struct mmc_omap_host *host = NULL;
1321 struct resource *res;
1322 dma_cap_mask_t mask;
1327 if (pdata == NULL) {
1328 dev_err(&pdev->dev, "platform data missing\n");
1331 if (pdata->nr_slots == 0) {
1332 dev_err(&pdev->dev, "no slots\n");
1336 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1337 irq = platform_get_irq(pdev, 0);
1338 if (res == NULL || irq < 0)
1341 res = request_mem_region(res->start, resource_size(res),
1346 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1349 goto err_free_mem_region;
1352 INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
1353 INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
1355 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1356 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1357 (unsigned long) host);
1359 spin_lock_init(&host->clk_lock);
1360 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1362 spin_lock_init(&host->dma_lock);
1363 spin_lock_init(&host->slot_lock);
1364 init_waitqueue_head(&host->slot_wq);
1366 host->pdata = pdata;
1367 host->dev = &pdev->dev;
1368 platform_set_drvdata(pdev, host);
1370 host->id = pdev->id;
1371 host->mem_res = res;
1375 host->phys_base = host->mem_res->start;
1376 host->virt_base = ioremap(res->start, resource_size(res));
1377 if (!host->virt_base)
1380 host->iclk = clk_get(&pdev->dev, "ick");
1381 if (IS_ERR(host->iclk)) {
1382 ret = PTR_ERR(host->iclk);
1383 goto err_free_mmc_host;
1385 clk_enable(host->iclk);
1387 host->fclk = clk_get(&pdev->dev, "fck");
1388 if (IS_ERR(host->fclk)) {
1389 ret = PTR_ERR(host->fclk);
1394 dma_cap_set(DMA_SLAVE, mask);
1396 host->dma_tx_burst = -1;
1397 host->dma_rx_burst = -1;
1399 if (cpu_is_omap24xx())
1400 sig = host->id == 0 ? OMAP24XX_DMA_MMC1_TX : OMAP24XX_DMA_MMC2_TX;
1402 sig = host->id == 0 ? OMAP_DMA_MMC_TX : OMAP_DMA_MMC2_TX;
1403 host->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1405 if (!host->dma_tx) {
1406 dev_err(host->dev, "unable to obtain TX DMA engine channel %u\n",
1412 dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
1415 if (cpu_is_omap24xx())
1416 sig = host->id == 0 ? OMAP24XX_DMA_MMC1_RX : OMAP24XX_DMA_MMC2_RX;
1418 sig = host->id == 0 ? OMAP_DMA_MMC_RX : OMAP_DMA_MMC2_RX;
1419 host->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1421 if (!host->dma_rx) {
1422 dev_err(host->dev, "unable to obtain RX DMA engine channel %u\n",
1428 dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
1432 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1436 if (pdata->init != NULL) {
1437 ret = pdata->init(&pdev->dev);
1442 host->nr_slots = pdata->nr_slots;
1443 host->reg_shift = (cpu_is_omap7xx() ? 1 : 2);
1445 host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
1446 if (!host->mmc_omap_wq)
1447 goto err_plat_cleanup;
1449 for (i = 0; i < pdata->nr_slots; i++) {
1450 ret = mmc_omap_new_slot(host, i);
1453 mmc_omap_remove_slot(host->slots[i]);
1455 goto err_destroy_wq;
1462 destroy_workqueue(host->mmc_omap_wq);
1465 pdata->cleanup(&pdev->dev);
1467 free_irq(host->irq, host);
1470 dma_release_channel(host->dma_tx);
1472 dma_release_channel(host->dma_rx);
1473 clk_put(host->fclk);
1475 clk_disable(host->iclk);
1476 clk_put(host->iclk);
1478 iounmap(host->virt_base);
1481 err_free_mem_region:
1482 release_mem_region(res->start, resource_size(res));
1486 static int __devexit mmc_omap_remove(struct platform_device *pdev)
1488 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1491 platform_set_drvdata(pdev, NULL);
1493 BUG_ON(host == NULL);
1495 for (i = 0; i < host->nr_slots; i++)
1496 mmc_omap_remove_slot(host->slots[i]);
1498 if (host->pdata->cleanup)
1499 host->pdata->cleanup(&pdev->dev);
1501 mmc_omap_fclk_enable(host, 0);
1502 free_irq(host->irq, host);
1503 clk_put(host->fclk);
1504 clk_disable(host->iclk);
1505 clk_put(host->iclk);
1508 dma_release_channel(host->dma_tx);
1510 dma_release_channel(host->dma_rx);
1512 iounmap(host->virt_base);
1513 release_mem_region(pdev->resource[0].start,
1514 pdev->resource[0].end - pdev->resource[0].start + 1);
1515 destroy_workqueue(host->mmc_omap_wq);
1523 static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1526 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1528 if (host == NULL || host->suspended)
1531 for (i = 0; i < host->nr_slots; i++) {
1532 struct mmc_omap_slot *slot;
1534 slot = host->slots[i];
1535 ret = mmc_suspend_host(slot->mmc);
1538 slot = host->slots[i];
1539 mmc_resume_host(slot->mmc);
1544 host->suspended = 1;
1548 static int mmc_omap_resume(struct platform_device *pdev)
1551 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1553 if (host == NULL || !host->suspended)
1556 for (i = 0; i < host->nr_slots; i++) {
1557 struct mmc_omap_slot *slot;
1558 slot = host->slots[i];
1559 ret = mmc_resume_host(slot->mmc);
1563 host->suspended = 0;
1568 #define mmc_omap_suspend NULL
1569 #define mmc_omap_resume NULL
1572 static struct platform_driver mmc_omap_driver = {
1573 .probe = mmc_omap_probe,
1574 .remove = __devexit_p(mmc_omap_remove),
1575 .suspend = mmc_omap_suspend,
1576 .resume = mmc_omap_resume,
1578 .name = DRIVER_NAME,
1579 .owner = THIS_MODULE,
1583 module_platform_driver(mmc_omap_driver);
1584 MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1585 MODULE_LICENSE("GPL");
1586 MODULE_ALIAS("platform:" DRIVER_NAME);
1587 MODULE_AUTHOR("Juha Yrjölä");