]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/mmc/host/pxamci.c
mmc: fix platform driver hotplug/coldplug
[linux-2.6-omap-h63xx.git] / drivers / mmc / host / pxamci.c
index 657901eecfced1b6222d2f772a81415a5b836eaa..65210fca37ede70f3c42199579b142e01a2a120c 100644 (file)
 #include <linux/delay.h>
 #include <linux/interrupt.h>
 #include <linux/dma-mapping.h>
+#include <linux/clk.h>
+#include <linux/err.h>
 #include <linux/mmc/host.h>
 
 #include <asm/dma.h>
 #include <asm/io.h>
-#include <asm/scatterlist.h>
 #include <asm/sizes.h>
 
 #include <asm/arch/pxa-regs.h>
 #define DRIVER_NAME    "pxa2xx-mci"
 
 #define NR_SG  1
+#define CLKRT_OFF      (~0)
 
 struct pxamci_host {
        struct mmc_host         *mmc;
        spinlock_t              lock;
        struct resource         *res;
        void __iomem            *base;
+       struct clk              *clk;
+       unsigned long           clkrate;
        int                     irq;
        int                     dma;
        unsigned int            clkrt;
@@ -61,6 +65,8 @@ struct pxamci_host {
        unsigned int            dma_len;
 
        unsigned int            dma_dir;
+       unsigned int            dma_drcmrrx;
+       unsigned int            dma_drcmrtx;
 };
 
 static void pxamci_stop_clock(struct pxamci_host *host)
@@ -119,7 +125,7 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
        writel(nob, host->base + MMC_NOB);
        writel(data->blksz, host->base + MMC_BLKLEN);
 
-       clks = (unsigned long long)data->timeout_ns * CLOCKRATE;
+       clks = (unsigned long long)data->timeout_ns * host->clkrate;
        do_div(clks, 1000000000UL);
        timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
        writel((timeout + 255) / 256, host->base + MMC_RDTO);
@@ -127,13 +133,13 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
        if (data->flags & MMC_DATA_READ) {
                host->dma_dir = DMA_FROM_DEVICE;
                dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
-               DRCMRTXMMC = 0;
-               DRCMRRXMMC = host->dma | DRCMR_MAPVLD;
+               DRCMR(host->dma_drcmrtx) = 0;
+               DRCMR(host->dma_drcmrrx) = host->dma | DRCMR_MAPVLD;
        } else {
                host->dma_dir = DMA_TO_DEVICE;
                dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
-               DRCMRRXMMC = 0;
-               DRCMRTXMMC = host->dma | DRCMR_MAPVLD;
+               DRCMR(host->dma_drcmrrx) = 0;
+               DRCMR(host->dma_drcmrtx) = host->dma | DRCMR_MAPVLD;
        }
 
        dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
@@ -365,18 +371,39 @@ static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
        struct pxamci_host *host = mmc_priv(mmc);
 
        if (ios->clock) {
-               unsigned int clk = CLOCKRATE / ios->clock;
-               if (CLOCKRATE / clk > ios->clock)
-                       clk <<= 1;
-               host->clkrt = fls(clk) - 1;
-               pxa_set_cken(CKEN_MMC, 1);
+               unsigned long rate = host->clkrate;
+               unsigned int clk = rate / ios->clock;
+
+               if (host->clkrt == CLKRT_OFF)
+                       clk_enable(host->clk);
+
+               if (ios->clock == 26000000) {
+                       /* to support 26MHz on pxa300/pxa310 */
+                       host->clkrt = 7;
+               } else {
+                       /* to handle (19.5MHz, 26MHz) */
+                       if (!clk)
+                               clk = 1;
+
+                       /*
+                        * clk might result in a lower divisor than we
+                        * desire.  check for that condition and adjust
+                        * as appropriate.
+                        */
+                       if (rate / clk > ios->clock)
+                               clk <<= 1;
+                       host->clkrt = fls(clk) - 1;
+               }
 
                /*
                 * we write clkrt on the next command
                 */
        } else {
                pxamci_stop_clock(host);
-               pxa_set_cken(CKEN_MMC, 0);
+               if (host->clkrt != CLKRT_OFF) {
+                       host->clkrt = CLKRT_OFF;
+                       clk_disable(host->clk);
+               }
        }
 
        if (host->power_mode != ios->power_mode) {
@@ -443,7 +470,7 @@ static int pxamci_probe(struct platform_device *pdev)
 {
        struct mmc_host *mmc;
        struct pxamci_host *host = NULL;
-       struct resource *r;
+       struct resource *r, *dmarx, *dmatx;
        int ret, irq;
 
        r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -462,8 +489,6 @@ static int pxamci_probe(struct platform_device *pdev)
        }
 
        mmc->ops = &pxamci_ops;
-       mmc->f_min = CLOCKRATE_MIN;
-       mmc->f_max = CLOCKRATE_MAX;
 
        /*
         * We can do SG-DMA, but we don't because we never know how much
@@ -490,6 +515,24 @@ static int pxamci_probe(struct platform_device *pdev)
        host->mmc = mmc;
        host->dma = -1;
        host->pdata = pdev->dev.platform_data;
+       host->clkrt = CLKRT_OFF;
+
+       host->clk = clk_get(&pdev->dev, "MMCCLK");
+       if (IS_ERR(host->clk)) {
+               ret = PTR_ERR(host->clk);
+               host->clk = NULL;
+               goto out;
+       }
+
+       host->clkrate = clk_get_rate(host->clk);
+
+       /*
+        * Calculate minimum clock rate, rounding up.
+        */
+       mmc->f_min = (host->clkrate + 63) / 64;
+       mmc->f_max = (cpu_is_pxa300() || cpu_is_pxa310()) ? 26000000
+                                                         : host->clkrate;
+
        mmc->ocr_avail = host->pdata ?
                         host->pdata->ocr_mask :
                         MMC_VDD_32_33|MMC_VDD_33_34;
@@ -498,6 +541,9 @@ static int pxamci_probe(struct platform_device *pdev)
        if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) {
                mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
                host->cmdat |= CMDAT_SDIO_INT_EN;
+               if (cpu_is_pxa300() || cpu_is_pxa310())
+                       mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
+                                    MMC_CAP_SD_HIGHSPEED;
        }
 
        host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
@@ -539,6 +585,20 @@ static int pxamci_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, mmc);
 
