blob: c96345eff2dc0cf9ca85c6bb50b7494e614b43db [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>
Paul Kocialkowski0d139722015-04-27 10:20:22 +020011#include <linux/compiler.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040012#include <asm/u-boot.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040013#include <mmc.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
Paul Kocialkowski17675c82014-11-08 23:14:56 +010018static int mmc_load_image_raw_sector(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 -
Paul Kocialkowski0d139722015-04-27 10:20:22 +020025 sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040026
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) /
Paul Kocialkowski0d139722015-04-27 10:20:22 +020039 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)
Paul Kocialkowski0d139722015-04-27 10:20:22 +020048 printf("spl: mmc block read error\n");
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
Paul Kocialkowski17675c82014-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 Korsgaard01b542f2013-05-13 08:36:29 +000070#ifdef CONFIG_SPL_OS_BOOT
71static int mmc_load_image_raw_os(struct mmc *mmc)
72{
Paul Kocialkowski0d139722015-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);
Tim Harvey295be402015-05-21 15:57:16 -070079 if (err == 0) {
Paul Burtona8015fd2013-09-04 16:12:24 +010080#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +020081 printf("spl: mmc block read error\n");
Paul Burtona8015fd2013-09-04 16:12:24 +010082#endif
Peter Korsgaard01b542f2013-05-13 08:36:29 +000083 return -1;
84 }
85
Paul Kocialkowski17675c82014-11-08 23:14:56 +010086 return mmc_load_image_raw_sector(mmc,
Paul Kocialkowski0d139722015-04-27 10:20:22 +020087 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard01b542f2013-05-13 08:36:29 +000088}
89#endif
90
Simon Schwarze21d2d82011-09-14 15:33:34 -040091void spl_mmc_load_image(void)
92{
93 struct mmc *mmc;
Simon Schwarze21d2d82011-09-14 15:33:34 -040094 u32 boot_mode;
Paul Kocialkowski0d139722015-04-27 10:20:22 +020095 int err;
96 __maybe_unused int part;
Simon Schwarze21d2d82011-09-14 15:33:34 -040097
98 mmc_initialize(gd->bd);
Paul Kocialkowski0d139722015-04-27 10:20:22 +020099
Simon Schwarze21d2d82011-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 Burtona8015fd2013-09-04 16:12:24 +0100103#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200104 puts("spl: mmc device not found\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100105#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400106 hang();
107 }
108
109 err = mmc_init(mmc);
110 if (err) {
Paul Burtona8015fd2013-09-04 16:12:24 +0100111#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200112 printf("spl: mmc init failed with error: %d\n", err);
Paul Burtona8015fd2013-09-04 16:12:24 +0100113#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400114 hang();
115 }
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000116
Tom Rinia76ff952012-08-14 09:19:44 -0700117 boot_mode = spl_boot_mode();
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200118 switch (boot_mode) {
119 case MMCSD_MODE_RAW:
120 debug("spl: mmc boot mode: raw\n");
121
Peter Korsgaard01b542f2013-05-13 08:36:29 +0000122#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-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 Korsgaard01b542f2013-05-13 08:36:29 +0000128#endif
Paul Kocialkowski17675c82014-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 Murphyb7b5b0c2014-01-16 11:23:29 -0600134 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100135#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200136 if (!err)
137 return;
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200138#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200139 case MMCSD_MODE_FS:
140 debug("spl: mmc boot mode: fs\n");
141
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200142#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard465f1f82013-05-13 08:36:27 +0000143#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-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 Korsgaard465f1f82013-05-13 08:36:27 +0000150#endif
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600151 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowski0d139722015-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 GARDET1eb410c2014-10-15 17:53:12 +0200157#ifdef CONFIG_SPL_EXT_SUPPORT
158#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-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 Rinia0d8cca2012-08-10 09:27:14 -0700165#endif
Guillaume GARDET1eb410c2014-10-15 17:53:12 +0200166 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowski0d139722015-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 Rinicc612692014-02-05 10:24:18 -0500173#ifdef CONFIG_SUPPORT_EMMC_BOOT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200174 case MMCSD_MODE_EMMCBOOT:
Tom Rinicc612692014-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 Kocialkowski0d139722015-04-27 10:20:22 +0200180 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
Tom Rinicc612692014-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 Kocialkowski0d139722015-04-27 10:20:22 +0200187 puts("spl: mmc partition switch failed\n");
Tom Rinicc612692014-02-05 10:24:18 -0500188#endif
189 hang();
190 }
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200191
Tom Rinicc612692014-02-05 10:24:18 -0500192#ifdef CONFIG_SPL_OS_BOOT
Paul Kocialkowski0d139722015-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 Rinicc612692014-02-05 10:24:18 -0500198#endif
Paul Kocialkowski409d4f72015-04-27 10:20:23 +0200199#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
200 err = mmc_load_image_raw_partition(mmc,
201 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
202#else
Paul Kocialkowski17675c82014-11-08 23:14:56 +0100203 err = mmc_load_image_raw_sector(mmc,
Tom Rinicc612692014-02-05 10:24:18 -0500204 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowski409d4f72015-04-27 10:20:23 +0200205#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200206 if (!err)
207 return;
Guillaume GARDET1143b0a2014-12-16 12:00:44 +0100208#endif
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200209 case MMCSD_MODE_UNDEFINED:
210 default:
Paul Burtona8015fd2013-09-04 16:12:24 +0100211#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200212 if (err)
213 puts("spl: mmc: no boot mode left to try\n");
214 else
215 puts("spl: mmc: wrong boot mode\n");
Paul Burtona8015fd2013-09-04 16:12:24 +0100216#endif
Peter Korsgaard5e5483d2013-05-13 08:36:25 +0000217 hang();
Paul Kocialkowski0d139722015-04-27 10:20:22 +0200218 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400219}