2 * linux/drivers/mmc/host/pxa.c - PXA MMCI driver
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This hardware is really sick:
11 * - No way to clear interrupts.
12 * - Have to turn off the clock whenever we touch the device.
13 * - Doesn't tell you how many data blocks were transferred.
16 * 1 and 3 byte data transfers not supported
17 * max block length up to 1023
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28 #include <linux/mmc/host.h>
32 #include <asm/sizes.h>
34 #include <asm/arch/pxa-regs.h>
35 #include <asm/arch/mmc.h>
39 #define DRIVER_NAME "pxa2xx-mci"
49 unsigned long clkrate;
55 unsigned int power_mode;
56 struct pxamci_platform_data *pdata;
58 struct mmc_request *mrq;
59 struct mmc_command *cmd;
60 struct mmc_data *data;
63 struct pxa_dma_desc *sg_cpu;
69 static void pxamci_stop_clock(struct pxamci_host *host)
71 if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
72 unsigned long timeout = 10000;
75 writel(STOP_CLOCK, host->base + MMC_STRPCL);
78 v = readl(host->base + MMC_STAT);
79 if (!(v & STAT_CLK_EN))
85 dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
89 static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
93 spin_lock_irqsave(&host->lock, flags);
95 writel(host->imask, host->base + MMC_I_MASK);
96 spin_unlock_irqrestore(&host->lock, flags);
99 static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
103 spin_lock_irqsave(&host->lock, flags);
105 writel(host->imask, host->base + MMC_I_MASK);
106 spin_unlock_irqrestore(&host->lock, flags);
109 static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
111 unsigned int nob = data->blocks;
112 unsigned long long clks;
113 unsigned int timeout;
119 if (data->flags & MMC_DATA_STREAM)
122 writel(nob, host->base + MMC_NOB);
123 writel(data->blksz, host->base + MMC_BLKLEN);
125 clks = (unsigned long long)data->timeout_ns * host->clkrate;
126 do_div(clks, 1000000000UL);
127 timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
128 writel((timeout + 255) / 256, host->base + MMC_RDTO);
130 if (data->flags & MMC_DATA_READ) {
131 host->dma_dir = DMA_FROM_DEVICE;
132 dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
134 DRCMRRXMMC = host->dma | DRCMR_MAPVLD;
136 host->dma_dir = DMA_TO_DEVICE;
137 dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
139 DRCMRTXMMC = host->dma | DRCMR_MAPVLD;
142 dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
144 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
147 for (i = 0; i < host->dma_len; i++) {
148 unsigned int length = sg_dma_len(&data->sg[i]);
149 host->sg_cpu[i].dcmd = dcmd | length;
150 if (length & 31 && !(data->flags & MMC_DATA_READ))
151 host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
152 if (data->flags & MMC_DATA_READ) {
153 host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
154 host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
156 host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
157 host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
159 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
160 sizeof(struct pxa_dma_desc);
162 host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
165 DDADR(host->dma) = host->sg_dma;
166 DCSR(host->dma) = DCSR_RUN;
169 static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
171 WARN_ON(host->cmd != NULL);
174 if (cmd->flags & MMC_RSP_BUSY)
177 #define RSP_TYPE(x) ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
178 switch (RSP_TYPE(mmc_resp_type(cmd))) {
179 case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
180 cmdat |= CMDAT_RESP_SHORT;
182 case RSP_TYPE(MMC_RSP_R3):
183 cmdat |= CMDAT_RESP_R3;
185 case RSP_TYPE(MMC_RSP_R2):
186 cmdat |= CMDAT_RESP_R2;
192 writel(cmd->opcode, host->base + MMC_CMD);
193 writel(cmd->arg >> 16, host->base + MMC_ARGH);
194 writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
195 writel(cmdat, host->base + MMC_CMDAT);
196 writel(host->clkrt, host->base + MMC_CLKRT);
198 writel(START_CLOCK, host->base + MMC_STRPCL);
200 pxamci_enable_irq(host, END_CMD_RES);
203 static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
208 mmc_request_done(host->mmc, mrq);
211 static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
213 struct mmc_command *cmd = host->cmd;
223 * Did I mention this is Sick. We always need to
224 * discard the upper 8 bits of the first 16-bit word.
226 v = readl(host->base + MMC_RES) & 0xffff;
227 for (i = 0; i < 4; i++) {
228 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
229 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
230 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
234 if (stat & STAT_TIME_OUT_RESPONSE) {
235 cmd->error = -ETIMEDOUT;
236 } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
239 * workaround for erratum #42:
240 * Intel PXA27x Family Processor Specification Update Rev 001
241 * A bogus CRC error can appear if the msb of a 136 bit
244 if (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000) {
245 pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
248 cmd->error = -EILSEQ;
251 pxamci_disable_irq(host, END_CMD_RES);
252 if (host->data && !cmd->error) {
253 pxamci_enable_irq(host, DATA_TRAN_DONE);
255 pxamci_finish_request(host, host->mrq);
261 static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
263 struct mmc_data *data = host->data;
269 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
272 if (stat & STAT_READ_TIME_OUT)
273 data->error = -ETIMEDOUT;
274 else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
275 data->error = -EILSEQ;
278 * There appears to be a hardware design bug here. There seems to
279 * be no way to find out how much data was transferred to the card.
280 * This means that if there was an error on any block, we mark all
281 * data blocks as being in error.
284 data->bytes_xfered = data->blocks * data->blksz;
286 data->bytes_xfered = 0;
288 pxamci_disable_irq(host, DATA_TRAN_DONE);
291 if (host->mrq->stop) {
292 pxamci_stop_clock(host);
293 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
295 pxamci_finish_request(host, host->mrq);
301 static irqreturn_t pxamci_irq(int irq, void *devid)
303 struct pxamci_host *host = devid;
307 ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
310 unsigned stat = readl(host->base + MMC_STAT);
312 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
314 if (ireg & END_CMD_RES)
315 handled |= pxamci_cmd_done(host, stat);
316 if (ireg & DATA_TRAN_DONE)
317 handled |= pxamci_data_done(host, stat);
318 if (ireg & SDIO_INT) {
319 mmc_signal_sdio_irq(host->mmc);
324 return IRQ_RETVAL(handled);
327 static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
329 struct pxamci_host *host = mmc_priv(mmc);
332 WARN_ON(host->mrq != NULL);
336 pxamci_stop_clock(host);
339 host->cmdat &= ~CMDAT_INIT;
342 pxamci_setup_data(host, mrq->data);
344 cmdat &= ~CMDAT_BUSY;
345 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
346 if (mrq->data->flags & MMC_DATA_WRITE)
347 cmdat |= CMDAT_WRITE;
349 if (mrq->data->flags & MMC_DATA_STREAM)
350 cmdat |= CMDAT_STREAM;
353 pxamci_start_cmd(host, mrq->cmd, cmdat);
356 static int pxamci_get_ro(struct mmc_host *mmc)
358 struct pxamci_host *host = mmc_priv(mmc);
360 if (host->pdata && host->pdata->get_ro)
361 return host->pdata->get_ro(mmc_dev(mmc));
362 /* Host doesn't support read only detection so assume writeable */
366 static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
368 struct pxamci_host *host = mmc_priv(mmc);
371 unsigned long rate = host->clkrate;
372 unsigned int clk = rate / ios->clock;
375 * clk might result in a lower divisor than we
376 * desire. check for that condition and adjust
379 if (rate / clk > ios->clock)
381 host->clkrt = fls(clk) - 1;
382 clk_enable(host->clk);
385 * we write clkrt on the next command
388 pxamci_stop_clock(host);
389 clk_disable(host->clk);
392 if (host->power_mode != ios->power_mode) {
393 host->power_mode = ios->power_mode;
395 if (host->pdata && host->pdata->setpower)
396 host->pdata->setpower(mmc_dev(mmc), ios->vdd);
398 if (ios->power_mode == MMC_POWER_ON)
399 host->cmdat |= CMDAT_INIT;
402 if (ios->bus_width == MMC_BUS_WIDTH_4)
403 host->cmdat |= CMDAT_SD_4DAT;
405 host->cmdat &= ~CMDAT_SD_4DAT;
407 pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
408 host->clkrt, host->cmdat);
411 static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
413 struct pxamci_host *pxa_host = mmc_priv(host);
416 pxamci_enable_irq(pxa_host, SDIO_INT);
418 pxamci_disable_irq(pxa_host, SDIO_INT);
421 static const struct mmc_host_ops pxamci_ops = {
422 .request = pxamci_request,
423 .get_ro = pxamci_get_ro,
424 .set_ios = pxamci_set_ios,
425 .enable_sdio_irq = pxamci_enable_sdio_irq,
428 static void pxamci_dma_irq(int dma, void *devid)
430 struct pxamci_host *host = devid;
431 int dcsr = DCSR(dma);
432 DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
434 if (dcsr & DCSR_ENDINTR) {
435 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
437 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
438 mmc_hostname(host->mmc), dma, dcsr);
439 host->data->error = -EIO;
440 pxamci_data_done(host, 0);
444 static irqreturn_t pxamci_detect_irq(int irq, void *devid)
446 struct pxamci_host *host = mmc_priv(devid);
448 mmc_detect_change(devid, host->pdata->detect_delay);
452 static int pxamci_probe(struct platform_device *pdev)
454 struct mmc_host *mmc;
455 struct pxamci_host *host = NULL;
459 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
460 irq = platform_get_irq(pdev, 0);
464 r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
468 mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
474 mmc->ops = &pxamci_ops;
477 * We can do SG-DMA, but we don't because we never know how much
478 * data we successfully wrote to the card.
480 mmc->max_phys_segs = NR_SG;
483 * Our hardware DMA can handle a maximum of one page per SG entry.
485 mmc->max_seg_size = PAGE_SIZE;
488 * Block length register is only 10 bits before PXA27x.
490 mmc->max_blk_size = (cpu_is_pxa21x() || cpu_is_pxa25x()) ? 1023 : 2048;
493 * Block count register is 16 bits.
495 mmc->max_blk_count = 65535;
497 host = mmc_priv(mmc);
500 host->pdata = pdev->dev.platform_data;
502 host->clk = clk_get(&pdev->dev, "MMCCLK");
503 if (IS_ERR(host->clk)) {
504 ret = PTR_ERR(host->clk);
509 host->clkrate = clk_get_rate(host->clk);
512 * Calculate minimum clock rate, rounding up.
514 mmc->f_min = (host->clkrate + 63) / 64;
515 mmc->f_max = host->clkrate;
517 mmc->ocr_avail = host->pdata ?
518 host->pdata->ocr_mask :
519 MMC_VDD_32_33|MMC_VDD_33_34;
522 if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) {
523 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
524 host->cmdat |= CMDAT_SDIO_INT_EN;
527 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
533 spin_lock_init(&host->lock);
536 host->imask = MMC_I_MASK_ALL;
538 host->base = ioremap(r->start, SZ_4K);
545 * Ensure that the host controller is shut down, and setup
548 pxamci_stop_clock(host);
549 writel(0, host->base + MMC_SPI);
550 writel(64, host->base + MMC_RESTO);
551 writel(host->imask, host->base + MMC_I_MASK);
553 host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
554 pxamci_dma_irq, host);
560 ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
564 platform_set_drvdata(pdev, mmc);
566 if (host->pdata && host->pdata->init)
567 host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
576 pxa_free_dma(host->dma);
580 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
590 static int pxamci_remove(struct platform_device *pdev)
592 struct mmc_host *mmc = platform_get_drvdata(pdev);
594 platform_set_drvdata(pdev, NULL);
597 struct pxamci_host *host = mmc_priv(mmc);
599 if (host->pdata && host->pdata->exit)
600 host->pdata->exit(&pdev->dev, mmc);
602 mmc_remove_host(mmc);
604 pxamci_stop_clock(host);
605 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
606 END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
607 host->base + MMC_I_MASK);
612 free_irq(host->irq, host);
613 pxa_free_dma(host->dma);
615 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
619 release_resource(host->res);
627 static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
629 struct mmc_host *mmc = platform_get_drvdata(dev);
633 ret = mmc_suspend_host(mmc, state);
638 static int pxamci_resume(struct platform_device *dev)
640 struct mmc_host *mmc = platform_get_drvdata(dev);
644 ret = mmc_resume_host(mmc);
649 #define pxamci_suspend NULL
650 #define pxamci_resume NULL
653 static struct platform_driver pxamci_driver = {
654 .probe = pxamci_probe,
655 .remove = pxamci_remove,
656 .suspend = pxamci_suspend,
657 .resume = pxamci_resume,
663 static int __init pxamci_init(void)
665 return platform_driver_register(&pxamci_driver);
668 static void __exit pxamci_exit(void)
670 platform_driver_unregister(&pxamci_driver);
673 module_init(pxamci_init);
674 module_exit(pxamci_exit);
676 MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
677 MODULE_LICENSE("GPL");