]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/mmc/host/s3cmci.c
tree-wide: fix assorted typos all over the place
[linux-2.6.git] / drivers / mmc / host / s3cmci.c
index c6a4d3cadf2bd1e3731d9ea961728454ec064ef4..941a4d35ef8da44cdc627bee9dde0a06a9b0455e 100644 (file)
@@ -3,6 +3,9 @@
  *
  *  Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
  *
+ * Current driver maintained by Ben Dooks and Simtec Electronics
+ *  Copyright (C) 2008 Simtec Electronics <ben-linux@fluff.org>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
 #include <linux/clk.h>
 #include <linux/mmc/host.h>
 #include <linux/platform_device.h>
+#include <linux/cpufreq.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+#include <linux/gpio.h>
 #include <linux/irq.h>
 #include <linux/io.h>
 
-#include <asm/dma.h>
+#include <mach/dma.h>
+
+#include <mach/regs-sdi.h>
+#include <mach/regs-gpio.h>
 
-#include <asm/arch/regs-sdi.h>
-#include <asm/arch/regs-gpio.h>
+#include <plat/mci.h>
 
 #include "s3cmci.h"
 
@@ -37,9 +46,9 @@ enum dbg_channels {
        dbg_conf  = (1 << 8),
 };
 
-static const int dbgmap_err   = dbg_err | dbg_fail;
+static const int dbgmap_err   = dbg_fail;
 static const int dbgmap_info  = dbg_info | dbg_conf;
-static const int dbgmap_debug = dbg_debug;
+static const int dbgmap_debug = dbg_err | dbg_debug;
 
 #define dbg(host, channels, args...)             \
        do {                                      \
@@ -51,8 +60,6 @@ static const int dbgmap_debug = dbg_debug;
                dev_dbg(&host->pdev->dev, args);  \
        } while (0)
 
-#define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)
-
 static struct s3c2410_dma_client s3cmci_dma_client = {
        .name           = "s3c-mci",
 };
@@ -157,6 +164,40 @@ static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
 
 #endif /* CONFIG_MMC_DEBUG */
 
+/**
+ * s3cmci_host_usedma - return whether the host is using dma or pio
+ * @host: The host state
+ *
+ * Return true if the host is using DMA to transfer data, else false
+ * to use PIO mode. Will return static data depending on the driver
+ * configuration.
+ */
+static inline bool s3cmci_host_usedma(struct s3cmci_host *host)
+{
+#ifdef CONFIG_MMC_S3C_PIO
+       return false;
+#elif defined(CONFIG_MMC_S3C_DMA)
+       return true;
+#else
+       return host->dodma;
+#endif
+}
+
+/**
+ * s3cmci_host_canpio - return true if host has pio code available
+ *
+ * Return true if the driver has been compiled with the PIO support code
+ * available.
+ */
+static inline bool s3cmci_host_canpio(void)
+{
+#ifdef CONFIG_MMC_S3C_PIO
+       return true;
+#else
+       return false;
+#endif
+}
+
 static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
 {
        u32 newmask;
@@ -183,11 +224,37 @@ static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
 
 static inline void clear_imask(struct s3cmci_host *host)
 {
-       writel(0, host->base + host->sdiimsk);
+       u32 mask = readl(host->base + host->sdiimsk);
+
+       /* preserve the SDIO IRQ mask state */
+       mask &= S3C2410_SDIIMSK_SDIOIRQ;
+       writel(mask, host->base + host->sdiimsk);
+}
+
+/**
+ * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
+ * @host: The host to check.
+ *
+ * Test to see if the SDIO interrupt is being signalled in case the
+ * controller has failed to re-detect a card interrupt. Read GPE8 and
+ * see if it is low and if so, signal a SDIO interrupt.
+ *
+ * This is currently called if a request is finished (we assume that the
+ * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
+ * already being indicated.
+*/
+static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
+{
+       if (host->sdio_irqen) {
+               if (gpio_get_value(S3C2410_GPE(8)) == 0) {
+                       printk(KERN_DEBUG "%s: signalling irq\n", __func__);
+                       mmc_signal_sdio_irq(host->mmc);
+               }
+       }
 }
 
 static inline int get_data_buffer(struct s3cmci_host *host,
-                                 u32 *words, u32 **pointer)
+                                 u32 *bytes, u32 **pointer)
 {
        struct scatterlist *sg;
 
@@ -204,7 +271,7 @@ static inline int get_data_buffer(struct s3cmci_host *host,
        }
        sg = &host->mrq->data->sg[host->pio_sgptr];
 
-       *words = sg->length >> 2;
+       *bytes = sg->length;
        *pointer = sg_virt(sg);
 
        host->pio_sgptr++;
@@ -220,7 +287,7 @@ static inline u32 fifo_count(struct s3cmci_host *host)
        u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
 
        fifostat &= S3C2410_SDIFSTA_COUNTMASK;
-       return fifostat >> 2;
+       return fifostat;
 }
 
 static inline u32 fifo_free(struct s3cmci_host *host)
@@ -228,13 +295,73 @@ static inline u32 fifo_free(struct s3cmci_host *host)
        u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
 
        fifostat &= S3C2410_SDIFSTA_COUNTMASK;
