xref: /rk3399_rockchip-uboot/common/spl/spl_ufs.c (revision 0aca89f213e8a80b8eff5385303341b2304c527d)
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 
40 	desc = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
41 	if (!desc)
42 		return -ENODEV;
43 
44 	load.dev = desc;
45 	load.priv = NULL;
46 	load.filename = NULL;
47 	load.bl_len = desc->blksz;
48 	load.read = h_spl_load_read;
49 
50 #ifdef CONFIG_SPL_LIBDISK_SUPPORT
51 	disk_partition_t info;
52 
53 	ret = part_get_info_by_name(desc, PART_UBOOT, &info);
54 	if (ret > 0)
55 		image_sector = info.start;
56 #endif
57 
58 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
59 		header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
60 					 sizeof(struct image_header));
61 		ret = blk_dread(desc, image_sector, 1, header);
62 		if (ret != 1)
63 			return -ENODEV;
64 
65 #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
66 		if (image_get_magic(header) == FDT_MAGIC ||
67 		    CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
68 #else
69 		if (image_get_magic(header) == FDT_MAGIC) {
70 #endif
71 			ret = spl_load_simple_fit(spl_image, &load,
72 						  image_sector,
73 						  header);
74 		}
75 	}
76 
77 	if (!ret)
78 		return 0;
79 
80 	return 0;
81 }
82 SPL_LOAD_IMAGE_METHOD("UFS", 0, BOOT_DEVICE_UFS, spl_ufs_load_image);
83