2 * linux/drivers/mmc/mmci.c - ARM PrimeCell MMCI PL180/1 driver
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, 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 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/ioport.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/highmem.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/protocol.h>
21 #include <linux/amba/bus.h>
22 #include <linux/clk.h>
24 #include <asm/cacheflush.h>
25 #include <asm/div64.h>
27 #include <asm/scatterlist.h>
28 #include <asm/sizes.h>
29 #include <asm/mach/mmc.h>
33 #define DRIVER_NAME "mmci-pl18x"
35 #define DBG(host,fmt,args...) \
36 pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
38 static unsigned int fmax = 515633;
41 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
43 writel(0, host->base + MMCICOMMAND);
49 mrq->data->bytes_xfered = host->data_xfered;
52 * Need to drop the host lock here; mmc_request_done may call
53 * back into the driver...
55 spin_unlock(&host->lock);
56 mmc_request_done(host->mmc, mrq);
57 spin_lock(&host->lock);
60 static void mmci_stop_data(struct mmci_host *host)
62 writel(0, host->base + MMCIDATACTRL);
63 writel(0, host->base + MMCIMASK1);
67 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
69 unsigned int datactrl, timeout, irqmask;
70 unsigned long long clks;
74 DBG(host, "blksz %04x blks %04x flags %08x\n",
75 data->blksz, data->blocks, data->flags);
78 host->size = data->blksz;
79 host->data_xfered = 0;
81 mmci_init_sg(host, data);
83 clks = (unsigned long long)data->timeout_ns * host->cclk;
84 do_div(clks, 1000000000UL);
86 timeout = data->timeout_clks + (unsigned int)clks;
89 writel(timeout, base + MMCIDATATIMER);
90 writel(host->size, base + MMCIDATALENGTH);
92 blksz_bits = ffs(data->blksz) - 1;
93 BUG_ON(1 << blksz_bits != data->blksz);
95 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
96 if (data->flags & MMC_DATA_READ) {
97 datactrl |= MCI_DPSM_DIRECTION;
98 irqmask = MCI_RXFIFOHALFFULLMASK;
101 * If we have less than a FIFOSIZE of bytes to transfer,
102 * trigger a PIO interrupt as soon as any data is available.
104 if (host->size < MCI_FIFOSIZE)
105 irqmask |= MCI_RXDATAAVLBLMASK;
108 * We don't actually need to include "FIFO empty" here
109 * since its implicit in "FIFO half empty".
111 irqmask = MCI_TXFIFOHALFEMPTYMASK;
114 writel(datactrl, base + MMCIDATACTRL);
115 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
116 writel(irqmask, base + MMCIMASK1);
120 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
122 void __iomem *base = host->base;
124 DBG(host, "op %02x arg %08x flags %08x\n",
125 cmd->opcode, cmd->arg, cmd->flags);
127 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
128 writel(0, base + MMCICOMMAND);
132 c |= cmd->opcode | MCI_CPSM_ENABLE;
133 if (cmd->flags & MMC_RSP_PRESENT) {
134 if (cmd->flags & MMC_RSP_136)
135 c |= MCI_CPSM_LONGRSP;
136 c |= MCI_CPSM_RESPONSE;
139 c |= MCI_CPSM_INTERRUPT;
143 writel(cmd->arg, base + MMCIARGUMENT);
144 writel(c, base + MMCICOMMAND);
148 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
151 if (status & MCI_DATABLOCKEND) {
152 host->data_xfered += data->blksz;
154 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
155 if (status & MCI_DATACRCFAIL)
156 data->error = MMC_ERR_BADCRC;
157 else if (status & MCI_DATATIMEOUT)
158 data->error = MMC_ERR_TIMEOUT;
159 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
160 data->error = MMC_ERR_FIFO;
161 status |= MCI_DATAEND;
164 * We hit an error condition. Ensure that any data
165 * partially written to a page is properly coherent.
167 if (host->sg_len && data->flags & MMC_DATA_READ)
168 flush_dcache_page(host->sg_ptr->page);
170 if (status & MCI_DATAEND) {
171 mmci_stop_data(host);
174 mmci_request_end(host, data->mrq);
176 mmci_start_command(host, data->stop, 0);
182 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
185 void __iomem *base = host->base;
189 cmd->resp[0] = readl(base + MMCIRESPONSE0);
190 cmd->resp[1] = readl(base + MMCIRESPONSE1);
191 cmd->resp[2] = readl(base + MMCIRESPONSE2);
192 cmd->resp[3] = readl(base + MMCIRESPONSE3);
194 if (status & MCI_CMDTIMEOUT) {
195 cmd->error = MMC_ERR_TIMEOUT;
196 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
197 cmd->error = MMC_ERR_BADCRC;
200 if (!cmd->data || cmd->error != MMC_ERR_NONE) {
201 mmci_request_end(host, cmd->mrq);
202 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
203 mmci_start_data(host, cmd->data);
207 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
209 void __iomem *base = host->base;
214 int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
222 readsl(base + MMCIFIFO, ptr, count >> 2);
230 status = readl(base + MMCISTATUS);
231 } while (status & MCI_RXDATAAVLBL);
236 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
238 void __iomem *base = host->base;
242 unsigned int count, maxcnt;
244 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
245 count = min(remain, maxcnt);
247 writesl(base + MMCIFIFO, ptr, count >> 2);
255 status = readl(base + MMCISTATUS);
256 } while (status & MCI_TXFIFOHALFEMPTY);
262 * PIO data transfer IRQ handler.
264 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
266 struct mmci_host *host = dev_id;
267 void __iomem *base = host->base;
270 status = readl(base + MMCISTATUS);
272 DBG(host, "irq1 %08x\n", status);
276 unsigned int remain, len;
280 * For write, we only need to test the half-empty flag
281 * here - if the FIFO is completely empty, then by
282 * definition it is more than half empty.
284 * For read, check for data available.
286 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
290 * Map the current scatter buffer.
292 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
293 remain = host->sg_ptr->length - host->sg_off;
296 if (status & MCI_RXACTIVE)
297 len = mmci_pio_read(host, buffer, remain);
298 if (status & MCI_TXACTIVE)
299 len = mmci_pio_write(host, buffer, remain, status);
304 mmci_kunmap_atomic(host, buffer, &flags);
314 * If we were reading, and we have completed this
315 * page, ensure that the data cache is coherent.
317 if (status & MCI_RXACTIVE)
318 flush_dcache_page(host->sg_ptr->page);
320 if (!mmci_next_sg(host))
323 status = readl(base + MMCISTATUS);
327 * If we're nearing the end of the read, switch to
328 * "any data available" mode.
330 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
331 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
334 * If we run out of data, disable the data IRQs; this
335 * prevents a race where the FIFO becomes empty before
336 * the chip itself has disabled the data path, and
337 * stops us racing with our data end IRQ.
339 if (host->size == 0) {
340 writel(0, base + MMCIMASK1);
341 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
348 * Handle completion of command and data transfers.
350 static irqreturn_t mmci_irq(int irq, void *dev_id)
352 struct mmci_host *host = dev_id;
356 spin_lock(&host->lock);
359 struct mmc_command *cmd;
360 struct mmc_data *data;
362 status = readl(host->base + MMCISTATUS);
363 status &= readl(host->base + MMCIMASK0);
364 writel(status, host->base + MMCICLEAR);
366 DBG(host, "irq0 %08x\n", status);
369 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
370 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
371 mmci_data_irq(host, data, status);
374 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
375 mmci_cmd_irq(host, cmd, status);
380 spin_unlock(&host->lock);
382 return IRQ_RETVAL(ret);
385 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
387 struct mmci_host *host = mmc_priv(mmc);
389 WARN_ON(host->mrq != NULL);
391 spin_lock_irq(&host->lock);
395 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
396 mmci_start_data(host, mrq->data);
398 mmci_start_command(host, mrq->cmd, 0);
400 spin_unlock_irq(&host->lock);
403 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
405 struct mmci_host *host = mmc_priv(mmc);
406 u32 clk = 0, pwr = 0;
409 if (ios->clock >= host->mclk) {
410 clk = MCI_CLK_BYPASS;
411 host->cclk = host->mclk;
413 clk = host->mclk / (2 * ios->clock) - 1;
416 host->cclk = host->mclk / (2 * (clk + 1));
418 clk |= MCI_CLK_ENABLE;
421 if (host->plat->translate_vdd)
422 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
424 switch (ios->power_mode) {
435 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
438 writel(clk, host->base + MMCICLOCK);
440 if (host->pwr != pwr) {
442 writel(pwr, host->base + MMCIPOWER);
446 static const struct mmc_host_ops mmci_ops = {
447 .request = mmci_request,
448 .set_ios = mmci_set_ios,
451 static void mmci_check_status(unsigned long data)
453 struct mmci_host *host = (struct mmci_host *)data;
456 status = host->plat->status(mmc_dev(host->mmc));
457 if (status ^ host->oldstat)
458 mmc_detect_change(host->mmc, 0);
460 host->oldstat = status;
461 mod_timer(&host->timer, jiffies + HZ);
464 static int mmci_probe(struct amba_device *dev, void *id)
466 struct mmc_platform_data *plat = dev->dev.platform_data;
467 struct mmci_host *host;
468 struct mmc_host *mmc;
471 /* must have platform data */
477 ret = amba_request_regions(dev, DRIVER_NAME);
481 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
487 host = mmc_priv(mmc);
488 host->clk = clk_get(&dev->dev, "MCLK");
489 if (IS_ERR(host->clk)) {
490 ret = PTR_ERR(host->clk);
495 ret = clk_enable(host->clk);
500 host->mclk = clk_get_rate(host->clk);
502 host->base = ioremap(dev->res.start, SZ_4K);
508 mmc->ops = &mmci_ops;
509 mmc->f_min = (host->mclk + 511) / 512;
510 mmc->f_max = min(host->mclk, fmax);
511 mmc->ocr_avail = plat->ocr_mask;
512 mmc->caps = MMC_CAP_MULTIWRITE;
517 mmc->max_hw_segs = 16;
518 mmc->max_phys_segs = NR_SG;
521 * Since we only have a 16-bit data length register, we must
522 * ensure that we don't exceed 2^16-1 bytes in a single request.
523 * Choose 64 (512-byte) sectors as the limit.
525 mmc->max_sectors = 64;
528 * Set the maximum segment size. Since we aren't doing DMA
529 * (yet) we are only limited by the data length register.
531 mmc->max_seg_size = mmc->max_sectors << 9;
533 spin_lock_init(&host->lock);
535 writel(0, host->base + MMCIMASK0);
536 writel(0, host->base + MMCIMASK1);
537 writel(0xfff, host->base + MMCICLEAR);
539 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
543 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
547 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
549 amba_set_drvdata(dev, mmc);
553 printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
554 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
555 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
557 init_timer(&host->timer);
558 host->timer.data = (unsigned long)host;
559 host->timer.function = mmci_check_status;
560 host->timer.expires = jiffies + HZ;
561 add_timer(&host->timer);
566 free_irq(dev->irq[0], host);
570 clk_disable(host->clk);
576 amba_release_regions(dev);
581 static int mmci_remove(struct amba_device *dev)
583 struct mmc_host *mmc = amba_get_drvdata(dev);
585 amba_set_drvdata(dev, NULL);
588 struct mmci_host *host = mmc_priv(mmc);
590 del_timer_sync(&host->timer);
592 mmc_remove_host(mmc);
594 writel(0, host->base + MMCIMASK0);
595 writel(0, host->base + MMCIMASK1);
597 writel(0, host->base + MMCICOMMAND);
598 writel(0, host->base + MMCIDATACTRL);
600 free_irq(dev->irq[0], host);
601 free_irq(dev->irq[1], host);
604 clk_disable(host->clk);
609 amba_release_regions(dev);
616 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
618 struct mmc_host *mmc = amba_get_drvdata(dev);
622 struct mmci_host *host = mmc_priv(mmc);
624 ret = mmc_suspend_host(mmc, state);
626 writel(0, host->base + MMCIMASK0);
632 static int mmci_resume(struct amba_device *dev)
634 struct mmc_host *mmc = amba_get_drvdata(dev);
638 struct mmci_host *host = mmc_priv(mmc);
640 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
642 ret = mmc_resume_host(mmc);
648 #define mmci_suspend NULL
649 #define mmci_resume NULL
652 static struct amba_id mmci_ids[] = {
664 static struct amba_driver mmci_driver = {
669 .remove = mmci_remove,
670 .suspend = mmci_suspend,
671 .resume = mmci_resume,
672 .id_table = mmci_ids,
675 static int __init mmci_init(void)
677 return amba_driver_register(&mmci_driver);
680 static void __exit mmci_exit(void)
682 amba_driver_unregister(&mmci_driver);
685 module_init(mmci_init);
686 module_exit(mmci_exit);
687 module_param(fmax, uint, 0444);
689 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
690 MODULE_LICENSE("GPL");