Merge git://git.denx.de/u-boot-dm
[oweals/u-boot.git] / common / spl / spl_mmc.c
index b68190a265bc1956902508efe18f7b934531a335..5676acdde3f282da8062d0c679c02116d590b969 100644 (file)
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
+static int mmc_load_legacy(struct mmc *mmc, ulong sector,
+                          struct image_header *header)
 {
-       unsigned long count;
        u32 image_size_sectors;
-       struct image_header *header;
-
-       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
-                                        sizeof(struct image_header));
-
-       /* read image header to find the image size & load address */
-       count = mmc->block_dev.block_read(0, sector, 1, header);
-       debug("read sector %lx, count=%lu\n", sector, count);
-       if (count == 0)
-               goto end;
-
-       if (image_get_magic(header) != IH_MAGIC) {
-               puts("bad magic\n");
-               return -1;
-       }
+       unsigned long count;
+       int ret;
 
-       spl_parse_image_header(header);
+       ret = spl_parse_image_header(header);
+       if (ret)
+               return ret;
 
        /* convert size to sectors - round up */
        image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
                             mmc->read_bl_len;
 
        /* Read the header too to avoid extra memcpy */
-       count = mmc->block_dev.block_read(0, sector, image_size_sectors,
+       count = mmc->block_dev.block_read(&mmc->block_dev, sector,
+                                         image_size_sectors,
                                          (void *)(ulong)spl_image.load_addr);
        debug("read %x sectors to %x\n", image_size_sectors,
              spl_image.load_addr);
+       if (count != image_size_sectors)
+               return -EIO;
 
-end:
+       return 0;
+}
+
+static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
+                            ulong count, void *buf)
+{
+       struct mmc *mmc = load->dev;
+
+       return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
+}
+
+static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
+{
+       unsigned long count;
+       struct image_header *header;
+       int ret = 0;
+
+       header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
+                                        sizeof(struct image_header));
+
+       /* read image header to find the image size & load address */
+       count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
+       debug("hdr read sector %lx, count=%lu\n", sector, count);
        if (count == 0) {
+               ret = -EIO;
+               goto end;
+       }
+
+       if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
+           image_get_magic(header) == FDT_MAGIC) {
+               struct spl_load_info load;
+
+               debug("Found FIT\n");
+               load.dev = mmc;
+               load.priv = NULL;
+               load.bl_len = mmc->read_bl_len;
+               load.read = h_spl_load_read;
+               ret = spl_load_simple_fit(&load, sector, header);
+       } else {
+               ret = mmc_load_legacy(mmc, sector, header);
+       }
+
+end:
+       if (ret) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
-               puts("spl: mmc block read error\n");
+               puts("mmc_load_image_raw_sector: mmc block read error\n");
 #endif
                return -1;
        }
@@ -61,38 +95,35 @@ end:
        return 0;
 }
 
-#ifdef CONFIG_DM_MMC
-static int spl_mmc_find_device(struct mmc **mmc)
+int spl_mmc_get_device_index(u32 boot_device)
 {
-       struct udevice *dev;
-       int err;
-
-       err = mmc_initialize(NULL);
-       if (err) {
-#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
-               printf("spl: could not initialize mmc. error: %d\n", err);
-#endif
-               return err;
+       switch (boot_device) {
+       case BOOT_DEVICE_MMC1:
+               return 0;
+       case BOOT_DEVICE_MMC2:
+       case BOOT_DEVICE_MMC2_2:
+               return 1;
        }
 
-       err = uclass_get_device(UCLASS_MMC, 0, &dev);
-       if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
-               printf("spl: could not find mmc device. error: %d\n", err);
+       printf("spl: unsupported mmc boot device.\n");
 #endif
-               return err;
-       }
 
-       *mmc = NULL;
-       *mmc = mmc_get_mmc_dev(dev);
-       return *mmc != NULL ? 0 : -ENODEV;
+       return -ENODEV;
 }
-#else
-static int spl_mmc_find_device(struct mmc **mmc)
+
+static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
 {
-       int err;
+#ifdef CONFIG_DM_MMC
+       struct udevice *dev;
+#endif
+       int err, mmc_dev;
+
+       mmc_dev = spl_mmc_get_device_index(boot_device);
+       if (mmc_dev < 0)
+               return mmc_dev;
 
-       err = mmc_initialize(gd->bd);
+       err = mmc_initialize(NULL);
        if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
                printf("spl: could not initialize mmc. error: %d\n", err);
@@ -100,18 +131,23 @@ static int spl_mmc_find_device(struct mmc **mmc)
                return err;
        }
 
-       /* We register only one device. So, the dev id is always 0 */
-       *mmc = find_mmc_device(0);
-       if (!*mmc) {
+#ifdef CONFIG_DM_MMC
+       err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
+       if (!err)
+               *mmcp = mmc_get_mmc_dev(dev);
+#else
+       *mmcp = find_mmc_device(mmc_dev);
+       err = *mmcp ? 0 : -ENODEV;
+#endif
+       if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
-               puts("spl: mmc device not found\n");
+               printf("spl: could not find mmc device. error: %d\n", err);
 #endif
-               return -ENODEV;
+               return err;
        }
 
        return 0;
 }
-#endif
 
 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
@@ -119,7 +155,7 @@ static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
        disk_partition_t info;
        int err;
 
-       err = get_partition_info(&mmc->block_dev, partition, &info);
+       err = part_get_info(&mmc->block_dev, partition, &info);
        if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
                puts("spl: partition error\n");
@@ -146,20 +182,30 @@ static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
 static int mmc_load_image_raw_os(struct mmc *mmc)
 {
        unsigned long count;
+       int ret;
 
-       count = mmc->block_dev.block_read(0,
+       count = mmc->block_dev.block_read(&mmc->block_dev,
                CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
                CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
                (void *) CONFIG_SYS_SPL_ARGS_ADDR);
        if (count == 0) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
-               puts("spl: mmc block read error\n");
+               puts("mmc_load_image_raw_os: mmc block read error\n");
 #endif
                return -1;
        }
 
-       return mmc_load_image_raw_sector(mmc,
+       ret = mmc_load_image_raw_sector(mmc,
                CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
+       if (ret)
+               return ret;
+
+       if (spl_image.os != IH_OS_LINUX) {
+               puts("Expected Linux image is not found. Trying to start U-boot\n");
+               return -ENOENT;
+       }
+
+       return 0;
 }
 #else
 int spl_start_uboot(void)
@@ -221,14 +267,14 @@ int spl_mmc_do_fs_boot(struct mmc *mmc)
 }
 #endif
 
-int spl_mmc_load_image(void)
+int spl_mmc_load_image(u32 boot_device)
 {
-       struct mmc *mmc;
+       struct mmc *mmc = NULL;
        u32 boot_mode;
        int err = 0;
        __maybe_unused int part;
 
-       err = spl_mmc_find_device(&mmc);
+       err = spl_mmc_find_device(&mmc, boot_device);
        if (err)
                return err;
 
@@ -254,7 +300,7 @@ int spl_mmc_load_image(void)
                        if (part == 7)
                                part = 0;
 
-                       err = mmc_switch_part(0, part);
+                       err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
                        if (err) {
 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
                                puts("spl: mmc partition switch failed\n");
@@ -281,7 +327,7 @@ int spl_mmc_load_image(void)
                if (!err)
                        return err;
 #endif
-               break;
+               /* If RAW mode fails, try FS mode. */
        case MMCSD_MODE_FS:
                debug("spl: mmc boot mode: fs\n");