[ARM] pxa: don't pass a consumer clock name for devices with unique clocks
[safe/jmp/linux-2.6] / drivers / mmc / host / pxamci.c
index d97d386..a1700a8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  linux/drivers/mmc/pxa.c - PXA MMCI driver
+ *  linux/drivers/mmc/host/pxa.c - PXA MMCI driver
  *
  *  Copyright (C) 2003 Russell King, All Rights Reserved.
  *
 #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>
-#include <asm/arch/mmc.h>
+#include <mach/pxa-regs.h>
+#include <mach/mmc.h>
 
 #include "pxamci.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)
@@ -108,6 +114,7 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
        unsigned int nob = data->blocks;
        unsigned long long clks;
        unsigned int timeout;
+       bool dalgn = 0;
        u32 dcmd;
        int i;
 
@@ -119,7 +126,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 +134,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;
@@ -142,6 +149,13 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
                                   host->dma_dir);
 
        for (i = 0; i < host->dma_len; i++) {
+               unsigned int length = sg_dma_len(&data->sg[i]);
+               host->sg_cpu[i].dcmd = dcmd | length;
+               if (length & 31 && !(data->flags & MMC_DATA_READ))
+                       host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
+               /* Not aligned to 8-byte boundary? */
+               if (sg_dma_address(&data->sg[i]) & 0x7)
+                       dalgn = 1;
                if (data->flags & MMC_DATA_READ) {
                        host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
                        host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
@@ -149,13 +163,21 @@ static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
                        host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
                        host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
                }
-               host->sg_cpu[i].dcmd = dcmd | sg_dma_len(&data->sg[i]);
                host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
                                        sizeof(struct pxa_dma_desc);
        }
        host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
        wmb();
 
+       /*
+        * The PXA27x DMA controller encounters overhead when working with
+        * unaligned (to 8-byte boundaries) data, so switch on byte alignment
+        * mode only if we have unaligned data.
+        */
+       if (dalgn)
+               DALGN |= (1 << host->dma);
+       else
+               DALGN &= ~(1 << host->dma);
        DDADR(host->dma) = host->sg_dma;
        DCSR(host->dma) = DCSR_RUN;
 }
@@ -226,30 +248,24 @@ static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
        }
 
        if (stat & STAT_TIME_OUT_RESPONSE) {
-               cmd->error = MMC_ERR_TIMEOUT;
+               cmd->error = -ETIMEDOUT;
        } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
 #ifdef CONFIG_PXA27x
                /*
                 * workaround for erratum #42:
                 * Intel PXA27x Family Processor Specification Update Rev 001
+                * A bogus CRC error can appear if the msb of a 136 bit
+                * response is a one.
                 */
-               if (cmd->opcode == MMC_ALL_SEND_CID ||
-                   cmd->opcode == MMC_SEND_CSD ||
-                   cmd->opcode == MMC_SEND_CID) {
-                       /* a bogus CRC error can appear if the msb of
-                          the 15 byte response is a one */
-                       if ((cmd->resp[0] & 0x80000000) == 0)
-                               cmd->error = MMC_ERR_BADCRC;
-               } else {
-                       pr_debug("ignoring CRC from command %d - *risky*\n",cmd->opcode);
-               }
-#else
-               cmd->error = MMC_ERR_BADCRC;
+               if (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000) {
+                       pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
+               } else
 #endif
+               cmd->error = -EILSEQ;
        }
 
        pxamci_disable_irq(host, END_CMD_RES);
