xref: /rk3399_rockchip-uboot/common/spl/spl_ufs.c (revision 2d51105d7c4099e1cf405e072d1fc2670416af41)
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 
h_spl_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)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 
spl_ufs_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)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 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
58 		header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
59 					 sizeof(struct image_header));
60 		ret = blk_dread(desc, image_sector, 1, header);
61 		if (ret != 1)
62 			return -ENODEV;
63 
64 #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
65 		if (image_get_magic(header) == FDT_MAGIC ||
66 		    CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
67 #else
68 		if (image_get_magic(header) == FDT_MAGIC) {
69 #endif
70 			ret = spl_load_simple_fit(spl_image, &load,
71 						  image_sector,
72 						  header);
73 		}
74 	}
75 
76 	return ret;
77 }
78 SPL_LOAD_IMAGE_METHOD("UFS", 0, BOOT_DEVICE_UFS, spl_ufs_load_image);
79