Merge branch '2021-09-24-arm64-optimized-str-funcs' into next
[platform/kernel/u-boot.git] / common / spl / spl_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010
4  * Texas Instruments, <www.ti.com>
5  *
6  * Aneesh V <aneesh@ti.com>
7  */
8 #include <common.h>
9 #include <dm.h>
10 #include <log.h>
11 #include <part.h>
12 #include <spl.h>
13 #include <linux/compiler.h>
14 #include <errno.h>
15 #include <asm/u-boot.h>
16 #include <errno.h>
17 #include <mmc.h>
18 #include <image.h>
19
20 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc,
21                            ulong sector, struct image_header *header)
22 {
23         u32 image_offset_sectors;
24         u32 image_size_sectors;
25         unsigned long count;
26         u32 image_offset;
27         int ret;
28
29         ret = spl_parse_image_header(spl_image, header);
30         if (ret)
31                 return ret;
32
33         /* convert offset to sectors - round down */
34         image_offset_sectors = spl_image->offset / mmc->read_bl_len;
35         /* calculate remaining offset */
36         image_offset = spl_image->offset % mmc->read_bl_len;
37
38         /* convert size to sectors - round up */
39         image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) /
40                              mmc->read_bl_len;
41
42         /* Read the header too to avoid extra memcpy */
43         count = blk_dread(mmc_get_blk_desc(mmc),
44                           sector + image_offset_sectors,
45                           image_size_sectors,
46                           (void *)(ulong)spl_image->load_addr);
47         debug("read %x sectors to %lx\n", image_size_sectors,
48               spl_image->load_addr);
49         if (count != image_size_sectors)
50                 return -EIO;
51
52         if (image_offset)
53                 memmove((void *)(ulong)spl_image->load_addr,
54                         (void *)(ulong)spl_image->load_addr + image_offset,
55                         spl_image->size);
56
57         return 0;
58 }
59
60 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
61                              ulong count, void *buf)
62 {
63         struct mmc *mmc = load->dev;
64
65         return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf);
66 }
67
68 static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
69 {
70 #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
71         if (part == 0)
72                 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
73 #endif
74
75         return 0;
76 }
77
78 static __maybe_unused
79 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
80                               struct mmc *mmc, unsigned long sector)
81 {
82         unsigned long count;
83         struct image_header *header;
84         struct blk_desc *bd = mmc_get_blk_desc(mmc);
85         int ret = 0;
86
87         header = spl_get_load_buffer(-sizeof(*header), bd->blksz);
88
89         /* read image header to find the image size & load address */
90         count = blk_dread(bd, sector, 1, header);
91         debug("hdr read sector %lx, count=%lu\n", sector, count);
92         if (count == 0) {
93                 ret = -EIO;
94                 goto end;
95         }
96
97         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
98             image_get_magic(header) == FDT_MAGIC) {
99                 struct spl_load_info load;
100
101                 debug("Found FIT\n");
102                 load.dev = mmc;
103                 load.priv = NULL;
104                 load.filename = NULL;
105                 load.bl_len = mmc->read_bl_len;
106                 load.read = h_spl_load_read;
107                 ret = spl_load_simple_fit(spl_image, &load, sector, header);
108         } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
109                 struct spl_load_info load;
110
111                 load.dev = mmc;
112                 load.priv = NULL;
113                 load.filename = NULL;
114                 load.bl_len = mmc->read_bl_len;
115                 load.read = h_spl_load_read;
116
117                 ret = spl_load_imx_container(spl_image, &load, sector);
118         } else {
119                 ret = mmc_load_legacy(spl_image, mmc, sector, header);
120         }
121
122 end:
123         if (ret) {
124 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125                 puts("mmc_load_image_raw_sector: mmc block read error\n");
126 #endif
127                 return -1;
128         }
129
130         return 0;
131 }
132
133 static int spl_mmc_get_device_index(u32 boot_device)
134 {
135         switch (boot_device) {
136         case BOOT_DEVICE_MMC1:
137                 return 0;
138         case BOOT_DEVICE_MMC2:
139         case BOOT_DEVICE_MMC2_2:
140                 return 1;
141         }
142
143 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
144         printf("spl: unsupported mmc boot device.\n");
145 #endif
146
147         return -ENODEV;
148 }
149
150 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
151 {
152         int err, mmc_dev;
153
154         mmc_dev = spl_mmc_get_device_index(boot_device);
155         if (mmc_dev < 0)
156                 return mmc_dev;
157
158 #if CONFIG_IS_ENABLED(DM_MMC)
159         err = mmc_init_device(mmc_dev);
160 #else
161         err = mmc_initialize(NULL);
162 #endif /* DM_MMC */
163         if (err) {
164 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
165                 printf("spl: could not initialize mmc. error: %d\n", err);
166 #endif
167                 return err;
168         }
169         *mmcp = find_mmc_device(mmc_dev);
170         err = *mmcp ? 0 : -ENODEV;
171         if (err) {
172 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
173                 printf("spl: could not find mmc device %d. error: %d\n",
174                        mmc_dev, err);
175 #endif
176                 return err;
177         }
178
179         return 0;
180 }
181
182 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
183 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
184                                         struct mmc *mmc, int partition,
185                                         unsigned long sector)
186 {
187         struct disk_partition info;
188         int err;
189
190 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
191         int type_part;
192         /* Only support MBR so DOS_ENTRY_NUMBERS */
193         for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
194                 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
195                 if (err)
196                         continue;
197                 if (info.sys_ind == 
198                         CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
199                         partition = type_part;
200                         break;
201                 }
202         }
203 #endif
204
205         err = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
206         if (err) {
207 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
208                 puts("spl: partition error\n");
209 #endif
210                 return -1;
211         }
212
213 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
214         return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector);
215 #else
216         return mmc_load_image_raw_sector(spl_image, mmc, info.start);
217 #endif
218 }
219 #endif
220
221 #ifdef CONFIG_SPL_OS_BOOT
222 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
223                                  struct mmc *mmc)
224 {
225         int ret;
226
227 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
228         unsigned long count;
229
230         count = blk_dread(mmc_get_blk_desc(mmc),
231                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
232                 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
233                 (void *) CONFIG_SYS_SPL_ARGS_ADDR);
234         if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
235 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
236                 puts("mmc_load_image_raw_os: mmc block read error\n");
237 #endif
238                 return -1;
239         }
240 #endif  /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
241
242         ret = mmc_load_image_raw_sector(spl_image, mmc,
243                 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
244         if (ret)
245                 return ret;
246
247         if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
248                 puts("Expected image is not found. Trying to start U-boot\n");
249                 return -ENOENT;
250         }
251
252         return 0;
253 }
254 #else
255 int spl_start_uboot(void)
256 {
257         return 1;
258 }
259 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
260                                  struct mmc *mmc)
261 {
262         return -ENOSYS;
263 }
264 #endif
265
266 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
267 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
268                               const char *filename)
269 {
270         int err = -ENOSYS;
271
272 #ifdef CONFIG_SPL_FS_FAT
273         if (!spl_start_uboot()) {
274                 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc),
275                         CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
276                 if (!err)
277                         return err;
278         }
279 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
280         err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc),
281                                  CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
282                                  filename);
283         if (!err)
284                 return err;
285 #endif
286 #endif
287 #ifdef CONFIG_SPL_FS_EXT4
288         if (!spl_start_uboot()) {
289                 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc),
290                         CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
291                 if (!err)
292                         return err;
293         }
294 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
295         err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc),
296                                  CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
297                                  filename);
298         if (!err)
299                 return err;
300 #endif
301 #endif
302
303 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
304         err = -ENOENT;
305 #endif
306
307         return err;
308 }
309 #else
310 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc,
311                               const char *filename)
312 {
313         return -ENOSYS;
314 }
315 #endif
316
317 u32 __weak spl_mmc_boot_mode(const u32 boot_device)
318 {
319 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
320         return MMCSD_MODE_FS;
321 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
322         return MMCSD_MODE_EMMCBOOT;
323 #else
324         return MMCSD_MODE_RAW;
325 #endif
326 }
327
328 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
329 int __weak spl_mmc_boot_partition(const u32 boot_device)
330 {
331         return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
332 }
333 #endif
334
335 unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
336                                                   unsigned long raw_sect)
337 {
338         return raw_sect;
339 }
340
341 int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
342 {
343         int part;
344 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
345         part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
346 #else
347         /*
348          * We need to check what the partition is configured to.
349          * 1 and 2 match up to boot0 / boot1 and 7 is user data
350          * which is the first physical partition (0).
351          */
352         part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
353         if (part == 7)
354                 part = 0;
355 #endif
356         return part;
357 }
358
359 int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
360 {
361         return default_spl_mmc_emmc_boot_partition(mmc);
362 }
363
364 int spl_mmc_load(struct spl_image_info *spl_image,
365                  struct spl_boot_device *bootdev,
366                  const char *filename,
367                  int raw_part,
368                  unsigned long raw_sect)
369 {
370         static struct mmc *mmc;
371         u32 boot_mode;
372         int err = 0;
373         __maybe_unused int part = 0;
374
375         /* Perform peripheral init only once */
376         if (!mmc) {
377                 err = spl_mmc_find_device(&mmc, bootdev->boot_device);
378                 if (err)
379                         return err;
380
381                 err = mmc_init(mmc);
382                 if (err) {
383                         mmc = NULL;
384 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
385                         printf("spl: mmc init failed with error: %d\n", err);
386 #endif
387                         return err;
388                 }
389         }
390
391         boot_mode = spl_mmc_boot_mode(bootdev->boot_device);
392         err = -EINVAL;
393         switch (boot_mode) {
394         case MMCSD_MODE_EMMCBOOT:
395                 part = spl_mmc_emmc_boot_partition(mmc);
396
397                 if (CONFIG_IS_ENABLED(MMC_TINY))
398                         err = mmc_switch_part(mmc, part);
399                 else
400                         err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
401
402                 if (err) {
403 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
404                         puts("spl: mmc partition switch failed\n");
405 #endif
406                         return err;
407                 }
408                 /* Fall through */
409         case MMCSD_MODE_RAW:
410                 debug("spl: mmc boot mode: raw\n");
411
412                 if (!spl_start_uboot()) {
413                         err = mmc_load_image_raw_os(spl_image, mmc);
414                         if (!err)
415                                 return err;
416                 }
417
418                 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
419
420 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
421                 err = mmc_load_image_raw_partition(spl_image, mmc, raw_part,
422                                                    raw_sect);
423                 if (!err)
424                         return err;
425 #endif
426 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
427                 err = mmc_load_image_raw_sector(spl_image, mmc,
428                                 raw_sect + spl_mmc_raw_uboot_offset(part));
429                 if (!err)
430                         return err;
431 #endif
432                 /* If RAW mode fails, try FS mode. */
433         case MMCSD_MODE_FS:
434                 debug("spl: mmc boot mode: fs\n");
435
436                 err = spl_mmc_do_fs_boot(spl_image, mmc, filename);
437                 if (!err)
438                         return err;
439
440                 break;
441 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
442         default:
443                 puts("spl: mmc: wrong boot mode\n");
444 #endif
445         }
446
447         return err;
448 }
449
450 int spl_mmc_load_image(struct spl_image_info *spl_image,
451                        struct spl_boot_device *bootdev)
452 {
453         return spl_mmc_load(spl_image, bootdev,
454 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
455                             CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
456 #else
457                             NULL,
458 #endif
459 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
460                             spl_mmc_boot_partition(bootdev->boot_device),
461 #else
462                             0,
463 #endif
464 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
465                             CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
466 #else
467                             0);
468 #endif
469 }
470
471 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
472 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
473 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);