-       if (host->data && cmd->error == MMC_ERR_NONE) {
+       if (host->data && !cmd->error) {
                pxamci_enable_irq(host, DATA_TRAN_DONE);
        } else {
                pxamci_finish_request(host, host->mrq);
@@ -270,9 +286,9 @@ static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
                     host->dma_dir);
 
        if (stat & STAT_READ_TIME_OUT)
-               data->error = MMC_ERR_TIMEOUT;
+               data->error = -ETIMEDOUT;
        else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
-               data->error = MMC_ERR_BADCRC;
+               data->error = -EILSEQ;
 
        /*
         * There appears to be a hardware design bug here.  There seems to
@@ -280,7 +296,7 @@ static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
         * This means that if there was an error on any block, we mark all
         * data blocks as being in error.
         */
-       if (data->error == MMC_ERR_NONE)
+       if (!data->error)
                data->bytes_xfered = data->blocks * data->blksz;
        else
                data->bytes_xfered = 0;
@@ -290,7 +306,7 @@ static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
        host->data = NULL;
        if (host->mrq->stop) {
                pxamci_stop_clock(host);
-               pxamci_start_cmd(host, host->mrq->stop, 0);
+               pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
        } else {
                pxamci_finish_request(host, host->mrq);
        }
@@ -304,7 +320,7 @@ static irqreturn_t pxamci_irq(int irq, void *devid)
        unsigned int ireg;
        int handled = 0;
 
-       ireg = readl(host->base + MMC_I_REG);
+       ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
 
        if (ireg) {
                unsigned stat = readl(host->base + MMC_STAT);
@@ -315,6 +331,10 @@ static irqreturn_t pxamci_irq(int irq, void *devid)
                        handled |= pxamci_cmd_done(host, stat);
                if (ireg & DATA_TRAN_DONE)
                        handled |= pxamci_data_done(host, stat);
+               if (ireg & SDIO_INT) {
+                       mmc_signal_sdio_irq(host->mmc);
+                       handled = 1;
+               }
        }
 
        return IRQ_RETVAL(handled);
@@ -354,9 +374,12 @@ static int pxamci_get_ro(struct mmc_host *mmc)
        struct pxamci_host *host = mmc_priv(mmc);
 
        if (host->pdata && host->pdata->get_ro)
-               return host->pdata->get_ro(mmc_dev(mmc));
-       /* Host doesn't support read only detection so assume writeable */
-       return 0;
+               return !!host->pdata->get_ro(mmc_dev(mmc));
+       /*
+        * Board doesn't support read only detection; let the mmc core
+        * decide what to do.
+        */
+       return -ENOSYS;
 }
 
 static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
@@ -364,18 +387,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) {
@@ -388,20 +432,46 @@ static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
                        host->cmdat |= CMDAT_INIT;
        }
 
+       if (ios->bus_width == MMC_BUS_WIDTH_4)
+               host->cmdat |= CMDAT_SD_4DAT;
+       else
+               host->cmdat &= ~CMDAT_SD_4DAT;
+
        pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
                 host->clkrt, host->cmdat);
 }
 
+static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
+{
+       struct pxamci_host *pxa_host = mmc_priv(host);
+
+       if (enable)
+               pxamci_enable_irq(pxa_host, SDIO_INT);
+       else
+               pxamci_disable_irq(pxa_host, SDIO_INT);
+}
+
 static const struct mmc_host_ops pxamci_ops = {
-       .request        = pxamci_request,
-       .get_ro         = pxamci_get_ro,
-       .set_ios        = pxamci_set_ios,
+       .request                = pxamci_request,
+       .get_ro                 = pxamci_get_ro,
+       .set_ios                = pxamci_set_ios,
+       .enable_sdio_irq        = pxamci_enable_sdio_irq,
 };
 
 static void pxamci_dma_irq(int dma, void *devid)
 {
-       printk(KERN_ERR "DMA%d: IRQ???\n", dma);
-       DCSR(dma) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
+       struct pxamci_host *host = devid;
+       int dcsr = DCSR(dma);
+       DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
+
+       if (dcsr & DCSR_ENDINTR) {
+               writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
+       } else {
+               printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
+                      mmc_hostname(host->mmc), dma, dcsr);
+               host->data->error = -EIO;
+               pxamci_data_done(host, 0);
+       }
 }
 
 static irqreturn_t pxamci_detect_irq(int irq, void *devid)
@@ -416,7 +486,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);
@@ -435,8 +505,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
@@ -450,9 +518,9 @@ static int pxamci_probe(struct platform_device *pdev)
        mmc->max_seg_size = PAGE_SIZE;
 
        /*
-        * Block length register is 10 bits.
+        * Block length register is only 10 bits before PXA27x.
         */
-       mmc->max_blk_size = 1023;
+       mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
 
        /*
         * Block count register is 16 bits.
@@ -463,9 +531,36 @@ 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, NULL);
+       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;
+       mmc->caps = 0;
+       host->cmdat = 0;
+       if (!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);
        if (!host->sg_cpu) {
@@ -506,6 +601,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);
 
@@ -521,6 +630,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);
@@ -547,14 +658,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);
@@ -596,6 +709,7 @@ static struct platform_driver pxamci_driver = {
        .resume         = pxamci_resume,
        .driver         = {
                .name   = DRIVER_NAME,
+               .owner  = THIS_MODULE,
        },
 };
 
@@ -614,3 +728,4 @@ module_exit(pxamci_exit);
 
 MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:pxa2xx-mci");