Merge branch '2020-06-15-misc-bugfixes'
[oweals/u-boot.git] / drivers / mmc / mxcmmc.c
index ab1fc82fbb68dd3f0d6f90f55b3be916fd8eac56..269882b2027decfa146ae181c6407c2f7097e6ba 100644 (file)
 #include <part.h>
 #include <malloc.h>
 #include <mmc.h>
-#include <asm/errno.h>
+#include <time.h>
+#include <linux/errno.h>
 #include <asm/io.h>
-#ifdef CONFIG_MX27
 #include <asm/arch/clock.h>
-#endif
 
 #define DRIVER_NAME "mxc-mmc"
 
@@ -124,6 +123,8 @@ struct mxcmci_host {
 };
 
 static struct mxcmci_host mxcmci_host;
+
+/* maintainer note: do we really want to have a global host pointer? */
 static struct mxcmci_host *host = &mxcmci_host;
 
 static inline int mxcmci_use_dma(struct mxcmci_host *host)
@@ -211,11 +212,11 @@ static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
                } else if (stat & STATUS_CRC_WRITE_ERR) {
                        u32 err_code = (stat >> 9) & 0x3;
                        if (err_code == 2) /* No CRC response */
-                               data_error = TIMEOUT;
+                               data_error = -ETIMEDOUT;
                        else
                                data_error = -EILSEQ;
                } else if (stat & STATUS_TIME_OUT_READ) {
-                       data_error = TIMEOUT;
+                       data_error = -ETIMEDOUT;
                } else {
                        data_error = -EIO;
                }
@@ -238,7 +239,7 @@ static int mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
 
        if (stat & STATUS_TIME_OUT_RESP) {
                printf("CMD TIMEOUT\n");
-               return TIMEOUT;
+               return -ETIMEDOUT;
        } else if (stat & STATUS_RESP_CRC_ERR && cmd->resp_type & MMC_RSP_CRC) {
                printf("cmd crc error\n");
                return -EILSEQ;
@@ -422,7 +423,7 @@ static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
 {
        unsigned int divider;
        int prescaler = 0;
-       unsigned long clk_in = imx_get_perclk2();
+       unsigned long clk_in = mxc_get_clock(MXC_ESDHC_CLK);
 
        while (prescaler <= 0x800) {
                for (divider = 1; divider <= 0xF; divider++) {
@@ -448,7 +449,7 @@ static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
        writel((prescaler << 4) | divider, &host->base->clk_rate);
 }
 
-static void mxcmci_set_ios(struct mmc *mmc)
+static int mxcmci_set_ios(struct mmc *mmc)
 {
        struct mxcmci_host *host = mmc->priv;
        if (mmc->bus_width == 4)
@@ -464,6 +465,8 @@ static void mxcmci_set_ios(struct mmc *mmc)
        }
 
        host->clock = mmc->clock;
+
+       return 0;
 }
 
 static int mxcmci_init(struct mmc *mmc)
@@ -487,33 +490,30 @@ static int mxcmci_init(struct mmc *mmc)
        return 0;
 }
 
-static int mxcmci_initialize(bd_t *bis)
-{
-       struct mmc *mmc = NULL;
-
-       mmc = malloc(sizeof(struct mmc));
-
-       if (!mmc)
-               return -ENOMEM;
+static const struct mmc_ops mxcmci_ops = {
+       .send_cmd       = mxcmci_request,
+       .set_ios        = mxcmci_set_ios,
+       .init           = mxcmci_init,
+};
 
-       sprintf(mmc->name, "MXC MCI");
-       mmc->send_cmd = mxcmci_request;
-       mmc->set_ios = mxcmci_set_ios;
-       mmc->init = mxcmci_init;
-       mmc->host_caps = MMC_MODE_4BIT;
+static struct mmc_config mxcmci_cfg = {
+       .name           = "MXC MCI",
+       .ops            = &mxcmci_ops,
+       .host_caps      = MMC_MODE_4BIT,
+       .voltages       = MMC_VDD_32_33 | MMC_VDD_33_34,
+       .b_max          = CONFIG_SYS_MMC_MAX_BLK_COUNT,
+};
 
+static int mxcmci_initialize(bd_t *bis)
+{
        host->base = (struct mxcmci_regs *)CONFIG_MXC_MCI_REGS_BASE;
-       mmc->priv = host;
-       host->mmc = mmc;
-
-       mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
-
-       mmc->f_min = imx_get_perclk2() >> 7;
-       mmc->f_max = imx_get_perclk2() >> 1;
 
-       mmc->b_max = 0;
+       mxcmci_cfg.f_min = mxc_get_clock(MXC_ESDHC_CLK) >> 7;
+       mxcmci_cfg.f_max = mxc_get_clock(MXC_ESDHC_CLK) >> 1;
 
-       mmc_register(mmc);
+       host->mmc = mmc_create(&mxcmci_cfg, host);
+       if (host->mmc == NULL)
+               return -1;
 
        return 0;
 }