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