ARM: 6309/1: mmci: allow neither ->status nor gpio_cd to be specified
[linux-2.6.git] / drivers / mmc / host / mmci.c
blobfd89d923092868c9865b499a7b5703c9db3a4539
1 /*
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 #include <linux/gpio.h>
26 #include <linux/amba/mmci.h>
27 #include <linux/regulator/consumer.h>
29 #include <asm/div64.h>
30 #include <asm/io.h>
31 #include <asm/sizes.h>
33 #include "mmci.h"
35 #define DRIVER_NAME "mmci-pl18x"
37 static unsigned int fmax = 515633;
39 /**
40 * struct variant_data - MMCI variant-specific quirks
41 * @clkreg: default value for MCICLOCK register
42 * @clkreg_enable: enable value for MMCICLOCK register
43 * @datalength_bits: number of bits in the MMCIDATALENGTH register
44 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
45 * is asserted (likewise for RX)
46 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
47 * is asserted (likewise for RX)
49 struct variant_data {
50 unsigned int clkreg;
51 unsigned int clkreg_enable;
52 unsigned int datalength_bits;
53 unsigned int fifosize;
54 unsigned int fifohalfsize;
57 static struct variant_data variant_arm = {
58 .fifosize = 16 * 4,
59 .fifohalfsize = 8 * 4,
60 .datalength_bits = 16,
63 static struct variant_data variant_u300 = {
64 .fifosize = 16 * 4,
65 .fifohalfsize = 8 * 4,
66 .clkreg_enable = 1 << 13, /* HWFCEN */
67 .datalength_bits = 16,
70 static struct variant_data variant_ux500 = {
71 .fifosize = 30 * 4,
72 .fifohalfsize = 8 * 4,
73 .clkreg = MCI_CLK_ENABLE,
74 .clkreg_enable = 1 << 14, /* HWFCEN */
75 .datalength_bits = 24,
78 * This must be called with host->lock held
80 static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
82 struct variant_data *variant = host->variant;
83 u32 clk = variant->clkreg;
85 if (desired) {
86 if (desired >= host->mclk) {
87 clk = MCI_CLK_BYPASS;
88 host->cclk = host->mclk;
89 } else {
90 clk = host->mclk / (2 * desired) - 1;
91 if (clk >= 256)
92 clk = 255;
93 host->cclk = host->mclk / (2 * (clk + 1));
96 clk |= variant->clkreg_enable;
97 clk |= MCI_CLK_ENABLE;
98 /* This hasn't proven to be worthwhile */
99 /* clk |= MCI_CLK_PWRSAVE; */
102 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
103 clk |= MCI_4BIT_BUS;
104 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
105 clk |= MCI_ST_8BIT_BUS;
107 writel(clk, host->base + MMCICLOCK);
110 static void
111 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
113 writel(0, host->base + MMCICOMMAND);
115 BUG_ON(host->data);
117 host->mrq = NULL;
118 host->cmd = NULL;
120 if (mrq->data)
121 mrq->data->bytes_xfered = host->data_xfered;
124 * Need to drop the host lock here; mmc_request_done may call
125 * back into the driver...
127 spin_unlock(&host->lock);
128 mmc_request_done(host->mmc, mrq);
129 spin_lock(&host->lock);
132 static void mmci_stop_data(struct mmci_host *host)
134 writel(0, host->base + MMCIDATACTRL);
135 writel(0, host->base + MMCIMASK1);
136 host->data = NULL;
139 static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
141 unsigned int flags = SG_MITER_ATOMIC;
143 if (data->flags & MMC_DATA_READ)
144 flags |= SG_MITER_TO_SG;
145 else
146 flags |= SG_MITER_FROM_SG;
148 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
151 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
153 struct variant_data *variant = host->variant;
154 unsigned int datactrl, timeout, irqmask;
155 unsigned long long clks;
156 void __iomem *base;
157 int blksz_bits;
159 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
160 data->blksz, data->blocks, data->flags);
162 host->data = data;
163 host->size = data->blksz * data->blocks;
164 host->data_xfered = 0;
166 mmci_init_sg(host, data);
168 clks = (unsigned long long)data->timeout_ns * host->cclk;
169 do_div(clks, 1000000000UL);
171 timeout = data->timeout_clks + (unsigned int)clks;
173 base = host->base;
174 writel(timeout, base + MMCIDATATIMER);
175 writel(host->size, base + MMCIDATALENGTH);
177 blksz_bits = ffs(data->blksz) - 1;
178 BUG_ON(1 << blksz_bits != data->blksz);
180 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
181 if (data->flags & MMC_DATA_READ) {
182 datactrl |= MCI_DPSM_DIRECTION;
183 irqmask = MCI_RXFIFOHALFFULLMASK;
186 * If we have less than a FIFOSIZE of bytes to transfer,
187 * trigger a PIO interrupt as soon as any data is available.
189 if (host->size < variant->fifosize)
190 irqmask |= MCI_RXDATAAVLBLMASK;
191 } else {
193 * We don't actually need to include "FIFO empty" here
194 * since its implicit in "FIFO half empty".
196 irqmask = MCI_TXFIFOHALFEMPTYMASK;
199 writel(datactrl, base + MMCIDATACTRL);
200 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
201 writel(irqmask, base + MMCIMASK1);
204 static void
205 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
207 void __iomem *base = host->base;
209 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
210 cmd->opcode, cmd->arg, cmd->flags);
212 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
213 writel(0, base + MMCICOMMAND);
214 udelay(1);
217 c |= cmd->opcode | MCI_CPSM_ENABLE;
218 if (cmd->flags & MMC_RSP_PRESENT) {
219 if (cmd->flags & MMC_RSP_136)
220 c |= MCI_CPSM_LONGRSP;
221 c |= MCI_CPSM_RESPONSE;
223 if (/*interrupt*/0)
224 c |= MCI_CPSM_INTERRUPT;
226 host->cmd = cmd;
228 writel(cmd->arg, base + MMCIARGUMENT);
229 writel(c, base + MMCICOMMAND);
232 static void
233 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
234 unsigned int status)
236 if (status & MCI_DATABLOCKEND) {
237 host->data_xfered += data->blksz;
238 #ifdef CONFIG_ARCH_U300
240 * On the U300 some signal or other is
241 * badly routed so that a data write does
242 * not properly terminate with a MCI_DATAEND
243 * status flag. This quirk will make writes
244 * work again.
246 if (data->flags & MMC_DATA_WRITE)
247 status |= MCI_DATAEND;
248 #endif
250 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
251 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
252 if (status & MCI_DATACRCFAIL)
253 data->error = -EILSEQ;
254 else if (status & MCI_DATATIMEOUT)
255 data->error = -ETIMEDOUT;
256 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
257 data->error = -EIO;
258 status |= MCI_DATAEND;
261 * We hit an error condition. Ensure that any data
262 * partially written to a page is properly coherent.
264 if (data->flags & MMC_DATA_READ) {
265 struct sg_mapping_iter *sg_miter = &host->sg_miter;
266 unsigned long flags;
268 local_irq_save(flags);
269 if (sg_miter_next(sg_miter)) {
270 flush_dcache_page(sg_miter->page);
271 sg_miter_stop(sg_miter);
273 local_irq_restore(flags);
276 if (status & MCI_DATAEND) {
277 mmci_stop_data(host);
279 if (!data->stop) {
280 mmci_request_end(host, data->mrq);
281 } else {
282 mmci_start_command(host, data->stop, 0);
287 static void
288 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
289 unsigned int status)
291 void __iomem *base = host->base;
293 host->cmd = NULL;
295 cmd->resp[0] = readl(base + MMCIRESPONSE0);
296 cmd->resp[1] = readl(base + MMCIRESPONSE1);
297 cmd->resp[2] = readl(base + MMCIRESPONSE2);
298 cmd->resp[3] = readl(base + MMCIRESPONSE3);
300 if (status & MCI_CMDTIMEOUT) {
301 cmd->error = -ETIMEDOUT;
302 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
303 cmd->error = -EILSEQ;
306 if (!cmd->data || cmd->error) {
307 if (host->data)
308 mmci_stop_data(host);
309 mmci_request_end(host, cmd->mrq);
310 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
311 mmci_start_data(host, cmd->data);
315 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
317 void __iomem *base = host->base;
318 char *ptr = buffer;
319 u32 status;
320 int host_remain = host->size;
322 do {
323 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
325 if (count > remain)
326 count = remain;
328 if (count <= 0)
329 break;
331 readsl(base + MMCIFIFO, ptr, count >> 2);
333 ptr += count;
334 remain -= count;
335 host_remain -= count;
337 if (remain == 0)
338 break;
340 status = readl(base + MMCISTATUS);
341 } while (status & MCI_RXDATAAVLBL);
343 return ptr - buffer;
346 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
348 struct variant_data *variant = host->variant;
349 void __iomem *base = host->base;
350 char *ptr = buffer;
352 do {
353 unsigned int count, maxcnt;
355 maxcnt = status & MCI_TXFIFOEMPTY ?
356 variant->fifosize : variant->fifohalfsize;
357 count = min(remain, maxcnt);
359 writesl(base + MMCIFIFO, ptr, count >> 2);
361 ptr += count;
362 remain -= count;
364 if (remain == 0)
365 break;
367 status = readl(base + MMCISTATUS);
368 } while (status & MCI_TXFIFOHALFEMPTY);
370 return ptr - buffer;
374 * PIO data transfer IRQ handler.
376 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
378 struct mmci_host *host = dev_id;
379 struct sg_mapping_iter *sg_miter = &host->sg_miter;
380 struct variant_data *variant = host->variant;
381 void __iomem *base = host->base;
382 unsigned long flags;
383 u32 status;
385 status = readl(base + MMCISTATUS);
387 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
389 local_irq_save(flags);
391 do {
392 unsigned int remain, len;
393 char *buffer;
396 * For write, we only need to test the half-empty flag
397 * here - if the FIFO is completely empty, then by
398 * definition it is more than half empty.
400 * For read, check for data available.
402 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
403 break;
405 if (!sg_miter_next(sg_miter))
406 break;
408 buffer = sg_miter->addr;
409 remain = sg_miter->length;
411 len = 0;
412 if (status & MCI_RXACTIVE)
413 len = mmci_pio_read(host, buffer, remain);
414 if (status & MCI_TXACTIVE)
415 len = mmci_pio_write(host, buffer, remain, status);
417 sg_miter->consumed = len;
419 host->size -= len;
420 remain -= len;
422 if (remain)
423 break;
425 if (status & MCI_RXACTIVE)
426 flush_dcache_page(sg_miter->page);
428 status = readl(base + MMCISTATUS);
429 } while (1);
431 sg_miter_stop(sg_miter);
433 local_irq_restore(flags);
436 * If we're nearing the end of the read, switch to
437 * "any data available" mode.
439 if (status & MCI_RXACTIVE && host->size < variant->fifosize)
440 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
443 * If we run out of data, disable the data IRQs; this
444 * prevents a race where the FIFO becomes empty before
445 * the chip itself has disabled the data path, and
446 * stops us racing with our data end IRQ.
448 if (host->size == 0) {
449 writel(0, base + MMCIMASK1);
450 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
453 return IRQ_HANDLED;
457 * Handle completion of command and data transfers.
459 static irqreturn_t mmci_irq(int irq, void *dev_id)
461 struct mmci_host *host = dev_id;
462 u32 status;
463 int ret = 0;
465 spin_lock(&host->lock);
467 do {
468 struct mmc_command *cmd;
469 struct mmc_data *data;
471 status = readl(host->base + MMCISTATUS);
472 status &= readl(host->base + MMCIMASK0);
473 writel(status, host->base + MMCICLEAR);
475 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
477 data = host->data;
478 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
479 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
480 mmci_data_irq(host, data, status);
482 cmd = host->cmd;
483 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
484 mmci_cmd_irq(host, cmd, status);
486 ret = 1;
487 } while (status);
489 spin_unlock(&host->lock);
491 return IRQ_RETVAL(ret);
494 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
496 struct mmci_host *host = mmc_priv(mmc);
497 unsigned long flags;
499 WARN_ON(host->mrq != NULL);
501 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
502 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
503 mrq->data->blksz);
504 mrq->cmd->error = -EINVAL;
505 mmc_request_done(mmc, mrq);
506 return;
509 spin_lock_irqsave(&host->lock, flags);
511 host->mrq = mrq;
513 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
514 mmci_start_data(host, mrq->data);
516 mmci_start_command(host, mrq->cmd, 0);
518 spin_unlock_irqrestore(&host->lock, flags);
521 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
523 struct mmci_host *host = mmc_priv(mmc);
524 u32 pwr = 0;
525 unsigned long flags;
527 switch (ios->power_mode) {
528 case MMC_POWER_OFF:
529 if(host->vcc &&
530 regulator_is_enabled(host->vcc))
531 regulator_disable(host->vcc);
532 break;
533 case MMC_POWER_UP:
534 #ifdef CONFIG_REGULATOR
535 if (host->vcc)
536 /* This implicitly enables the regulator */
537 mmc_regulator_set_ocr(host->vcc, ios->vdd);
538 #endif
539 if (host->plat->vdd_handler)
540 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
541 ios->power_mode);
542 /* The ST version does not have this, fall through to POWER_ON */
543 if (host->hw_designer != AMBA_VENDOR_ST) {
544 pwr |= MCI_PWR_UP;
545 break;
547 case MMC_POWER_ON:
548 pwr |= MCI_PWR_ON;
549 break;
552 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
553 if (host->hw_designer != AMBA_VENDOR_ST)
554 pwr |= MCI_ROD;
555 else {
557 * The ST Micro variant use the ROD bit for something
558 * else and only has OD (Open Drain).
560 pwr |= MCI_OD;
564 spin_lock_irqsave(&host->lock, flags);
566 mmci_set_clkreg(host, ios->clock);
568 if (host->pwr != pwr) {
569 host->pwr = pwr;
570 writel(pwr, host->base + MMCIPOWER);
573 spin_unlock_irqrestore(&host->lock, flags);
576 static int mmci_get_ro(struct mmc_host *mmc)
578 struct mmci_host *host = mmc_priv(mmc);
580 if (host->gpio_wp == -ENOSYS)
581 return -ENOSYS;
583 return gpio_get_value(host->gpio_wp);
586 static int mmci_get_cd(struct mmc_host *mmc)
588 struct mmci_host *host = mmc_priv(mmc);
589 struct mmci_platform_data *plat = host->plat;
590 unsigned int status;
592 if (host->gpio_cd == -ENOSYS) {
593 if (!plat->status)
594 return 1; /* Assume always present */
596 status = plat->status(mmc_dev(host->mmc));
597 } else
598 status = !!gpio_get_value(host->gpio_cd) ^ plat->cd_invert;
601 * Use positive logic throughout - status is zero for no card,
602 * non-zero for card inserted.
604 return status;
607 static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
609 struct mmci_host *host = dev_id;
611 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
613 return IRQ_HANDLED;
616 static const struct mmc_host_ops mmci_ops = {
617 .request = mmci_request,
618 .set_ios = mmci_set_ios,
619 .get_ro = mmci_get_ro,
620 .get_cd = mmci_get_cd,
623 static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
625 struct mmci_platform_data *plat = dev->dev.platform_data;
626 struct variant_data *variant = id->data;
627 struct mmci_host *host;
628 struct mmc_host *mmc;
629 int ret;
631 /* must have platform data */
632 if (!plat) {
633 ret = -EINVAL;
634 goto out;
637 ret = amba_request_regions(dev, DRIVER_NAME);
638 if (ret)
639 goto out;
641 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
642 if (!mmc) {
643 ret = -ENOMEM;
644 goto rel_regions;
647 host = mmc_priv(mmc);
648 host->mmc = mmc;
650 host->gpio_wp = -ENOSYS;
651 host->gpio_cd = -ENOSYS;
652 host->gpio_cd_irq = -1;
654 host->hw_designer = amba_manf(dev);
655 host->hw_revision = amba_rev(dev);
656 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
657 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
659 host->clk = clk_get(&dev->dev, NULL);
660 if (IS_ERR(host->clk)) {
661 ret = PTR_ERR(host->clk);
662 host->clk = NULL;
663 goto host_free;
666 ret = clk_enable(host->clk);
667 if (ret)
668 goto clk_free;
670 host->plat = plat;
671 host->variant = variant;
672 host->mclk = clk_get_rate(host->clk);
674 * According to the spec, mclk is max 100 MHz,
675 * so we try to adjust the clock down to this,
676 * (if possible).
678 if (host->mclk > 100000000) {
679 ret = clk_set_rate(host->clk, 100000000);
680 if (ret < 0)
681 goto clk_disable;
682 host->mclk = clk_get_rate(host->clk);
683 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
684 host->mclk);
686 host->base = ioremap(dev->res.start, resource_size(&dev->res));
687 if (!host->base) {
688 ret = -ENOMEM;
689 goto clk_disable;
692 mmc->ops = &mmci_ops;
693 mmc->f_min = (host->mclk + 511) / 512;
695 * If the platform data supplies a maximum operating
696 * frequency, this takes precedence. Else, we fall back
697 * to using the module parameter, which has a (low)
698 * default value in case it is not specified. Either
699 * value must not exceed the clock rate into the block,
700 * of course.
702 if (plat->f_max)
703 mmc->f_max = min(host->mclk, plat->f_max);
704 else
705 mmc->f_max = min(host->mclk, fmax);
706 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
708 #ifdef CONFIG_REGULATOR
709 /* If we're using the regulator framework, try to fetch a regulator */
710 host->vcc = regulator_get(&dev->dev, "vmmc");
711 if (IS_ERR(host->vcc))
712 host->vcc = NULL;
713 else {
714 int mask = mmc_regulator_get_ocrmask(host->vcc);
716 if (mask < 0)
717 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
718 mask);
719 else {
720 host->mmc->ocr_avail = (u32) mask;
721 if (plat->ocr_mask)
722 dev_warn(&dev->dev,
723 "Provided ocr_mask/setpower will not be used "
724 "(using regulator instead)\n");
727 #endif
728 /* Fall back to platform data if no regulator is found */
729 if (host->vcc == NULL)
730 mmc->ocr_avail = plat->ocr_mask;
731 mmc->caps = plat->capabilities;
734 * We can do SGIO
736 mmc->max_hw_segs = 16;
737 mmc->max_phys_segs = NR_SG;
740 * Since only a certain number of bits are valid in the data length
741 * register, we must ensure that we don't exceed 2^num-1 bytes in a
742 * single request.
744 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
747 * Set the maximum segment size. Since we aren't doing DMA
748 * (yet) we are only limited by the data length register.
750 mmc->max_seg_size = mmc->max_req_size;
753 * Block size can be up to 2048 bytes, but must be a power of two.
755 mmc->max_blk_size = 2048;
758 * No limit on the number of blocks transferred.
760 mmc->max_blk_count = mmc->max_req_size;
762 spin_lock_init(&host->lock);
764 writel(0, host->base + MMCIMASK0);
765 writel(0, host->base + MMCIMASK1);
766 writel(0xfff, host->base + MMCICLEAR);
768 if (gpio_is_valid(plat->gpio_cd)) {
769 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
770 if (ret == 0)
771 ret = gpio_direction_input(plat->gpio_cd);
772 if (ret == 0)
773 host->gpio_cd = plat->gpio_cd;
774 else if (ret != -ENOSYS)
775 goto err_gpio_cd;
777 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
778 mmci_cd_irq, 0,
779 DRIVER_NAME " (cd)", host);
780 if (ret >= 0)
781 host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
783 if (gpio_is_valid(plat->gpio_wp)) {
784 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
785 if (ret == 0)
786 ret = gpio_direction_input(plat->gpio_wp);
787 if (ret == 0)
788 host->gpio_wp = plat->gpio_wp;
789 else if (ret != -ENOSYS)
790 goto err_gpio_wp;
793 if ((host->plat->status || host->gpio_cd != -ENOSYS)
794 && host->gpio_cd_irq < 0)
795 mmc->caps |= MMC_CAP_NEEDS_POLL;
797 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
798 if (ret)
799 goto unmap;
801 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
802 if (ret)
803 goto irq0_free;
805 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
807 amba_set_drvdata(dev, mmc);
809 mmc_add_host(mmc);
811 dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
812 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
813 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
815 return 0;
817 irq0_free:
818 free_irq(dev->irq[0], host);
819 unmap:
820 if (host->gpio_wp != -ENOSYS)
821 gpio_free(host->gpio_wp);
822 err_gpio_wp:
823 if (host->gpio_cd_irq >= 0)
824 free_irq(host->gpio_cd_irq, host);
825 if (host->gpio_cd != -ENOSYS)
826 gpio_free(host->gpio_cd);
827 err_gpio_cd:
828 iounmap(host->base);
829 clk_disable:
830 clk_disable(host->clk);
831 clk_free:
832 clk_put(host->clk);
833 host_free:
834 mmc_free_host(mmc);
835 rel_regions:
836 amba_release_regions(dev);
837 out:
838 return ret;
841 static int __devexit mmci_remove(struct amba_device *dev)
843 struct mmc_host *mmc = amba_get_drvdata(dev);
845 amba_set_drvdata(dev, NULL);
847 if (mmc) {
848 struct mmci_host *host = mmc_priv(mmc);
850 mmc_remove_host(mmc);
852 writel(0, host->base + MMCIMASK0);
853 writel(0, host->base + MMCIMASK1);
855 writel(0, host->base + MMCICOMMAND);
856 writel(0, host->base + MMCIDATACTRL);
858 free_irq(dev->irq[0], host);
859 free_irq(dev->irq[1], host);
861 if (host->gpio_wp != -ENOSYS)
862 gpio_free(host->gpio_wp);
863 if (host->gpio_cd_irq >= 0)
864 free_irq(host->gpio_cd_irq, host);
865 if (host->gpio_cd != -ENOSYS)
866 gpio_free(host->gpio_cd);
868 iounmap(host->base);
869 clk_disable(host->clk);
870 clk_put(host->clk);
872 if (regulator_is_enabled(host->vcc))
873 regulator_disable(host->vcc);
874 regulator_put(host->vcc);
876 mmc_free_host(mmc);
878 amba_release_regions(dev);
881 return 0;
884 #ifdef CONFIG_PM
885 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
887 struct mmc_host *mmc = amba_get_drvdata(dev);
888 int ret = 0;
890 if (mmc) {
891 struct mmci_host *host = mmc_priv(mmc);
893 ret = mmc_suspend_host(mmc);
894 if (ret == 0)
895 writel(0, host->base + MMCIMASK0);
898 return ret;
901 static int mmci_resume(struct amba_device *dev)
903 struct mmc_host *mmc = amba_get_drvdata(dev);
904 int ret = 0;
906 if (mmc) {
907 struct mmci_host *host = mmc_priv(mmc);
909 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
911 ret = mmc_resume_host(mmc);
914 return ret;
916 #else
917 #define mmci_suspend NULL
918 #define mmci_resume NULL
919 #endif
921 static struct amba_id mmci_ids[] = {
923 .id = 0x00041180,
924 .mask = 0x000fffff,
925 .data = &variant_arm,
928 .id = 0x00041181,
929 .mask = 0x000fffff,
930 .data = &variant_arm,
932 /* ST Micro variants */
934 .id = 0x00180180,
935 .mask = 0x00ffffff,
936 .data = &variant_u300,
939 .id = 0x00280180,
940 .mask = 0x00ffffff,
941 .data = &variant_u300,
944 .id = 0x00480180,
945 .mask = 0x00ffffff,
946 .data = &variant_ux500,
948 { 0, 0 },
951 static struct amba_driver mmci_driver = {
952 .drv = {
953 .name = DRIVER_NAME,
955 .probe = mmci_probe,
956 .remove = __devexit_p(mmci_remove),
957 .suspend = mmci_suspend,
958 .resume = mmci_resume,
959 .id_table = mmci_ids,
962 static int __init mmci_init(void)
964 return amba_driver_register(&mmci_driver);
967 static void __exit mmci_exit(void)
969 amba_driver_unregister(&mmci_driver);
972 module_init(mmci_init);
973 module_exit(mmci_exit);
974 module_param(fmax, uint, 0444);
976 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
977 MODULE_LICENSE("GPL");