MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / mmc / mmci.c
blob828503c4ee62b509763a0dd0f722de70bfbad727
1 /*
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.
9 */
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>
26 #include <asm/io.h>
27 #include <asm/scatterlist.h>
28 #include <asm/sizes.h>
29 #include <asm/mach/mmc.h>
31 #include "mmci.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;
40 static void
41 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
43 writel(0, host->base + MMCICOMMAND);
45 host->mrq = NULL;
46 host->cmd = NULL;
48 if (mrq->data)
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);
64 host->data = NULL;
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;
71 void __iomem *base;
72 int blksz_bits;
74 DBG(host, "blksz %04x blks %04x flags %08x\n",
75 data->blksz, data->blocks, data->flags);
77 host->data = data;
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;
88 base = host->base;
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;
106 } else {
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);
119 static void
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);
129 udelay(1);
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;
138 if (/*interrupt*/0)
139 c |= MCI_CPSM_INTERRUPT;
141 host->cmd = cmd;
143 writel(cmd->arg, base + MMCIARGUMENT);
144 writel(c, base + MMCICOMMAND);
147 static void
148 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
149 unsigned int status)
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);
173 if (!data->stop) {
174 mmci_request_end(host, data->mrq);
175 } else {
176 mmci_start_command(host, data->stop, 0);
181 static void
182 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
183 unsigned int status)
185 void __iomem *base = host->base;
187 host->cmd = NULL;
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;
210 char *ptr = buffer;
211 u32 status;
213 do {
214 int count = host->size - (readl(base + MMCIFIFOCNT) << 2);
216 if (count > remain)
217 count = remain;
219 if (count <= 0)
220 break;
222 readsl(base + MMCIFIFO, ptr, count >> 2);
224 ptr += count;
225 remain -= count;
227 if (remain == 0)
228 break;
230 status = readl(base + MMCISTATUS);
231 } while (status & MCI_RXDATAAVLBL);
233 return ptr - buffer;
236 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
238 void __iomem *base = host->base;
239 char *ptr = buffer;
241 do {
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);
249 ptr += count;
250 remain -= count;
252 if (remain == 0)
253 break;
255 status = readl(base + MMCISTATUS);
256 } while (status & MCI_TXFIFOHALFEMPTY);
258 return ptr - buffer;
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;
268 u32 status;
270 status = readl(base + MMCISTATUS);
272 DBG(host, "irq1 %08x\n", status);
274 do {
275 unsigned long flags;
276 unsigned int remain, len;
277 char *buffer;
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)))
287 break;
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;
295 len = 0;
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);
302 * Unmap the buffer.
304 mmci_kunmap_atomic(host, buffer, &flags);
306 host->sg_off += len;
307 host->size -= len;
308 remain -= len;
310 if (remain)
311 break;
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))
321 break;
323 status = readl(base + MMCISTATUS);
324 } while (1);
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);
344 return IRQ_HANDLED;
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;
353 u32 status;
354 int ret = 0;
356 spin_lock(&host->lock);
358 do {
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);
368 data = host->data;
369 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
370 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
371 mmci_data_irq(host, data, status);
373 cmd = host->cmd;
374 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
375 mmci_cmd_irq(host, cmd, status);
377 ret = 1;
378 } while (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);
393 host->mrq = mrq;
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;
408 if (ios->clock) {
409 if (ios->clock >= host->mclk) {
410 clk = MCI_CLK_BYPASS;
411 host->cclk = host->mclk;
412 } else {
413 clk = host->mclk / (2 * ios->clock) - 1;
414 if (clk > 256)
415 clk = 255;
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) {
425 case MMC_POWER_OFF:
426 break;
427 case MMC_POWER_UP:
428 pwr |= MCI_PWR_UP;
429 break;
430 case MMC_POWER_ON:
431 pwr |= MCI_PWR_ON;
432 break;
435 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
436 pwr |= MCI_ROD;
438 writel(clk, host->base + MMCICLOCK);
440 if (host->pwr != pwr) {
441 host->pwr = pwr;
442 writel(pwr, host->base + MMCIPOWER);
446 static 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;
454 unsigned int status;
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;
469 int ret;
471 /* must have platform data */
472 if (!plat) {
473 ret = -EINVAL;
474 goto out;
477 ret = amba_request_regions(dev, DRIVER_NAME);
478 if (ret)
479 goto out;
481 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
482 if (!mmc) {
483 ret = -ENOMEM;
484 goto rel_regions;
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);
491 host->clk = NULL;
492 goto host_free;
495 ret = clk_enable(host->clk);
496 if (ret)
497 goto clk_free;
499 host->plat = plat;
500 host->mclk = clk_get_rate(host->clk);
501 host->mmc = mmc;
502 host->base = ioremap(dev->res.start, SZ_4K);
503 if (!host->base) {
504 ret = -ENOMEM;
505 goto clk_disable;
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;
515 * We can do SGIO
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);
540 if (ret)
541 goto unmap;
543 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
544 if (ret)
545 goto irq0_free;
547 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
549 amba_set_drvdata(dev, mmc);
551 mmc_add_host(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);
563 return 0;
565 irq0_free:
566 free_irq(dev->irq[0], host);
567 unmap:
568 iounmap(host->base);
569 clk_disable:
570 clk_disable(host->clk);
571 clk_free:
572 clk_put(host->clk);
573 host_free:
574 mmc_free_host(mmc);
575 rel_regions:
576 amba_release_regions(dev);
577 out:
578 return ret;
581 static int mmci_remove(struct amba_device *dev)
583 struct mmc_host *mmc = amba_get_drvdata(dev);
585 amba_set_drvdata(dev, NULL);
587 if (mmc) {
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);
603 iounmap(host->base);
604 clk_disable(host->clk);
605 clk_put(host->clk);
607 mmc_free_host(mmc);
609 amba_release_regions(dev);
612 return 0;
615 #ifdef CONFIG_PM
616 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
618 struct mmc_host *mmc = amba_get_drvdata(dev);
619 int ret = 0;
621 if (mmc) {
622 struct mmci_host *host = mmc_priv(mmc);
624 ret = mmc_suspend_host(mmc, state);
625 if (ret == 0)
626 writel(0, host->base + MMCIMASK0);
629 return ret;
632 static int mmci_resume(struct amba_device *dev)
634 struct mmc_host *mmc = amba_get_drvdata(dev);
635 int ret = 0;
637 if (mmc) {
638 struct mmci_host *host = mmc_priv(mmc);
640 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
642 ret = mmc_resume_host(mmc);
645 return ret;
647 #else
648 #define mmci_suspend NULL
649 #define mmci_resume NULL
650 #endif
652 static struct amba_id mmci_ids[] = {
654 .id = 0x00041180,
655 .mask = 0x000fffff,
658 .id = 0x00041181,
659 .mask = 0x000fffff,
661 { 0, 0 },
664 static struct amba_driver mmci_driver = {
665 .drv = {
666 .name = DRIVER_NAME,
668 .probe = mmci_probe,
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");