mmc: Add a MX2/MX3 specific SDHC driver
[safe/jmp/linux-2.6] / drivers / mmc / host / mmc_spi.c
index f30327b..87e211d 100644 (file)
  */
 #include <linux/hrtimer.h>
 #include <linux/delay.h>
-#include <linux/blkdev.h>
+#include <linux/bio.h>
 #include <linux/dma-mapping.h>
 #include <linux/crc7.h>
 #include <linux/crc-itu-t.h>
+#include <linux/scatterlist.h>
 
 #include <linux/mmc/host.h>
 #include <linux/mmc/mmc.h>             /* for R1_SPI_* bit values */
@@ -94,8 +95,6 @@
  * reads which takes nowhere near that long.  Older cards may be able to use
  * shorter timeouts ... but why bother?
  */
-#define readblock_timeout      ktime_set(0, 100 * 1000 * 1000)
-#define writeblock_timeout     ktime_set(0, 250 * 1000 * 1000)
 #define r1b_timeout            ktime_set(3, 0)
 
 
@@ -175,8 +174,6 @@ mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
                                DMA_FROM_DEVICE);
 
        status = spi_sync(host->spi, &host->readback);
-       if (status == 0)
-               status = host->readback.status;
 
        if (host->dma_dev)
                dma_sync_single_for_cpu(host->dma_dev,
@@ -221,9 +218,9 @@ mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
        return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
 }
 
-static int mmc_spi_readtoken(struct mmc_spi_host *host)
+static int mmc_spi_readtoken(struct mmc_spi_host *host, ktime_t timeout)
 {
-       return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
+       return mmc_spi_skip(host, timeout, 1, 0xff);
 }
 
 
@@ -341,7 +338,7 @@ checkstatus:
 
        /* SPI R3, R4, or R7 == R1 + 4 bytes */
        case MMC_RSP_SPI_R3:
-               cmd->resp[1] = be32_to_cpu(get_unaligned((u32 *)cp));
+               cmd->resp[1] = get_unaligned_be32(cp);
                break;
 
        /* SPI R1 == just one status byte */
@@ -479,8 +476,6 @@ mmc_spi_command_send(struct mmc_spi_host *host,
                                DMA_BIDIRECTIONAL);
        }
        status = spi_sync(host->spi, &host->m);
-       if (status == 0)
-               status = host->m.status;
 
        if (host->dma_dev)
                dma_sync_single_for_cpu(host->dma_dev,
@@ -608,7 +603,8 @@ mmc_spi_setup_data_message(
  * Return negative errno, else success.
  */
 static int
-mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
+mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t,
+       ktime_t timeout)
 {
        struct spi_device       *spi = host->spi;
        int                     status, i;
@@ -623,8 +619,6 @@ mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
                                DMA_BIDIRECTIONAL);
 
        status = spi_sync(spi, &host->m);
-       if (status == 0)
-               status = host->m.status;
 
        if (status != 0) {
                dev_dbg(&spi->dev, "write error (%d)\n", status);
@@ -678,7 +672,7 @@ mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
                if (scratch->status[i] != 0)
                        return 0;
        }
-       return mmc_spi_wait_unbusy(host, writeblock_timeout);
+       return mmc_spi_wait_unbusy(host, timeout);
 }
 
 /*
@@ -698,7 +692,8 @@ mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
  * STOP_TRANSMISSION command.
  */
 static int
-mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
+mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t,
+       ktime_t timeout)
 {
        struct spi_device       *spi = host->spi;
        int                     status;
@@ -712,7 +707,7 @@ mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
                return status;
        status = scratch->status[0];
        if (status == 0xff || status == 0)
-               status = mmc_spi_readtoken(host);
+               status = mmc_spi_readtoken(host, timeout);
 
        if (status == SPI_TOKEN_SINGLE) {
                if (host->dma_dev) {
@@ -725,8 +720,6 @@ mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
                }
 
                status = spi_sync(spi, &host->m);
-               if (status == 0)
-                       status = host->m.status;
 
                if (host->dma_dev) {
                        dma_sync_single_for_cpu(host->dma_dev,
@@ -785,6 +778,8 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
        struct scatterlist      *sg;
        unsigned                n_sg;
        int                     multiple = (data->blocks > 1);
+       u32                     clock_rate;
+       ktime_t                 timeout;
 
        if (data->flags & MMC_DATA_READ)
                direction = DMA_FROM_DEVICE;
@@ -793,6 +788,14 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
        mmc_spi_setup_data_message(host, multiple, direction);
        t = &host->t;
 
+       if (t->speed_hz)
+               clock_rate = t->speed_hz;
+       else
+               clock_rate = spi->max_speed_hz;
+
+       timeout = ktime_add_ns(ktime_set(0, 0), data->timeout_ns +
+                       data->timeout_clks * 1000000 / clock_rate);
+
        /* Handle scatterlist segments one at a time, with synch for
         * each 512-byte block
         */
@@ -812,7 +815,7 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
                                        && dir == DMA_FROM_DEVICE)
                                dir = DMA_BIDIRECTIONAL;
 
-                       dma_addr = dma_map_page(dma_dev, sg->page, 0,
+                       dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
                                                PAGE_SIZE, dir);
                        if (direction == DMA_TO_DEVICE)
                                t->tx_dma = dma_addr + sg->offset;
@@ -821,7 +824,7 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
                }
 
                /* allow pio too; we don't allow highmem */
-               kmap_addr = kmap(sg->page);
+               kmap_addr = kmap(sg_page(sg));
                if (direction == DMA_TO_DEVICE)
                        t->tx_buf = kmap_addr + sg->offset;
                else
@@ -839,9 +842,9 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
                                t->len);
 
                        if (direction == DMA_TO_DEVICE)
-                               status = mmc_spi_writeblock(host, t);
+                               status = mmc_spi_writeblock(host, t, timeout);
                        else
-                               status = mmc_spi_readblock(host, t);
+                               status = mmc_spi_readblock(host, t, timeout);
                        if (status < 0)
                                break;
 
@@ -854,8 +857,8 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
 
                /* discard mappings */
                if (direction == DMA_FROM_DEVICE)
-                       flush_kernel_dcache_page(sg->page);
-               kunmap(sg->page);
+                       flush_kernel_dcache_page(sg_page(sg));
+               kunmap(sg_page(sg));
                if (dma_dev)
                        dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
 
@@ -904,8 +907,6 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
                                        DMA_BIDIRECTIONAL);
 
                tmp = spi_sync(spi, &host->m);
-               if (tmp == 0)
-                       tmp = host->m.status;
 
                if (host->dma_dev)
                        dma_sync_single_for_cpu(host->dma_dev,
@@ -926,7 +927,7 @@ mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
                        if (scratch->status[tmp] != 0)
                                return;
                }
-               tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
+               tmp = mmc_spi_wait_unbusy(host, timeout);
                if (tmp < 0 && !data->error)
                        data->error = tmp;
        }
@@ -1085,6 +1086,7 @@ static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
                 */
                if (canpower && ios->power_mode == MMC_POWER_OFF) {
                        int mres;
+                       u8 nullbyte = 0;
 
                        host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
                        mres = spi_setup(host->spi);
@@ -1092,7 +1094,7 @@ static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
                                dev_dbg(&host->spi->dev,
                                        "switch to SPI mode 0 failed\n");
 
-                       if (spi_w8r8(host->spi, 0x00) < 0)
+                       if (spi_write(host->spi, &nullbyte, 1) < 0)
                                dev_dbg(&host->spi->dev,
                                        "put spi signals to low failed\n");
 
@@ -1135,16 +1137,28 @@ static int mmc_spi_get_ro(struct mmc_host *mmc)
        struct mmc_spi_host *host = mmc_priv(mmc);
 
        if (host->pdata && host->pdata->get_ro)
-               return host->pdata->get_ro(mmc->parent);
-       /* board doesn't support read only detection; assume writeable */
-       return 0;
+               return !!host->pdata->get_ro(mmc->parent);
+       /*
+        * Board doesn't support read only detection; let the mmc core
+        * decide what to do.
+        */
+       return -ENOSYS;
 }
 
+static int mmc_spi_get_cd(struct mmc_host *mmc)
+{
+       struct mmc_spi_host *host = mmc_priv(mmc);
+
+       if (host->pdata && host->pdata->get_cd)
+               return !!host->pdata->get_cd(mmc->parent);
+       return -ENOSYS;
+}
 
 static const struct mmc_host_ops mmc_spi_ops = {
        .request        = mmc_spi_request,
        .set_ios        = mmc_spi_set_ios,
        .get_ro         = mmc_spi_get_ro,
+       .get_cd         = mmc_spi_get_cd,
 };
 
 
@@ -1164,6 +1178,23 @@ mmc_spi_detect_irq(int irq, void *mmc)
        return IRQ_HANDLED;
 }
 
+struct count_children {
+       unsigned        n;
+       struct bus_type *bus;
+};
+
+static int maybe_count_child(struct device *dev, void *c)
+{
+       struct count_children *ccp = c;
+
+       if (dev->bus == ccp->bus) {
+               if (ccp->n)
+                       return -EBUSY;
+               ccp->n++;
+       }
+       return 0;
+}
+
 static int mmc_spi_probe(struct spi_device *spi)
 {
        void                    *ones;
@@ -1187,33 +1218,30 @@ static int mmc_spi_probe(struct spi_device *spi)
                return status;
        }
 
