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/config.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/highmem.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/protocol.h>
25 #include <asm/scatterlist.h>
26 #include <asm/hardware/amba.h>
27 #include <asm/hardware/clock.h>
28 #include <asm/mach/mmc.h>
32 #define DRIVER_NAME "mmci-pl18x"
34 #ifdef CONFIG_MMC_DEBUG
35 #define DBG(host,fmt,args...) \
36 pr_debug("%s: %s: " fmt, host->mmc->host_name, __func__ , args)
38 #define DBG(host,fmt,args...) do { } while (0)
41 static unsigned int fmax = 515633;
44 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
46 writel(0, host->base + MMCICOMMAND);
52 mrq->data->bytes_xfered = host->data_xfered;
55 * Need to drop the host lock here; mmc_request_done may call
56 * back into the driver...
58 spin_unlock(&host->lock);
59 mmc_request_done(host->mmc, mrq);
60 spin_lock(&host->lock);
63 static void mmci_stop_data(struct mmci_host *host)
65 writel(0, host->base + MMCIDATACTRL);
66 writel(0, host->base + MMCIMASK1);
70 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
72 unsigned int datactrl, timeout, irqmask;
75 DBG(host, "blksz %04x blks %04x flags %08x\n",
76 1 << data->blksz_bits, data->blocks, data->flags);
79 host->size = data->blocks << data->blksz_bits;
80 host->data_xfered = 0;
82 mmci_init_sg(host, data);
84 timeout = data->timeout_clks +
85 ((unsigned long long)data->timeout_ns * host->cclk) /
89 writel(timeout, base + MMCIDATATIMER);
90 writel(host->size, base + MMCIDATALENGTH);
92 datactrl = MCI_DPSM_ENABLE | data->blksz_bits << 4;
93 if (data->flags & MMC_DATA_READ) {
94 datactrl |= MCI_DPSM_DIRECTION;
95 irqmask = MCI_RXFIFOHALFFULLMASK;
98 * We don't actually need to include "FIFO empty" here
99 * since its implicit in "FIFO half empty".
101 irqmask = MCI_TXFIFOHALFEMPTYMASK;
104 writel(datactrl, base + MMCIDATACTRL);
105 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
106 writel(irqmask, base + MMCIMASK1);
110 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
112 void __iomem *base = host->base;
114 DBG(host, "op %02x arg %08x flags %08x\n",
115 cmd->opcode, cmd->arg, cmd->flags);
117 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
118 writel(0, base + MMCICOMMAND);
122 c |= cmd->opcode | MCI_CPSM_ENABLE;
123 switch (cmd->flags & MMC_RSP_MASK) {
128 c |= MCI_CPSM_LONGRSP;
130 c |= MCI_CPSM_RESPONSE;
134 c |= MCI_CPSM_INTERRUPT;
138 writel(cmd->arg, base + MMCIARGUMENT);
139 writel(c, base + MMCICOMMAND);
143 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
146 if (status & MCI_DATABLOCKEND) {
147 host->data_xfered += 1 << data->blksz_bits;
149 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
150 if (status & MCI_DATACRCFAIL)
151 data->error = MMC_ERR_BADCRC;
152 else if (status & MCI_DATATIMEOUT)
153 data->error = MMC_ERR_TIMEOUT;
154 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
155 data->error = MMC_ERR_FIFO;
156 status |= MCI_DATAEND;
158 if (status & MCI_DATAEND) {
159 mmci_stop_data(host);
162 mmci_request_end(host, data->mrq);
164 mmci_start_command(host, data->stop, 0);
170 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
173 void __iomem *base = host->base;
177 cmd->resp[0] = readl(base + MMCIRESPONSE0);
178 cmd->resp[1] = readl(base + MMCIRESPONSE1);
179 cmd->resp[2] = readl(base + MMCIRESPONSE2);
180 cmd->resp[3] = readl(base + MMCIRESPONSE3);
182 if (status & MCI_CMDTIMEOUT) {
183 cmd->error = MMC_ERR_TIMEOUT;
184 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
185 cmd->error = MMC_ERR_BADCRC;
188 if (!cmd->data || cmd->error != MMC_ERR_NONE) {
189 mmci_request_end(host, cmd->mrq);
190 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
191 mmci_start_data(host, cmd->data);
195 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
197 void __iomem *base = host->base;
202 int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
210 readsl(base + MMCIFIFO, ptr, count >> 2);
218 status = readl(base + MMCISTATUS);
219 } while (status & MCI_RXDATAAVLBL);
224 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
226 void __iomem *base = host->base;
230 unsigned int count, maxcnt;
232 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
233 count = min(remain, maxcnt);
235 writesl(base + MMCIFIFO, ptr, count >> 2);
243 status = readl(base + MMCISTATUS);
244 } while (status & MCI_TXFIFOHALFEMPTY);
250 * PIO data transfer IRQ handler.
252 static irqreturn_t mmci_pio_irq(int irq, void *dev_id, struct pt_regs *regs)
254 struct mmci_host *host = dev_id;
255 void __iomem *base = host->base;
258 status = readl(base + MMCISTATUS);
260 DBG(host, "irq1 %08x\n", status);
264 unsigned int remain, len;
268 * For write, we only need to test the half-empty flag
269 * here - if the FIFO is completely empty, then by
270 * definition it is more than half empty.
272 * For read, check for data available.
274 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
278 * Map the current scatter buffer.
280 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
281 remain = host->sg_ptr->length - host->sg_off;
284 if (status & MCI_RXACTIVE)
285 len = mmci_pio_read(host, buffer, remain);
286 if (status & MCI_TXACTIVE)
287 len = mmci_pio_write(host, buffer, remain, status);
292 mmci_kunmap_atomic(host, &flags);
301 if (!mmci_next_sg(host))
304 status = readl(base + MMCISTATUS);
308 * If we're nearing the end of the read, switch to
309 * "any data available" mode.
311 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
312 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
315 * If we run out of data, disable the data IRQs; this
316 * prevents a race where the FIFO becomes empty before
317 * the chip itself has disabled the data path, and
318 * stops us racing with our data end IRQ.
320 if (host->size == 0) {
321 writel(0, base + MMCIMASK1);
322 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
329 * Handle completion of command and data transfers.
331 static irqreturn_t mmci_irq(int irq, void *dev_id, struct pt_regs *regs)
333 struct mmci_host *host = dev_id;
337 spin_lock(&host->lock);
340 struct mmc_command *cmd;
341 struct mmc_data *data;
343 status = readl(host->base + MMCISTATUS);
344 status &= readl(host->base + MMCIMASK0);
345 writel(status, host->base + MMCICLEAR);
347 DBG(host, "irq0 %08x\n", status);
350 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
351 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
352 mmci_data_irq(host, data, status);
355 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
356 mmci_cmd_irq(host, cmd, status);
361 spin_unlock(&host->lock);
363 return IRQ_RETVAL(ret);
366 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
368 struct mmci_host *host = mmc_priv(mmc);
370 WARN_ON(host->mrq != NULL);
372 spin_lock_irq(&host->lock);
376 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
377 mmci_start_data(host, mrq->data);
379 mmci_start_command(host, mrq->cmd, 0);
381 spin_unlock_irq(&host->lock);
384 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
386 struct mmci_host *host = mmc_priv(mmc);
387 u32 clk = 0, pwr = 0;
389 DBG(host, "clock %uHz busmode %u powermode %u Vdd %u\n",
390 ios->clock, ios->bus_mode, ios->power_mode, ios->vdd);
393 if (ios->clock >= host->mclk) {
394 clk = MCI_CLK_BYPASS;
395 host->cclk = host->mclk;
397 clk = host->mclk / (2 * ios->clock) - 1;
400 host->cclk = host->mclk / (2 * (clk + 1));
402 clk |= MCI_CLK_ENABLE;
405 if (host->plat->translate_vdd)
406 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
408 switch (ios->power_mode) {
419 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
422 writel(clk, host->base + MMCICLOCK);
424 if (host->pwr != pwr) {
426 writel(pwr, host->base + MMCIPOWER);
430 static struct mmc_host_ops mmci_ops = {
431 .request = mmci_request,
432 .set_ios = mmci_set_ios,
435 static void mmci_check_status(unsigned long data)
437 struct mmci_host *host = (struct mmci_host *)data;
440 status = host->plat->status(mmc_dev(host->mmc));
441 if (status ^ host->oldstat)
442 mmc_detect_change(host->mmc);
444 host->oldstat = status;
445 mod_timer(&host->timer, jiffies + HZ);
448 static int mmci_probe(struct amba_device *dev, void *id)
450 struct mmc_platform_data *plat = dev->dev.platform_data;
451 struct mmci_host *host;
452 struct mmc_host *mmc;
455 /* must have platform data */
461 ret = amba_request_regions(dev, DRIVER_NAME);
465 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
471 host = mmc_priv(mmc);
472 host->clk = clk_get(&dev->dev, "MCLK");
473 if (IS_ERR(host->clk)) {
474 ret = PTR_ERR(host->clk);
479 ret = clk_use(host->clk);
483 ret = clk_enable(host->clk);
488 host->mclk = clk_get_rate(host->clk);
490 host->base = ioremap(dev->res.start, SZ_4K);
496 mmc->ops = &mmci_ops;
497 mmc->f_min = (host->mclk + 511) / 512;
498 mmc->f_max = min(host->mclk, fmax);
499 mmc->ocr_avail = plat->ocr_mask;
504 mmc->max_hw_segs = 16;
505 mmc->max_phys_segs = NR_SG;
508 * Since we only have a 16-bit data length register, we must
509 * ensure that we don't exceed 2^16-1 bytes in a single request.
510 * Choose 64 (512-byte) sectors as the limit.
512 mmc->max_sectors = 64;
515 * Set the maximum segment size. Since we aren't doing DMA
516 * (yet) we are only limited by the data length register.
518 mmc->max_seg_size = mmc->max_sectors << 9;
520 spin_lock_init(&host->lock);
522 writel(0, host->base + MMCIMASK0);
523 writel(0, host->base + MMCIMASK1);
524 writel(0xfff, host->base + MMCICLEAR);
526 ret = request_irq(dev->irq[0], mmci_irq, SA_SHIRQ, DRIVER_NAME " (cmd)", host);
530 ret = request_irq(dev->irq[1], mmci_pio_irq, SA_SHIRQ, DRIVER_NAME " (pio)", host);
534 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
536 amba_set_drvdata(dev, mmc);
540 printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%08lx irq %d,%d\n",
541 mmc->host_name, amba_rev(dev), amba_config(dev),
542 dev->res.start, dev->irq[0], dev->irq[1]);
544 init_timer(&host->timer);
545 host->timer.data = (unsigned long)host;
546 host->timer.function = mmci_check_status;
547 host->timer.expires = jiffies + HZ;
548 add_timer(&host->timer);
553 free_irq(dev->irq[0], host);
557 clk_disable(host->clk);
559 clk_unuse(host->clk);
565 amba_release_regions(dev);
570 static int mmci_remove(struct amba_device *dev)
572 struct mmc_host *mmc = amba_get_drvdata(dev);
574 amba_set_drvdata(dev, NULL);
577 struct mmci_host *host = mmc_priv(mmc);
579 del_timer_sync(&host->timer);
581 mmc_remove_host(mmc);
583 writel(0, host->base + MMCIMASK0);
584 writel(0, host->base + MMCIMASK1);
586 writel(0, host->base + MMCICOMMAND);
587 writel(0, host->base + MMCIDATACTRL);
589 free_irq(dev->irq[0], host);
590 free_irq(dev->irq[1], host);
593 clk_disable(host->clk);
594 clk_unuse(host->clk);
599 amba_release_regions(dev);
606 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
608 struct mmc_host *mmc = amba_get_drvdata(dev);
612 struct mmci_host *host = mmc_priv(mmc);
614 ret = mmc_suspend_host(mmc, state);
616 writel(0, host->base + MMCIMASK0);
622 static int mmci_resume(struct amba_device *dev)
624 struct mmc_host *mmc = amba_get_drvdata(dev);
628 struct mmci_host *host = mmc_priv(mmc);
630 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
632 ret = mmc_resume_host(mmc);
638 #define mmci_suspend NULL
639 #define mmci_resume NULL
642 static struct amba_id mmci_ids[] = {
654 static struct amba_driver mmci_driver = {
659 .remove = mmci_remove,
660 .suspend = mmci_suspend,
661 .resume = mmci_resume,
662 .id_table = mmci_ids,
665 static int __init mmci_init(void)
667 return amba_driver_register(&mmci_driver);
670 static void __exit mmci_exit(void)
672 amba_driver_unregister(&mmci_driver);
675 module_init(mmci_init);
676 module_exit(mmci_exit);
677 module_param(fmax, uint, 0444);
679 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
680 MODULE_LICENSE("GPL");