]> git.sur5r.net Git - u-boot/blobdiff - drivers/mmc/mmc.c
pinctrl: uniphier: fix NAND and SD pin-mux settings for PH1-LD11/LD20
[u-boot] / drivers / mmc / mmc.c
index 4eab274f732794b136ac4f88ac9231b42065cf33..d3c22abfd5a8c198499308cb4b8e24f161f9be3d 100644 (file)
 #include <config.h>
 #include <common.h>
 #include <command.h>
+#include <dm.h>
+#include <dm/device-internal.h>
 #include <errno.h>
 #include <mmc.h>
 #include <part.h>
 #include <malloc.h>
+#include <memalign.h>
 #include <linux/list.h>
 #include <div64.h>
 #include "mmc_private.h"
@@ -58,7 +61,10 @@ int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
        printf("CMD_SEND:%d\n", cmd->cmdidx);
        printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
        ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
-       switch (cmd->resp_type) {
+       if (ret) {
+               printf("\t\tRET\t\t\t %d\n", ret);
+       } else {
+               switch (cmd->resp_type) {
                case MMC_RSP_NONE:
                        printf("\t\tMMC_RSP_NONE\n");
                        break;
@@ -98,6 +104,7 @@ int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
                default:
                        printf("\t\tERROR MMC rsp not supported\n");
                        break;
+               }
        }
 #else
        ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
@@ -179,7 +186,7 @@ struct mmc *find_mmc_device(int dev_num)
        list_for_each(entry, &mmc_devices) {
                m = list_entry(entry, struct mmc, link);
 
-               if (m->block_dev.dev == dev_num)
+               if (m->block_dev.devnum == dev_num)
                        return m;
        }
 
@@ -231,8 +238,11 @@ static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
        return blkcnt;
 }
 
-static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
+static ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start,
+                      lbaint_t blkcnt, void *dst)
 {
+       int dev_num = block_dev->devnum;
+       int err;
        lbaint_t cur, blocks_todo = blkcnt;
 
        if (blkcnt == 0)
@@ -242,6 +252,10 @@ static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
        if (!mmc)
                return 0;
 
+       err = mmc_select_hwpart(dev_num, block_dev->hwpart);
+       if (err < 0)
+               return 0;
+
        if ((start + blkcnt) > mmc->block_dev.lba) {
 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
                printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
@@ -250,14 +264,18 @@ static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
                return 0;
        }
 
-       if (mmc_set_blocklen(mmc, mmc->read_bl_len))
+       if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
+               debug("%s: Failed to set blocklen\n", __func__);
                return 0;
+       }
 
        do {
                cur = (blocks_todo > mmc->cfg->b_max) ?
                        mmc->cfg->b_max : blocks_todo;
-               if(mmc_read_blocks(mmc, dst, start, cur) != cur)
+               if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
+                       debug("%s: Failed to read blocks\n", __func__);
                        return 0;
+               }
                blocks_todo -= cur;
                start += cur;
                dst += cur * mmc->read_bl_len;
@@ -572,7 +590,7 @@ int mmc_select_hwpart(int dev_num, int hwpart)
        if (!mmc)
                return -ENODEV;
 
-       if (mmc->part_num == hwpart)
+       if (mmc->block_dev.hwpart == hwpart)
                return 0;
 
        if (mmc->part_config == MMCPART_NOAVAILABLE) {
@@ -584,8 +602,6 @@ int mmc_select_hwpart(int dev_num, int hwpart)
        if (ret)
                return ret;
 
-       mmc->part_num = hwpart;
-
        return 0;
 }
 
@@ -606,8 +622,10 @@ int mmc_switch_part(int dev_num, unsigned int part_num)
         * Set the capacity if the switch succeeded or was intended
         * to return to representing the raw device.
         */
-       if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
+       if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
                ret = mmc_set_capacity(mmc, part_num);
+               mmc->block_dev.hwpart = part_num;
+       }
 
        return ret;
 }
@@ -892,20 +910,20 @@ retry_scr:
        mmc->scr[1] = __be32_to_cpu(scr[1]);
 
        switch ((mmc->scr[0] >> 24) & 0xf) {
-               case 0:
-                       mmc->version = SD_VERSION_1_0;
-                       break;
-               case 1:
-                       mmc->version = SD_VERSION_1_10;
-                       break;
-               case 2:
-                       mmc->version = SD_VERSION_2;
-                       if ((mmc->scr[0] >> 15) & 0x1)
-                               mmc->version = SD_VERSION_3;
-                       break;
-               default:
-                       mmc->version = SD_VERSION_1_0;
-                       break;
+       case 0:
+               mmc->version = SD_VERSION_1_0;
+               break;
+       case 1:
+               mmc->version = SD_VERSION_1_10;
+               break;
+       case 2:
+               mmc->version = SD_VERSION_2;
+               if ((mmc->scr[0] >> 15) & 0x1)
+                       mmc->version = SD_VERSION_3;
+               break;
+       default:
+               mmc->version = SD_VERSION_1_0;
+               break;
        }
 
        if (mmc->scr[0] & SD_DATA_4BIT)
