blob: f9c38512e41be6e7879acabc21484dd5c221220c [file] [log] [blame]
Simon Schwarze21d2d82011-09-14 15:33:34 -04001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04008 */
9#include <common.h>
Tom Rini28591df2012-08-13 12:03:19 -070010#include <spl.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040011#include <asm/u-boot.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040012#include <mmc.h>
Simon Glass50402412011-10-10 08:55:19 +000013#include <version.h>
Tom Rini1530e8a2013-06-28 14:43:01 -040014#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040015
16DECLARE_GLOBAL_DATA_PTR;
17
Peter Korsgaard15af8f52013-05-13 08:36:28 +000018static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040019{
Peter Korsgaardf2471292013-03-21 04:55:17 +000020 unsigned long err;
21 u32 image_size_sectors;
22 struct image_header *header;
Simon Schwarze21d2d82011-09-14 15:33:34 -040023
24 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
25 sizeof(struct image_header));
26
27 /* read image header to find the image size & load address */
Peter Korsgaard15af8f52013-05-13 08:36:28 +000028 err = mmc->block_dev.block_read(0, sector, 1, header);
Peter Korsgaardf2471292013-03-21 04:55:17 +000029 if (err == 0)
Simon Schwarze21d2d82011-09-14 15:33:34 -040030 goto end;
31
Tom Rini1530e8a2013-06-28 14:43:01 -040032 if (image_get_magic(header) != IH_MAGIC)
33 return -1;
34
Simon Schwarze21d2d82011-09-14 15:33:34 -040035 spl_parse_image_header(header);
36
37 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070038 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
39 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040040
41 /* Read the header too to avoid extra memcpy */
Peter Korsgaard15af8f52013-05-13 08:36:28 +000042 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
43 (void *)spl_image.load_addr);
Simon Schwarze21d2d82011-09-14 15:33:34 -040044
45end:
Paul Burtona8015fd2013-09-04 16:12:24 +010046#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard5e5483d2013-05-13 08:36:25 +000047 if (err == 0)
Peter Korsgaardf2471292013-03-21 04:55:17 +000048 printf("spl: mmc blk read err - %lu\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +010049#endif
Peter Korsgaard5e5483d2013-05-13 08:36:25 +000050
51 return (err == 0);
Simon Schwarze21d2d82011-09-14 15:33:34 -040052}
53
Peter Korsgaard01b542f2013-05-13 08:36:29 +000054#ifdef CONFIG_SPL_OS_BOOT
55static int mmc_load_image_raw_os(struct mmc *mmc)
56{
57 if (!mmc->block_dev.block_read(0,
58 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
59 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
60 (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
Paul Burtona8015fd2013-09-04 16:12:24 +010061#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard01b542f2013-05-13 08:36:29 +000062 printf("mmc args blk read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010063#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +000064 return -1;
65 }
66
67 return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
68}
69#endif
70
Simon Schwarze21d2d82011-09-14 15:33:34 -040071void spl_mmc_load_image(void)
72{
73 struct mmc *mmc;
74 int err;
75 u32 boot_mode;
76
77 mmc_initialize(gd->bd);
78 /* We register only one device. So, the dev id is always 0 */
79 mmc = find_mmc_device(0);
80 if (!mmc) {
Paul Burtona8015fd2013-09-04 16:12:24 +010081#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -040082 puts("spl: mmc device not found!!\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010083#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -040084 hang();
85 }
86
87 err = mmc_init(mmc);
88 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +010089#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -040090 printf("spl: mmc init failed: err - %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +010091#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -040092 hang();
93 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +000094
Tom Rinia76ff952012-08-14 09:19:44 -070095 boot_mode = spl_boot_mode();
Simon Schwarze21d2d82011-09-14 15:33:34 -040096 if (boot_mode == MMCSD_MODE_RAW) {
97 debug("boot mode - RAW\n");
Peter Korsgaard01b542f2013-05-13 08:36:29 +000098#ifdef CONFIG_SPL_OS_BOOT
99 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
100#endif
Peter Korsgaard15af8f52013-05-13 08:36:28 +0000101 err = mmc_load_image_raw(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600102 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200103#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200104 } else if (boot_mode == MMCSD_MODE_FS) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200105 debug("boot mode - FS\n");
106#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000107#ifdef CONFIG_SPL_OS_BOOT
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600108 if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100109 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000110#endif
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600111 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100112 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200113 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200114 if(err)
115#endif /* CONFIG_SPL_FAT_SUPPORT */
116 {
117#ifdef CONFIG_SPL_EXT_SUPPORT
118#ifdef CONFIG_SPL_OS_BOOT
119 if (spl_start_uboot() || spl_load_image_ext_os(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100120 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
Tom Rinia0d8cca2012-08-10 09:27:14 -0700121#endif
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200122 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100123 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200124 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
125#endif /* CONFIG_SPL_EXT_SUPPORT */
126 }
127#endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */
Tom Rinicc612692014-02-05 10:24:18 -0500128#ifdef CONFIG_SUPPORT_EMMC_BOOT
129 } else if (boot_mode == MMCSD_MODE_EMMCBOOT) {
130 /*
131 * We need to check what the partition is configured to.
132 * 1 and 2 match up to boot0 / boot1 and 7 is user data
133 * which is the first physical partition (0).
134 */
135 int part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
136
137 if (part == 7)
138 part = 0;
139
140 if (mmc_switch_part(0, part)) {
141#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
142 puts("MMC partition switch failed\n");
143#endif
144 hang();
145 }
146#ifdef CONFIG_SPL_OS_BOOT
147 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
148#endif
149 err = mmc_load_image_raw(mmc,
150 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
151#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400152 } else {
Paul Burtona8015fd2013-09-04 16:12:24 +0100153#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -0400154 puts("spl: wrong MMC boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100155#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400156 hang();
157 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000158
159 if (err)
160 hang();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400161}