2 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
5 * Copyright (C) 2010 ST-Ericsson AB.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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/log2.h>
21 #include <linux/mmc/host.h>
22 #include <linux/amba/bus.h>
23 #include <linux/clk.h>
24 #include <linux/scatterlist.h>
25 #if defined (CONFIG_ARCH_LPC32XX)
26 #include <linux/dma-mapping.h>
28 #include <linux/gpio.h>
29 #include <linux/amba/mmci.h>
30 #include <linux/regulator/consumer.h>
32 #include <asm/cacheflush.h>
33 #include <asm/div64.h>
35 #include <asm/sizes.h>
37 #if defined (CONFIG_ARCH_LPC32XX)
38 #include <mach/clkdev.h>
39 #include <mach/dmac.h>
40 #include <mach/sdcard.h>
42 #include <mach/hardware.h>
47 #define DRIVER_NAME "mmci-pl18x"
49 #if defined (CONFIG_ARCH_LPC32XX)
50 static unsigned int fmax = 26000000; /* 26MHz bit rate max */
52 #define DMA_BUFF_SIZE SZ_64K
53 static void *dma_p_base, *dma_v_base;
54 static struct dma_config dmacfgrx, dmacfgtx;
55 static int lastch = DMA_CH_SDCARD_TX;
57 #define MCI_WIDEBUS (1 << 11)
59 #define MCI_IRQENABLE \
60 (MCI_CMDCRCFAILMASK|MCI_DATACRCFAILMASK|MCI_CMDTIMEOUTMASK| \
61 MCI_DATATIMEOUTMASK|MCI_TXUNDERRUNMASK|MCI_RXOVERRUNMASK| \
62 MCI_CMDRESPENDMASK|MCI_CMDSENTMASK)
65 static unsigned int fmax = 515633;
68 #if defined (CONFIG_ARCH_LPC32XX)
69 static int mmc_dma_setup(void)
71 /* Setup TX DMA channel */
72 dmacfgtx.ch = DMA_CH_SDCARD_TX;
73 dmacfgtx.tc_inten = 0;
74 dmacfgtx.err_inten = 0;
75 dmacfgtx.src_size = 4;
77 dmacfgtx.src_ahb1 = 0;
78 dmacfgtx.src_bsize = DMAC_CHAN_SRC_BURST_8;
79 dmacfgtx.src_prph = DMAC_SRC_PERIP(DMA_PERID_SDCARD);
80 dmacfgtx.dst_size = 4;
82 dmacfgtx.dst_ahb1 = 0;
83 dmacfgtx.dst_bsize = DMAC_CHAN_DEST_BURST_8;
84 dmacfgtx.dst_prph = DMAC_DEST_PERIP(DMA_PERID_SDCARD);
85 dmacfgtx.flowctrl = DMAC_CHAN_FLOW_P_M2P;
86 if (lpc32xx_dma_ch_get(&dmacfgtx, "dma_sd_tx", NULL, NULL) < 0)
88 printk(KERN_ERR "Error setting up SD card TX DMA channel\n");
92 /* Setup RX DMA channel */
93 dmacfgrx.ch = DMA_CH_SDCARD_RX;
94 dmacfgrx.tc_inten = 0;
95 dmacfgrx.err_inten = 0;
96 dmacfgrx.src_size = 4;
98 dmacfgrx.src_ahb1 = 0;
99 dmacfgrx.src_bsize = DMAC_CHAN_SRC_BURST_8;
100 dmacfgrx.src_prph = DMAC_SRC_PERIP(DMA_PERID_SDCARD);
101 dmacfgrx.dst_size = 4;
102 dmacfgrx.dst_inc = 1;
103 dmacfgrx.dst_ahb1 = 0;
104 dmacfgrx.dst_bsize = DMAC_CHAN_DEST_BURST_8;
105 dmacfgrx.dst_prph = DMAC_DEST_PERIP(DMA_PERID_SDCARD);
106 dmacfgrx.flowctrl = DMAC_CHAN_FLOW_P_P2M;
107 if (lpc32xx_dma_ch_get(&dmacfgrx, "dma_sd_rx", NULL, NULL) < 0)
109 printk(KERN_ERR "Error setting up SD card RX DMA channel\n");
116 static void mmc_dma_start(int ch)
118 if (ch == DMA_CH_SDCARD_TX)
120 lpc32xx_dma_start_pflow_xfer(DMA_CH_SDCARD_TX,
121 dma_p_base, (void *) SD_FIFO(LPC32XX_SD_BASE), 1);
125 lpc32xx_dma_start_pflow_xfer(DMA_CH_SDCARD_RX,
126 (void *) SD_FIFO(LPC32XX_SD_BASE), dma_p_base, 1);
130 static void mmc_dma_stop(int ch)
132 lpc32xx_dma_ch_disable(ch);
135 static void mmc_tx_dma_copy(struct mmci_host *host)
137 char *src_buffer, *dst_buffer;
140 dst_buffer = (char *) dma_v_base;
144 * Map the current scatter buffer, copy data, and unmap
146 src_buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
147 memcpy(dst_buffer, src_buffer, host->sg_ptr->length);
148 dst_buffer += host->sg_ptr->length;
149 mmci_kunmap_atomic(host, src_buffer, &flags);
151 if (!mmci_next_sg(host))
156 static void mmc_dma_rx_copy(struct mmci_host *host)
158 char *src_buffer, *dst_buffer;
161 src_buffer = (char *) dma_v_base;
165 * Map the current scatter buffer, copy data, and unmap
167 dst_buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
168 memcpy(dst_buffer, src_buffer, host->sg_ptr->length);
169 src_buffer += host->sg_ptr->length;
170 mmci_kunmap_atomic(host, dst_buffer, &flags);
172 flush_dcache_page(sg_page(host->sg_ptr));
174 if (!mmci_next_sg(host))
181 * This must be called with host->lock held
183 static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
188 if (desired >= host->mclk) {
189 clk = MCI_CLK_BYPASS;
190 host->cclk = host->mclk;
192 clk = host->mclk / (2 * desired) - 1;
195 host->cclk = host->mclk / (2 * (clk + 1));
197 if (host->hw_designer == AMBA_VENDOR_ST)
198 clk |= MCI_FCEN; /* Bug fix in ST IP block */
199 clk |= MCI_CLK_ENABLE;
200 /* This hasn't proven to be worthwhile */
201 /* clk |= MCI_CLK_PWRSAVE; */
204 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
207 writel(clk, host->base + MMCICLOCK);
211 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
213 writel(0, host->base + MMCICOMMAND);
221 mrq->data->bytes_xfered = host->data_xfered;
224 * Need to drop the host lock here; mmc_request_done may call
225 * back into the driver...
227 spin_unlock(&host->lock);
228 mmc_request_done(host->mmc, mrq);
229 spin_lock(&host->lock);
232 static void mmci_stop_data(struct mmci_host *host)
234 writel(0, host->base + MMCIDATACTRL);
235 writel(0, host->base + MMCIMASK1);
237 #if defined(CONFIG_ARCH_LPC32XX)
238 mmc_dma_stop(lastch);
242 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
244 unsigned int datactrl, timeout, irqmask = 0;
245 unsigned long long clks;
249 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
250 data->blksz, data->blocks, data->flags);
253 host->size = data->blksz;
254 host->data_xfered = 0;
256 mmci_init_sg(host, data);
258 clks = (unsigned long long)data->timeout_ns * host->cclk;
259 do_div(clks, 1000000000UL);
261 timeout = data->timeout_clks + (unsigned int)clks;
264 writel(timeout, base + MMCIDATATIMER);
265 #if defined (CONFIG_ARCH_LPC32XX)
266 writel((host->size * data->blocks), base + MMCIDATALENGTH);
268 blksz_bits = ffs(data->blksz) - 1;
269 BUG_ON(1 << blksz_bits != data->blksz);
271 datactrl = MCI_DPSM_ENABLE | MCI_DPSM_DMAENABLE | blksz_bits << 4;
272 if (data->flags & MMC_DATA_READ) {
273 datactrl |= MCI_DPSM_DIRECTION;
274 lastch = DMA_CH_SDCARD_RX;
276 /* Copy data buffer to DMA buffer and start transfer */
277 lastch = DMA_CH_SDCARD_TX;
278 mmc_tx_dma_copy(host);
280 mmc_dma_start(lastch);
282 writel(datactrl, base + MMCIDATACTRL);
283 datactrl = readl(base + MMCIMASK0) & ~MCI_DATABLOCKENDMASK;
284 writel(datactrl | MCI_DATAENDMASK, base + MMCIMASK0);
287 writel(host->size, base + MMCIDATALENGTH);
289 blksz_bits = ffs(data->blksz) - 1;
290 BUG_ON(1 << blksz_bits != data->blksz);
292 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
293 if (data->flags & MMC_DATA_READ) {
294 datactrl |= MCI_DPSM_DIRECTION;
295 irqmask = MCI_RXFIFOHALFFULLMASK;
298 * If we have less than a FIFOSIZE of bytes to transfer,
299 * trigger a PIO interrupt as soon as any data is available.
301 if (host->size < MCI_FIFOSIZE)
302 irqmask |= MCI_RXDATAAVLBLMASK;
305 * We don't actually need to include "FIFO empty" here
306 * since its implicit in "FIFO half empty".
308 irqmask = MCI_TXFIFOHALFEMPTYMASK;
311 writel(datactrl, base + MMCIDATACTRL);
312 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
314 writel(irqmask, base + MMCIMASK1);
318 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
320 void __iomem *base = host->base;
322 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
323 cmd->opcode, cmd->arg, cmd->flags);
325 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
326 writel(0, base + MMCICOMMAND);
330 c |= cmd->opcode | MCI_CPSM_ENABLE;
331 if (cmd->flags & MMC_RSP_PRESENT) {
332 if (cmd->flags & MMC_RSP_136)
333 c |= MCI_CPSM_LONGRSP;
334 c |= MCI_CPSM_RESPONSE;
337 c |= MCI_CPSM_INTERRUPT;
341 writel(cmd->arg, base + MMCIARGUMENT);
342 writel(c, base + MMCICOMMAND);
346 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
349 #if defined (CONFIG_ARCH_LPC32XX)
350 if (status & MCI_DATAEND) {
351 host->data_xfered += data->blksz * data->blocks;
354 if (status & MCI_DATABLOCKEND) {
355 host->data_xfered += data->blksz;
357 #ifdef CONFIG_ARCH_U300
359 * On the U300 some signal or other is
360 * badly routed so that a data write does
361 * not properly terminate with a MCI_DATAEND
362 * status flag. This quirk will make writes
365 if (data->flags & MMC_DATA_WRITE)
366 status |= MCI_DATAEND;
369 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
370 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
371 if (status & MCI_DATACRCFAIL)
372 data->error = -EILSEQ;
373 else if (status & MCI_DATATIMEOUT)
374 data->error = -ETIMEDOUT;
375 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
377 status |= MCI_DATAEND;
379 #if !defined (CONFIG_ARCH_LPC32XX)
381 * We hit an error condition. Ensure that any data
382 * partially written to a page is properly coherent.
384 if (host->sg_len && data->flags & MMC_DATA_READ)
385 flush_dcache_page(sg_page(host->sg_ptr));
388 if (status & MCI_DATAEND) {
389 mmci_stop_data(host);
390 #if defined (CONFIG_ARCH_LPC32XX)
391 /* Copy DMA buffer to MMC buffer */
392 if (lastch == DMA_CH_SDCARD_RX)
394 /* Copy DMA buffer to data buffer */
395 mmc_dma_rx_copy(host);
399 mmci_request_end(host, data->mrq);
401 mmci_start_command(host, data->stop, 0);
407 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
410 void __iomem *base = host->base;
414 cmd->resp[0] = readl(base + MMCIRESPONSE0);
415 cmd->resp[1] = readl(base + MMCIRESPONSE1);
416 cmd->resp[2] = readl(base + MMCIRESPONSE2);
417 cmd->resp[3] = readl(base + MMCIRESPONSE3);
419 if (status & MCI_CMDTIMEOUT) {
420 cmd->error = -ETIMEDOUT;
421 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
422 cmd->error = -EILSEQ;
425 if (!cmd->data || cmd->error) {
427 mmci_stop_data(host);
428 mmci_request_end(host, cmd->mrq);
429 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
430 mmci_start_data(host, cmd->data);
434 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
436 void __iomem *base = host->base;
439 int host_remain = host->size;
442 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
450 readsl(base + MMCIFIFO, ptr, count >> 2);
454 host_remain -= count;
459 status = readl(base + MMCISTATUS);
460 } while (status & MCI_RXDATAAVLBL);
465 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
467 void __iomem *base = host->base;
471 unsigned int count, maxcnt;
473 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
474 count = min(remain, maxcnt);
476 writesl(base + MMCIFIFO, ptr, count >> 2);
484 status = readl(base + MMCISTATUS);
485 } while (status & MCI_TXFIFOHALFEMPTY);
491 * PIO data transfer IRQ handler.
493 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
495 struct mmci_host *host = dev_id;
496 void __iomem *base = host->base;
499 status = readl(base + MMCISTATUS);
501 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
505 unsigned int remain, len;
509 * For write, we only need to test the half-empty flag
510 * here - if the FIFO is completely empty, then by
511 * definition it is more than half empty.
513 * For read, check for data available.
515 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
519 * Map the current scatter buffer.
521 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
522 remain = host->sg_ptr->length - host->sg_off;
525 if (status & MCI_RXACTIVE)
526 len = mmci_pio_read(host, buffer, remain);
527 if (status & MCI_TXACTIVE)
528 len = mmci_pio_write(host, buffer, remain, status);
533 mmci_kunmap_atomic(host, buffer, &flags);
543 * If we were reading, and we have completed this
544 * page, ensure that the data cache is coherent.
546 if (status & MCI_RXACTIVE)
547 flush_dcache_page(sg_page(host->sg_ptr));
549 if (!mmci_next_sg(host))
552 status = readl(base + MMCISTATUS);
556 * If we're nearing the end of the read, switch to
557 * "any data available" mode.
559 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
560 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
563 * If we run out of data, disable the data IRQs; this
564 * prevents a race where the FIFO becomes empty before
565 * the chip itself has disabled the data path, and
566 * stops us racing with our data end IRQ.
568 if (host->size == 0) {
569 writel(0, base + MMCIMASK1);
570 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
577 * Handle completion of command and data transfers.
579 static irqreturn_t mmci_irq(int irq, void *dev_id)
581 struct mmci_host *host = dev_id;
585 spin_lock(&host->lock);
588 struct mmc_command *cmd;
589 struct mmc_data *data;
591 status = readl(host->base + MMCISTATUS);
592 status &= readl(host->base + MMCIMASK0);
593 #if defined (CONFIG_ARCH_LPC32XX)
594 writel((status | MCI_DATABLOCKEND), host->base + MMCICLEAR);
596 writel(status, host->base + MMCICLEAR);
599 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
602 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
603 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
604 mmci_data_irq(host, data, status);
607 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
608 mmci_cmd_irq(host, cmd, status);
613 spin_unlock(&host->lock);
615 return IRQ_RETVAL(ret);
618 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
620 struct mmci_host *host = mmc_priv(mmc);
623 WARN_ON(host->mrq != NULL);
625 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
626 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
628 mrq->cmd->error = -EINVAL;
629 mmc_request_done(mmc, mrq);
633 spin_lock_irqsave(&host->lock, flags);
637 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
638 mmci_start_data(host, mrq->data);
640 mmci_start_command(host, mrq->cmd, 0);
642 spin_unlock_irqrestore(&host->lock, flags);
645 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
647 struct mmci_host *host = mmc_priv(mmc);
651 switch (ios->power_mode) {
654 regulator_is_enabled(host->vcc))
655 regulator_disable(host->vcc);
658 #ifdef CONFIG_REGULATOR
660 /* This implicitly enables the regulator */
661 mmc_regulator_set_ocr(host->vcc, ios->vdd);
664 * The translate_vdd function is not used if you have
665 * an external regulator, or your design is really weird.
666 * Using it would mean sending in power control BOTH using
667 * a regulator AND the 4 MMCIPWR bits. If we don't have
668 * a regulator, we might have some other platform specific
669 * power control behind this translate function.
671 if (!host->vcc && host->plat->translate_vdd)
672 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
673 /* The ST version does not have this, fall through to POWER_ON */
674 if (host->hw_designer != AMBA_VENDOR_ST) {
683 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
684 if (host->hw_designer != AMBA_VENDOR_ST)
688 * The ST Micro variant use the ROD bit for something
689 * else and only has OD (Open Drain).
695 spin_lock_irqsave(&host->lock, flags);
697 mmci_set_clkreg(host, ios->clock);
699 if (host->pwr != pwr) {
701 writel(pwr, host->base + MMCIPOWER);
704 spin_unlock_irqrestore(&host->lock, flags);
707 static int mmci_get_ro(struct mmc_host *mmc)
709 struct mmci_host *host = mmc_priv(mmc);
711 if (host->gpio_wp == -ENOSYS)
714 return gpio_get_value(host->gpio_wp);
717 static int mmci_get_cd(struct mmc_host *mmc)
719 struct mmci_host *host = mmc_priv(mmc);
722 if (host->gpio_cd == -ENOSYS)
723 status = host->plat->status(mmc_dev(host->mmc));
725 status = gpio_get_value(host->gpio_cd);
730 static const struct mmc_host_ops mmci_ops = {
731 .request = mmci_request,
732 .set_ios = mmci_set_ios,
733 .get_ro = mmci_get_ro,
734 .get_cd = mmci_get_cd,
737 static void mmci_check_status(unsigned long data)
739 struct mmci_host *host = (struct mmci_host *)data;
740 unsigned int status = mmci_get_cd(host->mmc);
742 if (status ^ host->oldstat)
743 mmc_detect_change(host->mmc, 0);
745 host->oldstat = status;
746 mod_timer(&host->timer, jiffies + HZ);
749 static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
751 struct mmci_platform_data *plat = dev->dev.platform_data;
752 struct mmci_host *host;
753 struct mmc_host *mmc;
755 #if defined (CONFIG_ARCH_LPC32XX)
756 dma_addr_t dma_handle;
759 /* must have platform data */
765 #if defined (CONFIG_ARCH_LPC32XX)
766 /* Allocate a chunk of memory for the DMA buffers */
767 dma_v_base = dma_alloc_coherent(&dev->dev, DMA_BUFF_SIZE,
768 &dma_handle, GFP_KERNEL);
769 if (dma_v_base == NULL)
771 printk("%s: error getting DMA region.\n", DRIVER_NAME);
775 dma_p_base = (void *) dma_handle;
776 printk(KERN_INFO "%s: DMA buffer(%x bytes), P:0x%08x, V:0x%08x\n",
777 DRIVER_NAME, DMA_BUFF_SIZE, (u32) dma_p_base, (u32) dma_v_base);
780 ret = amba_request_regions(dev, DRIVER_NAME);
782 #if defined (CONFIG_ARCH_LPC32XX)
788 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
794 host = mmc_priv(mmc);
797 host->gpio_wp = -ENOSYS;
798 host->gpio_cd = -ENOSYS;
800 host->hw_designer = amba_manf(dev);
801 host->hw_revision = amba_rev(dev);
802 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
803 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
805 host->clk = clk_get(&dev->dev, NULL);
806 if (IS_ERR(host->clk)) {
807 ret = PTR_ERR(host->clk);
812 ret = clk_enable(host->clk);
817 host->mclk = clk_get_rate(host->clk);
819 * According to the spec, mclk is max 100 MHz,
820 * so we try to adjust the clock down to this,
823 if (host->mclk > 100000000) {
824 ret = clk_set_rate(host->clk, 100000000);
827 host->mclk = clk_get_rate(host->clk);
828 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
831 host->base = ioremap(dev->res.start, resource_size(&dev->res));
837 mmc->ops = &mmci_ops;
838 mmc->f_min = (host->mclk + 511) / 512;
839 mmc->f_max = min(host->mclk, fmax);
840 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
842 #ifdef CONFIG_REGULATOR
843 /* If we're using the regulator framework, try to fetch a regulator */
844 host->vcc = regulator_get(&dev->dev, "vmmc");
845 if (IS_ERR(host->vcc))
848 int mask = mmc_regulator_get_ocrmask(host->vcc);
851 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
854 host->mmc->ocr_avail = (u32) mask;
857 "Provided ocr_mask/setpower will not be used "
858 "(using regulator instead)\n");
862 /* Fall back to platform data if no regulator is found */
863 if (host->vcc == NULL)
864 mmc->ocr_avail = plat->ocr_mask;
865 mmc->caps = plat->capabilities;
870 mmc->max_hw_segs = 16;
871 mmc->max_phys_segs = NR_SG;
874 * Since we only have a 16-bit data length register, we must
875 * ensure that we don't exceed 2^16-1 bytes in a single request.
877 mmc->max_req_size = 65535;
880 * Set the maximum segment size. Since we aren't doing DMA
881 * (yet) we are only limited by the data length register.
883 mmc->max_seg_size = mmc->max_req_size;
886 * Block size can be up to 2048 bytes, but must be a power of two.
888 mmc->max_blk_size = 2048;
891 * No limit on the number of blocks transferred.
893 mmc->max_blk_count = mmc->max_req_size;
895 #if defined (CONFIG_ARCH_LPC32XX)
897 * Setup DMA for the interface
902 spin_lock_init(&host->lock);
904 writel(0, host->base + MMCIMASK0);
905 writel(0, host->base + MMCIMASK1);
906 writel(0xfff, host->base + MMCICLEAR);
908 if (gpio_is_valid(plat->gpio_cd)) {
909 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
911 ret = gpio_direction_input(plat->gpio_cd);
913 host->gpio_cd = plat->gpio_cd;
914 else if (ret != -ENOSYS)
917 if (gpio_is_valid(plat->gpio_wp)) {
918 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
920 ret = gpio_direction_input(plat->gpio_wp);
922 host->gpio_wp = plat->gpio_wp;
923 else if (ret != -ENOSYS)
927 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
931 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
934 #if defined (CONFIG_ARCH_LPC32XX)
935 writel(MCI_IRQENABLE|MCI_DATAENDMASK, host->base + MMCIMASK0);
937 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
939 amba_set_drvdata(dev, mmc);
940 host->oldstat = mmci_get_cd(host->mmc);
944 dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
945 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
946 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
948 init_timer(&host->timer);
949 host->timer.data = (unsigned long)host;
950 host->timer.function = mmci_check_status;
951 host->timer.expires = jiffies + HZ;
952 add_timer(&host->timer);
957 free_irq(dev->irq[0], host);
959 if (host->gpio_wp != -ENOSYS)
960 gpio_free(host->gpio_wp);
962 if (host->gpio_cd != -ENOSYS)
963 gpio_free(host->gpio_cd);
967 clk_disable(host->clk);
973 amba_release_regions(dev);
974 #if defined (CONFIG_ARCH_LPC32XX)
976 dma_free_coherent(&dev->dev, DMA_BUFF_SIZE,
977 dma_v_base, (dma_addr_t) dma_p_base);
983 static int __devexit mmci_remove(struct amba_device *dev)
985 struct mmc_host *mmc = amba_get_drvdata(dev);
987 amba_set_drvdata(dev, NULL);
990 struct mmci_host *host = mmc_priv(mmc);
992 del_timer_sync(&host->timer);
994 mmc_remove_host(mmc);
996 writel(0, host->base + MMCIMASK0);
997 writel(0, host->base + MMCIMASK1);
999 writel(0, host->base + MMCICOMMAND);
1000 writel(0, host->base + MMCIDATACTRL);
1002 free_irq(dev->irq[0], host);
1003 free_irq(dev->irq[1], host);
1005 if (host->gpio_wp != -ENOSYS)
1006 gpio_free(host->gpio_wp);
1007 if (host->gpio_cd != -ENOSYS)
1008 gpio_free(host->gpio_cd);
1010 iounmap(host->base);
1011 clk_disable(host->clk);
1014 if (regulator_is_enabled(host->vcc))
1015 regulator_disable(host->vcc);
1016 regulator_put(host->vcc);
1020 amba_release_regions(dev);
1021 #if defined (CONFIG_ARCH_LPC32XX)
1022 dma_free_coherent(&dev->dev, DMA_BUFF_SIZE,
1023 dma_v_base, (dma_addr_t) dma_p_base);
1031 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
1033 struct mmc_host *mmc = amba_get_drvdata(dev);
1037 struct mmci_host *host = mmc_priv(mmc);
1039 ret = mmc_suspend_host(mmc, state);
1041 writel(0, host->base + MMCIMASK0);
1047 static int mmci_resume(struct amba_device *dev)
1049 struct mmc_host *mmc = amba_get_drvdata(dev);
1053 struct mmci_host *host = mmc_priv(mmc);
1055 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1057 ret = mmc_resume_host(mmc);
1063 #define mmci_suspend NULL
1064 #define mmci_resume NULL
1067 static struct amba_id mmci_ids[] = {
1076 /* ST Micro variants */
1088 static struct amba_driver mmci_driver = {
1090 .name = DRIVER_NAME,
1092 .probe = mmci_probe,
1093 .remove = __devexit_p(mmci_remove),
1094 .suspend = mmci_suspend,
1095 .resume = mmci_resume,
1096 .id_table = mmci_ids,
1099 static int __init mmci_init(void)
1101 return amba_driver_register(&mmci_driver);
1104 static void __exit mmci_exit(void)
1106 amba_driver_unregister(&mmci_driver);
1109 module_init(mmci_init);
1110 module_exit(mmci_exit);
1111 module_param(fmax, uint, 0444);
1113 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1114 MODULE_LICENSE("GPL");