2 * linux/drivers/mmc/host/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/log2.h>
20 #include <linux/mmc/host.h>
21 #include <linux/amba/bus.h>
22 #include <linux/clk.h>
23 #include <linux/scatterlist.h>
24 #include <linux/gpio.h>
26 #include <asm/cacheflush.h>
27 #include <asm/div64.h>
29 #include <asm/sizes.h>
30 #include <asm/mach/mmc.h>
34 #define DRIVER_NAME "mmci-pl18x"
36 #define DBG(host,fmt,args...) \
37 pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
39 static unsigned int fmax = 515633;
42 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
44 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;
73 unsigned long long clks;
77 DBG(host, "blksz %04x blks %04x flags %08x\n",
78 data->blksz, data->blocks, data->flags);
81 host->size = data->blksz;
82 host->data_xfered = 0;
84 mmci_init_sg(host, data);
86 clks = (unsigned long long)data->timeout_ns * host->cclk;
87 do_div(clks, 1000000000UL);
89 timeout = data->timeout_clks + (unsigned int)clks;
92 writel(timeout, base + MMCIDATATIMER);
93 writel(host->size, base + MMCIDATALENGTH);
95 blksz_bits = ffs(data->blksz) - 1;
96 BUG_ON(1 << blksz_bits != data->blksz);
98 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
99 if (data->flags & MMC_DATA_READ) {
100 datactrl |= MCI_DPSM_DIRECTION;
101 irqmask = MCI_RXFIFOHALFFULLMASK;
104 * If we have less than a FIFOSIZE of bytes to transfer,
105 * trigger a PIO interrupt as soon as any data is available.
107 if (host->size < MCI_FIFOSIZE)
108 irqmask |= MCI_RXDATAAVLBLMASK;
111 * We don't actually need to include "FIFO empty" here
112 * since its implicit in "FIFO half empty".
114 irqmask = MCI_TXFIFOHALFEMPTYMASK;
117 writel(datactrl, base + MMCIDATACTRL);
118 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
119 writel(irqmask, base + MMCIMASK1);
123 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
125 void __iomem *base = host->base;
127 DBG(host, "op %02x arg %08x flags %08x\n",
128 cmd->opcode, cmd->arg, cmd->flags);
130 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
131 writel(0, base + MMCICOMMAND);
135 c |= cmd->opcode | MCI_CPSM_ENABLE;
136 if (cmd->flags & MMC_RSP_PRESENT) {
137 if (cmd->flags & MMC_RSP_136)
138 c |= MCI_CPSM_LONGRSP;
139 c |= MCI_CPSM_RESPONSE;
142 c |= MCI_CPSM_INTERRUPT;
146 writel(cmd->arg, base + MMCIARGUMENT);
147 writel(c, base + MMCICOMMAND);
151 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
154 if (status & MCI_DATABLOCKEND) {
155 host->data_xfered += data->blksz;
157 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
158 if (status & MCI_DATACRCFAIL)
159 data->error = -EILSEQ;
160 else if (status & MCI_DATATIMEOUT)
161 data->error = -ETIMEDOUT;
162 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
164 status |= MCI_DATAEND;
167 * We hit an error condition. Ensure that any data
168 * partially written to a page is properly coherent.
170 if (host->sg_len && data->flags & MMC_DATA_READ)
171 flush_dcache_page(sg_page(host->sg_ptr));
173 if (status & MCI_DATAEND) {
174 mmci_stop_data(host);
177 mmci_request_end(host, data->mrq);
179 mmci_start_command(host, data->stop, 0);
185 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
188 void __iomem *base = host->base;
192 cmd->resp[0] = readl(base + MMCIRESPONSE0);
193 cmd->resp[1] = readl(base + MMCIRESPONSE1);
194 cmd->resp[2] = readl(base + MMCIRESPONSE2);
195 cmd->resp[3] = readl(base + MMCIRESPONSE3);
197 if (status & MCI_CMDTIMEOUT) {
198 cmd->error = -ETIMEDOUT;
199 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
200 cmd->error = -EILSEQ;
203 if (!cmd->data || cmd->error) {
205 mmci_stop_data(host);
206 mmci_request_end(host, cmd->mrq);
207 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
208 mmci_start_data(host, cmd->data);
212 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
214 void __iomem *base = host->base;
217 int host_remain = host->size;
220 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
228 readsl(base + MMCIFIFO, ptr, count >> 2);
232 host_remain -= count;
237 status = readl(base + MMCISTATUS);
238 } while (status & MCI_RXDATAAVLBL);
243 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
245 void __iomem *base = host->base;
249 unsigned int count, maxcnt;
251 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
252 count = min(remain, maxcnt);
254 writesl(base + MMCIFIFO, ptr, count >> 2);
262 status = readl(base + MMCISTATUS);
263 } while (status & MCI_TXFIFOHALFEMPTY);
269 * PIO data transfer IRQ handler.
271 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
273 struct mmci_host *host = dev_id;
274 void __iomem *base = host->base;
277 status = readl(base + MMCISTATUS);
279 DBG(host, "irq1 %08x\n", status);
283 unsigned int remain, len;
287 * For write, we only need to test the half-empty flag
288 * here - if the FIFO is completely empty, then by
289 * definition it is more than half empty.
291 * For read, check for data available.
293 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
297 * Map the current scatter buffer.
299 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
300 remain = host->sg_ptr->length - host->sg_off;
303 if (status & MCI_RXACTIVE)
304 len = mmci_pio_read(host, buffer, remain);
305 if (status & MCI_TXACTIVE)
306 len = mmci_pio_write(host, buffer, remain, status);
311 mmci_kunmap_atomic(host, buffer, &flags);
321 * If we were reading, and we have completed this
322 * page, ensure that the data cache is coherent.
324 if (status & MCI_RXACTIVE)
325 flush_dcache_page(sg_page(host->sg_ptr));
327 if (!mmci_next_sg(host))
330 status = readl(base + MMCISTATUS);
334 * If we're nearing the end of the read, switch to
335 * "any data available" mode.
337 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
338 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
341 * If we run out of data, disable the data IRQs; this
342 * prevents a race where the FIFO becomes empty before
343 * the chip itself has disabled the data path, and
344 * stops us racing with our data end IRQ.
346 if (host->size == 0) {
347 writel(0, base + MMCIMASK1);
348 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
355 * Handle completion of command and data transfers.
357 static irqreturn_t mmci_irq(int irq, void *dev_id)
359 struct mmci_host *host = dev_id;
363 spin_lock(&host->lock);
366 struct mmc_command *cmd;
367 struct mmc_data *data;
369 status = readl(host->base + MMCISTATUS);
370 status &= readl(host->base + MMCIMASK0);
371 writel(status, host->base + MMCICLEAR);
373 DBG(host, "irq0 %08x\n", status);
376 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
377 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
378 mmci_data_irq(host, data, status);
381 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
382 mmci_cmd_irq(host, cmd, status);
387 spin_unlock(&host->lock);
389 return IRQ_RETVAL(ret);
392 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
394 struct mmci_host *host = mmc_priv(mmc);
397 WARN_ON(host->mrq != NULL);
399 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
400 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
401 mmc_hostname(mmc), mrq->data->blksz);
402 mrq->cmd->error = -EINVAL;
403 mmc_request_done(mmc, mrq);
407 spin_lock_irqsave(&host->lock, flags);
411 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
412 mmci_start_data(host, mrq->data);
414 mmci_start_command(host, mrq->cmd, 0);
416 spin_unlock_irqrestore(&host->lock, flags);
419 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
421 struct mmci_host *host = mmc_priv(mmc);
422 u32 clk = 0, pwr = 0;
425 if (ios->clock >= host->mclk) {
426 clk = MCI_CLK_BYPASS;
427 host->cclk = host->mclk;
429 clk = host->mclk / (2 * ios->clock) - 1;
432 host->cclk = host->mclk / (2 * (clk + 1));
434 if (host->hw_designer == AMBA_VENDOR_ST)
435 clk |= MCI_FCEN; /* Bug fix in ST IP block */
436 clk |= MCI_CLK_ENABLE;
439 if (host->plat->translate_vdd)
440 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
442 switch (ios->power_mode) {
446 /* The ST version does not have this, fall through to POWER_ON */
447 if (host->hw_designer != AMBA_VENDOR_ST) {
456 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
457 if (host->hw_designer != AMBA_VENDOR_ST)
461 * The ST Micro variant use the ROD bit for something
462 * else and only has OD (Open Drain).
468 writel(clk, host->base + MMCICLOCK);
470 if (host->pwr != pwr) {
472 writel(pwr, host->base + MMCIPOWER);
476 static int mmci_get_ro(struct mmc_host *mmc)
478 struct mmci_host *host = mmc_priv(mmc);
480 if (host->gpio_wp == -ENOSYS)
483 return gpio_get_value(host->gpio_wp);
486 static int mmci_get_cd(struct mmc_host *mmc)
488 struct mmci_host *host = mmc_priv(mmc);
491 if (host->gpio_cd == -ENOSYS)
492 status = host->plat->status(mmc_dev(host->mmc));
494 status = gpio_get_value(host->gpio_cd);
499 static const struct mmc_host_ops mmci_ops = {
500 .request = mmci_request,
501 .set_ios = mmci_set_ios,
502 .get_ro = mmci_get_ro,
503 .get_cd = mmci_get_cd,
506 static void mmci_check_status(unsigned long data)
508 struct mmci_host *host = (struct mmci_host *)data;
509 unsigned int status = mmci_get_cd(host->mmc);
511 if (status ^ host->oldstat)
512 mmc_detect_change(host->mmc, 0);
514 host->oldstat = status;
515 mod_timer(&host->timer, jiffies + HZ);
518 static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
520 struct mmc_platform_data *plat = dev->dev.platform_data;
521 struct mmci_host *host;
522 struct mmc_host *mmc;
525 /* must have platform data */
531 ret = amba_request_regions(dev, DRIVER_NAME);
535 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
541 host = mmc_priv(mmc);
544 host->gpio_wp = -ENOSYS;
545 host->gpio_cd = -ENOSYS;
547 host->hw_designer = amba_manf(dev);
548 host->hw_revision = amba_rev(dev);
549 DBG(host, "designer ID = 0x%02x\n", host->hw_designer);
550 DBG(host, "revision = 0x%01x\n", host->hw_revision);
552 host->clk = clk_get(&dev->dev, NULL);
553 if (IS_ERR(host->clk)) {
554 ret = PTR_ERR(host->clk);
559 ret = clk_enable(host->clk);
564 host->mclk = clk_get_rate(host->clk);
566 * According to the spec, mclk is max 100 MHz,
567 * so we try to adjust the clock down to this,
570 if (host->mclk > 100000000) {
571 ret = clk_set_rate(host->clk, 100000000);
574 host->mclk = clk_get_rate(host->clk);
575 DBG(host, "eventual mclk rate: %u Hz\n", host->mclk);
577 host->base = ioremap(dev->res.start, resource_size(&dev->res));
583 mmc->ops = &mmci_ops;
584 mmc->f_min = (host->mclk + 511) / 512;
585 mmc->f_max = min(host->mclk, fmax);
586 mmc->ocr_avail = plat->ocr_mask;
591 mmc->max_hw_segs = 16;
592 mmc->max_phys_segs = NR_SG;
595 * Since we only have a 16-bit data length register, we must
596 * ensure that we don't exceed 2^16-1 bytes in a single request.
598 mmc->max_req_size = 65535;
601 * Set the maximum segment size. Since we aren't doing DMA
602 * (yet) we are only limited by the data length register.
604 mmc->max_seg_size = mmc->max_req_size;
607 * Block size can be up to 2048 bytes, but must be a power of two.
609 mmc->max_blk_size = 2048;
612 * No limit on the number of blocks transferred.
614 mmc->max_blk_count = mmc->max_req_size;
616 spin_lock_init(&host->lock);
618 writel(0, host->base + MMCIMASK0);
619 writel(0, host->base + MMCIMASK1);
620 writel(0xfff, host->base + MMCICLEAR);
622 #ifdef CONFIG_GPIOLIB
623 if (gpio_is_valid(plat->gpio_cd)) {
624 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
626 ret = gpio_direction_input(plat->gpio_cd);
628 host->gpio_cd = plat->gpio_cd;
629 else if (ret != -ENOSYS)
632 if (gpio_is_valid(plat->gpio_wp)) {
633 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
635 ret = gpio_direction_input(plat->gpio_wp);
637 host->gpio_wp = plat->gpio_wp;
638 else if (ret != -ENOSYS)
643 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
647 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
651 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
653 amba_set_drvdata(dev, mmc);
654 host->oldstat = mmci_get_cd(host->mmc);
658 printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
659 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
660 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
662 init_timer(&host->timer);
663 host->timer.data = (unsigned long)host;
664 host->timer.function = mmci_check_status;
665 host->timer.expires = jiffies + HZ;
666 add_timer(&host->timer);
671 free_irq(dev->irq[0], host);
673 if (host->gpio_wp != -ENOSYS)
674 gpio_free(host->gpio_wp);
676 if (host->gpio_cd != -ENOSYS)
677 gpio_free(host->gpio_cd);
681 clk_disable(host->clk);
687 amba_release_regions(dev);
692 static int __devexit mmci_remove(struct amba_device *dev)
694 struct mmc_host *mmc = amba_get_drvdata(dev);
696 amba_set_drvdata(dev, NULL);
699 struct mmci_host *host = mmc_priv(mmc);
701 del_timer_sync(&host->timer);
703 mmc_remove_host(mmc);
705 writel(0, host->base + MMCIMASK0);
706 writel(0, host->base + MMCIMASK1);
708 writel(0, host->base + MMCICOMMAND);
709 writel(0, host->base + MMCIDATACTRL);
711 free_irq(dev->irq[0], host);
712 free_irq(dev->irq[1], host);
714 if (host->gpio_wp != -ENOSYS)
715 gpio_free(host->gpio_wp);
716 if (host->gpio_cd != -ENOSYS)
717 gpio_free(host->gpio_cd);
720 clk_disable(host->clk);
725 amba_release_regions(dev);
732 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
734 struct mmc_host *mmc = amba_get_drvdata(dev);
738 struct mmci_host *host = mmc_priv(mmc);
740 ret = mmc_suspend_host(mmc, state);
742 writel(0, host->base + MMCIMASK0);
748 static int mmci_resume(struct amba_device *dev)
750 struct mmc_host *mmc = amba_get_drvdata(dev);
754 struct mmci_host *host = mmc_priv(mmc);
756 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
758 ret = mmc_resume_host(mmc);
764 #define mmci_suspend NULL
765 #define mmci_resume NULL
768 static struct amba_id mmci_ids[] = {
777 /* ST Micro variants */
789 static struct amba_driver mmci_driver = {
794 .remove = __devexit_p(mmci_remove),
795 .suspend = mmci_suspend,
796 .resume = mmci_resume,
797 .id_table = mmci_ids,
800 static int __init mmci_init(void)
802 return amba_driver_register(&mmci_driver);
805 static void __exit mmci_exit(void)
807 amba_driver_unregister(&mmci_driver);
810 module_init(mmci_init);
811 module_exit(mmci_exit);
812 module_param(fmax, uint, 0444);
814 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
815 MODULE_LICENSE("GPL");