-       /* We can use the bus safely iff nobody else will interfere with
-        * us.  That is, either we have the experimental exclusive access
-        * primitives ... or else there's nobody to share it with.
+       /* We can use the bus safely iff nobody else will interfere with us.
+        * Most commands consist of one SPI message to issue a command, then
+        * several more to collect its response, then possibly more for data
+        * transfer.  Clocking access to other devices during that period will
+        * corrupt the command execution.
+        *
+        * Until we have software primitives which guarantee non-interference,
+        * we'll aim for a hardware-level guarantee.
+        *
+        * REVISIT we can't guarantee another device won't be added later...
         */
        if (spi->master->num_chipselect > 1) {
-               struct device   *parent = spi->dev.parent;
+               struct count_children cc;
 
-               /* If there are multiple devices on this bus, we
-                * can't proceed.
-                */
-               spin_lock(&parent->klist_children.k_lock);
-               if (parent->klist_children.k_list.next
-                               != parent->klist_children.k_list.prev)
-                       status = -EMLINK;
-               else
-                       status = 0;
-               spin_unlock(&parent->klist_children.k_lock);
+               cc.n = 0;
+               cc.bus = spi->dev.bus;
+               status = device_for_each_child(spi->dev.parent, &cc,
+                               maybe_count_child);
                if (status < 0) {
                        dev_err(&spi->dev, "can't share SPI bus\n");
                        return status;
                }
 
-               /* REVISIT we can't guarantee another device won't
-                * be added later.  It's uncommon though ... for now,
-                * work as if this is safe.
-                */
-               dev_warn(&spi->dev, "ASSUMING unshared SPI bus!\n");
+               dev_warn(&spi->dev, "ASSUMING SPI bus stays unshared!\n");
        }
 
        /* We need a supply of ones to transmit.  This is the only time
@@ -1235,10 +1263,7 @@ static int mmc_spi_probe(struct spi_device *spi)
        mmc->ops = &mmc_spi_ops;
        mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
 
-       /* As long as we keep track of the number of successfully
-        * transmitted blocks, we're good for multiwrite.
-        */
-       mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
+       mmc->caps = MMC_CAP_SPI;
 
        /* SPI doesn't need the lowspeed device identification thing for
         * MMC or SD cards, since it never comes up in open drain mode.
@@ -1260,7 +1285,7 @@ static int mmc_spi_probe(struct spi_device *spi)
        /* Platform data is used to hook up things like card sensing
         * and power switching gpios.
         */
-       host->pdata = spi->dev.platform_data;
+       host->pdata = mmc_spi_get_pdata(spi);
        if (host->pdata)
                mmc->ocr_avail = host->pdata->ocr_mask;
        if (!mmc->ocr_avail) {
@@ -1280,8 +1305,8 @@ static int mmc_spi_probe(struct spi_device *spi)
        if (!host->data)
                goto fail_nobuf1;
 
-       if (spi->master->cdev.dev->dma_mask) {
-               struct device   *dev = spi->master->cdev.dev;
+       if (spi->master->dev.parent->dma_mask) {
+               struct device   *dev = spi->master->dev.parent;
 
                host->dma_dev = dev;
                host->ones_dma = dma_map_single(dev, ones,
@@ -1314,17 +1339,23 @@ static int mmc_spi_probe(struct spi_device *spi)
                        goto fail_glue_init;
        }
 
+       /* pass platform capabilities, if any */
+       if (host->pdata)
+               mmc->caps |= host->pdata->caps;
+
        status = mmc_add_host(mmc);
        if (status != 0)
                goto fail_add_host;
 
-       dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
-                       mmc->class_dev.bus_id,
+       dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",
+                       dev_name(&mmc->class_dev),
                        host->dma_dev ? "" : ", no DMA",
                        (host->pdata && host->pdata->get_ro)
                                ? "" : ", no WP",
                        (host->pdata && host->pdata->setpower)
-                               ? "" : ", no poweroff");
+                               ? "" : ", no poweroff",
+                       (mmc->caps & MMC_CAP_NEEDS_POLL)
+                               ? ", cd polling" : "");
        return 0;
 
 fail_add_host:
@@ -1337,6 +1368,7 @@ fail_glue_init:
 
 fail_nobuf1:
        mmc_free_host(mmc);
+       mmc_spi_put_pdata(spi);
        dev_set_drvdata(&spi->dev, NULL);
 
 nomem:
@@ -1371,6 +1403,7 @@ static int __devexit mmc_spi_remove(struct spi_device *spi)
 
                spi->max_speed_hz = mmc->f_max;
                mmc_free_host(mmc);
+               mmc_spi_put_pdata(spi);
                dev_set_drvdata(&spi->dev, NULL);
        }
        return 0;