xref: /rk3399_rockchip-uboot/common/spl/spl_mtd_blk.c (revision b5f6b28fa3454b1189d8fefe01a26dd09f2e3f1e)
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 	u32 boot_sectors = CONFIG_RKFW_BOOT_SECTOR;
85 	struct spl_load_info load;
86 
87 	load.dev = desc;
88 	load.priv = NULL;
89 	load.filename = NULL;
90 	load.bl_len = desc->blksz;
91 	load.read = mtd_spl_load_read;
92 
93 #ifdef CONFIG_SPL_AB
94 	char trust_partition[] = "trust";
95 	char uboot_partition[] = "uboot";
96 
97 	spl_get_partitions_sector(desc, trust_partition,
98 				  &trust_sectors);
99 	spl_get_partitions_sector(desc, uboot_partition,
100 				  &uboot_sectors);
101 #endif
102 
103 	ret = spl_load_rkfw_image(spl_image, &load,
104 				  trust_sectors,
105 				  uboot_sectors,
106 				  boot_sectors);
107 	if (ret) {
108 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
109 		puts("spl_mtd_load_rkfw: mtd block read error\n");
110 #endif
111 		return -1;
112 	}
113 
114 	return ret;
115 }
116 #endif
117 
118 int spl_mtd_load_image(struct spl_image_info *spl_image,
119 		       struct spl_boot_device *bootdev)
120 {
121 	struct blk_desc *desc;
122 	int ret = 0;
123 
124 	desc = find_mtd_device(spl_mtd_get_device_index(bootdev->boot_device));
125 	if (!desc)
126 		return -ENODEV;
127 #ifdef CONFIG_SPL_LOAD_RKFW
128 	ret = spl_mtd_load_rkfw(spl_image, desc);
129 #endif
130 	return ret;
131 }
132 
133 SPL_LOAD_IMAGE_METHOD("MTD1", 0, BOOT_DEVICE_MTD_BLK_NAND, spl_mtd_load_image);
134 SPL_LOAD_IMAGE_METHOD("MTD2", 0, BOOT_DEVICE_MTD_BLK_SPI_NAND, spl_mtd_load_image);
135 SPL_LOAD_IMAGE_METHOD("MTD3", 0, BOOT_DEVICE_MTD_BLK_SPI_NOR, spl_mtd_load_image);
136