blob: e580f2291af24d78d64e098200cdbdf4734308ca [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>
Tom Rini1530e8a2013-06-28 14:43:01 -040013#include <image.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014
15DECLARE_GLOBAL_DATA_PTR;
16
Paul Kocialkowski17675c82014-11-08 23:14:56 +010017static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarze21d2d82011-09-14 15:33:34 -040018{
Peter Korsgaardf2471292013-03-21 04:55:17 +000019 unsigned long err;
20 u32 image_size_sectors;
21 struct image_header *header;
Simon Schwarze21d2d82011-09-14 15:33:34 -040022
23 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
24 sizeof(struct image_header));
25
26 /* read image header to find the image size & load address */
Peter Korsgaard15af8f52013-05-13 08:36:28 +000027 err = mmc->block_dev.block_read(0, sector, 1, header);
Peter Korsgaardf2471292013-03-21 04:55:17 +000028 if (err == 0)
Simon Schwarze21d2d82011-09-14 15:33:34 -040029 goto end;
30
Tom Rini1530e8a2013-06-28 14:43:01 -040031 if (image_get_magic(header) != IH_MAGIC)
32 return -1;
33
Simon Schwarze21d2d82011-09-14 15:33:34 -040034 spl_parse_image_header(header);
35
36 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070037 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
38 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040039
40 /* Read the header too to avoid extra memcpy */
Peter Korsgaard15af8f52013-05-13 08:36:28 +000041 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
42 (void *)spl_image.load_addr);
Simon Schwarze21d2d82011-09-14 15:33:34 -040043
44end:
Paul Burtona8015fd2013-09-04 16:12:24 +010045#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard5e5483d2013-05-13 08:36:25 +000046 if (err == 0)
Peter Korsgaardf2471292013-03-21 04:55:17 +000047 printf("spl: mmc blk read err - %lu\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +010048#endif
Peter Korsgaard5e5483d2013-05-13 08:36:25 +000049
50 return (err == 0);
Simon Schwarze21d2d82011-09-14 15:33:34 -040051}
52
Paul Kocialkowski17675c82014-11-08 23:14:56 +010053#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
54static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
55{
56 disk_partition_t info;
57
58 if (get_partition_info(&mmc->block_dev, partition, &info)) {
59#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
60 printf("spl: partition error\n");
61#endif
62 return -1;
63 }
64
65 return mmc_load_image_raw_sector(mmc, info.start);
66}
67#endif
68
Peter Korsgaard01b542f2013-05-13 08:36:29 +000069#ifdef CONFIG_SPL_OS_BOOT
70static int mmc_load_image_raw_os(struct mmc *mmc)
71{
72 if (!mmc->block_dev.block_read(0,
73 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
74 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
75 (void *)CONFIG_SYS_SPL_ARGS_ADDR)) {
Paul Burtona8015fd2013-09-04 16:12:24 +010076#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard01b542f2013-05-13 08:36:29 +000077 printf("mmc args blk read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010078#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +000079 return -1;
80 }
81
Paul Kocialkowski17675c82014-11-08 23:14:56 +010082 return mmc_load_image_raw_sector(mmc,
83 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard01b542f2013-05-13 08:36:29 +000084}
85#endif
86
Simon Schwarze21d2d82011-09-14 15:33:34 -040087void spl_mmc_load_image(void)
88{
89 struct mmc *mmc;
90 int err;
91 u32 boot_mode;
92
93 mmc_initialize(gd->bd);
94 /* We register only one device. So, the dev id is always 0 */
95 mmc = find_mmc_device(0);
96 if (!mmc) {
Paul Burtona8015fd2013-09-04 16:12:24 +010097#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -040098 puts("spl: mmc device not found!!\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010099#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400100 hang();
101 }
102
103 err = mmc_init(mmc);
104 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100105#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -0400106 printf("spl: mmc init failed: err - %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100107#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400108 hang();
109 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000110
Tom Rinia76ff952012-08-14 09:19:44 -0700111 boot_mode = spl_boot_mode();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400112 if (boot_mode == MMCSD_MODE_RAW) {
113 debug("boot mode - RAW\n");
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000114#ifdef CONFIG_SPL_OS_BOOT
115 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
116#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100117#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
118 err = mmc_load_image_raw_partition(mmc,
119 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
120#else
121 err = mmc_load_image_raw_sector(mmc,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600122 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100123#endif
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200124#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
Guillaume GARDET4536a632014-11-18 10:44:46 +0100125 }
126 if (err || boot_mode == MMCSD_MODE_FS) {
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200127 debug("boot mode - FS\n");
128#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000129#ifdef CONFIG_SPL_OS_BOOT
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600130 if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100131 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000132#endif
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600133 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100134 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200135 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200136 if(err)
137#endif /* CONFIG_SPL_FAT_SUPPORT */
138 {
139#ifdef CONFIG_SPL_EXT_SUPPORT
140#ifdef CONFIG_SPL_OS_BOOT
141 if (spl_start_uboot() || spl_load_image_ext_os(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100142 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
Tom Rinia0d8cca2012-08-10 09:27:14 -0700143#endif
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200144 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski341e8cd2014-11-08 23:14:55 +0100145 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200146 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
147#endif /* CONFIG_SPL_EXT_SUPPORT */
148 }
149#endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */
Tom Rinicc612692014-02-05 10:24:18 -0500150#ifdef CONFIG_SUPPORT_EMMC_BOOT
151 } else if (boot_mode == MMCSD_MODE_EMMCBOOT) {
152 /*
153 * We need to check what the partition is configured to.
154 * 1 and 2 match up to boot0 / boot1 and 7 is user data
155 * which is the first physical partition (0).
156 */
157 int part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
158
159 if (part == 7)
160 part = 0;
161
162 if (mmc_switch_part(0, part)) {
163#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
164 puts("MMC partition switch failed\n");
165#endif
166 hang();
167 }
168#ifdef CONFIG_SPL_OS_BOOT
169 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
170#endif
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100171 err = mmc_load_image_raw_sector(mmc,
Tom Rinicc612692014-02-05 10:24:18 -0500172 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
173#endif
Guillaume GARDET1143b0a2014-12-16 12:00:44 +0100174 }
175
176 switch(boot_mode){
177 case MMCSD_MODE_RAW:
178#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
179 case MMCSD_MODE_FS:
180#endif
181#ifdef CONFIG_SUPPORT_EMMC_BOOT
182 case MMCSD_MODE_EMMCBOOT:
183#endif
184 /* Boot mode is ok. Nothing to do. */
185 break;
186 case MMCSD_MODE_UNDEFINED:
187 default:
Paul Burtona8015fd2013-09-04 16:12:24 +0100188#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Guillaume GARDET1143b0a2014-12-16 12:00:44 +0100189 puts("spl: wrong MMC boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100190#endif
Guillaume GARDET1143b0a2014-12-16 12:00:44 +0100191 hang();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400192 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000193
194 if (err)
195 hang();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400196}