-       return (63 - fifostat) >> 2;
+       return 63 - fifostat;
+}
+
+/**
+ * s3cmci_enable_irq - enable IRQ, after having disabled it.
+ * @host: The device state.
+ * @more: True if more IRQs are expected from transfer.
+ *
+ * Enable the main IRQ if needed after it has been disabled.
+ *
+ * The IRQ can be one of the following states:
+ *     - disabled during IDLE
+ *     - disabled whilst processing data
+ *     - enabled during transfer
+ *     - enabled whilst awaiting SDIO interrupt detection
+ */
+static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
+{
+       unsigned long flags;
+       bool enable = false;
+
+       local_irq_save(flags);
+
+       host->irq_enabled = more;
+       host->irq_disabled = false;
+
+       enable = more | host->sdio_irqen;
+
+       if (host->irq_state != enable) {
+               host->irq_state = enable;
+
+               if (enable)
+                       enable_irq(host->irq);
+               else
+                       disable_irq(host->irq);
+       }
+
+       local_irq_restore(flags);
+}
+
+/**
+ *
+ */
+static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
+{
+       unsigned long flags;
+
+       local_irq_save(flags);
+
+       //printk(KERN_DEBUG "%s: transfer %d\n", __func__, transfer);
+
+       host->irq_disabled = transfer;
+
+       if (transfer && host->irq_state) {
+               host->irq_state = false;
+               disable_irq(host->irq);
+       }
+
+       local_irq_restore(flags);
 }
 
 static void do_pio_read(struct s3cmci_host *host)
 {
        int res;
        u32 fifo;
+       u32 *ptr;
+       u32 fifo_words;
        void __iomem *from_ptr;
 
        /* write real prescaler to host, it might be set slow to fix */
@@ -243,8 +370,8 @@ static void do_pio_read(struct s3cmci_host *host)
        from_ptr = host->base + host->sdidata;
 
        while ((fifo = fifo_count(host))) {
-               if (!host->pio_words) {
-                       res = get_data_buffer(host, &host->pio_words,
+               if (!host->pio_bytes) {
+                       res = get_data_buffer(host, &host->pio_bytes,
                                              &host->pio_ptr);
                        if (res) {
                                host->pio_active = XFER_NONE;
@@ -257,26 +384,47 @@ static void do_pio_read(struct s3cmci_host *host)
 
                        dbg(host, dbg_pio,
                            "pio_read(): new target: [%i]@[%p]\n",
-                           host->pio_words, host->pio_ptr);
+                           host->pio_bytes, host->pio_ptr);
                }
 
                dbg(host, dbg_pio,
                    "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
-                   fifo, host->pio_words,
+                   fifo, host->pio_bytes,
                    readl(host->base + S3C2410_SDIDCNT));
 
-               if (fifo > host->pio_words)
-                       fifo = host->pio_words;
+               /* If we have reached the end of the block, we can
+                * read a word and get 1 to 3 bytes.  If we in the
+                * middle of the block, we have to read full words,
+                * otherwise we will write garbage, so round down to
+                * an even multiple of 4. */
+               if (fifo >= host->pio_bytes)
+                       fifo = host->pio_bytes;
+               else
+                       fifo -= fifo & 3;
 
-               host->pio_words -= fifo;
+               host->pio_bytes -= fifo;
                host->pio_count += fifo;
 
-               while (fifo--)
-                       *(host->pio_ptr++) = readl(from_ptr);
+               fifo_words = fifo >> 2;
+               ptr = host->pio_ptr;
+               while (fifo_words--)
+                       *ptr++ = readl(from_ptr);
+               host->pio_ptr = ptr;
+
+               if (fifo & 3) {
+                       u32 n = fifo & 3;
+                       u32 data = readl(from_ptr);
+                       u8 *p = (u8 *)host->pio_ptr;
+
+                       while (n--) {
+                               *p++ = data;
+                               data >>= 8;
+                       }
+               }
        }
 
-       if (!host->pio_words) {
-               res = get_data_buffer(host, &host->pio_words, &host->pio_ptr);
+       if (!host->pio_bytes) {
+               res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
                if (res) {
                        dbg(host, dbg_pio,
                            "pio_read(): complete (no more buffers).\n");
@@ -296,12 +444,13 @@ static void do_pio_write(struct s3cmci_host *host)
        void __iomem *to_ptr;
        int res;
        u32 fifo;
+       u32 *ptr;
 
        to_ptr = host->base + host->sdidata;
 
-       while ((fifo = fifo_free(host))) {
-               if (!host->pio_words) {
-                       res = get_data_buffer(host, &host->pio_words,
+       while ((fifo = fifo_free(host)) > 3) {
+               if (!host->pio_bytes) {
+                       res = get_data_buffer(host, &host->pio_bytes,
                                                        &host->pio_ptr);
                        if (res) {
                                dbg(host, dbg_pio,
@@ -313,18 +462,27 @@ static void do_pio_write(struct s3cmci_host *host)
 
                        dbg(host, dbg_pio,
                            "pio_write(): new source: [%i]@[%p]\n",
-                           host->pio_words, host->pio_ptr);
+                           host->pio_bytes, host->pio_ptr);
 
                }
 
-               if (fifo > host->pio_words)
-                       fifo = host->pio_words;
+               /* If we have reached the end of the block, we have to
+                * write exactly the remaining number of bytes.  If we
+                * in the middle of the block, we have to write full
+                * words, so round down to an even multiple of 4. */
+               if (fifo >= host->pio_bytes)
+                       fifo = host->pio_bytes;
+               else
+                       fifo -= fifo & 3;
 
-               host->pio_words -= fifo;
+               host->pio_bytes -= fifo;
                host->pio_count += fifo;
 
+               fifo = (fifo + 3) >> 2;
+               ptr = host->pio_ptr;
                while (fifo--)
-                       writel(*(host->pio_ptr++), to_ptr);
+                       writel(*ptr++, to_ptr);
+               host->pio_ptr = ptr;
        }
 
        enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
@@ -334,6 +492,7 @@ static void pio_tasklet(unsigned long data)
 {
        struct s3cmci_host *host = (struct s3cmci_host *) data;
 
+       s3cmci_disable_irq(host, true);
 
        if (host->pio_active == XFER_WRITE)
                do_pio_write(host);
@@ -345,16 +504,18 @@ static void pio_tasklet(unsigned long data)
                clear_imask(host);
                if (host->pio_active != XFER_NONE) {
                        dbg(host, dbg_err, "unfinished %s "
-                           "- pio_count:[%u] pio_words:[%u]\n",
+                           "- pio_count:[%u] pio_bytes:[%u]\n",
                            (host->pio_active == XFER_READ) ? "read" : "write",
-                           host->pio_count, host->pio_words);
+                           host->pio_count, host->pio_bytes);
 
-                       host->mrq->data->error = -EINVAL;
+                       if (host->mrq->data)
+                               host->mrq->data->error = -EINVAL;
                }
 
-               disable_irq(host->irq);
+               s3cmci_enable_irq(host, false);
                finalize_request(host);
-       }
+       } else
+               s3cmci_enable_irq(host, true);
 }
 
 /*
@@ -389,17 +550,27 @@ static irqreturn_t s3cmci_irq(int irq, void *dev_id)
        struct s3cmci_host *host = dev_id;
        struct mmc_command *cmd;
        u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
-       u32 mci_cclear, mci_dclear;
+       u32 mci_cclear = 0, mci_dclear;
        unsigned long iflags;
 
+       mci_dsta = readl(host->base + S3C2410_SDIDSTA);
+       mci_imsk = readl(host->base + host->sdiimsk);
+
+       if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
+               if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
+                       mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
+                       writel(mci_dclear, host->base + S3C2410_SDIDSTA);
+
+                       mmc_signal_sdio_irq(host->mmc);
+                       return IRQ_HANDLED;
+               }
+       }
+
        spin_lock_irqsave(&host->complete_lock, iflags);
 
        mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
-       mci_dsta = readl(host->base + S3C2410_SDIDSTA);
        mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
        mci_fsta = readl(host->base + S3C2410_SDIFSTA);
-       mci_imsk = readl(host->base + host->sdiimsk);
-       mci_cclear = 0;
        mci_dclear = 0;
 
        if ((host->complete_what == COMPLETION_NONE) ||
@@ -423,7 +594,7 @@ static irqreturn_t s3cmci_irq(int irq, void *dev_id)
                goto irq_out;
        }
 
-       if (!host->dodma) {
+       if (!s3cmci_host_usedma(host)) {
                if ((host->pio_active == XFER_WRITE) &&
                    (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
 
@@ -445,6 +616,7 @@ static irqreturn_t s3cmci_irq(int irq, void *dev_id)
        }
 
        if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
+               dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
                cmd->error = -ETIMEDOUT;
                host->status = "error: command timeout";
                goto fail_transfer;
@@ -461,9 +633,19 @@ static irqreturn_t s3cmci_irq(int irq, void *dev_id)
 
        if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
                if (cmd->flags & MMC_RSP_CRC) {
-                       cmd->error = -EILSEQ;
-                       host->status = "error: bad command crc";
-                       goto fail_transfer;
+                       if (host->mrq->cmd->flags & MMC_RSP_136) {
+                               dbg(host, dbg_irq,
+                                   "fixup: ignore CRC fail with long rsp\n");
+                       } else {
+                               /* note, we used to fail the transfer
+                                * here, but it seems that this is just
+                                * the hardware getting it wrong.
+                                *
+                                * cmd->error = -EILSEQ;
+                                * host->status = "error: bad command crc";
+                                * goto fail_transfer;
+                               */
+                       }
                }
 
                mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
@@ -490,12 +672,14 @@ static irqreturn_t s3cmci_irq(int irq, void *dev_id)
        /* Check for FIFO failure */
        if (host->is2440) {
                if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
+                       dbg(host, dbg_err, "FIFO failure\n");
                        host->mrq->data->error = -EILSEQ;
                        host->status = "error: 2440 fifo failure";
                        goto fail_transfer;
                }
        } else {
                if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
+                       dbg(host, dbg_err, "FIFO failure\n");
                        cmd->data->error = -EILSEQ;
                        host->status = "error:  fifo failure";
                        goto fail_transfer;
@@ -503,18 +687,21 @@ static irqreturn_t s3cmci_irq(int irq, void *dev_id)
        }
 
        if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
+               dbg(host, dbg_err, "bad data crc (outgoing)\n");
                cmd->data->error = -EILSEQ;
                host->status = "error: bad data crc (outgoing)";
                goto fail_transfer;
        }
 
        if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
+               dbg(host, dbg_err, "bad data crc (incoming)\n");
                cmd->data->error = -EILSEQ;
                host->status = "error: bad data crc (incoming)";
                goto fail_transfer;
        }
 
        if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
+               dbg(host, dbg_err, "data timeout\n");
                cmd->data->error = -ETIMEDOUT;
                host->status = "error: data timeout";
                goto fail_transfer;
@@ -569,13 +756,14 @@ static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
 
        dbg(host, dbg_irq, "card detect\n");
 
-       mmc_detect_change(host->mmc, 500);
+       mmc_detect_change(host->mmc, msecs_to_jiffies(500));
 
        return IRQ_HANDLED;
 }
 
-void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch, void *buf_id,
-                             int size, enum s3c2410_dma_buffresult result)
+static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
+                                    void *buf_id, int size,
+                                    enum s3c2410_dma_buffresult result)
 {
        struct s3cmci_host *host = buf_id;
        unsigned long iflags;
@@ -613,6 +801,7 @@ void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch, void *buf_id,
        dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
                size, mci_dsta, mci_dcnt);
 