@@ -1088,24 +1106,24 @@ static int mmc_startup(struct mmc *mmc)
                int version = (cmd.response[0] >> 26) & 0xf;
 
                switch (version) {
-                       case 0:
-                               mmc->version = MMC_VERSION_1_2;
-                               break;
-                       case 1:
-                               mmc->version = MMC_VERSION_1_4;
-                               break;
-                       case 2:
-                               mmc->version = MMC_VERSION_2_2;
-                               break;
-                       case 3:
-                               mmc->version = MMC_VERSION_3;
-                               break;
-                       case 4:
-                               mmc->version = MMC_VERSION_4;
-                               break;
-                       default:
-                               mmc->version = MMC_VERSION_1_2;
-                               break;
+               case 0:
+                       mmc->version = MMC_VERSION_1_2;
+                       break;
+               case 1:
+                       mmc->version = MMC_VERSION_1_4;
+                       break;
+               case 2:
+                       mmc->version = MMC_VERSION_2_2;
+                       break;
+               case 3:
+                       mmc->version = MMC_VERSION_3;
+                       break;
+               case 4:
+                       mmc->version = MMC_VERSION_4;
+                       break;
+               default:
+                       mmc->version = MMC_VERSION_1_2;
+                       break;
                }
        }
 
@@ -1317,7 +1335,7 @@ static int mmc_startup(struct mmc *mmc)
                mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
        }
 
-       err = mmc_set_capacity(mmc, mmc->part_num);
+       err = mmc_set_capacity(mmc, mmc->block_dev.hwpart);
        if (err)
                return err;
 
@@ -1458,11 +1476,14 @@ static int mmc_startup(struct mmc *mmc)
 
        /* fill in device description */
        mmc->block_dev.lun = 0;
+       mmc->block_dev.hwpart = 0;
        mmc->block_dev.type = 0;
        mmc->block_dev.blksz = mmc->read_bl_len;
        mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
        mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
-#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
+#if !defined(CONFIG_SPL_BUILD) || \
+               (defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
+               !defined(CONFIG_USE_TINY_PRINTF))
        sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
                mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
                (mmc->cid[3] >> 16) & 0xffff);
@@ -1478,7 +1499,7 @@ static int mmc_startup(struct mmc *mmc)
        mmc->block_dev.revision[0] = 0;
 #endif
 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
-       init_part(&mmc->block_dev);
+       part_init(&mmc->block_dev);
 #endif
 
        return 0;
@@ -1539,7 +1560,7 @@ struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
        mmc->dsr = 0xffffffff;
        /* Setup the universal parts of the block interface just once */
        mmc->block_dev.if_type = IF_TYPE_MMC;
-       mmc->block_dev.dev = cur_dev_num++;
+       mmc->block_dev.devnum = cur_dev_num++;
        mmc->block_dev.removable = 1;
        mmc->block_dev.block_read = mmc_bread;
        mmc->block_dev.block_write = mmc_bwrite;
@@ -1562,7 +1583,7 @@ void mmc_destroy(struct mmc *mmc)
 }
 
 #ifdef CONFIG_PARTITIONS
-block_dev_desc_t *mmc_get_dev(int dev)
+struct blk_desc *mmc_get_dev(int dev)
 {
        struct mmc *mmc = find_mmc_device(dev);
        if (!mmc || mmc_init(mmc))
@@ -1615,7 +1636,7 @@ int mmc_start_init(struct mmc *mmc)
                return err;
 
        /* The internal partition reset to user partition(0) at every CMD0*/
-       mmc->part_num = 0;
+       mmc->block_dev.hwpart = 0;
 
        /* Test for SD version 2 */
        err = mmc_send_if_cond(mmc);
@@ -1711,7 +1732,7 @@ void print_mmc_devices(char separator)
                else
                        mmc_type = NULL;
 
-               printf("%s: %d", m->cfg->name, m->block_dev.dev);
+               printf("%s: %d", m->cfg->name, m->block_dev.devnum);
                if (mmc_type)
                        printf(" (%s)", mmc_type);
 
@@ -1755,16 +1776,64 @@ static void do_preinit(void)
        }
 }
 
+#if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD)
+static int mmc_probe(bd_t *bis)
+{
+       return 0;
+}
+#elif defined(CONFIG_DM_MMC)
+static int mmc_probe(bd_t *bis)
+{
+       int ret, i;
+       struct uclass *uc;
+       struct udevice *dev;
+
+       ret = uclass_get(UCLASS_MMC, &uc);
+       if (ret)
+               return ret;
+
+       /*
+        * Try to add them in sequence order. Really with driver model we
+        * should allow holes, but the current MMC list does not allow that.
+        * So if we request 0, 1, 3 we will get 0, 1, 2.
+        */
+       for (i = 0; ; i++) {
+               ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
+               if (ret == -ENODEV)
+                       break;
+       }
+       uclass_foreach_dev(dev, uc) {
+               ret = device_probe(dev);
+               if (ret)
+                       printf("%s - probe failed: %d\n", dev->name, ret);
+       }
+
+       return 0;
+}
+#else
+static int mmc_probe(bd_t *bis)
+{
+       if (board_mmc_init(bis) < 0)
+               cpu_mmc_init(bis);
+
+       return 0;
+}
+#endif
 
 int mmc_initialize(bd_t *bis)
 {
+       static int initialized = 0;
+       int ret;
+       if (initialized)        /* Avoid initializing mmc multiple times */
+               return 0;
+       initialized = 1;
+
        INIT_LIST_HEAD (&mmc_devices);
        cur_dev_num = 0;
 
-#ifndef CONFIG_DM_MMC
-       if (board_mmc_init(bis) < 0)
-               cpu_mmc_init(bis);
-#endif
+       ret = mmc_probe(bis);
+       if (ret)
+               return ret;
 
 #ifndef CONFIG_SPL_BUILD
        print_mmc_devices(',');