2 * linux/drivers/mmc/host/msm_sdcc.c - Qualcomm MSM 7X00A SDCC Driver
4 * Copyright (C) 2007 Google Inc,
5 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
6 * Copyright (C) 2009, Code Aurora Forum. All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * Author: San Mehat (san@android.com)
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
26 #include <linux/highmem.h>
27 #include <linux/log2.h>
28 #include <linux/mmc/host.h>
29 #include <linux/mmc/card.h>
30 #include <linux/mmc/sdio.h>
31 #include <linux/clk.h>
32 #include <linux/scatterlist.h>
33 #include <linux/platform_device.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/debugfs.h>
37 #include <linux/memory.h>
38 #include <linux/gfp.h>
40 #include <asm/cacheflush.h>
41 #include <asm/div64.h>
42 #include <asm/sizes.h>
45 #include <mach/msm_iomap.h>
50 #define DRIVER_NAME "msm-sdcc"
52 #define BUSCLK_PWRSAVE 1
53 #define BUSCLK_TIMEOUT (HZ)
54 static unsigned int msmsdcc_fmin = 144000;
55 static unsigned int msmsdcc_fmax = 50000000;
56 static unsigned int msmsdcc_4bit = 1;
57 static unsigned int msmsdcc_pwrsave = 1;
58 static unsigned int msmsdcc_piopoll = 1;
59 static unsigned int msmsdcc_sdioirq;
61 #define PIO_SPINMAX 30
62 #define CMD_SPINMAX 20
66 msmsdcc_disable_clocks(struct msmsdcc_host *host, int deferr)
68 WARN_ON(!host->clks_on);
70 BUG_ON(host->curr.mrq);
73 mod_timer(&host->busclk_timer, jiffies + BUSCLK_TIMEOUT);
75 del_timer_sync(&host->busclk_timer);
76 /* Need to check clks_on again in case the busclk
80 clk_disable(host->clk);
81 clk_disable(host->pclk);
88 msmsdcc_enable_clocks(struct msmsdcc_host *host)
92 del_timer_sync(&host->busclk_timer);
95 rc = clk_enable(host->pclk);
98 rc = clk_enable(host->clk);
100 clk_disable(host->pclk);
103 udelay(1 + ((3 * USEC_PER_SEC) /
104 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
110 static inline unsigned int
111 msmsdcc_readl(struct msmsdcc_host *host, unsigned int reg)
113 return readl(host->base + reg);
117 msmsdcc_writel(struct msmsdcc_host *host, u32 data, unsigned int reg)
119 writel(data, host->base + reg);
120 /* 3 clk delay required! */
121 udelay(1 + ((3 * USEC_PER_SEC) /
122 (host->clk_rate ? host->clk_rate : msmsdcc_fmin)));
126 msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd,
130 msmsdcc_request_end(struct msmsdcc_host *host, struct mmc_request *mrq)
132 BUG_ON(host->curr.data);
134 host->curr.mrq = NULL;
135 host->curr.cmd = NULL;
138 mrq->data->bytes_xfered = host->curr.data_xfered;
139 if (mrq->cmd->error == -ETIMEDOUT)
143 msmsdcc_disable_clocks(host, 1);
146 * Need to drop the host lock here; mmc_request_done may call
147 * back into the driver...
149 spin_unlock(&host->lock);
150 mmc_request_done(host->mmc, mrq);
151 spin_lock(&host->lock);
155 msmsdcc_stop_data(struct msmsdcc_host *host)
157 host->curr.data = NULL;
158 host->curr.got_dataend = host->curr.got_datablkend = 0;
161 uint32_t msmsdcc_fifo_addr(struct msmsdcc_host *host)
163 return host->memres->start + MMCIFIFO;
167 msmsdcc_start_command_exec(struct msmsdcc_host *host, u32 arg, u32 c) {
168 msmsdcc_writel(host, arg, MMCIARGUMENT);
169 msmsdcc_writel(host, c, MMCICOMMAND);
173 msmsdcc_dma_exec_func(struct msm_dmov_cmd *cmd)
175 struct msmsdcc_host *host = (struct msmsdcc_host *)cmd->data;
177 msmsdcc_writel(host, host->cmd_timeout, MMCIDATATIMER);
178 msmsdcc_writel(host, (unsigned int)host->curr.xfer_size,
180 msmsdcc_writel(host, host->cmd_pio_irqmask, MMCIMASK1);
181 msmsdcc_writel(host, host->cmd_datactrl, MMCIDATACTRL);
184 msmsdcc_start_command_exec(host,
185 (u32) host->cmd_cmd->arg,
188 host->dma.active = 1;
192 msmsdcc_dma_complete_func(struct msm_dmov_cmd *cmd,
194 struct msm_dmov_errdata *err)
196 struct msmsdcc_dma_data *dma_data =
197 container_of(cmd, struct msmsdcc_dma_data, hdr);
198 struct msmsdcc_host *host = dma_data->host;
200 struct mmc_request *mrq;
202 spin_lock_irqsave(&host->lock, flags);
203 host->dma.active = 0;
205 mrq = host->curr.mrq;
209 if (!(result & DMOV_RSLT_VALID)) {
210 pr_err("msmsdcc: Invalid DataMover result\n");
214 if (result & DMOV_RSLT_DONE) {
215 host->curr.data_xfered = host->curr.xfer_size;
218 if (result & DMOV_RSLT_ERROR)
219 pr_err("%s: DMA error (0x%.8x)\n",
220 mmc_hostname(host->mmc), result);
221 if (result & DMOV_RSLT_FLUSH)
222 pr_err("%s: DMA channel flushed (0x%.8x)\n",
223 mmc_hostname(host->mmc), result);
225 pr_err("Flush data: %.8x %.8x %.8x %.8x %.8x %.8x\n",
226 err->flush[0], err->flush[1], err->flush[2],
227 err->flush[3], err->flush[4], err->flush[5]);
228 if (!mrq->data->error)
229 mrq->data->error = -EIO;
231 dma_unmap_sg(mmc_dev(host->mmc), host->dma.sg, host->dma.num_ents,
234 if (host->curr.user_pages) {
235 struct scatterlist *sg = host->dma.sg;
238 for (i = 0; i < host->dma.num_ents; i++)
239 flush_dcache_page(sg_page(sg++));
245 if ((host->curr.got_dataend && host->curr.got_datablkend)
246 || mrq->data->error) {
249 * If we've already gotten our DATAEND / DATABLKEND
250 * for this request, then complete it through here.
252 msmsdcc_stop_data(host);
254 if (!mrq->data->error)
255 host->curr.data_xfered = host->curr.xfer_size;
256 if (!mrq->data->stop || mrq->cmd->error) {
257 host->curr.mrq = NULL;
258 host->curr.cmd = NULL;
259 mrq->data->bytes_xfered = host->curr.data_xfered;
261 spin_unlock_irqrestore(&host->lock, flags);
263 msmsdcc_disable_clocks(host, 1);
265 mmc_request_done(host->mmc, mrq);
268 msmsdcc_start_command(host, mrq->data->stop, 0);
272 spin_unlock_irqrestore(&host->lock, flags);
276 static int validate_dma(struct msmsdcc_host *host, struct mmc_data *data)
278 if (host->dma.channel == -1)
281 if ((data->blksz * data->blocks) < MCI_FIFOSIZE)
283 if ((data->blksz * data->blocks) % MCI_FIFOSIZE)
288 static int msmsdcc_config_dma(struct msmsdcc_host *host, struct mmc_data *data)
290 struct msmsdcc_nc_dmadata *nc;
296 struct scatterlist *sg = data->sg;
298 rc = validate_dma(host, data);
302 host->dma.sg = data->sg;
303 host->dma.num_ents = data->sg_len;
305 BUG_ON(host->dma.num_ents > NR_SG); /* Prevent memory corruption */
309 switch (host->pdev_id) {
311 crci = MSMSDCC_CRCI_SDC1;
314 crci = MSMSDCC_CRCI_SDC2;
317 crci = MSMSDCC_CRCI_SDC3;
320 crci = MSMSDCC_CRCI_SDC4;
324 host->dma.num_ents = 0;
328 if (data->flags & MMC_DATA_READ)
329 host->dma.dir = DMA_FROM_DEVICE;
331 host->dma.dir = DMA_TO_DEVICE;
333 host->curr.user_pages = 0;
336 for (i = 0; i < host->dma.num_ents; i++) {
337 box->cmd = CMD_MODE_BOX;
339 /* Initialize sg dma address */
340 sg->dma_address = page_to_dma(mmc_dev(host->mmc), sg_page(sg))
343 if (i == (host->dma.num_ents - 1))
345 rows = (sg_dma_len(sg) % MCI_FIFOSIZE) ?
346 (sg_dma_len(sg) / MCI_FIFOSIZE) + 1 :
347 (sg_dma_len(sg) / MCI_FIFOSIZE) ;
349 if (data->flags & MMC_DATA_READ) {
350 box->src_row_addr = msmsdcc_fifo_addr(host);
351 box->dst_row_addr = sg_dma_address(sg);
353 box->src_dst_len = (MCI_FIFOSIZE << 16) |
355 box->row_offset = MCI_FIFOSIZE;
357 box->num_rows = rows * ((1 << 16) + 1);
358 box->cmd |= CMD_SRC_CRCI(crci);
360 box->src_row_addr = sg_dma_address(sg);
361 box->dst_row_addr = msmsdcc_fifo_addr(host);
363 box->src_dst_len = (MCI_FIFOSIZE << 16) |
365 box->row_offset = (MCI_FIFOSIZE << 16);
367 box->num_rows = rows * ((1 << 16) + 1);
368 box->cmd |= CMD_DST_CRCI(crci);
374 /* location of command block must be 64 bit aligned */
375 BUG_ON(host->dma.cmd_busaddr & 0x07);
377 nc->cmdptr = (host->dma.cmd_busaddr >> 3) | CMD_PTR_LP;
378 host->dma.hdr.cmdptr = DMOV_CMD_PTR_LIST |
379 DMOV_CMD_ADDR(host->dma.cmdptr_busaddr);
380 host->dma.hdr.complete_func = msmsdcc_dma_complete_func;
382 n = dma_map_sg(mmc_dev(host->mmc), host->dma.sg,
383 host->dma.num_ents, host->dma.dir);
384 /* dsb inside dma_map_sg will write nc out to mem as well */
386 if (n != host->dma.num_ents) {
387 printk(KERN_ERR "%s: Unable to map in all sg elements\n",
388 mmc_hostname(host->mmc));
390 host->dma.num_ents = 0;
398 snoop_cccr_abort(struct mmc_command *cmd)
400 if ((cmd->opcode == 52) &&
401 (cmd->arg & 0x80000000) &&
402 (((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT))
408 msmsdcc_start_command_deferred(struct msmsdcc_host *host,
409 struct mmc_command *cmd, u32 *c)
411 *c |= (cmd->opcode | MCI_CPSM_ENABLE);
413 if (cmd->flags & MMC_RSP_PRESENT) {
414 if (cmd->flags & MMC_RSP_136)
415 *c |= MCI_CPSM_LONGRSP;
416 *c |= MCI_CPSM_RESPONSE;
420 *c |= MCI_CPSM_INTERRUPT;
422 if ((((cmd->opcode == 17) || (cmd->opcode == 18)) ||
423 ((cmd->opcode == 24) || (cmd->opcode == 25))) ||
425 *c |= MCI_CSPM_DATCMD;
427 if (cmd == cmd->mrq->stop)
428 *c |= MCI_CSPM_MCIABORT;
430 if (snoop_cccr_abort(cmd))
431 *c |= MCI_CSPM_MCIABORT;
433 if (host->curr.cmd != NULL) {
434 printk(KERN_ERR "%s: Overlapping command requests\n",
435 mmc_hostname(host->mmc));
437 host->curr.cmd = cmd;
441 msmsdcc_start_data(struct msmsdcc_host *host, struct mmc_data *data,
442 struct mmc_command *cmd, u32 c)
444 unsigned int datactrl, timeout;
445 unsigned long long clks;
446 unsigned int pio_irqmask = 0;
448 host->curr.data = data;
449 host->curr.xfer_size = data->blksz * data->blocks;
450 host->curr.xfer_remain = host->curr.xfer_size;
451 host->curr.data_xfered = 0;
452 host->curr.got_dataend = 0;
453 host->curr.got_datablkend = 0;
455 memset(&host->pio, 0, sizeof(host->pio));
457 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
459 if (!msmsdcc_config_dma(host, data))
460 datactrl |= MCI_DPSM_DMAENABLE;
462 host->pio.sg = data->sg;
463 host->pio.sg_len = data->sg_len;
464 host->pio.sg_off = 0;
466 if (data->flags & MMC_DATA_READ) {
467 pio_irqmask = MCI_RXFIFOHALFFULLMASK;
468 if (host->curr.xfer_remain < MCI_FIFOSIZE)
469 pio_irqmask |= MCI_RXDATAAVLBLMASK;
471 pio_irqmask = MCI_TXFIFOHALFEMPTYMASK;
474 if (data->flags & MMC_DATA_READ)
475 datactrl |= MCI_DPSM_DIRECTION;
477 clks = (unsigned long long)data->timeout_ns * host->clk_rate;
478 do_div(clks, NSEC_PER_SEC);
479 timeout = data->timeout_clks + (unsigned int)clks*2 ;
481 if (datactrl & MCI_DPSM_DMAENABLE) {
482 /* Save parameters for the exec function */
483 host->cmd_timeout = timeout;
484 host->cmd_pio_irqmask = pio_irqmask;
485 host->cmd_datactrl = datactrl;
488 host->dma.hdr.execute_func = msmsdcc_dma_exec_func;
489 host->dma.hdr.data = (void *)host;
493 msmsdcc_start_command_deferred(host, cmd, &c);
496 msm_dmov_enqueue_cmd(host->dma.channel, &host->dma.hdr);
498 msmsdcc_writel(host, timeout, MMCIDATATIMER);
500 msmsdcc_writel(host, host->curr.xfer_size, MMCIDATALENGTH);
502 msmsdcc_writel(host, pio_irqmask, MMCIMASK1);
503 msmsdcc_writel(host, datactrl, MMCIDATACTRL);
506 /* Daisy-chain the command if requested */
507 msmsdcc_start_command(host, cmd, c);
513 msmsdcc_start_command(struct msmsdcc_host *host, struct mmc_command *cmd, u32 c)
515 if (cmd == cmd->mrq->stop)
516 c |= MCI_CSPM_MCIABORT;
520 msmsdcc_start_command_deferred(host, cmd, &c);
521 msmsdcc_start_command_exec(host, cmd->arg, c);
525 msmsdcc_data_err(struct msmsdcc_host *host, struct mmc_data *data,
528 if (status & MCI_DATACRCFAIL) {
529 pr_err("%s: Data CRC error\n", mmc_hostname(host->mmc));
530 pr_err("%s: opcode 0x%.8x\n", __func__,
531 data->mrq->cmd->opcode);
532 pr_err("%s: blksz %d, blocks %d\n", __func__,
533 data->blksz, data->blocks);
534 data->error = -EILSEQ;
535 } else if (status & MCI_DATATIMEOUT) {
536 pr_err("%s: Data timeout\n", mmc_hostname(host->mmc));
537 data->error = -ETIMEDOUT;
538 } else if (status & MCI_RXOVERRUN) {
539 pr_err("%s: RX overrun\n", mmc_hostname(host->mmc));
541 } else if (status & MCI_TXUNDERRUN) {
542 pr_err("%s: TX underrun\n", mmc_hostname(host->mmc));
545 pr_err("%s: Unknown error (0x%.8x)\n",
546 mmc_hostname(host->mmc), status);
553 msmsdcc_pio_read(struct msmsdcc_host *host, char *buffer, unsigned int remain)
555 uint32_t *ptr = (uint32_t *) buffer;
558 while (msmsdcc_readl(host, MMCISTATUS) & MCI_RXDATAAVLBL) {
559 *ptr = msmsdcc_readl(host, MMCIFIFO + (count % MCI_FIFOSIZE));
561 count += sizeof(uint32_t);
563 remain -= sizeof(uint32_t);
571 msmsdcc_pio_write(struct msmsdcc_host *host, char *buffer,
572 unsigned int remain, u32 status)
574 void __iomem *base = host->base;
578 unsigned int count, maxcnt;
580 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE :
582 count = min(remain, maxcnt);
584 writesl(base + MMCIFIFO, ptr, count >> 2);
591 status = msmsdcc_readl(host, MMCISTATUS);
592 } while (status & MCI_TXFIFOHALFEMPTY);
598 msmsdcc_spin_on_status(struct msmsdcc_host *host, uint32_t mask, int maxspin)
601 if ((msmsdcc_readl(host, MMCISTATUS) & mask))
610 msmsdcc_pio_irq(int irq, void *dev_id)
612 struct msmsdcc_host *host = dev_id;
615 status = msmsdcc_readl(host, MMCISTATUS);
619 unsigned int remain, len;
622 if (!(status & (MCI_TXFIFOHALFEMPTY | MCI_RXDATAAVLBL))) {
623 if (host->curr.xfer_remain == 0 || !msmsdcc_piopoll)
626 if (msmsdcc_spin_on_status(host,
627 (MCI_TXFIFOHALFEMPTY |
634 /* Map the current scatter buffer */
635 local_irq_save(flags);
636 buffer = kmap_atomic(sg_page(host->pio.sg),
637 KM_BIO_SRC_IRQ) + host->pio.sg->offset;
638 buffer += host->pio.sg_off;
639 remain = host->pio.sg->length - host->pio.sg_off;
641 if (status & MCI_RXACTIVE)
642 len = msmsdcc_pio_read(host, buffer, remain);
643 if (status & MCI_TXACTIVE)
644 len = msmsdcc_pio_write(host, buffer, remain, status);
646 /* Unmap the buffer */
647 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
648 local_irq_restore(flags);
650 host->pio.sg_off += len;
651 host->curr.xfer_remain -= len;
652 host->curr.data_xfered += len;
656 /* This sg page is full - do some housekeeping */
657 if (status & MCI_RXACTIVE && host->curr.user_pages)
658 flush_dcache_page(sg_page(host->pio.sg));
660 if (!--host->pio.sg_len) {
661 memset(&host->pio, 0, sizeof(host->pio));
665 /* Advance to next sg */
667 host->pio.sg_off = 0;
670 status = msmsdcc_readl(host, MMCISTATUS);
673 if (status & MCI_RXACTIVE && host->curr.xfer_remain < MCI_FIFOSIZE)
674 msmsdcc_writel(host, MCI_RXDATAAVLBLMASK, MMCIMASK1);
676 if (!host->curr.xfer_remain)
677 msmsdcc_writel(host, 0, MMCIMASK1);
682 static void msmsdcc_do_cmdirq(struct msmsdcc_host *host, uint32_t status)
684 struct mmc_command *cmd = host->curr.cmd;
686 host->curr.cmd = NULL;
687 cmd->resp[0] = msmsdcc_readl(host, MMCIRESPONSE0);
688 cmd->resp[1] = msmsdcc_readl(host, MMCIRESPONSE1);
689 cmd->resp[2] = msmsdcc_readl(host, MMCIRESPONSE2);
690 cmd->resp[3] = msmsdcc_readl(host, MMCIRESPONSE3);
692 if (status & MCI_CMDTIMEOUT) {
693 cmd->error = -ETIMEDOUT;
694 } else if (status & MCI_CMDCRCFAIL &&
695 cmd->flags & MMC_RSP_CRC) {
696 pr_err("%s: Command CRC error\n", mmc_hostname(host->mmc));
697 cmd->error = -EILSEQ;
700 if (!cmd->data || cmd->error) {
701 if (host->curr.data && host->dma.sg)
702 msm_dmov_stop_cmd(host->dma.channel,
704 else if (host->curr.data) { /* Non DMA */
705 msmsdcc_stop_data(host);
706 msmsdcc_request_end(host, cmd->mrq);
707 } else /* host->data == NULL */
708 msmsdcc_request_end(host, cmd->mrq);
709 } else if (cmd->data)
710 if (!(cmd->data->flags & MMC_DATA_READ))
711 msmsdcc_start_data(host, cmd->data,
716 msmsdcc_handle_irq_data(struct msmsdcc_host *host, u32 status,
719 struct mmc_data *data = host->curr.data;
721 if (status & (MCI_CMDSENT | MCI_CMDRESPEND | MCI_CMDCRCFAIL |
722 MCI_CMDTIMEOUT) && host->curr.cmd) {
723 msmsdcc_do_cmdirq(host, status);
729 /* Check for data errors */
730 if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
731 MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
732 msmsdcc_data_err(host, data, status);
733 host->curr.data_xfered = 0;
735 msm_dmov_stop_cmd(host->dma.channel,
739 msmsdcc_stop_data(host);
741 msmsdcc_request_end(host, data->mrq);
743 msmsdcc_start_command(host, data->stop, 0);
747 /* Check for data done */
748 if (!host->curr.got_dataend && (status & MCI_DATAEND))
749 host->curr.got_dataend = 1;
751 if (!host->curr.got_datablkend && (status & MCI_DATABLOCKEND))
752 host->curr.got_datablkend = 1;
755 * If DMA is still in progress, we complete via the completion handler
757 if (host->curr.got_dataend && host->curr.got_datablkend &&
760 * There appears to be an issue in the controller where
761 * if you request a small block transfer (< fifo size),
762 * you may get your DATAEND/DATABLKEND irq without the
765 * Check to see if there is still data to be read,
766 * and simulate a PIO irq.
768 if (readl(base + MMCISTATUS) & MCI_RXDATAAVLBL)
769 msmsdcc_pio_irq(1, host);
771 msmsdcc_stop_data(host);
773 host->curr.data_xfered = host->curr.xfer_size;
776 msmsdcc_request_end(host, data->mrq);
778 msmsdcc_start_command(host, data->stop, 0);
783 msmsdcc_irq(int irq, void *dev_id)
785 struct msmsdcc_host *host = dev_id;
786 void __iomem *base = host->base;
791 spin_lock(&host->lock);
794 status = msmsdcc_readl(host, MMCISTATUS);
795 status &= (msmsdcc_readl(host, MMCIMASK0) |
796 MCI_DATABLOCKENDMASK);
797 msmsdcc_writel(host, status, MMCICLEAR);
799 if (status & MCI_SDIOINTR)
800 status &= ~MCI_SDIOINTR;
805 msmsdcc_handle_irq_data(host, status, base);
807 if (status & MCI_SDIOINTOPER) {
809 status &= ~MCI_SDIOINTOPER;
814 spin_unlock(&host->lock);
817 * We have to delay handling the card interrupt as it calls
818 * back into the driver.
821 mmc_signal_sdio_irq(host->mmc);
823 return IRQ_RETVAL(ret);
827 msmsdcc_request(struct mmc_host *mmc, struct mmc_request *mrq)
829 struct msmsdcc_host *host = mmc_priv(mmc);
832 WARN_ON(host->curr.mrq != NULL);
833 WARN_ON(host->pwr == 0);
835 spin_lock_irqsave(&host->lock, flags);
840 if (mrq->data && !(mrq->data->flags & MMC_DATA_READ)) {
842 mrq->data->bytes_xfered = mrq->data->blksz *
845 mrq->cmd->error = -ENOMEDIUM;
847 spin_unlock_irqrestore(&host->lock, flags);
848 mmc_request_done(mmc, mrq);
852 msmsdcc_enable_clocks(host);
854 host->curr.mrq = mrq;
856 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
857 /* Queue/read data, daisy-chain command when data starts */
858 msmsdcc_start_data(host, mrq->data, mrq->cmd, 0);
860 msmsdcc_start_command(host, mrq->cmd, 0);
862 if (host->cmdpoll && !msmsdcc_spin_on_status(host,
863 MCI_CMDRESPEND|MCI_CMDCRCFAIL|MCI_CMDTIMEOUT,
865 uint32_t status = msmsdcc_readl(host, MMCISTATUS);
866 msmsdcc_do_cmdirq(host, status);
868 MCI_CMDRESPEND | MCI_CMDCRCFAIL | MCI_CMDTIMEOUT,
870 host->stats.cmdpoll_hits++;
872 host->stats.cmdpoll_misses++;
874 spin_unlock_irqrestore(&host->lock, flags);
878 msmsdcc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
880 struct msmsdcc_host *host = mmc_priv(mmc);
881 u32 clk = 0, pwr = 0;
885 spin_lock_irqsave(&host->lock, flags);
887 msmsdcc_enable_clocks(host);
890 if (ios->clock != host->clk_rate) {
891 rc = clk_set_rate(host->clk, ios->clock);
893 pr_err("%s: Error setting clock rate (%d)\n",
894 mmc_hostname(host->mmc), rc);
896 host->clk_rate = ios->clock;
898 clk |= MCI_CLK_ENABLE;
901 if (ios->bus_width == MMC_BUS_WIDTH_4)
902 clk |= (2 << 10); /* Set WIDEBUS */
904 if (ios->clock > 400000 && msmsdcc_pwrsave)
905 clk |= (1 << 9); /* PWRSAVE */
907 clk |= (1 << 12); /* FLOW_ENA */
908 clk |= (1 << 15); /* feedback clock */
910 if (host->plat->translate_vdd)
911 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
913 switch (ios->power_mode) {
924 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
927 msmsdcc_writel(host, clk, MMCICLOCK);
929 if (host->pwr != pwr) {
931 msmsdcc_writel(host, pwr, MMCIPOWER);
934 msmsdcc_disable_clocks(host, 1);
936 spin_unlock_irqrestore(&host->lock, flags);
939 static void msmsdcc_enable_sdio_irq(struct mmc_host *mmc, int enable)
941 struct msmsdcc_host *host = mmc_priv(mmc);
945 spin_lock_irqsave(&host->lock, flags);
946 if (msmsdcc_sdioirq == 1) {
947 status = msmsdcc_readl(host, MMCIMASK0);
949 status |= MCI_SDIOINTOPERMASK;
951 status &= ~MCI_SDIOINTOPERMASK;
952 host->saved_irq0mask = status;
953 msmsdcc_writel(host, status, MMCIMASK0);
955 spin_unlock_irqrestore(&host->lock, flags);
958 static const struct mmc_host_ops msmsdcc_ops = {
959 .request = msmsdcc_request,
960 .set_ios = msmsdcc_set_ios,
961 .enable_sdio_irq = msmsdcc_enable_sdio_irq,
965 msmsdcc_check_status(unsigned long data)
967 struct msmsdcc_host *host = (struct msmsdcc_host *)data;
970 if (!host->plat->status) {
971 mmc_detect_change(host->mmc, 0);
975 status = host->plat->status(mmc_dev(host->mmc));
976 host->eject = !status;
977 if (status ^ host->oldstat) {
978 pr_info("%s: Slot status change detected (%d -> %d)\n",
979 mmc_hostname(host->mmc), host->oldstat, status);
981 mmc_detect_change(host->mmc, (5 * HZ) / 2);
983 mmc_detect_change(host->mmc, 0);
986 host->oldstat = status;
989 if (host->timer.function)
990 mod_timer(&host->timer, jiffies + HZ);
994 msmsdcc_platform_status_irq(int irq, void *dev_id)
996 struct msmsdcc_host *host = dev_id;
998 printk(KERN_DEBUG "%s: %d\n", __func__, irq);
999 msmsdcc_check_status((unsigned long) host);
1004 msmsdcc_status_notify_cb(int card_present, void *dev_id)
1006 struct msmsdcc_host *host = dev_id;
1008 printk(KERN_DEBUG "%s: card_present %d\n", mmc_hostname(host->mmc),
1010 msmsdcc_check_status((unsigned long) host);
1014 msmsdcc_busclk_expired(unsigned long _data)
1016 struct msmsdcc_host *host = (struct msmsdcc_host *) _data;
1019 msmsdcc_disable_clocks(host, 0);
1023 msmsdcc_init_dma(struct msmsdcc_host *host)
1025 memset(&host->dma, 0, sizeof(struct msmsdcc_dma_data));
1026 host->dma.host = host;
1027 host->dma.channel = -1;
1032 host->dma.nc = dma_alloc_coherent(NULL,
1033 sizeof(struct msmsdcc_nc_dmadata),
1034 &host->dma.nc_busaddr,
1036 if (host->dma.nc == NULL) {
1037 pr_err("Unable to allocate DMA buffer\n");
1040 memset(host->dma.nc, 0x00, sizeof(struct msmsdcc_nc_dmadata));
1041 host->dma.cmd_busaddr = host->dma.nc_busaddr;
1042 host->dma.cmdptr_busaddr = host->dma.nc_busaddr +
1043 offsetof(struct msmsdcc_nc_dmadata, cmdptr);
1044 host->dma.channel = host->dmares->start;
1050 msmsdcc_probe(struct platform_device *pdev)
1052 struct msm_mmc_platform_data *plat = pdev->dev.platform_data;
1053 struct msmsdcc_host *host;
1054 struct mmc_host *mmc;
1055 struct resource *cmd_irqres = NULL;
1056 struct resource *pio_irqres = NULL;
1057 struct resource *stat_irqres = NULL;
1058 struct resource *memres = NULL;
1059 struct resource *dmares = NULL;
1062 /* must have platform data */
1064 pr_err("%s: Platform data not available\n", __func__);
1069 if (pdev->id < 1 || pdev->id > 4)
1072 if (pdev->resource == NULL || pdev->num_resources < 2) {
1073 pr_err("%s: Invalid resource\n", __func__);
1077 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1078 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1079 cmd_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1081 pio_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1083 stat_irqres = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1086 if (!cmd_irqres || !pio_irqres || !memres) {
1087 pr_err("%s: Invalid resource\n", __func__);
1092 * Setup our host structure
1095 mmc = mmc_alloc_host(sizeof(struct msmsdcc_host), &pdev->dev);
1101 host = mmc_priv(mmc);
1102 host->pdev_id = pdev->id;
1105 host->curr.cmd = NULL;
1109 host->base = ioremap(memres->start, PAGE_SIZE);
1115 host->cmd_irqres = cmd_irqres;
1116 host->pio_irqres = pio_irqres;
1117 host->memres = memres;
1118 host->dmares = dmares;
1119 spin_lock_init(&host->lock);
1124 msmsdcc_init_dma(host);
1126 /* Get our clocks */
1127 host->pclk = clk_get(&pdev->dev, "sdc_pclk");
1128 if (IS_ERR(host->pclk)) {
1129 ret = PTR_ERR(host->pclk);
1133 host->clk = clk_get(&pdev->dev, "sdc_clk");
1134 if (IS_ERR(host->clk)) {
1135 ret = PTR_ERR(host->clk);
1140 ret = msmsdcc_enable_clocks(host);
1144 ret = clk_set_rate(host->clk, msmsdcc_fmin);
1146 pr_err("%s: Clock rate set failed (%d)\n", __func__, ret);
1150 host->pclk_rate = clk_get_rate(host->pclk);
1151 host->clk_rate = clk_get_rate(host->clk);
1154 * Setup MMC host structure
1156 mmc->ops = &msmsdcc_ops;
1157 mmc->f_min = msmsdcc_fmin;
1158 mmc->f_max = msmsdcc_fmax;
1159 mmc->ocr_avail = plat->ocr_mask;
1162 mmc->caps |= MMC_CAP_4_BIT_DATA;
1163 if (msmsdcc_sdioirq)
1164 mmc->caps |= MMC_CAP_SDIO_IRQ;
1165 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
1167 mmc->max_segs = NR_SG;
1168 mmc->max_blk_size = 4096; /* MCI_DATA_CTL BLOCKSIZE up to 4096 */
1169 mmc->max_blk_count = 65536;
1171 mmc->max_req_size = 33554432; /* MCI_DATA_LENGTH is 25 bits */
1172 mmc->max_seg_size = mmc->max_req_size;
1174 msmsdcc_writel(host, 0, MMCIMASK0);
1175 msmsdcc_writel(host, 0x5e007ff, MMCICLEAR);
1177 msmsdcc_writel(host, MCI_IRQENABLE, MMCIMASK0);
1178 host->saved_irq0mask = MCI_IRQENABLE;
1181 * Setup card detect change
1184 memset(&host->timer, 0, sizeof(host->timer));
1186 if (stat_irqres && !(stat_irqres->flags & IORESOURCE_DISABLED)) {
1187 unsigned long irqflags = IRQF_SHARED |
1188 (stat_irqres->flags & IRQF_TRIGGER_MASK);
1190 host->stat_irq = stat_irqres->start;
1191 ret = request_irq(host->stat_irq,
1192 msmsdcc_platform_status_irq,
1194 DRIVER_NAME " (slot)",
1197 pr_err("%s: Unable to get slot IRQ %d (%d)\n",
1198 mmc_hostname(mmc), host->stat_irq, ret);
1201 } else if (plat->register_status_notify) {
1202 plat->register_status_notify(msmsdcc_status_notify_cb, host);
1203 } else if (!plat->status)
1204 pr_err("%s: No card detect facilities available\n",
1207 init_timer(&host->timer);
1208 host->timer.data = (unsigned long)host;
1209 host->timer.function = msmsdcc_check_status;
1210 host->timer.expires = jiffies + HZ;
1211 add_timer(&host->timer);
1215 host->oldstat = host->plat->status(mmc_dev(host->mmc));
1216 host->eject = !host->oldstat;
1219 init_timer(&host->busclk_timer);
1220 host->busclk_timer.data = (unsigned long) host;
1221 host->busclk_timer.function = msmsdcc_busclk_expired;
1223 ret = request_irq(cmd_irqres->start, msmsdcc_irq, IRQF_SHARED,
1224 DRIVER_NAME " (cmd)", host);
1228 ret = request_irq(pio_irqres->start, msmsdcc_pio_irq, IRQF_SHARED,
1229 DRIVER_NAME " (pio)", host);
1233 mmc_set_drvdata(pdev, mmc);
1236 pr_info("%s: Qualcomm MSM SDCC at 0x%016llx irq %d,%d dma %d\n",
1237 mmc_hostname(mmc), (unsigned long long)memres->start,
1238 (unsigned int) cmd_irqres->start,
1239 (unsigned int) host->stat_irq, host->dma.channel);
1240 pr_info("%s: 4 bit data mode %s\n", mmc_hostname(mmc),
1241 (mmc->caps & MMC_CAP_4_BIT_DATA ? "enabled" : "disabled"));
1242 pr_info("%s: MMC clock %u -> %u Hz, PCLK %u Hz\n",
1243 mmc_hostname(mmc), msmsdcc_fmin, msmsdcc_fmax, host->pclk_rate);
1244 pr_info("%s: Slot eject status = %d\n", mmc_hostname(mmc), host->eject);
1245 pr_info("%s: Power save feature enable = %d\n",
1246 mmc_hostname(mmc), msmsdcc_pwrsave);
1248 if (host->dma.channel != -1) {
1249 pr_info("%s: DM non-cached buffer at %p, dma_addr 0x%.8x\n",
1250 mmc_hostname(mmc), host->dma.nc, host->dma.nc_busaddr);
1251 pr_info("%s: DM cmd busaddr 0x%.8x, cmdptr busaddr 0x%.8x\n",
1252 mmc_hostname(mmc), host->dma.cmd_busaddr,
1253 host->dma.cmdptr_busaddr);
1255 pr_info("%s: PIO transfer enabled\n", mmc_hostname(mmc));
1256 if (host->timer.function)
1257 pr_info("%s: Polling status mode enabled\n", mmc_hostname(mmc));
1260 msmsdcc_disable_clocks(host, 1);
1264 free_irq(cmd_irqres->start, host);
1267 free_irq(host->stat_irq, host);
1269 msmsdcc_disable_clocks(host, 0);
1273 clk_put(host->pclk);
1281 #ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1283 do_resume_work(struct work_struct *work)
1285 struct msmsdcc_host *host =
1286 container_of(work, struct msmsdcc_host, resume_task);
1287 struct mmc_host *mmc = host->mmc;
1290 mmc_resume_host(mmc);
1292 enable_irq(host->stat_irq);
1299 msmsdcc_suspend(struct platform_device *dev, pm_message_t state)
1301 struct mmc_host *mmc = mmc_get_drvdata(dev);
1305 struct msmsdcc_host *host = mmc_priv(mmc);
1308 disable_irq(host->stat_irq);
1310 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1311 rc = mmc_suspend_host(mmc);
1313 msmsdcc_writel(host, 0, MMCIMASK0);
1315 msmsdcc_disable_clocks(host, 0);
1321 msmsdcc_resume(struct platform_device *dev)
1323 struct mmc_host *mmc = mmc_get_drvdata(dev);
1326 struct msmsdcc_host *host = mmc_priv(mmc);
1328 msmsdcc_enable_clocks(host);
1330 msmsdcc_writel(host, host->saved_irq0mask, MMCIMASK0);
1332 if (mmc->card && mmc->card->type != MMC_TYPE_SDIO)
1333 mmc_resume_host(mmc);
1335 enable_irq(host->stat_irq);
1337 msmsdcc_disable_clocks(host, 1);
1343 #define msmsdcc_suspend 0
1344 #define msmsdcc_resume 0
1347 static struct platform_driver msmsdcc_driver = {
1348 .probe = msmsdcc_probe,
1349 .suspend = msmsdcc_suspend,
1350 .resume = msmsdcc_resume,
1356 static int __init msmsdcc_init(void)
1358 return platform_driver_register(&msmsdcc_driver);
1361 static void __exit msmsdcc_exit(void)
1363 platform_driver_unregister(&msmsdcc_driver);
1366 module_init(msmsdcc_init);
1367 module_exit(msmsdcc_exit);
1369 MODULE_DESCRIPTION("Qualcomm MSM 7X00A Multimedia Card Interface driver");
1370 MODULE_LICENSE("GPL");