+       host->dma_complete = 1;
        host->complete_what = COMPLETION_FINALIZE;
 
 out:
@@ -620,13 +809,12 @@ out:
        spin_unlock_irqrestore(&host->complete_lock, iflags);
        return;
 
-
 fail_request:
        host->mrq->data->error = -EINVAL;
        host->complete_what = COMPLETION_FINALIZE;
-       writel(0, host->base + host->sdiimsk);
-       goto out;
+       clear_imask(host);
 
+       goto out;
 }
 
 static void finalize_request(struct s3cmci_host *host)
@@ -643,8 +831,9 @@ static void finalize_request(struct s3cmci_host *host)
 
        if (cmd->data && (cmd->error == 0) &&
            (cmd->data->error == 0)) {
-               if (host->dodma && (!host->dma_complete)) {
-                       dbg(host, dbg_dma, "DMA Missing!\n");
+               if (s3cmci_host_usedma(host) && (!host->dma_complete)) {
+                       dbg(host, dbg_dma, "DMA Missing (%d)!\n",
+                           host->dma_complete);
                        return;
                }
        }
@@ -667,9 +856,9 @@ static void finalize_request(struct s3cmci_host *host)
 
        /* Cleanup controller */
        writel(0, host->base + S3C2410_SDICMDARG);
-       writel(0, host->base + S3C2410_SDIDCON);
+       writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
        writel(0, host->base + S3C2410_SDICMDCON);
-       writel(0, host->base + host->sdiimsk);
+       clear_imask(host);
 
        if (cmd->data && cmd->error)
                cmd->data->error = cmd->error;
@@ -695,7 +884,7 @@ static void finalize_request(struct s3cmci_host *host)
        /* If we had an error while transfering data we flush the
         * DMA channel and the fifo to clear out any garbage. */
        if (mrq->data->error != 0) {
-               if (host->dodma)
+               if (s3cmci_host_usedma(host))
                        s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
 
                if (host->is2440) {
@@ -717,11 +906,13 @@ static void finalize_request(struct s3cmci_host *host)
 request_done:
        host->complete_what = COMPLETION_NONE;
        host->mrq = NULL;
+
+       s3cmci_check_sdio_irq(host);
        mmc_request_done(host->mmc, mrq);
 }
 
-
-void s3cmci_dma_setup(struct s3cmci_host *host, enum s3c2410_dmasrc source)
+static void s3cmci_dma_setup(struct s3cmci_host *host,
+                            enum s3c2410_dmasrc source)
 {
        static enum s3c2410_dmasrc last_source = -1;
        static int setup_ok;
@@ -731,12 +922,11 @@ void s3cmci_dma_setup(struct s3cmci_host *host, enum s3c2410_dmasrc source)
 
        last_source = source;
 
-       s3c2410_dma_devconfig(host->dma, source, 3,
+       s3c2410_dma_devconfig(host->dma, source,
                              host->mem->start + host->sdidata);
 
        if (!setup_ok) {
-               s3c2410_dma_config(host->dma, 4,
-                       (S3C2410_DCON_HWTRIG | S3C2410_DCON_CH0_SDI));
+               s3c2410_dma_config(host->dma, 4);
                s3c2410_dma_set_buffdone_fn(host->dma,
                                            s3cmci_dma_done_callback);
                s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
@@ -787,13 +977,23 @@ static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
                return 0;
        }
 
+       if ((data->blksz & 3) != 0) {
+               /* We cannot deal with unaligned blocks with more than
+                * one block being transfered. */
+
+               if (data->blocks > 1) {
+                       pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
+                       return -EINVAL;
+               }
+       }
+
        while (readl(host->base + S3C2410_SDIDSTA) &
               (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
 
                dbg(host, dbg_err,
                    "mci_setup_data() transfer stillin progress.\n");
 
-               writel(0, host->base + S3C2410_SDIDCON);
+               writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
                s3cmci_reset(host);
 
                if ((stoptries--) == 0) {
@@ -804,7 +1004,7 @@ static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
 
        dcon  = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
 
-       if (host->dodma)
+       if (s3cmci_host_usedma(host))
                dcon |= S3C2410_SDIDCON_DMAEN;
 
        if (host->bus_width == MMC_BUS_WIDTH_4)
@@ -864,7 +1064,7 @@ static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
        BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
 
        host->pio_sgptr = 0;
-       host->pio_words = 0;
+       host->pio_bytes = 0;
        host->pio_count = 0;
        host->pio_active = rw ? XFER_WRITE : XFER_READ;
 
@@ -882,7 +1082,7 @@ static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
 static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
 {
        int dma_len, i;
-       int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
+       int rw = data->flags & MMC_DATA_WRITE;
 
        BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
 
@@ -890,7 +1090,7 @@ static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
        s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
 
        dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
-                            (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
+                            rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
 
        if (dma_len == 0)
                return -ENOMEM;
@@ -901,11 +1101,11 @@ static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
        for (i = 0; i < dma_len; i++) {
                int res;
 
-               dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
-                       sg_dma_address(&data->sg[i]),
-                       sg_dma_len(&data->sg[i]));
+               dbg(host, dbg_dma, "enqueue %i: %08x@%u\n", i,
+                   sg_dma_address(&data->sg[i]),
+                   sg_dma_len(&data->sg[i]));
 
-               res = s3c2410_dma_enqueue(host->dma, (void *) host,
+               res = s3c2410_dma_enqueue(host->dma, host,
                                          sg_dma_address(&data->sg[i]),
                                          sg_dma_len(&data->sg[i]));
 
@@ -942,19 +1142,21 @@ static void s3cmci_send_request(struct mmc_host *mmc)
                host->dcnt++;
 
                if (res) {
-                       cmd->error = -EINVAL;
-                       cmd->data->error = -EINVAL;
+                       dbg(host, dbg_err, "setup data error %d\n", res);
+                       cmd->error = res;
+                       cmd->data->error = res;
 
                        mmc_request_done(mmc, mrq);
                        return;
                }
 
-               if (host->dodma)
+               if (s3cmci_host_usedma(host))
                        res = s3cmci_prepare_dma(host, cmd->data);
                else
                        res = s3cmci_prepare_pio(host, cmd->data);
 
                if (res) {
+                       dbg(host, dbg_err, "data prepare error %d\n", res);
                        cmd->error = res;
                        cmd->data->error = res;
 
@@ -967,7 +1169,20 @@ static void s3cmci_send_request(struct mmc_host *mmc)
        s3cmci_send_command(host, cmd);
 
        /* Enable Interrupt */
-       enable_irq(host->irq);
+       s3cmci_enable_irq(host, true);
+}
+
+static int s3cmci_card_present(struct mmc_host *mmc)
+{
+       struct s3cmci_host *host = mmc_priv(mmc);
+       struct s3c24xx_mci_pdata *pdata = host->pdata;
+       int ret;
+
+       if (pdata->gpio_detect == 0)
+               return -ENOSYS;
+
+       ret = gpio_get_value(pdata->gpio_detect) ? 0 : 1;
+       return ret ^ pdata->detect_invert;
 }
 
 static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
@@ -978,13 +1193,41 @@ static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
        host->cmd_is_stop = 0;
        host->mrq = mrq;
 
-       s3cmci_send_request(mmc);
+       if (s3cmci_card_present(mmc) == 0) {
+               dbg(host, dbg_err, "%s: no medium present\n", __func__);
+               host->mrq->cmd->error = -ENOMEDIUM;
+               mmc_request_done(mmc, mrq);
+       } else
+               s3cmci_send_request(mmc);
+}
+
+static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
+{
+       u32 mci_psc;
+
+       /* Set clock */
+       for (mci_psc = 0; mci_psc < 255; mci_psc++) {
+               host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
+
+               if (host->real_rate <= ios->clock)
+                       break;
+       }
+
+       if (mci_psc > 255)
+               mci_psc = 255;
+
+       host->prescaler = mci_psc;
+       writel(host->prescaler, host->base + S3C2410_SDIPRE);
+
+       /* If requested clock is 0, real_rate will be 0, too */
+       if (ios->clock == 0)
+               host->real_rate = 0;
 }
 
 static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 {
        struct s3cmci_host *host = mmc_priv(mmc);
-       u32 mci_psc, mci_con;
+       u32 mci_con;
 
        /* Set the power state */
 
@@ -993,12 +1236,15 @@ static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
        switch (ios->power_mode) {
        case MMC_POWER_ON:
        case MMC_POWER_UP:
-               s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
-               s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
-               s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
-               s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
-               s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
-               s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
+               s3c2410_gpio_cfgpin(S3C2410_GPE(5), S3C2410_GPE5_SDCLK);
+               s3c2410_gpio_cfgpin(S3C2410_GPE(6), S3C2410_GPE6_SDCMD);
+               s3c2410_gpio_cfgpin(S3C2410_GPE(7), S3C2410_GPE7_SDDAT0);
+               s3c2410_gpio_cfgpin(S3C2410_GPE(8), S3C2410_GPE8_SDDAT1);
+               s3c2410_gpio_cfgpin(S3C2410_GPE(9), S3C2410_GPE9_SDDAT2);
+               s3c2410_gpio_cfgpin(S3C2410_GPE(10), S3C2410_GPE10_SDDAT3);
+
+               if (host->pdata->set_power)
+                       host->pdata->set_power(ios->power_mode, ios->vdd);
 
                if (!host->is2440)
                        mci_con |= S3C2410_SDICON_FIFORESET;
@@ -1007,32 +1253,18 @@ static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 
        case MMC_POWER_OFF:
        default:
-               s3c2410_gpio_setpin(S3C2410_GPE5, 0);
-               s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);
+               gpio_direction_output(S3C2410_GPE(5), 0);
 
                if (host->is2440)
                        mci_con |= S3C2440_SDICON_SDRESET;
 
-               break;
-       }
-
-       /* Set clock */
-       for (mci_psc = 0; mci_psc < 255; mci_psc++) {
-               host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
+               if (host->pdata->set_power)
+                       host->pdata->set_power(ios->power_mode, ios->vdd);
 
-               if (host->real_rate <= ios->clock)
-                       break;
+               break;
        }
 
-       if (mci_psc > 255)
-               mci_psc = 255;
-
-       host->prescaler = mci_psc;
-       writel(host->prescaler, host->base + S3C2410_SDIPRE);
-
-       /* If requested clock is 0, real_rate will be 0, too */
-       if (ios->clock == 0)
-               host->real_rate = 0;
+       s3cmci_set_clk(host, ios);
 
        /* Set CLOCK_ENABLE */
        if (ios->clock)
@@ -1061,16 +1293,265 @@ static void s3cmci_reset(struct s3cmci_host *host)
        writel(con, host->base + S3C2410_SDICON);
 }
 
+static int s3cmci_get_ro(struct mmc_host *mmc)
+{
+       struct s3cmci_host *host = mmc_priv(mmc);
+       struct s3c24xx_mci_pdata *pdata = host->pdata;
+       int ret;
+
+       if (pdata->no_wprotect)
+               return 0;
+
+       ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
+
+       if (pdata->wprotect_invert)
+               ret = !ret;
+
+       return ret;
+}
+
+static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
+{
+       struct s3cmci_host *host = mmc_priv(mmc);
+       unsigned long flags;
+       u32 con;
+
+       local_irq_save(flags);
+
+       con = readl(host->base + S3C2410_SDICON);
+       host->sdio_irqen = enable;
+
+       if (enable == host->sdio_irqen)
+               goto same_state;
+
+       if (enable) {
+               con |= S3C2410_SDICON_SDIOIRQ;
+               enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
+
+               if (!host->irq_state && !host->irq_disabled) {
+                       host->irq_state = true;
+                       enable_irq(host->irq);
+               }
+       } else {
+               disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
+               con &= ~S3C2410_SDICON_SDIOIRQ;
+
+               if (!host->irq_enabled && host->irq_state) {
+                       disable_irq_nosync(host->irq);
+                       host->irq_state = false;
+               }
+       }
+
+       writel(con, host->base + S3C2410_SDICON);
+
+ same_state:
+       local_irq_restore(flags);
+
+       s3cmci_check_sdio_irq(host);
+}
+
 static struct mmc_host_ops s3cmci_ops = {
        .request        = s3cmci_request,
        .set_ios        = s3cmci_set_ios,
+       .get_ro         = s3cmci_get_ro,
+       .get_cd         = s3cmci_card_present,
+       .enable_sdio_irq = s3cmci_enable_sdio_irq,
+};
+
+static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
+       /* This is currently here to avoid a number of if (host->pdata)
+        * checks. Any zero fields to ensure reasonable defaults are picked. */
+};
+
+#ifdef CONFIG_CPU_FREQ
+
+static int s3cmci_cpufreq_transition(struct notifier_block *nb,
+                                    unsigned long val, void *data)
+{
+       struct s3cmci_host *host;
+       struct mmc_host *mmc;
+       unsigned long newclk;
+       unsigned long flags;
+
+       host = container_of(nb, struct s3cmci_host, freq_transition);
+       newclk = clk_get_rate(host->clk);
+       mmc = host->mmc;
+
+       if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
+           (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
+               spin_lock_irqsave(&mmc->lock, flags);
+
+               host->clk_rate = newclk;
+
+               if (mmc->ios.power_mode != MMC_POWER_OFF &&
+                   mmc->ios.clock != 0)
+                       s3cmci_set_clk(host, &mmc->ios);
+
+               spin_unlock_irqrestore(&mmc->lock, flags);
+       }
+
+       return 0;
+}
+
+static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
+{
+       host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
+
+       return cpufreq_register_notifier(&host->freq_transition,
+                                        CPUFREQ_TRANSITION_NOTIFIER);
+}
+
+static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
+{
+       cpufreq_unregister_notifier(&host->freq_transition,
+                                   CPUFREQ_TRANSITION_NOTIFIER);
+}
+
+#else
+static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
+{
+       return 0;
+}
+
+static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
+{
+}
+#endif
+
+
+#ifdef CONFIG_DEBUG_FS
+
+static int s3cmci_state_show(struct seq_file *seq, void *v)
+{
+       struct s3cmci_host *host = seq->private;
+
+       seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
+       seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
+       seq_printf(seq, "Prescale = %d\n", host->prescaler);
+       seq_printf(seq, "is2440 = %d\n", host->is2440);
+       seq_printf(seq, "IRQ = %d\n", host->irq);
+       seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
+       seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
+       seq_printf(seq, "IRQ state = %d\n", host->irq_state);
+       seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
+       seq_printf(seq, "Do DMA = %d\n", s3cmci_host_usedma(host));
+       seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
+       seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
+
+       return 0;
+}
+
+static int s3cmci_state_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, s3cmci_state_show, inode->i_private);
+}
+
+static const struct file_operations s3cmci_fops_state = {
+       .owner          = THIS_MODULE,
+       .open           = s3cmci_state_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
+#define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
+
+struct s3cmci_reg {
+       unsigned short  addr;
+       unsigned char   *name;
+} debug_regs[] = {
+       DBG_REG(CON),
+       DBG_REG(PRE),
+       DBG_REG(CMDARG),
+       DBG_REG(CMDCON),
+       DBG_REG(CMDSTAT),
+       DBG_REG(RSP0),
+       DBG_REG(RSP1),
+       DBG_REG(RSP2),
+       DBG_REG(RSP3),
+       DBG_REG(TIMER),
+       DBG_REG(BSIZE),
+       DBG_REG(DCON),
+       DBG_REG(DCNT),
+       DBG_REG(DSTA),
+       DBG_REG(FSTA),
+       {}
+};
+
+static int s3cmci_regs_show(struct seq_file *seq, void *v)
+{
+       struct s3cmci_host *host = seq->private;
+       struct s3cmci_reg *rptr = debug_regs;
+
+       for (; rptr->name; rptr++)
+               seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
+                          readl(host->base + rptr->addr));
+
+       seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
+
+       return 0;
+}
+
+static int s3cmci_regs_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, s3cmci_regs_show, inode->i_private);
+}
+
+static const struct file_operations s3cmci_fops_regs = {
+       .owner          = THIS_MODULE,
+       .open           = s3cmci_regs_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
 };
 
-static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
+static void s3cmci_debugfs_attach(struct s3cmci_host *host)
+{
+       struct device *dev = &host->pdev->dev;
+
+       host->debug_root = debugfs_create_dir(dev_name(dev), NULL);
+       if (IS_ERR(host->debug_root)) {
+               dev_err(dev, "failed to create debugfs root\n");
+               return;
+       }
+
+       host->debug_state = debugfs_create_file("state", 0444,
+                                               host->debug_root, host,
+                                               &s3cmci_fops_state);
+
+       if (IS_ERR(host->debug_state))
+               dev_err(dev, "failed to create debug state file\n");
+
+       host->debug_regs = debugfs_create_file("regs", 0444,
+                                              host->debug_root, host,
+                                              &s3cmci_fops_regs);
+
+       if (IS_ERR(host->debug_regs))
+               dev_err(dev, "failed to create debug regs file\n");
+}
+
+static void s3cmci_debugfs_remove(struct s3cmci_host *host)
+{
+       debugfs_remove(host->debug_regs);
+       debugfs_remove(host->debug_state);
+       debugfs_remove(host->debug_root);
+}
+
+#else
+static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
+static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
+
+#endif /* CONFIG_DEBUG_FS */
+
+static int __devinit s3cmci_probe(struct platform_device *pdev)
 {
        struct s3cmci_host *host;
        struct mmc_host *mmc;
        int ret;
+       int is2440;
+       int i;
+
+       is2440 = platform_get_device_id(pdev)->driver_data;
 
        mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
        if (!mmc) {
@@ -1078,11 +1559,29 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
                goto probe_out;
        }
 
+       for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
+               ret = gpio_request(i, dev_name(&pdev->dev));
+               if (ret) {
+                       dev_err(&pdev->dev, "failed to get gpio %d\n", i);
+
+                       for (i--; i >= S3C2410_GPE(5); i--)
+                               gpio_free(i);
+
+                       goto probe_free_host;
+               }
+       }
+
        host = mmc_priv(mmc);
        host->mmc       = mmc;
        host->pdev      = pdev;
        host->is2440    = is2440;
 
+       host->pdata = pdev->dev.platform_data;
+       if (!host->pdata) {
+               pdev->dev.platform_data = &s3cmci_def_pdata;
+               host->pdata = &s3cmci_def_pdata;
+       }
+
        spin_lock_init(&host->complete_lock);
        tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
 
@@ -1096,12 +1595,12 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
                host->clk_div   = 2;
        }
 
-       host->dodma             = 0;
        host->complete_what     = COMPLETION_NONE;
        host->pio_active        = XFER_NONE;
 
-       host->dma               = S3CMCI_DMA;
-       host->irq_cd            = IRQ_EINT2;
+#ifdef CONFIG_MMC_S3C_PIODMA
+       host->dodma             = host->pdata->dma;
+#endif
 
        host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!host->mem) {
@@ -1109,20 +1608,20 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
                        "failed to get io memory region resouce.\n");
 
                ret = -ENOENT;
-               goto probe_free_host;
+               goto probe_free_gpio;
        }
 
        host->mem = request_mem_region(host->mem->start,
-                                      RESSIZE(host->mem), pdev->name);
+                                      resource_size(host->mem), pdev->name);
 
        if (!host->mem) {
                dev_err(&pdev->dev, "failed to request io memory region.\n");
                ret = -ENOENT;
-               goto probe_free_host;
+               goto probe_free_gpio;
        }
 
