xref: /rk3399_rockchip-uboot/common/spl/spl_ufs.c (revision b2b4c2f52306f77e9c04f19015bd6573c3ad8c4a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Rockchip UFS Host Controller driver
4  *
5  * Copyright (C) 2024 Rockchip Electronics Co.Ltd.
6  */
7 
8 #include <common.h>
9 #include <asm/u-boot.h>
10 #include <blk.h>
11 #include <boot_rkimg.h>
12 #include <errno.h>
13 #include <image.h>
14 #include <part.h>
15 #include <scsi.h>
16 #include <ufs.h>
17 #include <spl.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
22 			     ulong count, void *buf)
23 {
24 	return blk_dread(load->dev, sector, count, buf);
25 }
26 
27 static int spl_ufs_load_image(struct spl_image_info *spl_image,
28 			       struct spl_boot_device *bootdev)
29 {
30 	lbaint_t image_sector = CONFIG_SYS_UFS_RAW_MODE_U_BOOT_SECTOR;
31 	int ret;
32 	struct blk_desc *desc;
33 	struct image_header *header;
34 	struct spl_load_info load;
35 
36 	/* try to recognize storage devices immediately */
37 	ufs_probe();
38 	scsi_scan(true);
39 	scsi_scan(true);
40 
41 	desc = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
42 	if (!desc)
43 		return -ENODEV;
44 
45 	load.dev = desc;
46 	load.priv = NULL;
47 	load.filename = NULL;
48 	load.bl_len = desc->blksz;
49 	load.read = h_spl_load_read;
50 
51 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
52 	disk_partition_t info;
53 
54 	ret = part_get_info_by_name(desc, PART_UBOOT, &info);
55 	if (ret > 0)
56 		image_sector = info.start;
57 #endif
58 
59 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
60 		header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
61 					 sizeof(struct image_header));
62 		ret = blk_dread(desc, image_sector, 1, header);
63 		if (ret != 1)
64 			return -ENODEV;
65 
66 #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
67 		if (image_get_magic(header) == FDT_MAGIC ||
68 		    CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
69 #else
70 		if (image_get_magic(header) == FDT_MAGIC) {
71 #endif
72 			ret = spl_load_simple_fit(spl_image, &load,
73 						  image_sector,
74 						  header);
75 		}
76 	}
77 
78 	if (!ret)
79 		return 0;
80 
81 	return 0;
82 }
83 SPL_LOAD_IMAGE_METHOD("UFS", 0, BOOT_DEVICE_UFS, spl_ufs_load_image);
84