1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Driver interface derived from:
4  * /include/sandbox_host.h
5  * Copyright 2022 Google LLC
6  *
7  * Copyright 2023 Johan Jonker <jbx6244@gmail.com>
8  */
9 
10 #ifndef __RKMTD__
11 #define __RKMTD__
12 
13 #include <part_efi.h>
14 #include <u-boot/uuid.h>
15 
16 #define LBA			64 + 512 + 33
17 
18 #define RK_TAG			0xFCDC8C3B
19 #define NFC_SYS_DATA_SIZE	4
20 #define BLK_SIZE		2048
21 #define STEP_SIZE		1024
22 #define BUF_SIZE		512 * 512
23 
24 struct nand_para_info {
25 	u8 id_bytes;
26 	u8 nand_id[6];
27 	u8 vendor;
28 	u8 die_per_chip;
29 	u8 sec_per_page;
30 	u16 page_per_blk;
31 	u8 cell;
32 	u8 plane_per_die;
33 	u16 blk_per_plane;
34 	u16 operation_opt;
35 	u8 lsb_mode;
36 	u8 read_retry_mode;
37 	u8 ecc_bits;
38 	u8 access_freq;
39 	u8 opt_mode;
40 	u8 die_gap;
41 	u8 bad_block_mode;
42 	u8 multi_plane_mode;
43 	u8 slc_mode;
44 	u8 reserved[5];
45 };
46 
47 struct bootblk {
48 	int blk;
49 	int boot_size;
50 	int offset;
51 };
52 
53 struct rkmtd_dev {
54 	struct udevice *dev;
55 	struct blk_desc *desc;
56 	char *label;
57 	legacy_mbr *mbr;
58 	gpt_header *gpt_h;
59 	gpt_header *gpt_h2;
60 	gpt_entry *gpt_e;
61 	char *check;
62 	char *idb;
63 	char *str;
64 	char uuid_part_str[UUID_STR_LEN + 1];
65 	char uuid_disk_str[UUID_STR_LEN + 1];
66 	char *datbuf;
67 	char *oobbuf;
68 	struct mtd_info *mtd;
69 	struct nand_para_info *info;
70 	u16 page_table[512];
71 	u32 idb_need_write_back;
72 	struct bootblk idblock[5];
73 	u32 blk_counter;
74 	u32 boot_blks;
75 	u32 offset;
76 	u32 boot_size;
77 	u32 lsb_mode;
78 };
79 
80 struct sector0 {
81 	u32 magic;
82 	u8  reserved[4];
83 	u32 rc4_flag;
84 	u16 boot_code1_offset;
85 	u16 boot_code2_offset;
86 	u8  reserved1[490];
87 	u16 flash_data_size;
88 	u16 flash_boot_size;
89 	u8  reserved2[2];
90 } __packed;
91 
92 /**
93  * rkmtd_rc4() - Rockchip specific RC4 Encryption Algorithm
94  *
95  * Encrypt Rockchip boot block header version 1 and data
96  *
97  * @buf: Pointer to data buffer
98  * @len: Data buffer size
99  */
100 void rkmtd_rc4(u8 *buf, u32 len);
101 
102 /**
103  * struct rkmtd_ops - operations supported by UCLASS_RKMTD
104  */
105 struct rkmtd_ops {
106 	/**
107 	 * @attach_mtd: - Attach a new rkmtd driver to the device structure
108 	 *
109 	 * @attach_mtd.dev: Device to update
110 	 * @attach_mtd.Returns: 0 if OK, -EEXIST if a driver is already attached,
111 	 * other -ve on other error
112 	 */
113 	int (*attach_mtd)(struct udevice *dev);
114 
115 	/**
116 	 * @detach_mtd: - Detach a rkmtd driver from the device structure
117 	 *
118 	 * @detach_mtd.dev: Device to detach from
119 	 * @detach_mtd.Returns: 0 if OK, -ENOENT if no driver is attached,
120 	 * other -ve on other error
121 	 */
122 	int (*detach_mtd)(struct udevice *dev);
123 };
124 
125 #define rkmtd_get_ops(dev)        ((struct rkmtd_ops *)(dev)->driver->ops)
126 
127 /**
128  * rkmtd_get_cur_dev() - Get the current device
129  *
130  * Returns current device, or NULL if none
131  */
132 struct udevice *rkmtd_get_cur_dev(void);
133 
134 /**
135  * rkmtd_set_cur_dev() - Set the current device
136  *
137  * Sets the current device, or clears it if @dev is NULL
138  *
139  * @dev: Device to set as the current one
140  */
141 void rkmtd_set_cur_dev(struct udevice *dev);
142 
143 /**
144  * rkmtd_find_by_label() - Find a rkmtd device by label
145  *
146  * Searches all rkmtd devices to find one with the given label
147  *
148  * @label: Label to find
149  * Returns: associated device, or NULL if not found
150  */
151 struct udevice *rkmtd_find_by_label(const char *label);
152 
153 /**
154  * rkmtd_attach() - Attach a new rkmtd driver to the device structure
155  *
156  * @dev: Device to update
157  * Returns: 0 if OK, -EEXIST if a file is already attached, other -ve on
158  * other error
159  */
160 int rkmtd_attach(struct udevice *dev);
161 
162 /**
163  * rkmtd_detach() - Detach a rkmtd driver from the device structure
164  *
165  * @dev: Device to detach from
166  * Returns: 0 if OK, -ENOENT if no file is attached, other -ve on other
167  * error
168  */
169 int rkmtd_detach(struct udevice *dev);
170 
171 /**
172  * rkmtd_create_device() - Create a new rkmtd device
173  *
174  * Any existing device with the same label is removed and unbound first
175  *
176  * @label: Label of the attachment, e.g. "test1"
177  * @devp: Returns the device created, on success
178  * Returns: 0 if OK, -ve on error
179  */
180 int rkmtd_create_device(const char *label, struct udevice **devp);
181 
182 /**
183  * rkmtd_create_attach_mtd() - Create a new rkmtd device and attach driver
184  *
185  * @label: Label of the attachment, e.g. "test1"
186  * @devp: Returns the device created, on success
187  * Returns: 0 if OK, -ve on error
188  */
189 int rkmtd_create_attach_mtd(const char *label, struct udevice **devp);
190 
191 #endif /* __RKMTD__ */
192