blob: de495c0dc4895ff3341ebcbb3d1a23f1a5edf5f1 [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 Burton8112f5f2013-09-04 16:12:24 +010046#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020047 if (count == 0)
Paul Kocialkowski91199f42015-04-27 10:20:22 +020048 printf("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010049#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000050
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020051 if (count == 0)
52 return -1;
53
54 return 0;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040055}
56
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010057#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
58static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
59{
60 disk_partition_t info;
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020061 int err;
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010062
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020063 err = get_partition_info(&mmc->block_dev, partition, &info);
64 if (err) {
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010065#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
66 printf("spl: partition error\n");
67#endif
68 return -1;
69 }
70
71 return mmc_load_image_raw_sector(mmc, info.start);
72}
73#endif
74
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000075#ifdef CONFIG_SPL_OS_BOOT
76static int mmc_load_image_raw_os(struct mmc *mmc)
77{
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020078 unsigned long count;
Paul Kocialkowski91199f42015-04-27 10:20:22 +020079
Paul Kocialkowski3bc37b62015-05-22 12:45:35 +020080 count = mmc->block_dev.block_read(0,
81 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
82 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
83 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
84 if (count == 0) {
Paul Burton8112f5f2013-09-04 16:12:24 +010085#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +020086 printf("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010087#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000088 return -1;
89 }
90
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010091 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +020092 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000093}
94#endif
95
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040096void spl_mmc_load_image(void)
97{
98 struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040099 u32 boot_mode;
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200100 int err;
101 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400102
103 mmc_initialize(gd->bd);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200104
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400105 /* We register only one device. So, the dev id is always 0 */
106 mmc = find_mmc_device(0);
107 if (!mmc) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100108#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200109 puts("spl: mmc device not found\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100110#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400111 hang();
112 }
113
114 err = mmc_init(mmc);
115 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100116#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200117 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100118#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400119 hang();
120 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000121
Tom Rini37189a12012-08-14 09:19:44 -0700122 boot_mode = spl_boot_mode();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200123 switch (boot_mode) {
124 case MMCSD_MODE_RAW:
125 debug("spl: mmc boot mode: raw\n");
126
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000127#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200128 if (!spl_start_uboot()) {
129 err = mmc_load_image_raw_os(mmc);
130 if (!err)
131 return;
132 }
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000133#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100134#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
135 err = mmc_load_image_raw_partition(mmc,
136 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
137#else
138 err = mmc_load_image_raw_sector(mmc,
Dan Murphy773b5942014-01-16 11:23:29 -0600139 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100140#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200141 if (!err)
142 return;
Guillaume GARDET592f9222014-10-15 17:53:12 +0200143#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200144 case MMCSD_MODE_FS:
145 debug("spl: mmc boot mode: fs\n");
146
Guillaume GARDET592f9222014-10-15 17:53:12 +0200147#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000148#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200149 if (!spl_start_uboot()) {
150 err = spl_load_image_fat_os(&mmc->block_dev,
151 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
152 if (!err)
153 return;
154 }
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000155#endif
Dan Murphy773b5942014-01-16 11:23:29 -0600156 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200157 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
158 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
159 if (!err)
160 return;
161#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200162#ifdef CONFIG_SPL_EXT_SUPPORT
163#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200164 if (!spl_start_uboot()) {
165 err = spl_load_image_ext_os(&mmc->block_dev,
166 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
167 if (!err)
168 return;
169 }
Tom Rini0da113e2012-08-10 09:27:14 -0700170#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200171 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200172 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
173 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
174 if (!err)
175 return;
176#endif
177#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500178#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200179 case MMCSD_MODE_EMMCBOOT:
Tom Rini7dbe63b2014-02-05 10:24:18 -0500180 /*
181 * We need to check what the partition is configured to.
182 * 1 and 2 match up to boot0 / boot1 and 7 is user data
183 * which is the first physical partition (0).
184 */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200185 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500186
187 if (part == 7)
188 part = 0;
189
190 if (mmc_switch_part(0, part)) {
191#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200192 puts("spl: mmc partition switch failed\n");
Tom Rini7dbe63b2014-02-05 10:24:18 -0500193#endif
194 hang();
195 }
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200196
Tom Rini7dbe63b2014-02-05 10:24:18 -0500197#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200198 if (!spl_start_uboot()) {
199 err = mmc_load_image_raw_os(mmc);
200 if (!err)
201 return;
202 }
Tom Rini7dbe63b2014-02-05 10:24:18 -0500203#endif
Paul Kocialkowskiecb30132015-04-27 10:20:23 +0200204#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
205 err = mmc_load_image_raw_partition(mmc,
206 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
207#else
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100208 err = mmc_load_image_raw_sector(mmc,
Tom Rini7dbe63b2014-02-05 10:24:18 -0500209 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowskiecb30132015-04-27 10:20:23 +0200210#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200211 if (!err)
212 return;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500213#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200214 case MMCSD_MODE_UNDEFINED:
215 default:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100216#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200217 if (err)
218 puts("spl: mmc: no boot mode left to try\n");
219 else
220 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100221#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000222 hang();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200223 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400224}