-       host->base = ioremap(host->mem->start, RESSIZE(host->mem));
-       if (host->base == 0) {
+       host->base = ioremap(host->mem->start, resource_size(host->mem));
+       if (!host->base) {
                dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
                ret = -EINVAL;
                goto probe_free_mem_region;
@@ -1146,21 +1645,60 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
         * ensure we don't lock the system with un-serviceable requests. */
 
        disable_irq(host->irq);
+       host->irq_state = false;
 
-       s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);
-       set_irq_type(host->irq_cd, IRQT_BOTHEDGE);
+       if (!host->pdata->no_detect) {
+               ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");
+               if (ret) {
+                       dev_err(&pdev->dev, "failed to get detect gpio\n");
+                       goto probe_free_irq;
+               }
 
-       if (request_irq(host->irq_cd, s3cmci_irq_cd, 0, DRIVER_NAME, host)) {
-               dev_err(&pdev->dev,
-                       "failed to request card detect interrupt.\n");
-               ret = -ENOENT;
-               goto probe_free_irq;
+               host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
+
+               if (host->irq_cd >= 0) {
+                       if (request_irq(host->irq_cd, s3cmci_irq_cd,
+                                       IRQF_TRIGGER_RISING |
+                                       IRQF_TRIGGER_FALLING,
+                                       DRIVER_NAME, host)) {
+                               dev_err(&pdev->dev,
+                                       "can't get card detect irq.\n");
+                               ret = -ENOENT;
+                               goto probe_free_gpio_cd;
+                       }
+               } else {
+                       dev_warn(&pdev->dev,
+                                "host detect has no irq available\n");
+                       gpio_direction_input(host->pdata->gpio_detect);
+               }
+       } else
+               host->irq_cd = -1;
+
+       if (!host->pdata->no_wprotect) {
+               ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");
+               if (ret) {
+                       dev_err(&pdev->dev, "failed to get writeprotect\n");
+                       goto probe_free_irq_cd;
+               }
+
+               gpio_direction_input(host->pdata->gpio_wprotect);
        }
 
-       if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL)) {
-               dev_err(&pdev->dev, "unable to get DMA channel.\n");
-               ret = -EBUSY;
-               goto probe_free_irq_cd;
+       /* depending on the dma state, get a dma channel to use. */
+
+       if (s3cmci_host_usedma(host)) {
+               host->dma = s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client,
+                                               host);
+               if (host->dma < 0) {
+                       dev_err(&pdev->dev, "cannot get DMA channel.\n");
+                       if (!s3cmci_host_canpio()) {
+                               ret = -EBUSY;
+                               goto probe_free_gpio_wp;
+                       } else {
+                               dev_warn(&pdev->dev, "falling back to PIO.\n");
+                               host->dodma = 0;
+                       }
+               }
        }
 
        host->clk = clk_get(&pdev->dev, "sdi");
