blob: e580f2291af24d78d64e098200cdbdf4734308ca [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>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040011#include <asm/u-boot.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040012#include <mmc.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040013#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040014
15DECLARE_GLOBAL_DATA_PTR;
16
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010017static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040018{
Peter Korsgaard2e222102013-03-21 04:55:17 +000019 unsigned long err;
20 u32 image_size_sectors;
21 struct image_header *header;
Simon Schwarz9ea5c6e2011-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 Korsgaard721931f2013-05-13 08:36:28 +000027 err = mmc->block_dev.block_read(0, sector, 1, header);
Peter Korsgaard2e222102013-03-21 04:55:17 +000028 if (err == 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040029 goto end;
30
Tom Rinie4c444b2013-06-28 14:43:01 -040031 if (image_get_magic(header) != IH_MAGIC)
32 return -1;
33
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040034 spl_parse_image_header(header);
35
36 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070037 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
38 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040039
40 /* Read the header too to avoid extra memcpy */
Peter Korsgaard721931f2013-05-13 08:36:28 +000041 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
42 (void *)spl_image.load_addr);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040043
44end:
Paul Burton8112f5f2013-09-04 16:12:24 +010045#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000046 if (err == 0)
Peter Korsgaard2e222102013-03-21 04:55:17 +000047 printf("spl: mmc blk read err - %lu\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +010048#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000049
50 return (err == 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040051}
52
Paul Kocialkowskib97300b2014-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 Korsgaard2b75b0a2013-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 Burton8112f5f2013-09-04 16:12:24 +010076#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000077 printf("mmc args blk read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010078#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000079 return -1;
80 }
81
Paul Kocialkowskib97300b2014-11-08 23:14:56 +010082 return mmc_load_image_raw_sector(mmc,
83 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000084}
85#endif
86
Simon Schwarz9ea5c6e2011-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 Burton8112f5f2013-09-04 16:12:24 +010097#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040098 puts("spl: mmc device not found!!\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010099#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400100 hang();
101 }
102
103 err = mmc_init(mmc);
104 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100105#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400106 printf("spl: mmc init failed: err - %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100107#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400108 hang();
109 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000110
Tom Rini37189a12012-08-14 09:19:44 -0700111 boot_mode = spl_boot_mode();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400112 if (boot_mode == MMCSD_MODE_RAW) {
113 debug("boot mode - RAW\n");
Peter Korsgaard2b75b0a2013-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 Kocialkowskib97300b2014-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 Murphy773b5942014-01-16 11:23:29 -0600122 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Paul Kocialkowskib97300b2014-11-08 23:14:56 +0100123#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200124#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
Guillaume GARDET4c5bbc22014-11-18 10:44:46 +0100125 }
126 if (err || boot_mode == MMCSD_MODE_FS) {
Guillaume GARDET592f9222014-10-15 17:53:12 +0200127 debug("boot mode - FS\n");
128#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000129#ifdef CONFIG_SPL_OS_BOOT
Dan Murphy773b5942014-01-16 11:23:29 -0600130 if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev,
Paul Kocialkowskie2ccdf82014-11-08 23:14:55 +0100131 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000132#endif
Dan Murphy773b5942014-01-16 11:23:29 -0600133 err = spl_load_image_fat(&mmc->block_dev,
Paul Kocialkowskie2ccdf82014-11-08 23:14:55 +0100134 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200135 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
Guillaume GARDET592f9222014-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 Kocialkowskie2ccdf82014-11-08 23:14:55 +0100142 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
Tom Rini0da113e2012-08-10 09:27:14 -0700143#endif
Guillaume GARDET592f9222014-10-15 17:53:12 +0200144 err = spl_load_image_ext(&mmc->block_dev,
Paul Kocialkowskie2ccdf82014-11-08 23:14:55 +0100145 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
Guillaume GARDET592f9222014-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 Rini7dbe63b2014-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 Kocialkowskib97300b2014-11-08 23:14:56 +0100171 err = mmc_load_image_raw_sector(mmc,
Tom Rini7dbe63b2014-02-05 10:24:18 -0500172 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
173#endif
Guillaume GARDET2c84c9a2014-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:
Paul Burton8112f5f2013-09-04 16:12:24 +0100180#endif
Guillaume GARDET2c84c9a2014-12-16 12:00:44 +0100181#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:
188#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
189 puts("spl: wrong MMC boot mode\n");
190#endif
191 hang();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400192 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000193
194 if (err)
195 hang();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400196}