+       dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
+       if (!dmarx) {
+               ret = -ENXIO;
+               goto out;
+       }
+       host->dma_drcmrrx = dmarx->start;
+
+       dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
+       if (!dmatx) {
+               ret = -ENXIO;
+               goto out;
+       }
+       host->dma_drcmrtx = dmatx->start;
+
        if (host->pdata && host->pdata->init)
                host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
 
@@ -554,6 +614,8 @@ static int pxamci_probe(struct platform_device *pdev)
                        iounmap(host->base);
                if (host->sg_cpu)
                        dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
+               if (host->clk)
+                       clk_put(host->clk);
        }
        if (mmc)
                mmc_free_host(mmc);
@@ -580,14 +642,16 @@ static int pxamci_remove(struct platform_device *pdev)
                       END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
                       host->base + MMC_I_MASK);
 
-               DRCMRRXMMC = 0;
-               DRCMRTXMMC = 0;
+               DRCMR(host->dma_drcmrrx) = 0;
+               DRCMR(host->dma_drcmrtx) = 0;
 
                free_irq(host->irq, host);
                pxa_free_dma(host->dma);
                iounmap(host->base);
                dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
 
+               clk_put(host->clk);
+
                release_resource(host->res);
 
                mmc_free_host(mmc);
@@ -629,6 +693,7 @@ static struct platform_driver pxamci_driver = {
        .resume         = pxamci_resume,
        .driver         = {
                .name   = DRIVER_NAME,
+               .owner  = THIS_MODULE,
        },
 };
 
@@ -647,3 +712,4 @@ module_exit(pxamci_exit);
 
 MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:pxa2xx-mci");