blob: bea541ef44a0edff2ee76ca13accd9357f040bd8 [file] [log] [blame]
Bo Lv72d0e902023-01-02 14:27:34 +00001/* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#ifndef __STORAGE_H__
7#define __STORAGE_H__
8
9#include <common.h>
10#include <command.h>
11#include <console.h>
12#include <watchdog.h>
13#include <malloc.h>
14#include <asm/byteorder.h>
Bichao Zheng91df7812023-08-08 17:36:56 +080015
Bo Lv72d0e902023-01-02 14:27:34 +000016/* storage plat data */
17#include <asm/amlogic/arch/storage.h>
18
Feng Chenee8403d2024-01-03 14:13:40 +080019extern struct storage_startup_parameter g_ssp;
20
Bo Lv72d0e902023-01-02 14:27:34 +000021#define RSV_INVALID 140 /* rsv invalid error */
22
23#define DISPROTECT_KEY BIT(0)
24#define DISPROTECT_SECURE BIT(1)
25#define DISPROTECT_FBBT BIT(2)
26#define DISPROTECT_HYNIX BIT(3)
27
28#define PART_PROTECT_FLAG BIT(4)
29enum boot_type_e {
30 BOOT_EMMC = 1,
31 BOOT_SD = 1 << 1,
32 BOOT_NAND_NFTL = 1 << 2,
33 BOOT_NAND_MTD = 1 << 3,
34 BOOT_SNAND = 1 << 4,/* spi nand */
35 BOOT_SNOR = 1 << 5,/* spi nor */
36 BOOT_NONE = 0xFF
37};
38
39#define RSV_KEY "key"
40#define RSV_ENV "env"
41#define RSV_DTB "dtb"
42#define RSV_BBT "bbt"
43#define RSV_DDR_PARA "ddr_para"
44
45struct nand_startup_parameter {
46 int page_size;
47 int block_size;
48 int layout_reserve_size;
49 int pages_per_block;
50 int setup_data;
51 /* */
52 int page0_disable;
53};
54
55typedef struct boot_area_entry {
56 /* name */
57 char name[11];
58 /* index */
59 uint8_t idx;
60 uint64_t offset;
61 uint64_t size;
62} boot_area_entry_t;
63
64struct boot_layout {
65 boot_area_entry_t *boot_entry;
66};
67
68struct emmc_startup_parameter {
69 //sd_emmc_setup_t setup;
70};
71
72struct spi_nand_startup_parameter {
73 uint32_t pagesize;
74 uint32_t pages_per_eraseblock;
75 uint32_t eraseblocks_per_lun;
76 uint32_t planes_per_lun;
77 uint32_t luns_per_target;
78 uint32_t ntargets;
79 int layout_reserve_size;
80};
81
82struct storage_boot_entry {
83 uint32_t offset;
84 uint32_t size;
85};
86
87union storage_independent_parameter {
88 struct nand_startup_parameter nsp;
89 struct emmc_startup_parameter esp;
90 struct spi_nand_startup_parameter snasp;
91};
92
93struct storage_startup_parameter {
94 uint8_t boot_device;
95 uint8_t boot_seq;
96 uint8_t boot_backups;
97 uint8_t reserved;
98 struct storage_boot_entry boot_entry[MAX_BOOT_AREA_ENTRIES];
99 union storage_independent_parameter sip;
100};
101
102struct storage_info_t {
103 u8 name[32];
104 u8 id[8];
105 u32 read_unit;
106 u32 write_unit;
107 u32 erase_unit;
108 u64 caps;/* total size */
109 u8 mode;/* bootloader mode, compact or discrete */
110};
111
112struct storage_t {
113 enum boot_type_e type;
114 struct storage_info_t info;
115 u32 init_flag;
116 struct list_head list;
117 int (*get_part_count)(void);
118 int (*list_part_name)(int idx, char *part_name);
119 /* when part_name is null, default to ops in whole chip */
120 /* int (*block_is_bad)(const char *part_name, loff_t off); */
121 u64 (*get_part_size)(const char *part_name);
122 int (*read)(const char *part_name, loff_t off,
123 size_t size, void *dest);
124 int (*write)(const char *part_name, loff_t off,
125 size_t size, void *source);
126 int (*erase)(const char *part_name, loff_t off,
127 size_t size, int flag);
128
129#define BOOT_OPS_ALL 0xFF/* for cpy parameter operates all copies */
130 u8 (*get_copies)(const char *part_name);
131 u64 (*get_copy_size)(const char *part_name);
132 int (*boot_read)(const char *part_name,
133 u8 cpy, size_t size, void *dest);
134 int (*boot_write)(const char *part_name,
135 u8 cpy, size_t size, void *source);
136 int (*boot_erase)(const char *part_name, u8 cpy);
137 int (*gpt_read)(void *dest);
138 int (*gpt_write)(void *source);
139 int (*gpt_erase)(void);
jinbiaod6719f12023-12-22 05:34:45 +0000140 int (*boot_copy_enable)(int index);
Bo Lv72d0e902023-01-02 14:27:34 +0000141 u32 (*get_rsv_size)(const char *rsv_name);
142 int (*read_rsv)(const char *rsv_name, size_t size, void *buf);
143 int (*write_rsv)(const char *rsv_name, size_t size, void *buf);
144 int (*erase_rsv)(const char *rsv_name);
145 int (*protect_rsv)(const char *rsv_name,
146 bool ops);/*true:on false:off*/
Bichao Zheng5ced7b52023-07-25 15:20:20 +0800147 int (*param_ops)(void);
Bo Lv72d0e902023-01-02 14:27:34 +0000148};
149
150struct device_node_t {
151 enum boot_type_e index;
152 char *type;
153 int (*pre)(void);
154 int (*probe)(u32 init_flag);
155};
156
157/**
158 * we use a valid device list to manage the storage devices,
159 * and every type of device can only exist ONE in this list.
160 * we will scan and add the valid storage device to the list
161 * in the init process, and of cause you can register the device
162 * by you own.
163 */
164#define NAND_BOOT_NORMAL 0
165#define NAND_BOOT_UPGRATE 1
166#define NAND_BOOT_ERASE_PROTECT_CACHE 2
167#define NAND_BOOT_ERASE_ALL 3
168#define NAND_BOOT_SCRUB_ALL 4
169#define NAND_SCAN_ID_INIT 5
170
171#define STORE_SCRUB BIT(0)
172#define STORE_ERASE_DATA BIT(1)
173#define STORE_ERASE_RSV BIT(2)
174/**
175 * @usage: init all the valid storage device
176 *
177 * @init_flag: it's only works for MLC/EMMC driver
178 * 0 NAND_BOOT_NORMAL:normal init, but can't operates the phy data area
179 * 1 NAND_BOOT_UPGRATE:same as 0, but operation on phy data area is allowed
180 * 2 NAND_BOOT_ERASE_PROTECT_CACHE:only erase rsv area
181 * 3 NAND_BOOT_ERASE_ALL:erase whole device
182 * 4 NAND_BOOT_SCRUB_ALL:erase whole device
183 * 5 NAND_SCAN_ID_INIT:only read nand id
184 *
185 * @return: init result
186 * 0 = failed
187 * other = device_index that device probe successfully
188 */
189int store_init(u32 init_flag);
190
191/**
192 * @usage: register a storage device to the valid list
193 *
194 * @param: the description pointer of your storage device
195 *
196 * @return: registration result
197 * 0 = success
198 * 1 = fail
199 */
200int store_register(struct storage_t *store_dev);
201
202/**
203 * @usage: unregister a storage device from the valid list
204 *
205 * @store_dev: the description pointer of your storage device
206 */
207void store_unregister(struct storage_t *store_dev);
208
209/**
210 * @usage: check the type of device is valid on this board
211 *
212 * @type: the device type that you want to check
213 *
214 * @return: is the device valid
215 * 0 = invalid
216 * 1 = valid
217 */
218u8 store_device_valid(enum boot_type_e type);
219
220/**
221 * @usage: set the 'type' device as current device, and you can operates it
222 *
223 * @type: the device type that you want to set
224 *
225 * @return: result of the operation
226 * 0 = success
227 * other = fail
228 */
229int store_set_device(enum boot_type_e type);
230
231/**
232 * @usage: get the type of current storage device
233 *
234 * @return: storage device type
235 */
236enum boot_type_e store_get_type(void);
237
238/**
239 * @usage: get information about the current device
240 *
241 * @info: the pointer for the information
242 *
243 * @return: result of the operation
244 * 0 = success
245 * other = fail
246 */
247int store_get_device_info(struct storage_info_t *info);
248
249/**
250 * @usage: read data from storage device
251 *
252 * @name: partition name, when it's null the target
253 * will regards as whole device.
254 * @off: offset to the 0 address of partition/device
255 * @size: the amount of bytes to read
256 * @buf: pointer of target buffer
257 *
258 * @return: result of the operation
259 * 0 = success
260 * other = fail
261 */
262int store_read(const char *name, loff_t off, size_t size, void *buf);
263
264/**
265 * @usage: write data to storage device
266 *
267 * @name: partition name, when it's null the target
268 * will regards as whole device.
269 * @off: offset to the 0 address of partition/device
270 * @size: the amount of bytes to write
271 * @buf: pointer of source buffer
272 *
273 * @return: result of the operation
274 * 0 = success
275 * other = fail
276 */
277int store_write(const char *name, loff_t off, size_t size, void *buf);
278
279/**
280 * @usage: erase the storage device
281 *
282 * @name: partition name, when it's null the target
283 * will regards as whole device.
284 * @off: offset to the 0 address of partition/device
285 * @size: the amount of bytes to erase
286 * @scrub: scrub flag(scrub operates will works only when the device support)
287 * 0 = no scrub, just erase
288 * 1 = use scrub operates instead of erase
289 * @return: result of the operation
290 * 0 = success
291 * other = fail
292 */
293int store_erase(const char *name, loff_t off, size_t size, int scrub);
294
295/**
296 * @usage: get the partition size or capacity of device
297 *
298 * @name: partition name, when it's null the target
299 * will regards as whole device.
300 *
301 * @return: the amount of bytes to the partition or device size
302 */
303u64 store_part_size(const char *name);
304
305/**
306 * @usage: get the copy number of [name]
307 *
308 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
309 * be "bootloader" in compact mode
310 * @return: the copy number of the "bootloader" or "tpl"
311 */
312u8 store_boot_copy_num(const char *name);
313
314/**
315 * @usage: get the 1st boot copy number of current device.
316 * for eMMC: 0 -> user partition; 1 -> boot0; 2 -> boot1
317 */
318u8 store_boot_copy_start(void);
319
320/**
321 * @usage: get the bootup index of [name]
322 *
323 * @name: do not care discrete mode or compact mode
324 * "bl2" "spl" could be used as the one romboot loaded
325 * "fip" "devfip" "tpl" or "bootloader" would be the main u-boot.
326 * @return: the copy number of the "bootloader" or "tpl"
327 */
328u8 store_bootup_bootidx(const char *name);
329
330/**
331 * @usage: restore the bootidx/bootdev etc.
332 */
333void store_restore_bootidx(void);
334
335/**
336 * @usage: get the copy size of [name]
337 *
338 * @name: name: only can be "bl2" or "tpl"/"fip" in discrete mode
339 * be "bootloader" in compact mode
340 *
341 * @return: the size of every copy
342 */
343u64 store_boot_copy_size(const char *name);
344
345/**
346 * @usage: read the [name] data from storage device
347 *
348 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
349 * be "bootloader" in compact mode
350 * @copy: which copy you want read
351 * @size: the amount of bytes to read
352 * @buf: pointer of the target buffer
353 *
354 * @return: result of the operation
355 * 0 = success
356 * other = fail
357 */
358int store_boot_read(const char *name, u8 copy, size_t size, void *buf);
359
360/**
361 * @usage: write the [name] data into storage device
362 *
363 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
364 * be "bootloader" in compact mode
365 * @copy: which copy you want write,
366 * it will write to all copies when copy = BOOT_OPS_ALL
367 * @size: the amount of bytes to write
368 * @buf: pointer of the source buffer
369 *
370 * @return: result of the operation
371 * 0 = success
372 * other = fail
373 */
374int store_boot_write(const char *name, u8 copy, size_t size, void *buf);
375
376/**
377 * @usage: erase the [name] data
378 *
379 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
380 * be "bootloader" in compact mode
381 * @copy: which copy you want erase,
382 * it will erase all copies when copy = BOOT_OPS_ALL
383 *
384 * @return: result of the operation
385 * 0 = success
386 * other = fail
387 */
388int store_boot_erase(const char *name, u8 copy);
389
390/**
391 * @usage: get the rsv info size
392 *
393 * @name: rsv info name, please refer to
394 * RSV_KEY "key"
395 * RSV_ENV "env"
396 * RSV_DTB "dtb"
397 * RSV_BBT "bbt"
398 *
399 * @return: the amount bytes of the rsv info
400 */
401u32 store_rsv_size(const char *name);
402
403/**
404 * @usage: read the rsv info from storage device
405 *
406 * @name: rsv info name, please refer to
407 * RSV_KEY "key"
408 * RSV_ENV "env"
409 * RSV_DTB "dtb"
410 * RSV_BBT "bbt"
411 * @size: the amount of bytes to read
412 * @buf: pointer of the target buffer
413 *
414 * @return: result of the operation
415 * 0 = success
416 * other = fail
417 */
418int store_rsv_read(const char *name, size_t size, void *buf);
419
420/**
421 * @usage: write the rsv info to the storage device
422 *
423 * @name: rsv info name, please refer to
424 * RSV_KEY "key"
425 * RSV_ENV "env"
426 * RSV_DTB "dtb"
427 * RSV_BBT "bbt"
428 * @size: the amount of bytes to write
429 * @buf: pointer of the source buffer
430 *
431 * @return: result of the operation
432 * 0 = success
433 * other = fail
434 */
435int store_rsv_write(const char *name, size_t size, void *buf);
436
437/**
438 * @usage: erase the rsv info
439 *
440 * @name: rsv info name, please refer to
441 * RSV_KEY "key"
442 * RSV_ENV "env"
443 * RSV_DTB "dtb"
444 * RSV_BBT "bbt"
445 * it will erase all reserve information
446 * when name is null
447 *
448 * @return: result of the operation
449 * 0 = success
450 * other = fail
451 */
452int store_rsv_erase(const char *name);
453
454/**
455 * @usage: turn on/off the protection of rsv info
456 *
457 * @name: rsv info name, please refer to
458 * RSV_KEY "key"
459 * RSV_ENV "env"
460 * RSV_DTB "dtb"
461 * RSV_BBT "bbt"
462 * it will operates all reserve information
463 * when name is null
464 * @ops: turn on/off the rsv info protection
465 * true = turn on the protection
466 * flase = turn off the protection
467 *
468 * @return: result of the operation
469 * 0 = success
470 * other = fail
471 */
472int store_rsv_protect(const char *name, bool ops);
473
474/**
475 * @usage: get bootloader mode for current storage
476 *
477 * @return: result of the operation
478 * 0 = COMPACT_BOOTLOADER
479 * 1 = DISCRETE_BOOTLOADER
480 */
481int store_get_device_bootloader_mode(void);
482
483int sheader_need(void);
484void sheader_load(void *addr);
485
486int store_gpt_read(void *buf);
487int store_gpt_write(void *buf);
488int store_gpt_erase(void);
489
Sam Wu9b53f962023-08-01 19:46:50 +0800490int check_valid_dts(unsigned char *buffer);
jinbiaod6719f12023-12-22 05:34:45 +0000491int store_boot_copy_enable(int index);
Sam Wu9b53f962023-08-01 19:46:50 +0800492
Bo Lv72d0e902023-01-02 14:27:34 +0000493#endif/* __STORAGE_H__ */