@@ -1168,7 +1706,7 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
                dev_err(&pdev->dev, "failed to find clock source.\n");
                ret = PTR_ERR(host->clk);
                host->clk = NULL;
-               goto probe_free_host;
+               goto probe_free_dma;
        }
 
        ret = clk_enable(host->clk);
@@ -1180,11 +1718,18 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
        host->clk_rate = clk_get_rate(host->clk);
 
        mmc->ops        = &s3cmci_ops;
-       mmc->ocr_avail  = MMC_VDD_32_33;
+       mmc->ocr_avail  = MMC_VDD_32_33 | MMC_VDD_33_34;
+#ifdef CONFIG_MMC_S3C_HW_SDIO_IRQ
+       mmc->caps       = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
+#else
        mmc->caps       = MMC_CAP_4_BIT_DATA;
+#endif
        mmc->f_min      = host->clk_rate / (host->clk_div * 256);
        mmc->f_max      = host->clk_rate / host->clk_div;
 
+       if (host->pdata->ocr_avail)
+               mmc->ocr_avail = host->pdata->ocr_avail;
+
        mmc->max_blk_count      = 4095;
        mmc->max_blk_size       = 4095;
        mmc->max_req_size       = 4095 * 512;
@@ -1198,25 +1743,51 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
            (host->is2440?"2440":""),
            host->base, host->irq, host->irq_cd, host->dma);
 
