blob: 552f80d1e3dd761dd3101e66f6d01d97bf683004 [file] [log] [blame]
Simon Schwarz9ea5c6e2011-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 Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04008 */
9#include <common.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070010#include <spl.h>
Paul Kocialkowski91199f42015-04-27 10:20:22 +020011#include <linux/compiler.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040012#include <asm/u-boot.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040013#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040014#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040015
16DECLARE_GLOBAL_DATA_PTR;
17
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010018static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040019{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020020 unsigned long count;
Peter Korsgaard2e222102013-03-21 04:55:17 +000021 u32 image_size_sectors;
22 struct image_header *header;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040023
24 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
Paul Kocialkowski91199f42015-04-27 10:20:22 +020025 sizeof(struct image_header));
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040026
27 /* read image header to find the image size & load address */
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020028 count = mmc->block_dev.block_read(0, sector, 1, header);
29 if (count == 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040030 goto end;
31
Tom Rinie4c444b2013-06-28 14:43:01 -040032 if (image_get_magic(header) != IH_MAGIC)
33 return -1;
34
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040035 spl_parse_image_header(header);
36
37 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070038 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
Paul Kocialkowski91199f42015-04-27 10:20:22 +020039 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040040
41 /* Read the header too to avoid extra memcpy */
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020042 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
43 (void *) spl_image.load_addr);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040044
45end:
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020046 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +010047#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020048 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010049#endif
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020050 return -1;
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020051 }
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020052
53 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040054}
55
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010056#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
57static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
58{
59 disk_partition_t info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020060 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010061
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020062 err = get_partition_info(&mmc->block_dev, partition, &info);
63 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010064#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020065 puts("spl: partition error\n");
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010066#endif
67 return -1;
68 }
69
70 return mmc_load_image_raw_sector(mmc, info.start);
71}
72#endif
73
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000074#ifdef CONFIG_SPL_OS_BOOT
75static int mmc_load_image_raw_os(struct mmc *mmc)
76{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020077 unsigned long count;
Paul Kocialkowski91199f42015-04-27 10:20:22 +020078
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020079 count = mmc->block_dev.block_read(0,
80 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
81 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
82 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
83 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +010084#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski1ec26462015-06-03 18:48:51 +020085 puts("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010086#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000087 return -1;
88 }
89
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010090 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +020091 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000092}
93#endif
94
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040095void spl_mmc_load_image(void)
96{
97 struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040098 u32 boot_mode;
Paul Kocialkowski91199f42015-04-27 10:20:22 +020099 int err;
100 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400101
102 mmc_initialize(gd->bd);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200103
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400104 /* We register only one device. So, the dev id is always 0 */
105 mmc = find_mmc_device(0);
106 if (!mmc) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100107#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200108 puts("spl: mmc device not found\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100109#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400110 hang();
111 }
112
113 err = mmc_init(mmc);
114 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100115#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200116 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100117#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400118 hang();
119 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000120
Tom Rini37189a12012-08-14 09:19:44 -0700121 boot_mode = spl_boot_mode();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200122 switch (boot_mode) {
123 case MMCSD_MODE_RAW:
124 debug("spl: mmc boot mode: raw\n");
125
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000126#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200127 if (!spl_start_uboot()) {
128 err = mmc_load_image_raw_os(mmc);
129 if (!err)
130 return;
131 }
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000132#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200133#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100134 err = mmc_load_image_raw_partition(mmc,
135 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200136 if (!err)
137 return;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200138#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
139 err = mmc_load_image_raw_sector(mmc,
140 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
141 if (!err)
142 return;
143#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200144 case MMCSD_MODE_FS:
145 debug("spl: mmc boot mode: fs\n");
146
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200147#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
Guillaume GARDET592f9222014-10-15 17:53:12 +0200148#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000149#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200150 if (!spl_start_uboot()) {
151 err = spl_load_image_fat_os(&mmc->block_dev,
152 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
153 if (!err)
154 return;
155 }
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000156#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200157#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Dan Murphy773b5942014-01-16 11:23:29 -0600158 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200159 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
160 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
161 if (!err)
162 return;
163#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200164#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200165#ifdef CONFIG_SPL_EXT_SUPPORT
166#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200167 if (!spl_start_uboot()) {
168 err = spl_load_image_ext_os(&mmc->block_dev,
169 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
170 if (!err)
171 return;
172 }
Tom Rini0da113e2012-08-10 09:27:14 -0700173#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200174#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
Guillaume GARDET592f9222014-10-15 17:53:12 +0200175 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200176 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
177 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
178 if (!err)
179 return;
180#endif
181#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200182#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500183#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200184 case MMCSD_MODE_EMMCBOOT:
Tom Rini7dbe63b2014-02-05 10:24:18 -0500185 /*
186 * We need to check what the partition is configured to.
187 * 1 and 2 match up to boot0 / boot1 and 7 is user data
188 * which is the first physical partition (0).
189 */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200190 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500191
192 if (part == 7)
193 part = 0;
194
195 if (mmc_switch_part(0, part)) {
196#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200197 puts("spl: mmc partition switch failed\n");
Tom Rini7dbe63b2014-02-05 10:24:18 -0500198#endif
199 hang();
200 }
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200201
Tom Rini7dbe63b2014-02-05 10:24:18 -0500202#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200203 if (!spl_start_uboot()) {
204 err = mmc_load_image_raw_os(mmc);
205 if (!err)
206 return;
207 }
Tom Rini7dbe63b2014-02-05 10:24:18 -0500208#endif
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200209#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
Paul Kocialkowskiecb30132015-04-27 10:20:23 +0200210 err = mmc_load_image_raw_partition(mmc,
211 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200212 if (!err)
213 return;
Paul Kocialkowski3ae8f4c2015-06-08 23:05:09 +0200214#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
215 err = mmc_load_image_raw_sector(mmc,
216 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
217 if (!err)
218 return;
219#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500220#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200221 case MMCSD_MODE_UNDEFINED:
222 default:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100223#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200224 if (err)
225 puts("spl: mmc: no boot mode left to try\n");
226 else
227 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100228#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000229 hang();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200230 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400231}