xref: /rk3399_rockchip-uboot/common/spl/spl_mtd_blk.c (revision 028a3c087958354767360c818b7df81ef657708a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <blk.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <image.h>
11 #include <malloc.h>
12 #include <part.h>
13 #include <spl.h>
14 #include <spl_ab.h>
15 #include <spl_rkfw.h>
16 #include <asm/u-boot.h>
17 #include <dm/device-internal.h>
18 #include <linux/compiler.h>
19 #include <linux/mtd/mtd.h>
20 
21 static int spl_mtd_get_device_index(u32 boot_device)
22 {
23 	switch (boot_device) {
24 	case BOOT_DEVICE_MTD_BLK_NAND:
25 		return 0;
26 	case BOOT_DEVICE_MTD_BLK_SPI_NAND:
27 		return 1;
28 	case BOOT_DEVICE_MTD_BLK_SPI_NOR:
29 		return 2;
30 	}
31 
32 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
33 	printf("spl: unsupported mtd boot device.\n");
34 #endif
35 
36 	return -ENODEV;
37 }
38 
39 struct blk_desc *find_mtd_device(int dev_num)
40 {
41 	struct udevice *dev;
42 	struct blk_desc *desc;
43 	int ret;
44 
45 	ret = blk_find_device(IF_TYPE_MTD, dev_num, &dev);
46 
47 	if (ret) {
48 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
49 		printf("MTD Device %d not found\n", dev_num);
50 #endif
51 		return NULL;
52 	}
53 
54 	ret = device_probe(dev);
55 	if (ret) {
56 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
57 		printf("MTD Device %d not found\n", dev_num);
58 #endif
59 		return NULL;
60 	}
61 
62 	desc = dev_get_uclass_platdata(dev);
63 	if (!desc)
64 		return NULL;
65 
66 	return desc;
67 }
68 
69 #ifdef CONFIG_SPL_LOAD_RKFW
70 static ulong mtd_spl_load_read(struct spl_load_info *load, ulong sector,
71 			       ulong count, void *buf)
72 {
73 	struct blk_desc *desc = load->dev;
74 
75 	return blk_dread(desc, sector, count, buf);
76 }
77 
78 int spl_mtd_load_rkfw(struct spl_image_info *spl_image, struct blk_desc *desc)
79 {
80 	int ret = -1;
81 
82 	u32 trust_sectors = CONFIG_RKFW_TRUST_SECTOR;
83 	u32 uboot_sectors = CONFIG_RKFW_U_BOOT_SECTOR;
84 	struct spl_load_info load;
85 
86 	load.dev = desc;
87 	load.priv = NULL;
88 	load.filename = NULL;
89 	load.bl_len = desc->blksz;
90 	load.read = mtd_spl_load_read;
91 
92 #ifdef CONFIG_SPL_AB
93 	char trust_partition[] = "trust";
94 	char uboot_partition[] = "uboot";
95 
96 	spl_get_partitions_sector(desc, trust_partition,
97 				  &trust_sectors);
98 	spl_get_partitions_sector(desc, uboot_partition,
99 				  &uboot_sectors);
100 #endif
101 
102 	ret = spl_load_rkfw_image(spl_image, &load,
103 				  trust_sectors,
104 				  uboot_sectors);
105 	if (ret) {
106 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
107 		puts("spl_mtd_load_rkfw: mtd block read error\n");
108 #endif
109 		return -1;
110 	}
111 
112 	return ret;
113 }
114 #endif
115 
116 int spl_mtd_load_image(struct spl_image_info *spl_image,
117 		       struct spl_boot_device *bootdev)
118 {
119 	struct blk_desc *desc;
120 	int ret = 0;
121 
122 	desc = find_mtd_device(spl_mtd_get_device_index(bootdev->boot_device));
123 	if (!desc)
124 		return -ENODEV;
125 #ifdef CONFIG_SPL_LOAD_RKFW
126 	ret = spl_mtd_load_rkfw(spl_image, desc);
127 #endif
128 	return ret;
129 }
130 
131 SPL_LOAD_IMAGE_METHOD("MTD1", 0, BOOT_DEVICE_MTD_BLK_NAND, spl_mtd_load_image);
132 SPL_LOAD_IMAGE_METHOD("MTD2", 0, BOOT_DEVICE_MTD_BLK_SPI_NAND, spl_mtd_load_image);
133 SPL_LOAD_IMAGE_METHOD("MTD3", 0, BOOT_DEVICE_MTD_BLK_SPI_NOR, spl_mtd_load_image);
134