+       ret = s3cmci_cpufreq_register(host);
+       if (ret) {
+               dev_err(&pdev->dev, "failed to register cpufreq\n");
+               goto free_dmabuf;
+       }
+
        ret = mmc_add_host(mmc);
        if (ret) {
                dev_err(&pdev->dev, "failed to add mmc host.\n");
-               goto free_dmabuf;
+               goto free_cpufreq;
        }
 
+       s3cmci_debugfs_attach(host);
+
        platform_set_drvdata(pdev, mmc);
-       dev_info(&pdev->dev, "initialisation done.\n");
+       dev_info(&pdev->dev, "%s - using %s, %s SDIO IRQ\n", mmc_hostname(mmc),
+                s3cmci_host_usedma(host) ? "dma" : "pio",
+                mmc->caps & MMC_CAP_SDIO_IRQ ? "hw" : "sw");
 
        return 0;
 
+ free_cpufreq:
+       s3cmci_cpufreq_deregister(host);
+
  free_dmabuf:
        clk_disable(host->clk);
 
  clk_free:
        clk_put(host->clk);
 
+ probe_free_dma:
+       if (s3cmci_host_usedma(host))
+               s3c2410_dma_free(host->dma, &s3cmci_dma_client);
+
+ probe_free_gpio_wp:
+       if (!host->pdata->no_wprotect)
+               gpio_free(host->pdata->gpio_wprotect);
+
+ probe_free_gpio_cd:
+       if (!host->pdata->no_detect)
+               gpio_free(host->pdata->gpio_detect);
+
  probe_free_irq_cd:
