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