xref: /rk3399_rockchip-uboot/common/spl/spl_rknand.c (revision ffc357038c3450975a8295e237cc942c1c95a33a)
1*ffc35703SYifeng Zhao /*
2*ffc35703SYifeng Zhao  * Copyright (c) 2020 Rockchip Electronics Co., Ltd
3*ffc35703SYifeng Zhao  *
4*ffc35703SYifeng Zhao  * SPDX-License-Identifier:     GPL-2.0+
5*ffc35703SYifeng Zhao  */
6*ffc35703SYifeng Zhao 
7*ffc35703SYifeng Zhao #include <common.h>
8*ffc35703SYifeng Zhao #include <blk.h>
9*ffc35703SYifeng Zhao #include <boot_rkimg.h>
10*ffc35703SYifeng Zhao #include <dm.h>
11*ffc35703SYifeng Zhao #include <errno.h>
12*ffc35703SYifeng Zhao #include <image.h>
13*ffc35703SYifeng Zhao #include <malloc.h>
14*ffc35703SYifeng Zhao #include <mtd_blk.h>
15*ffc35703SYifeng Zhao #include <part.h>
16*ffc35703SYifeng Zhao #include <spl.h>
17*ffc35703SYifeng Zhao #include <spl_ab.h>
18*ffc35703SYifeng Zhao #include <spl_rkfw.h>
19*ffc35703SYifeng Zhao #include <asm/u-boot.h>
20*ffc35703SYifeng Zhao #include <dm/device-internal.h>
21*ffc35703SYifeng Zhao #include <linux/compiler.h>
22*ffc35703SYifeng Zhao 
find_rknand_device(int dev_num)23*ffc35703SYifeng Zhao static struct blk_desc *find_rknand_device(int dev_num)
24*ffc35703SYifeng Zhao {
25*ffc35703SYifeng Zhao 	struct udevice *dev;
26*ffc35703SYifeng Zhao 	struct blk_desc *desc;
27*ffc35703SYifeng Zhao 	int ret;
28*ffc35703SYifeng Zhao 
29*ffc35703SYifeng Zhao 	ret = blk_find_device(IF_TYPE_RKNAND, dev_num, &dev);
30*ffc35703SYifeng Zhao 	if (ret)
31*ffc35703SYifeng Zhao 		return NULL;
32*ffc35703SYifeng Zhao 
33*ffc35703SYifeng Zhao 	ret = device_probe(dev);
34*ffc35703SYifeng Zhao 	if (ret) {
35*ffc35703SYifeng Zhao 		debug("RKNAND Device %d not found\n", dev_num);
36*ffc35703SYifeng Zhao 		return NULL;
37*ffc35703SYifeng Zhao 	}
38*ffc35703SYifeng Zhao 
39*ffc35703SYifeng Zhao 	desc = dev_get_uclass_platdata(dev);
40*ffc35703SYifeng Zhao 	if (!desc)
41*ffc35703SYifeng Zhao 		return NULL;
42*ffc35703SYifeng Zhao 
43*ffc35703SYifeng Zhao 	return desc;
44*ffc35703SYifeng Zhao }
45*ffc35703SYifeng Zhao 
h_spl_load_read(struct spl_load_info * load,ulong sector,ulong count,void * buf)46*ffc35703SYifeng Zhao static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
47*ffc35703SYifeng Zhao 			     ulong count, void *buf)
48*ffc35703SYifeng Zhao {
49*ffc35703SYifeng Zhao 	return blk_dread(load->dev, sector, count, buf);
50*ffc35703SYifeng Zhao }
51*ffc35703SYifeng Zhao 
spl_rknand_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)52*ffc35703SYifeng Zhao static int spl_rknand_load_image(struct spl_image_info *spl_image,
53*ffc35703SYifeng Zhao 				 struct spl_boot_device *bootdev)
54*ffc35703SYifeng Zhao {
55*ffc35703SYifeng Zhao 	lbaint_t image_sector = CONFIG_RKNAND_BLK_U_BOOT_OFFS;
56*ffc35703SYifeng Zhao 	struct image_header *header;
57*ffc35703SYifeng Zhao 	struct spl_load_info load;
58*ffc35703SYifeng Zhao 	struct blk_desc *desc;
59*ffc35703SYifeng Zhao 	int ret = -1;
60*ffc35703SYifeng Zhao 
61*ffc35703SYifeng Zhao 	desc = find_rknand_device(0);
62*ffc35703SYifeng Zhao 	if (!desc)
63*ffc35703SYifeng Zhao 		return -ENODEV;
64*ffc35703SYifeng Zhao 
65*ffc35703SYifeng Zhao 	load.dev = desc;
66*ffc35703SYifeng Zhao 	load.priv = NULL;
67*ffc35703SYifeng Zhao 	load.filename = NULL;
68*ffc35703SYifeng Zhao 	load.bl_len = desc->blksz;
69*ffc35703SYifeng Zhao 	load.read = h_spl_load_read;
70*ffc35703SYifeng Zhao 
71*ffc35703SYifeng Zhao #ifdef CONFIG_SPL_LIBDISK_SUPPORT
72*ffc35703SYifeng Zhao 	disk_partition_t info;
73*ffc35703SYifeng Zhao 
74*ffc35703SYifeng Zhao 	ret = part_get_info_by_name(desc, PART_UBOOT, &info);
75*ffc35703SYifeng Zhao 	if (ret > 0)
76*ffc35703SYifeng Zhao 		image_sector = info.start;
77*ffc35703SYifeng Zhao #endif
78*ffc35703SYifeng Zhao 
79*ffc35703SYifeng Zhao 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) {
80*ffc35703SYifeng Zhao 		header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
81*ffc35703SYifeng Zhao 					 sizeof(struct image_header));
82*ffc35703SYifeng Zhao 		ret = blk_dread(desc, image_sector, 1, header);
83*ffc35703SYifeng Zhao 		if (ret != 1)
84*ffc35703SYifeng Zhao 			return -ENODEV;
85*ffc35703SYifeng Zhao 
86*ffc35703SYifeng Zhao #ifdef CONFIG_SPL_FIT_IMAGE_MULTIPLE
87*ffc35703SYifeng Zhao 		if (image_get_magic(header) == FDT_MAGIC ||
88*ffc35703SYifeng Zhao 		    CONFIG_SPL_FIT_IMAGE_MULTIPLE > 1) {
89*ffc35703SYifeng Zhao #else
90*ffc35703SYifeng Zhao 		if (image_get_magic(header) == FDT_MAGIC) {
91*ffc35703SYifeng Zhao #endif
92*ffc35703SYifeng Zhao 			ret = spl_load_simple_fit(spl_image, &load,
93*ffc35703SYifeng Zhao 						  image_sector,
94*ffc35703SYifeng Zhao 						  header);
95*ffc35703SYifeng Zhao 		}
96*ffc35703SYifeng Zhao 	}
97*ffc35703SYifeng Zhao 
98*ffc35703SYifeng Zhao 	if (!ret)
99*ffc35703SYifeng Zhao 		return 0;
100*ffc35703SYifeng Zhao 
101*ffc35703SYifeng Zhao #ifdef CONFIG_SPL_LOAD_RKFW
102*ffc35703SYifeng Zhao 	ret = spl_load_rkfw_image(spl_image, &load);
103*ffc35703SYifeng Zhao #endif
104*ffc35703SYifeng Zhao 
105*ffc35703SYifeng Zhao 	return ret;
106*ffc35703SYifeng Zhao }
107*ffc35703SYifeng Zhao 
108*ffc35703SYifeng Zhao SPL_LOAD_IMAGE_METHOD("RKNAND", 0, BOOT_DEVICE_RKNAND, spl_rknand_load_image);
109