-       free_irq(host->irq_cd, host);
+       if (host->irq_cd >= 0)
+               free_irq(host->irq_cd, host);
 
  probe_free_irq:
        free_irq(host->irq, host);
@@ -1225,114 +1796,133 @@ static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
        iounmap(host->base);
 
  probe_free_mem_region:
-       release_mem_region(host->mem->start, RESSIZE(host->mem));
+       release_mem_region(host->mem->start, resource_size(host->mem));
+
+ probe_free_gpio:
+       for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
+               gpio_free(i);
 
  probe_free_host:
        mmc_free_host(mmc);
+
  probe_out:
        return ret;
 }
 
+static void s3cmci_shutdown(struct platform_device *pdev)
+{
+       struct mmc_host *mmc = platform_get_drvdata(pdev);
+       struct s3cmci_host *host = mmc_priv(mmc);
+
+       if (host->irq_cd >= 0)
+               free_irq(host->irq_cd, host);
+
+       s3cmci_debugfs_remove(host);
+       s3cmci_cpufreq_deregister(host);
+       mmc_remove_host(mmc);
+       clk_disable(host->clk);
+}
+
 static int __devexit s3cmci_remove(struct platform_device *pdev)
 {
        struct mmc_host         *mmc  = platform_get_drvdata(pdev);
        struct s3cmci_host      *host = mmc_priv(mmc);
+       struct s3c24xx_mci_pdata *pd = host->pdata;
+       int i;
 
-       mmc_remove_host(mmc);
+       s3cmci_shutdown(pdev);
 
-       clk_disable(host->clk);
        clk_put(host->clk);
 
        tasklet_disable(&host->pio_tasklet);
 
-       free_irq(host->irq_cd, host);
+       if (s3cmci_host_usedma(host))
+               s3c2410_dma_free(host->dma, &s3cmci_dma_client);
+
        free_irq(host->irq, host);
 
+       if (!pd->no_wprotect)
+               gpio_free(pd->gpio_wprotect);
+
+       if (!pd->no_detect)
+               gpio_free(pd->gpio_detect);
+
+       for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
+               gpio_free(i);
+
+
        iounmap(host->base);
-       release_mem_region(host->mem->start, RESSIZE(host->mem));
+       release_mem_region(host->mem->start, resource_size(host->mem));
 
        mmc_free_host(mmc);
        return 0;
 }
 
