]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/spl/spl_mmc.c
spl: nand: remove code duplication
[people/ms/u-boot.git] / common / spl / spl_mmc.c
CommitLineData
9ea5c6ef
SS
1/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
1a459660 7 * SPDX-License-Identifier: GPL-2.0+
9ea5c6ef
SS
8 */
9#include <common.h>
dc3dedfe 10#include <dm.h>
47f7bcae 11#include <spl.h>
91199f4a 12#include <linux/compiler.h>
9ea5c6ef 13#include <asm/u-boot.h>
9ea5c6ef 14#include <mmc.h>
e4c444b3 15#include <image.h>
9ea5c6ef
SS
16
17DECLARE_GLOBAL_DATA_PTR;
18
b97300b6 19static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
9ea5c6ef 20{
3bc37b6d 21 unsigned long count;
2e222105
PK
22 u32 image_size_sectors;
23 struct image_header *header;
9ea5c6ef
SS
24
25 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
91199f4a 26 sizeof(struct image_header));
9ea5c6ef
SS
27
28 /* read image header to find the image size & load address */
3bc37b6d 29 count = mmc->block_dev.block_read(0, sector, 1, header);
fdfa39d3 30 debug("read sector %lx, count=%lu\n", sector, count);
3bc37b6d 31 if (count == 0)
9ea5c6ef
SS
32 goto end;
33
fdfa39d3
SG
34 if (image_get_magic(header) != IH_MAGIC) {
35 puts("bad magic\n");
e4c444b3 36 return -1;
fdfa39d3 37 }
e4c444b3 38
9ea5c6ef
SS
39 spl_parse_image_header(header);
40
41 /* convert size to sectors - round up */
f0881250 42 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
91199f4a 43 mmc->read_bl_len;
9ea5c6ef
SS
44
45 /* Read the header too to avoid extra memcpy */
3bc37b6d 46 count = mmc->block_dev.block_read(0, sector, image_size_sectors,
7ef4c45c 47 (void *)(ulong)spl_image.load_addr);
fdfa39d3
SG
48 debug("read %x sectors to %x\n", image_size_sectors,
49 spl_image.load_addr);
9ea5c6ef
SS
50
51end:
1ec26469 52 if (count == 0) {
8112f5fa 53#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 54 puts("spl: mmc block read error\n");
8112f5fa 55#endif
3bc37b6d 56 return -1;
1ec26469 57 }
3bc37b6d
PK
58
59 return 0;
9ea5c6ef
SS
60}
61
b97300b6
PK
62#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
63static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
64{
65 disk_partition_t info;
3bc37b6d 66 int err;
b97300b6 67
3bc37b6d
PK
68 err = get_partition_info(&mmc->block_dev, partition, &info);
69 if (err) {
b97300b6 70#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 71 puts("spl: partition error\n");
b97300b6
PK
72#endif
73 return -1;
74 }
75
4bfcc54c
SR
76#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
77 return mmc_load_image_raw_sector(mmc, info.start +
78 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
79#else
b97300b6 80 return mmc_load_image_raw_sector(mmc, info.start);
4bfcc54c 81#endif
b97300b6
PK
82}
83#endif
84
2b75b0ad
PK
85#ifdef CONFIG_SPL_OS_BOOT
86static int mmc_load_image_raw_os(struct mmc *mmc)
87{
3bc37b6d 88 unsigned long count;
91199f4a 89
3bc37b6d
PK
90 count = mmc->block_dev.block_read(0,
91 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
92 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
93 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
94 if (count == 0) {
8112f5fa 95#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 96 puts("spl: mmc block read error\n");
8112f5fa 97#endif
2b75b0ad
PK
98 return -1;
99 }
100
b97300b6 101 return mmc_load_image_raw_sector(mmc,
91199f4a 102 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
2b75b0ad
PK
103}
104#endif
105
9ea5c6ef
SS
106void spl_mmc_load_image(void)
107{
108 struct mmc *mmc;
9ea5c6ef 109 u32 boot_mode;
dc3dedfe 110 int err = 0;
91199f4a 111 __maybe_unused int part;
9ea5c6ef 112
dc3dedfe
SG
113#ifdef CONFIG_DM_MMC
114 struct udevice *dev;
115
116 mmc_initialize(NULL);
117 err = uclass_get_device(UCLASS_MMC, 0, &dev);
118 mmc = NULL;
119 if (!err)
120 mmc = mmc_get_mmc_dev(dev);
121#else
9ea5c6ef 122 mmc_initialize(gd->bd);
91199f4a 123
9ea5c6ef
SS
124 /* We register only one device. So, the dev id is always 0 */
125 mmc = find_mmc_device(0);
126 if (!mmc) {
8112f5fa 127#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 128 puts("spl: mmc device not found\n");
8112f5fa 129#endif
9ea5c6ef
SS
130 hang();
131 }
dc3dedfe
SG
132#endif
133
134 if (!err)
135 err = mmc_init(mmc);
9ea5c6ef 136
9ea5c6ef 137 if (err) {
8112f5fa 138#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 139 printf("spl: mmc init failed with error: %d\n", err);
8112f5fa 140#endif
9ea5c6ef
SS
141 hang();
142 }
79adb7a2 143
37189a19 144 boot_mode = spl_boot_mode();
91199f4a
PK
145 switch (boot_mode) {
146 case MMCSD_MODE_RAW:
147 debug("spl: mmc boot mode: raw\n");
148
2b75b0ad 149#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
150 if (!spl_start_uboot()) {
151 err = mmc_load_image_raw_os(mmc);
152 if (!err)
153 return;
154 }
2b75b0ad 155#endif
3ae8f4c8 156#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
b97300b6
PK
157 err = mmc_load_image_raw_partition(mmc,
158 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
3ae8f4c8
PK
159 if (!err)
160 return;
161#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
b97300b6 162 err = mmc_load_image_raw_sector(mmc,
773b5940 163 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
91199f4a
PK
164 if (!err)
165 return;
3ae8f4c8 166#endif
91199f4a
PK
167 case MMCSD_MODE_FS:
168 debug("spl: mmc boot mode: fs\n");
169
3ae8f4c8 170#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
592f9222 171#ifdef CONFIG_SPL_FAT_SUPPORT
7ad2cc79 172#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
173 if (!spl_start_uboot()) {
174 err = spl_load_image_fat_os(&mmc->block_dev,
175 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
176 if (!err)
177 return;
178 }
7ad2cc79 179#endif
3ae8f4c8 180#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
773b5940 181 err = spl_load_image_fat(&mmc->block_dev,
91199f4a
PK
182 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
183 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
184 if (!err)
185 return;
186#endif
3ae8f4c8 187#endif
592f9222
GG
188#ifdef CONFIG_SPL_EXT_SUPPORT
189#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
190 if (!spl_start_uboot()) {
191 err = spl_load_image_ext_os(&mmc->block_dev,
192 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
193 if (!err)
194 return;
195 }
7dbe63bc 196#endif
3ae8f4c8 197#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
592f9222 198 err = spl_load_image_ext(&mmc->block_dev,
91199f4a
PK
199 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
200 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
201 if (!err)
202 return;
203#endif
204#endif
3ae8f4c8 205#endif
7dbe63bc 206#ifdef CONFIG_SUPPORT_EMMC_BOOT
91199f4a 207 case MMCSD_MODE_EMMCBOOT:
7dbe63bc
TR
208 /*
209 * We need to check what the partition is configured to.
210 * 1 and 2 match up to boot0 / boot1 and 7 is user data
211 * which is the first physical partition (0).
212 */
91199f4a 213 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
7dbe63bc
TR
214
215 if (part == 7)
216 part = 0;
217
218 if (mmc_switch_part(0, part)) {
219#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 220 puts("spl: mmc partition switch failed\n");
7dbe63bc
TR
221#endif
222 hang();
223 }
91199f4a 224
7dbe63bc 225#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
226 if (!spl_start_uboot()) {
227 err = mmc_load_image_raw_os(mmc);
228 if (!err)
229 return;
230 }
7dbe63bc 231#endif
3ae8f4c8 232#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
ecb30139
PK
233 err = mmc_load_image_raw_partition(mmc,
234 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
3ae8f4c8
PK
235 if (!err)
236 return;
237#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
b97300b6 238 err = mmc_load_image_raw_sector(mmc,
7dbe63bc 239 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
91199f4a
PK
240 if (!err)
241 return;
3ae8f4c8 242#endif
0da113e9 243#endif
91199f4a
PK
244 case MMCSD_MODE_UNDEFINED:
245 default:
8112f5fa 246#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a
PK
247 if (err)
248 puts("spl: mmc: no boot mode left to try\n");
249 else
250 puts("spl: mmc: wrong boot mode\n");
8112f5fa 251#endif
79adb7a2 252 hang();
91199f4a 253 }
9ea5c6ef 254}