blob: 3cc05bb53121b20aef941db631209aeaedee8e59 [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{
Peter Korsgaard2e222102013-03-21 04:55:17 +000020 unsigned long err;
21 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 */
Peter Korsgaard721931f2013-05-13 08:36:28 +000028 err = mmc->block_dev.block_read(0, sector, 1, header);
Peter Korsgaard2e222102013-03-21 04:55:17 +000029 if (err == 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 */
Peter Korsgaard721931f2013-05-13 08:36:28 +000042 err = 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
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000047 if (err == 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
51 return (err == 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040052}
53
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010054#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
55static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
56{
57 disk_partition_t info;
58
59 if (get_partition_info(&mmc->block_dev, partition, &info)) {
60#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
61 printf("spl: partition error\n");
62#endif
63 return -1;
64 }
65
66 return mmc_load_image_raw_sector(mmc, info.start);
67}
68#endif
69
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000070#ifdef CONFIG_SPL_OS_BOOT
71static int mmc_load_image_raw_os(struct mmc *mmc)
72{
Paul Kocialkowski91199f42015-04-27 10:20:22 +020073 unsigned long err;
74
75 err = mmc->block_dev.block_read(0,
76 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
77 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
78 (void *)CONFIG_SYS_SPL_ARGS_ADDR);
79 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +010080#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +020081 printf("spl: mmc block read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010082#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000083 return -1;
84 }
85
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010086 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski91199f42015-04-27 10:20:22 +020087 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000088}
89#endif
90
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040091void spl_mmc_load_image(void)
92{
93 struct mmc *mmc;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040094 u32 boot_mode;
Paul Kocialkowski91199f42015-04-27 10:20:22 +020095 int err;
96 __maybe_unused int part;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040097
98 mmc_initialize(gd->bd);
Paul Kocialkowski91199f42015-04-27 10:20:22 +020099
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400100 /* We register only one device. So, the dev id is always 0 */
101 mmc = find_mmc_device(0);
102 if (!mmc) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100103#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200104 puts("spl: mmc device not found\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100105#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400106 hang();
107 }
108
109 err = mmc_init(mmc);
110 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100111#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200112 printf("spl: mmc init failed with error: %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100113#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400114 hang();
115 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000116
Tom Rini37189a12012-08-14 09:19:44 -0700117 boot_mode = spl_boot_mode();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200118 switch (boot_mode) {
119 case MMCSD_MODE_RAW:
120 debug("spl: mmc boot mode: raw\n");
121
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000122#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200123 if (!spl_start_uboot()) {
124 err = mmc_load_image_raw_os(mmc);
125 if (!err)
126 return;
127 }
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000128#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100129#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
130 err = mmc_load_image_raw_partition(mmc,
131 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
132#else
133 err = mmc_load_image_raw_sector(mmc,
Dan Murphy773b5942014-01-16 11:23:29 -0600134 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100135#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200136 if (!err)
137 return;
Guillaume GARDET592f9222014-10-15 17:53:12 +0200138#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200139 case MMCSD_MODE_FS:
140 debug("spl: mmc boot mode: fs\n");
141
Guillaume GARDET592f9222014-10-15 17:53:12 +0200142#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000143#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200144 if (!spl_start_uboot()) {
145 err = spl_load_image_fat_os(&mmc->block_dev,
146 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
147 if (!err)
148 return;
149 }
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000150#endif
Dan Murphy773b5942014-01-16 11:23:29 -0600151 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200152 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
153 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
154 if (!err)
155 return;
156#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200157#ifdef CONFIG_SPL_EXT_SUPPORT
158#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200159 if (!spl_start_uboot()) {
160 err = spl_load_image_ext_os(&mmc->block_dev,
161 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
162 if (!err)
163 return;
164 }
Tom Rini0da113e2012-08-10 09:27:14 -0700165#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200166 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200167 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
168 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
169 if (!err)
170 return;
171#endif
172#endif
Tom Rini7dbe63b2014-02-05 10:24:18 -0500173#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200174 case MMCSD_MODE_EMMCBOOT:
Tom Rini7dbe63b2014-02-05 10:24:18 -0500175 /*
176 * We need to check what the partition is configured to.
177 * 1 and 2 match up to boot0 / boot1 and 7 is user data
178 * which is the first physical partition (0).
179 */
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200180 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500181
182 if (part == 7)
183 part = 0;
184
185 if (mmc_switch_part(0, part)) {
186#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200187 puts("spl: mmc partition switch failed\n");
Tom Rini7dbe63b2014-02-05 10:24:18 -0500188#endif
189 hang();
190 }
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200191
Tom Rini7dbe63b2014-02-05 10:24:18 -0500192#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200193 if (!spl_start_uboot()) {
194 err = mmc_load_image_raw_os(mmc);
195 if (!err)
196 return;
197 }
Tom Rini7dbe63b2014-02-05 10:24:18 -0500198#endif
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100199 err = mmc_load_image_raw_sector(mmc,
Tom Rini7dbe63b2014-02-05 10:24:18 -0500200 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200201 if (!err)
202 return;
Tom Rini7dbe63b2014-02-05 10:24:18 -0500203#endif
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200204 case MMCSD_MODE_UNDEFINED:
205 default:
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100206#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200207 if (err)
208 puts("spl: mmc: no boot mode left to try\n");
209 else
210 puts("spl: mmc: wrong boot mode\n");
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100211#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000212 hang();
Paul Kocialkowski91199f42015-04-27 10:20:22 +0200213 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400214}