]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/spl/spl_mmc.c
Merge git://git.denx.de/u-boot-marvell
[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,
fdfa39d3
SG
47 (void *)spl_image.load_addr);
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
76 return mmc_load_image_raw_sector(mmc, info.start);
77}
78#endif
79
2b75b0ad
PK
80#ifdef CONFIG_SPL_OS_BOOT
81static int mmc_load_image_raw_os(struct mmc *mmc)
82{
3bc37b6d 83 unsigned long count;
91199f4a 84
3bc37b6d
PK
85 count = mmc->block_dev.block_read(0,
86 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
87 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
88 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
89 if (count == 0) {
8112f5fa 90#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
1ec26469 91 puts("spl: mmc block read error\n");
8112f5fa 92#endif
2b75b0ad
PK
93 return -1;
94 }
95
b97300b6 96 return mmc_load_image_raw_sector(mmc,
91199f4a 97 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
2b75b0ad
PK
98}
99#endif
100
9ea5c6ef
SS
101void spl_mmc_load_image(void)
102{
103 struct mmc *mmc;
9ea5c6ef 104 u32 boot_mode;
dc3dedfe 105 int err = 0;
91199f4a 106 __maybe_unused int part;
9ea5c6ef 107
dc3dedfe
SG
108#ifdef CONFIG_DM_MMC
109 struct udevice *dev;
110
111 mmc_initialize(NULL);
112 err = uclass_get_device(UCLASS_MMC, 0, &dev);
113 mmc = NULL;
114 if (!err)
115 mmc = mmc_get_mmc_dev(dev);
116#else
9ea5c6ef 117 mmc_initialize(gd->bd);
91199f4a 118
9ea5c6ef
SS
119 /* We register only one device. So, the dev id is always 0 */
120 mmc = find_mmc_device(0);
121 if (!mmc) {
8112f5fa 122#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 123 puts("spl: mmc device not found\n");
8112f5fa 124#endif
9ea5c6ef
SS
125 hang();
126 }
dc3dedfe
SG
127#endif
128
129 if (!err)
130 err = mmc_init(mmc);
9ea5c6ef 131
9ea5c6ef 132 if (err) {
8112f5fa 133#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 134 printf("spl: mmc init failed with error: %d\n", err);
8112f5fa 135#endif
9ea5c6ef
SS
136 hang();
137 }
79adb7a2 138
37189a19 139 boot_mode = spl_boot_mode();
91199f4a
PK
140 switch (boot_mode) {
141 case MMCSD_MODE_RAW:
142 debug("spl: mmc boot mode: raw\n");
143
2b75b0ad 144#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
145 if (!spl_start_uboot()) {
146 err = mmc_load_image_raw_os(mmc);
147 if (!err)
148 return;
149 }
2b75b0ad 150#endif
3ae8f4c8 151#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
b97300b6
PK
152 err = mmc_load_image_raw_partition(mmc,
153 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
3ae8f4c8
PK
154 if (!err)
155 return;
156#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
b97300b6 157 err = mmc_load_image_raw_sector(mmc,
773b5940 158 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
91199f4a
PK
159 if (!err)
160 return;
3ae8f4c8 161#endif
91199f4a
PK
162 case MMCSD_MODE_FS:
163 debug("spl: mmc boot mode: fs\n");
164
3ae8f4c8 165#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
592f9222 166#ifdef CONFIG_SPL_FAT_SUPPORT
7ad2cc79 167#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
168 if (!spl_start_uboot()) {
169 err = spl_load_image_fat_os(&mmc->block_dev,
170 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
171 if (!err)
172 return;
173 }
7ad2cc79 174#endif
3ae8f4c8 175#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
773b5940 176 err = spl_load_image_fat(&mmc->block_dev,
91199f4a
PK
177 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
178 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
179 if (!err)
180 return;
181#endif
3ae8f4c8 182#endif
592f9222
GG
183#ifdef CONFIG_SPL_EXT_SUPPORT
184#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
185 if (!spl_start_uboot()) {
186 err = spl_load_image_ext_os(&mmc->block_dev,
187 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
188 if (!err)
189 return;
190 }
7dbe63bc 191#endif
3ae8f4c8 192#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
592f9222 193 err = spl_load_image_ext(&mmc->block_dev,
91199f4a
PK
194 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
195 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
196 if (!err)
197 return;
198#endif
199#endif
3ae8f4c8 200#endif
7dbe63bc 201#ifdef CONFIG_SUPPORT_EMMC_BOOT
91199f4a 202 case MMCSD_MODE_EMMCBOOT:
7dbe63bc
TR
203 /*
204 * We need to check what the partition is configured to.
205 * 1 and 2 match up to boot0 / boot1 and 7 is user data
206 * which is the first physical partition (0).
207 */
91199f4a 208 part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
7dbe63bc
TR
209
210 if (part == 7)
211 part = 0;
212
213 if (mmc_switch_part(0, part)) {
214#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a 215 puts("spl: mmc partition switch failed\n");
7dbe63bc
TR
216#endif
217 hang();
218 }
91199f4a 219
7dbe63bc 220#ifdef CONFIG_SPL_OS_BOOT
91199f4a
PK
221 if (!spl_start_uboot()) {
222 err = mmc_load_image_raw_os(mmc);
223 if (!err)
224 return;
225 }
7dbe63bc 226#endif
3ae8f4c8 227#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION)
ecb30139
PK
228 err = mmc_load_image_raw_partition(mmc,
229 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
3ae8f4c8
PK
230 if (!err)
231 return;
232#elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
b97300b6 233 err = mmc_load_image_raw_sector(mmc,
7dbe63bc 234 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
91199f4a
PK
235 if (!err)
236 return;
3ae8f4c8 237#endif
0da113e9 238#endif
91199f4a
PK
239 case MMCSD_MODE_UNDEFINED:
240 default:
8112f5fa 241#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91199f4a
PK
242 if (err)
243 puts("spl: mmc: no boot mode left to try\n");
244 else
245 puts("spl: mmc: wrong boot mode\n");
8112f5fa 246#endif
79adb7a2 247 hang();
91199f4a 248 }
9ea5c6ef 249}