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.
6 * Copyright (C) 2010 NXP Semiconductors (LPC32xx DMA modifications)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/highmem.h>
21 #include <linux/log2.h>
22 #include <linux/mmc/host.h>
23 #include <linux/amba/bus.h>
24 #include <linux/clk.h>
25 #include <linux/scatterlist.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/gpio.h>
28 #include <linux/amba/mmci.h>
29 #include <linux/regulator/consumer.h>
31 #include <asm/cacheflush.h>
32 #include <asm/div64.h>
34 #include <asm/sizes.h>
36 #include <mach/clkdev.h>
37 #include <mach/dmac.h>
38 #include <mach/sdcard.h>
40 #include <mach/hardware.h>
44 #define DRIVER_NAME "mmci-pl18x"
46 static unsigned int fmax = 26000000; /* 26MHz bit rate max */
48 #define DMA_BUFF_SIZE SZ_64K
50 struct LPC32XX_SDDRV_DATA {
51 struct dma_config dmacfgtx;
52 struct dma_config dmacfgrx;
55 dma_addr_t dma_handle_tx;
59 static struct LPC32XX_SDDRV_DATA lpc32xx_drvdat;
61 #define MCI_WIDEBUS (1 << 11)
63 #define MCI_IRQENABLE \
64 (MCI_CMDCRCFAILMASK|MCI_DATACRCFAILMASK|MCI_CMDTIMEOUTMASK| \
65 MCI_DATATIMEOUTMASK|MCI_TXUNDERRUNMASK|MCI_RXOVERRUNMASK| \
66 MCI_CMDRESPENDMASK|MCI_CMDSENTMASK)
68 static int mmc_dma_setup(void)
74 * There is a quirk with the LPC32XX and SD burst DMA. DMA sg
75 * transfers where DMA is the flow controller will not transfer
76 * the last few bytes to or from the SD card controller and
77 * memory. For RX, the last few bytes in the SD transfer can be
78 * forced out with a software DMA burst request. For TX, this
79 * can't be done, so TX sg support cannot be supported. For TX,
80 * a temporary bouncing buffer is used if more than 1 sg segment
81 * is passed in the data request. The bouncing buffer will get a
82 * contiguous copy of the TX data and it will be used instead.
85 /* Allocate a chunk of memory for the DMA TX buffers */
86 lpc32xx_drvdat.dma_v_base = dma_alloc_coherent(lpc32xx_drvdat.dev,
87 DMA_BUFF_SIZE, &lpc32xx_drvdat.dma_handle_tx, GFP_KERNEL);
88 if (lpc32xx_drvdat.dma_v_base == NULL) {
89 dev_err(lpc32xx_drvdat.dev, "error getting DMA region\n");
93 dev_info(lpc32xx_drvdat.dev, "DMA buffer: phy:%p, virt:%p\n",
94 (void *) lpc32xx_drvdat.dma_handle_tx, lpc32xx_drvdat.dma_v_base);
96 /* Setup TX DMA channel */
97 lpc32xx_drvdat.dmacfgtx.ch = DMA_CH_SDCARD_TX;
98 lpc32xx_drvdat.dmacfgtx.tc_inten = 0;
99 lpc32xx_drvdat.dmacfgtx.err_inten = 0;
100 lpc32xx_drvdat.dmacfgtx.src_size = 4;
101 lpc32xx_drvdat.dmacfgtx.src_inc = 1;
102 lpc32xx_drvdat.dmacfgtx.src_ahb1 = 0;
103 lpc32xx_drvdat.dmacfgtx.src_bsize = DMAC_CHAN_SRC_BURST_8;
104 lpc32xx_drvdat.dmacfgtx.src_prph = DMAC_SRC_PERIP(DMA_PERID_SDCARD);
105 lpc32xx_drvdat.dmacfgtx.dst_size = 4;
106 lpc32xx_drvdat.dmacfgtx.dst_inc = 0;
107 lpc32xx_drvdat.dmacfgtx.dst_ahb1 = 0;
108 lpc32xx_drvdat.dmacfgtx.dst_bsize = DMAC_CHAN_DEST_BURST_8;
109 lpc32xx_drvdat.dmacfgtx.dst_prph = DMAC_DEST_PERIP(DMA_PERID_SDCARD);
110 lpc32xx_drvdat.dmacfgtx.flowctrl = DMAC_CHAN_FLOW_P_M2P;
111 if (lpc32xx_dma_ch_get(&lpc32xx_drvdat.dmacfgtx, "dma_sd_tx", NULL, NULL) < 0)
113 dev_err(lpc32xx_drvdat.dev, "Error setting up SD card TX DMA channel\n");
118 /* Allocate a linked list for DMA support */
119 llptrtx = lpc32xx_dma_alloc_llist(lpc32xx_drvdat.dmacfgtx.ch, NR_SG * 2);
121 dev_err(lpc32xx_drvdat.dev, "Error allocating list buffer (MMC TX)\n");
126 /* Setup RX DMA channel */
127 lpc32xx_drvdat.dmacfgrx.ch = DMA_CH_SDCARD_RX;
128 lpc32xx_drvdat.dmacfgrx.tc_inten = 0;
129 lpc32xx_drvdat.dmacfgrx.err_inten = 0;
130 lpc32xx_drvdat.dmacfgrx.src_size = 4;
131 lpc32xx_drvdat.dmacfgrx.src_inc = 0;
132 lpc32xx_drvdat.dmacfgrx.src_ahb1 = 0;
133 lpc32xx_drvdat.dmacfgrx.src_bsize = DMAC_CHAN_SRC_BURST_8;
134 lpc32xx_drvdat.dmacfgrx.src_prph = DMAC_SRC_PERIP(DMA_PERID_SDCARD);
135 lpc32xx_drvdat.dmacfgrx.dst_size = 4;
136 lpc32xx_drvdat.dmacfgrx.dst_inc = 1;
137 lpc32xx_drvdat.dmacfgrx.dst_ahb1 = 0;
138 lpc32xx_drvdat.dmacfgrx.dst_bsize = DMAC_CHAN_DEST_BURST_8;
139 lpc32xx_drvdat.dmacfgrx.dst_prph = DMAC_DEST_PERIP(DMA_PERID_SDCARD);
140 lpc32xx_drvdat.dmacfgrx.flowctrl = DMAC_CHAN_FLOW_D_P2M;
141 if (lpc32xx_dma_ch_get(&lpc32xx_drvdat.dmacfgrx, "dma_sd_rx", NULL, NULL) < 0)
143 dev_err(lpc32xx_drvdat.dev, "Error setting up SD card RX DMA channel\n");
148 /* Allocate a linked list for DMA support */
149 llptrrx = lpc32xx_dma_alloc_llist(lpc32xx_drvdat.dmacfgrx.ch, NR_SG * 2);
151 dev_err(lpc32xx_drvdat.dev, "Error allocating list buffer (MMC RX)\n");
159 lpc32xx_dma_ch_put(lpc32xx_drvdat.dmacfgrx.ch);
160 lpc32xx_drvdat.dmacfgrx.ch = -1;
162 lpc32xx_dma_dealloc_llist(lpc32xx_drvdat.dmacfgtx.ch);
164 lpc32xx_dma_ch_put(lpc32xx_drvdat.dmacfgtx.ch);
165 lpc32xx_drvdat.dmacfgtx.ch = -1;
167 dma_free_coherent(lpc32xx_drvdat.dev, DMA_BUFF_SIZE,
168 lpc32xx_drvdat.dma_v_base, lpc32xx_drvdat.dma_handle_tx);
173 static void mmc_dma_dealloc(void)
175 lpc32xx_dma_dealloc_llist(lpc32xx_drvdat.dmacfgrx.ch);
176 lpc32xx_dma_ch_put(lpc32xx_drvdat.dmacfgrx.ch);
177 lpc32xx_drvdat.dmacfgrx.ch = -1;
178 lpc32xx_dma_dealloc_llist(lpc32xx_drvdat.dmacfgtx.ch);
179 lpc32xx_dma_ch_put(lpc32xx_drvdat.dmacfgtx.ch);
180 lpc32xx_drvdat.dmacfgtx.ch = -1;
181 dma_free_coherent(lpc32xx_drvdat.dev, DMA_BUFF_SIZE,
182 lpc32xx_drvdat.dma_v_base, lpc32xx_drvdat.dma_handle_tx);
185 /* Supports scatter/gather */
186 static void mmc_dma_rx_start(struct mmci_host *host)
190 struct scatterlist *sg;
191 struct mmc_request *mrq = host->mrq;
192 struct mmc_data *reqdata = mrq->data;
194 u32 dmalen, dmaxferlen;
197 len = reqdata->sg_len;
199 dma_len = dma_map_sg(mmc_dev(host->mmc), reqdata->sg, reqdata->sg_len, DMA_FROM_DEVICE);
204 for (i = 0; i < len; i++) {
205 dmalen = (u32) sg_dma_len(&sg[i]);
206 dmaaddr = (void *) sg_dma_address(&sg[i]);
208 /* Build a list with a max size if 15872 bytes per seg */
211 if (dmaxferlen > 15872)
214 lpc32xx_dma_queue_llist_entry(lpc32xx_drvdat.lastch,
215 (void *) SD_FIFO(LPC32XX_SD_BASE),
216 dmaaddr, dmaxferlen);
218 dmaaddr += dmaxferlen;
219 dmalen -= dmaxferlen;
223 //printk("DMARX %d\n", len);
227 /* May need to reorganize buffer for scatter/gather */
228 static void mmc_dma_tx_start(struct mmci_host *host)
232 struct scatterlist *sg;
233 struct mmc_request *mrq = host->mrq;
234 struct mmc_data *reqdata = mrq->data;
236 char *src_buffer, *dst_buffer;
240 len = reqdata->sg_len;
242 /* Only 1 segment? */
244 dma_len = dma_map_sg(mmc_dev(host->mmc), reqdata->sg,
245 reqdata->sg_len, DMA_TO_DEVICE);
249 dmaaddr = (void *) sg_dma_address(&sg[0]);
250 lpc32xx_drvdat.mapped = 1;
253 /* Move data to contiguous buffer first, then transfer it */
254 dst_buffer = (char *) lpc32xx_drvdat.dma_v_base;
258 * Map the current scatter buffer, copy data, and unmap
260 src_buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
261 memcpy(dst_buffer, src_buffer, host->sg_ptr->length);
262 dst_buffer += host->sg_ptr->length;
263 mmci_kunmap_atomic(host, src_buffer, &flags);
265 if (!mmci_next_sg(host))
269 lpc32xx_drvdat.mapped = 0;
270 dmaaddr = (void *) lpc32xx_drvdat.dma_handle_tx;
273 lpc32xx_dma_start_pflow_xfer(DMA_CH_SDCARD_TX, dmaaddr,
274 (void *) SD_FIFO(LPC32XX_SD_BASE), 1);
278 * This must be called with host->lock held
280 static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
285 if (desired >= host->mclk) {
286 clk = MCI_CLK_BYPASS;
287 host->cclk = host->mclk;
289 clk = host->mclk / (2 * desired) - 1;
292 host->cclk = host->mclk / (2 * (clk + 1));
294 if (host->hw_designer == AMBA_VENDOR_ST)
295 clk |= MCI_FCEN; /* Bug fix in ST IP block */
296 clk |= MCI_CLK_ENABLE;
297 /* This hasn't proven to be worthwhile */
298 clk |= MCI_CLK_PWRSAVE;
301 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
304 writel(clk, host->base + MMCICLOCK);
308 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
310 writel(0, host->base + MMCICOMMAND);
318 mrq->data->bytes_xfered = host->data_xfered;
321 * Need to drop the host lock here; mmc_request_done may call
322 * back into the driver...
324 spin_unlock(&host->lock);
325 mmc_request_done(host->mmc, mrq);
326 spin_lock(&host->lock);
329 static void mmci_stop_data(struct mmci_host *host)
331 writel(0, host->base + MMCIDATACTRL);
332 writel(0, host->base + MMCIMASK1);
336 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
338 unsigned int datactrl, timeout, irqmask = 0;
339 unsigned long long clks;
343 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
344 data->blksz, data->blocks, data->flags);
347 host->size = data->blksz;
348 host->data_xfered = 0;
350 mmci_init_sg(host, data);
352 clks = (unsigned long long)data->timeout_ns * host->cclk;
353 do_div(clks, 1000000000UL);
355 timeout = data->timeout_clks + (unsigned int)clks;
358 writel(timeout, base + MMCIDATATIMER);
359 writel((host->size * data->blocks), base + MMCIDATALENGTH);
360 blksz_bits = ffs(data->blksz) - 1;
361 BUG_ON(1 << blksz_bits != data->blksz);
363 datactrl = MCI_DPSM_ENABLE | MCI_DPSM_DMAENABLE | blksz_bits << 4;
365 if (data->flags & MMC_DATA_READ) {
366 datactrl |= MCI_DPSM_DIRECTION;
367 lpc32xx_drvdat.lastch = DMA_CH_SDCARD_RX;
368 mmc_dma_rx_start(host);
371 lpc32xx_drvdat.lastch = DMA_CH_SDCARD_TX;
372 mmc_dma_tx_start(host);
375 writel(datactrl, base + MMCIDATACTRL);
376 datactrl = readl(base + MMCIMASK0) & ~MCI_DATABLOCKENDMASK;
377 writel(datactrl | MCI_DATAENDMASK, base + MMCIMASK0);
379 writel(irqmask, base + MMCIMASK1);
383 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
385 void __iomem *base = host->base;
387 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
388 cmd->opcode, cmd->arg, cmd->flags);
390 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
391 writel(0, base + MMCICOMMAND);
395 c |= cmd->opcode | MCI_CPSM_ENABLE;
396 if (cmd->flags & MMC_RSP_PRESENT) {
397 if (cmd->flags & MMC_RSP_136)
398 c |= MCI_CPSM_LONGRSP;
399 c |= MCI_CPSM_RESPONSE;
402 c |= MCI_CPSM_INTERRUPT;
406 writel(cmd->arg, base + MMCIARGUMENT);
407 writel(c, base + MMCICOMMAND);
411 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
414 if (status & MCI_DATAEND) {
415 host->data_xfered += data->blksz * data->blocks;
417 #ifdef CONFIG_ARCH_U300
419 * On the U300 some signal or other is
420 * badly routed so that a data write does
421 * not properly terminate with a MCI_DATAEND
422 * status flag. This quirk will make writes
425 if (data->flags & MMC_DATA_WRITE)
426 status |= MCI_DATAEND;
429 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
430 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
431 if (status & MCI_DATACRCFAIL)
432 data->error = -EILSEQ;
433 else if (status & MCI_DATATIMEOUT)
434 data->error = -ETIMEDOUT;
435 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
437 status |= MCI_DATAEND;
440 * We hit an error condition. Ensure that any data
441 * partially written to a page is properly coherent.
443 if (host->sg_len && data->flags & MMC_DATA_READ)
444 flush_dcache_page(sg_page(host->sg_ptr));
447 if (status & MCI_DATAEND) {
448 if (data->flags & MMC_DATA_READ) {
449 lpc32xx_dma_force_burst(lpc32xx_drvdat.lastch, DMA_PERID_SDCARD);
450 lpc32xx_dma_flush_llist(lpc32xx_drvdat.lastch);
451 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, DMA_FROM_DEVICE);
454 lpc32xx_dma_ch_disable(lpc32xx_drvdat.lastch);
455 if (lpc32xx_drvdat.mapped)
456 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
457 data->sg_len, DMA_TO_DEVICE);
460 mmci_stop_data(host);
463 mmci_request_end(host, data->mrq);
465 mmci_start_command(host, data->stop, 0);
471 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
474 void __iomem *base = host->base;
478 cmd->resp[0] = readl(base + MMCIRESPONSE0);
479 cmd->resp[1] = readl(base + MMCIRESPONSE1);
480 cmd->resp[2] = readl(base + MMCIRESPONSE2);
481 cmd->resp[3] = readl(base + MMCIRESPONSE3);
483 if (status & MCI_CMDTIMEOUT) {
484 cmd->error = -ETIMEDOUT;
485 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
486 cmd->error = -EILSEQ;
489 if (!cmd->data || cmd->error) {
491 mmci_stop_data(host);
492 mmci_request_end(host, cmd->mrq);
493 } else if (!(cmd->data->flags & MMC_DATA_READ))
494 mmci_start_data(host, cmd->data);
497 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
499 void __iomem *base = host->base;
502 int host_remain = host->size;
505 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
513 readsl(base + MMCIFIFO, ptr, count >> 2);
517 host_remain -= count;
522 status = readl(base + MMCISTATUS);
523 } while (status & MCI_RXDATAAVLBL);
528 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
530 void __iomem *base = host->base;
534 unsigned int count, maxcnt;
536 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
537 count = min(remain, maxcnt);
539 writesl(base + MMCIFIFO, ptr, count >> 2);
547 status = readl(base + MMCISTATUS);
548 } while (status & MCI_TXFIFOHALFEMPTY);
554 * PIO data transfer IRQ handler.
556 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
558 struct mmci_host *host = dev_id;
559 void __iomem *base = host->base;
562 status = readl(base + MMCISTATUS);
564 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
568 unsigned int remain, len;
572 * For write, we only need to test the half-empty flag
573 * here - if the FIFO is completely empty, then by
574 * definition it is more than half empty.
576 * For read, check for data available.
578 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
582 * Map the current scatter buffer.
584 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
585 remain = host->sg_ptr->length - host->sg_off;
588 if (status & MCI_RXACTIVE)
589 len = mmci_pio_read(host, buffer, remain);
590 if (status & MCI_TXACTIVE)
591 len = mmci_pio_write(host, buffer, remain, status);
596 mmci_kunmap_atomic(host, buffer, &flags);
606 * If we were reading, and we have completed this
607 * page, ensure that the data cache is coherent.
609 if (status & MCI_RXACTIVE)
610 flush_dcache_page(sg_page(host->sg_ptr));
612 if (!mmci_next_sg(host))
615 status = readl(base + MMCISTATUS);
619 * If we're nearing the end of the read, switch to
620 * "any data available" mode.
622 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
623 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
626 * If we run out of data, disable the data IRQs; this
627 * prevents a race where the FIFO becomes empty before
628 * the chip itself has disabled the data path, and
629 * stops us racing with our data end IRQ.
631 if (host->size == 0) {
632 writel(0, base + MMCIMASK1);
633 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
640 * Handle completion of command and data transfers.
642 static irqreturn_t mmci_irq(int irq, void *dev_id)
644 struct mmci_host *host = dev_id;
648 spin_lock(&host->lock);
651 struct mmc_command *cmd;
652 struct mmc_data *data;
654 status = readl(host->base + MMCISTATUS);
655 status &= readl(host->base + MMCIMASK0);
656 writel((status | MCI_DATABLOCKEND), host->base + MMCICLEAR);
658 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
661 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
662 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
663 mmci_data_irq(host, data, status);
666 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
667 mmci_cmd_irq(host, cmd, status);
672 spin_unlock(&host->lock);
674 return IRQ_RETVAL(ret);
677 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
679 struct mmci_host *host = mmc_priv(mmc);
682 WARN_ON(host->mrq != NULL);
684 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
685 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
687 mrq->cmd->error = -EINVAL;
688 mmc_request_done(mmc, mrq);
692 spin_lock_irqsave(&host->lock, flags);
696 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
697 mmci_start_data(host, mrq->data);
699 mmci_start_command(host, mrq->cmd, 0);
701 spin_unlock_irqrestore(&host->lock, flags);
704 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
706 struct mmci_host *host = mmc_priv(mmc);
710 switch (ios->power_mode) {
713 regulator_is_enabled(host->vcc))
714 regulator_disable(host->vcc);
717 #ifdef CONFIG_REGULATOR
719 /* This implicitly enables the regulator */
720 mmc_regulator_set_ocr(host->vcc, ios->vdd);
723 * The translate_vdd function is not used if you have
724 * an external regulator, or your design is really weird.
725 * Using it would mean sending in power control BOTH using
726 * a regulator AND the 4 MMCIPWR bits. If we don't have
727 * a regulator, we might have some other platform specific
728 * power control behind this translate function.
730 if (!host->vcc && host->plat->translate_vdd)
731 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
732 /* The ST version does not have this, fall through to POWER_ON */
733 if (host->hw_designer != AMBA_VENDOR_ST) {
742 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
743 if (host->hw_designer != AMBA_VENDOR_ST)
747 * The ST Micro variant use the ROD bit for something
748 * else and only has OD (Open Drain).
754 spin_lock_irqsave(&host->lock, flags);
756 mmci_set_clkreg(host, ios->clock);
758 if (host->pwr != pwr) {
760 writel(pwr, host->base + MMCIPOWER);
763 spin_unlock_irqrestore(&host->lock, flags);
766 static int mmci_get_ro(struct mmc_host *mmc)
768 struct mmci_host *host = mmc_priv(mmc);
770 if (host->gpio_wp == -ENOSYS)
773 return gpio_get_value(host->gpio_wp);
776 static int mmci_get_cd(struct mmc_host *mmc)
778 struct mmci_host *host = mmc_priv(mmc);
781 if (host->gpio_cd == -ENOSYS)
782 status = host->plat->status(mmc_dev(host->mmc));
784 status = gpio_get_value(host->gpio_cd);
789 static const struct mmc_host_ops mmci_ops = {
790 .request = mmci_request,
791 .set_ios = mmci_set_ios,
792 .get_ro = mmci_get_ro,
793 .get_cd = mmci_get_cd,
796 static void mmci_check_status(unsigned long data)
798 struct mmci_host *host = (struct mmci_host *)data;
799 unsigned int status = mmci_get_cd(host->mmc);
801 if (status ^ host->oldstat)
802 mmc_detect_change(host->mmc, 0);
804 host->oldstat = status;
805 mod_timer(&host->timer, jiffies + HZ);
808 static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
810 struct mmci_platform_data *plat = dev->dev.platform_data;
811 struct mmci_host *host;
812 struct mmc_host *mmc;
815 /* must have platform data */
821 ret = amba_request_regions(dev, DRIVER_NAME);
824 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
830 host = mmc_priv(mmc);
833 host->gpio_wp = -ENOSYS;
834 host->gpio_cd = -ENOSYS;
836 host->hw_designer = amba_manf(dev);
837 host->hw_revision = amba_rev(dev);
838 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
839 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
841 host->clk = clk_get(&dev->dev, NULL);
842 if (IS_ERR(host->clk)) {
843 ret = PTR_ERR(host->clk);
848 ret = clk_enable(host->clk);
853 host->mclk = clk_get_rate(host->clk);
855 * According to the spec, mclk is max 100 MHz,
856 * so we try to adjust the clock down to this,
859 if (host->mclk > 100000000) {
860 ret = clk_set_rate(host->clk, 100000000);
863 host->mclk = clk_get_rate(host->clk);
864 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
867 host->base = ioremap(dev->res.start, resource_size(&dev->res));
873 mmc->ops = &mmci_ops;
874 mmc->f_min = (host->mclk + 511) / 512;
875 mmc->f_max = min(host->mclk, fmax);
876 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
878 #ifdef CONFIG_REGULATOR
879 /* If we're using the regulator framework, try to fetch a regulator */
880 host->vcc = regulator_get(&dev->dev, "vmmc");
881 if (IS_ERR(host->vcc))
884 int mask = mmc_regulator_get_ocrmask(host->vcc);
887 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
890 host->mmc->ocr_avail = (u32) mask;
893 "Provided ocr_mask/setpower will not be used "
894 "(using regulator instead)\n");
898 /* Fall back to platform data if no regulator is found */
899 if (host->vcc == NULL)
900 mmc->ocr_avail = plat->ocr_mask;
901 mmc->caps = plat->capabilities;
906 mmc->max_hw_segs = 16;
907 mmc->max_phys_segs = NR_SG;
910 * Since we only have a 16-bit data length register, we must
911 * ensure that we don't exceed 2^16-1 bytes in a single request.
913 mmc->max_req_size = 65535;
916 * Set the maximum segment size. Since we aren't doing DMA
917 * (yet) we are only limited by the data length register.
920 * The LPC32x0 DMA controller can handle up to a 16383 byte DMA
921 * transfer. We'll rely on the mmc core to make sure the passed
922 * size for a request is block aligned.
924 mmc->max_seg_size = 65535;
927 * Block size can be up to 2048 bytes, but must be a power of two.
929 mmc->max_blk_size = 2048;
932 * No limit on the number of blocks transferred.
934 mmc->max_blk_count = mmc->max_req_size;
937 * Setup DMA for the interface
939 lpc32xx_drvdat.dev = &dev->dev;
943 spin_lock_init(&host->lock);
945 writel(0, host->base + MMCIMASK0);
946 writel(0, host->base + MMCIMASK1);
947 writel(0xfff, host->base + MMCICLEAR);
949 if (gpio_is_valid(plat->gpio_cd)) {
950 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
952 ret = gpio_direction_input(plat->gpio_cd);
954 host->gpio_cd = plat->gpio_cd;
955 else if (ret != -ENOSYS)
958 if (gpio_is_valid(plat->gpio_wp)) {
959 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
961 ret = gpio_direction_input(plat->gpio_wp);
963 host->gpio_wp = plat->gpio_wp;
964 else if (ret != -ENOSYS)
968 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
972 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
975 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
976 amba_set_drvdata(dev, mmc);
977 host->oldstat = mmci_get_cd(host->mmc);
981 dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
982 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
983 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
985 init_timer(&host->timer);
986 host->timer.data = (unsigned long)host;
987 host->timer.function = mmci_check_status;
988 host->timer.expires = jiffies + HZ;
989 add_timer(&host->timer);
994 free_irq(dev->irq[0], host);
996 if (host->gpio_wp != -ENOSYS)
997 gpio_free(host->gpio_wp);
999 if (host->gpio_cd != -ENOSYS)
1000 gpio_free(host->gpio_cd);
1004 iounmap(host->base);
1006 clk_disable(host->clk);
1012 amba_release_regions(dev);
1017 static int __devexit mmci_remove(struct amba_device *dev)
1019 struct mmc_host *mmc = amba_get_drvdata(dev);
1021 amba_set_drvdata(dev, NULL);
1024 struct mmci_host *host = mmc_priv(mmc);
1026 del_timer_sync(&host->timer);
1028 mmc_remove_host(mmc);
1030 writel(0, host->base + MMCIMASK0);
1031 writel(0, host->base + MMCIMASK1);
1033 writel(0, host->base + MMCICOMMAND);
1034 writel(0, host->base + MMCIDATACTRL);
1036 free_irq(dev->irq[0], host);
1037 free_irq(dev->irq[1], host);
1039 if (host->gpio_wp != -ENOSYS)
1040 gpio_free(host->gpio_wp);
1041 if (host->gpio_cd != -ENOSYS)
1042 gpio_free(host->gpio_cd);
1046 iounmap(host->base);
1047 clk_disable(host->clk);
1050 if (regulator_is_enabled(host->vcc))
1051 regulator_disable(host->vcc);
1052 regulator_put(host->vcc);
1056 amba_release_regions(dev);
1063 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
1065 struct mmc_host *mmc = amba_get_drvdata(dev);
1069 struct mmci_host *host = mmc_priv(mmc);
1071 ret = mmc_suspend_host(mmc, state);
1073 writel(0, host->base + MMCIMASK0);
1079 static int mmci_resume(struct amba_device *dev)
1081 struct mmc_host *mmc = amba_get_drvdata(dev);
1085 struct mmci_host *host = mmc_priv(mmc);
1087 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1089 ret = mmc_resume_host(mmc);
1095 #define mmci_suspend NULL
1096 #define mmci_resume NULL
1099 static struct amba_id mmci_ids[] = {
1108 /* ST Micro variants */
1120 static struct amba_driver mmci_driver = {
1122 .name = DRIVER_NAME,
1124 .probe = mmci_probe,
1125 .remove = __devexit_p(mmci_remove),
1126 .suspend = mmci_suspend,
1127 .resume = mmci_resume,
1128 .id_table = mmci_ids,
1131 static int __init mmci_init(void)
1133 return amba_driver_register(&mmci_driver);
1136 static void __exit mmci_exit(void)
1138 amba_driver_unregister(&mmci_driver);
1141 module_init(mmci_init);
1142 module_exit(mmci_exit);
1143 module_param(fmax, uint, 0444);
1145 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
1146 MODULE_LICENSE("GPL");