Merge https://gitlab.denx.de/u-boot/custodians/u-boot-marvell
[oweals/u-boot.git] / common / spl / spl_sata.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2013
4  * Texas Instruments, <www.ti.com>
5  *
6  * Dan Murphy <dmurphy@ti.com>
7  *
8  * Derived work from spl_usb.c
9  */
10
11 #include <common.h>
12 #include <spl.h>
13 #include <asm/u-boot.h>
14 #include <sata.h>
15 #include <scsi.h>
16 #include <errno.h>
17 #include <fat.h>
18 #include <image.h>
19
20 #ifndef CONFIG_SYS_SATA_FAT_BOOT_PARTITION
21 #define CONFIG_SYS_SATA_FAT_BOOT_PARTITION      1
22 #endif
23
24 #ifndef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
25 #define CONFIG_SPL_FS_LOAD_PAYLOAD_NAME "u-boot.img"
26 #endif
27
28 static int spl_sata_load_image(struct spl_image_info *spl_image,
29                                struct spl_boot_device *bootdev)
30 {
31         int err = 0;
32         struct blk_desc *stor_dev;
33
34 #if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI)
35         err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE);
36 #endif
37         if (err) {
38 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
39                 printf("spl: sata init failed: err - %d\n", err);
40 #endif
41                 return err;
42         } else {
43                 /* try to recognize storage devices immediately */
44                 scsi_scan(false);
45                 stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
46                 if (!stor_dev)
47                         return -ENODEV;
48         }
49
50 #ifdef CONFIG_SPL_OS_BOOT
51         if (spl_start_uboot() ||
52             spl_load_image_fat_os(spl_image, stor_dev,
53                                   CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
54 #endif
55         {
56                 err = -ENOSYS;
57
58                 if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
59                         err = spl_load_image_fat(spl_image, stor_dev,
60                                         CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
61                                         CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
62                 }
63         }
64         if (err) {
65                 puts("Error loading sata device\n");
66                 return err;
67         }
68
69         return 0;
70 }
71 SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);