-static int __devinit s3cmci_probe_2410(struct platform_device *dev)
-{
-       return s3cmci_probe(dev, 0);
-}
+static struct platform_device_id s3cmci_driver_ids[] = {
+       {
+               .name   = "s3c2410-sdi",
+               .driver_data    = 0,
+       }, {
+               .name   = "s3c2412-sdi",
+               .driver_data    = 1,
+       }, {
+               .name   = "s3c2440-sdi",
+               .driver_data    = 1,
+       },
+       { }
+};
 
-static int __devinit s3cmci_probe_2412(struct platform_device *dev)
-{
-       return s3cmci_probe(dev, 1);
-}
+MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
 
-static int __devinit s3cmci_probe_2440(struct platform_device *dev)
-{
-       return s3cmci_probe(dev, 1);
-}
 
 #ifdef CONFIG_PM
 
-static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
+static int s3cmci_suspend(struct device *dev)
 {
-       struct mmc_host *mmc = platform_get_drvdata(dev);
+       struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
+       struct pm_message event = { PM_EVENT_SUSPEND };
 
-       return  mmc_suspend_host(mmc, state);
+       return mmc_suspend_host(mmc, event);
 }
 
-static int s3cmci_resume(struct platform_device *dev)
+static int s3cmci_resume(struct device *dev)
 {
-       struct mmc_host *mmc = platform_get_drvdata(dev);
+       struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
 
        return mmc_resume_host(mmc);
 }
 
-#else /* CONFIG_PM */
-#define s3cmci_suspend NULL
-#define s3cmci_resume NULL
-#endif /* CONFIG_PM */
-
-
-static struct platform_driver s3cmci_driver_2410 = {
-       .driver.name    = "s3c2410-sdi",
-       .driver.owner   = THIS_MODULE,
-       .probe          = s3cmci_probe_2410,
-       .remove         = __devexit_p(s3cmci_remove),
+static struct dev_pm_ops s3cmci_pm = {
        .suspend        = s3cmci_suspend,
        .resume         = s3cmci_resume,
 };
 
-static struct platform_driver s3cmci_driver_2412 = {
-       .driver.name    = "s3c2412-sdi",
-       .driver.owner   = THIS_MODULE,
-       .probe          = s3cmci_probe_2412,
-       .remove         = __devexit_p(s3cmci_remove),
-       .suspend        = s3cmci_suspend,
-       .resume         = s3cmci_resume,
-};
+#define s3cmci_pm_ops &s3cmci_pm
+#else /* CONFIG_PM */
+#define s3cmci_pm_ops NULL
+#endif /* CONFIG_PM */
+
 
-static struct platform_driver s3cmci_driver_2440 = {
-       .driver.name    = "s3c2440-sdi",
-       .driver.owner   = THIS_MODULE,
-       .probe          = s3cmci_probe_2440,
+static struct platform_driver s3cmci_driver = {
+       .driver = {
+               .name   = "s3c-sdi",
+               .owner  = THIS_MODULE,
+               .pm     = s3cmci_pm_ops,
+       },
+       .id_table       = s3cmci_driver_ids,
+       .probe          = s3cmci_probe,
        .remove         = __devexit_p(s3cmci_remove),
-       .suspend        = s3cmci_suspend,
-       .resume         = s3cmci_resume,
+       .shutdown       = s3cmci_shutdown,
 };
 
-
 static int __init s3cmci_init(void)
 {
-       platform_driver_register(&s3cmci_driver_2410);
-       platform_driver_register(&s3cmci_driver_2412);
-       platform_driver_register(&s3cmci_driver_2440);
-       return 0;
+       return platform_driver_register(&s3cmci_driver);
 }
 
 static void __exit s3cmci_exit(void)
 {
-       platform_driver_unregister(&s3cmci_driver_2410);
-       platform_driver_unregister(&s3cmci_driver_2412);
-       platform_driver_unregister(&s3cmci_driver_2440);
+       platform_driver_unregister(&s3cmci_driver);
 }
 
 module_init(s3cmci_init);
@@ -1340,4 +1930,4 @@ module_exit(s3cmci_exit);
 
 MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
 MODULE_LICENSE("GPL v2");
-MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>");
+MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");