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