blob: c3124a77520ad382639c3c9b130c528c8898feed [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);
140 u32 (*get_rsv_size)(const char *rsv_name);
141 int (*read_rsv)(const char *rsv_name, size_t size, void *buf);
142 int (*write_rsv)(const char *rsv_name, size_t size, void *buf);
143 int (*erase_rsv)(const char *rsv_name);
144 int (*protect_rsv)(const char *rsv_name,
145 bool ops);/*true:on false:off*/
Bichao Zheng5ced7b52023-07-25 15:20:20 +0800146 int (*param_ops)(void);
Bo Lv72d0e902023-01-02 14:27:34 +0000147};
148
149struct device_node_t {
150 enum boot_type_e index;
151 char *type;
152 int (*pre)(void);
153 int (*probe)(u32 init_flag);
154};
155
156/**
157 * we use a valid device list to manage the storage devices,
158 * and every type of device can only exist ONE in this list.
159 * we will scan and add the valid storage device to the list
160 * in the init process, and of cause you can register the device
161 * by you own.
162 */
163#define NAND_BOOT_NORMAL 0
164#define NAND_BOOT_UPGRATE 1
165#define NAND_BOOT_ERASE_PROTECT_CACHE 2
166#define NAND_BOOT_ERASE_ALL 3
167#define NAND_BOOT_SCRUB_ALL 4
168#define NAND_SCAN_ID_INIT 5
169
170#define STORE_SCRUB BIT(0)
171#define STORE_ERASE_DATA BIT(1)
172#define STORE_ERASE_RSV BIT(2)
173/**
174 * @usage: init all the valid storage device
175 *
176 * @init_flag: it's only works for MLC/EMMC driver
177 * 0 NAND_BOOT_NORMAL:normal init, but can't operates the phy data area
178 * 1 NAND_BOOT_UPGRATE:same as 0, but operation on phy data area is allowed
179 * 2 NAND_BOOT_ERASE_PROTECT_CACHE:only erase rsv area
180 * 3 NAND_BOOT_ERASE_ALL:erase whole device
181 * 4 NAND_BOOT_SCRUB_ALL:erase whole device
182 * 5 NAND_SCAN_ID_INIT:only read nand id
183 *
184 * @return: init result
185 * 0 = failed
186 * other = device_index that device probe successfully
187 */
188int store_init(u32 init_flag);
189
190/**
191 * @usage: register a storage device to the valid list
192 *
193 * @param: the description pointer of your storage device
194 *
195 * @return: registration result
196 * 0 = success
197 * 1 = fail
198 */
199int store_register(struct storage_t *store_dev);
200
201/**
202 * @usage: unregister a storage device from the valid list
203 *
204 * @store_dev: the description pointer of your storage device
205 */
206void store_unregister(struct storage_t *store_dev);
207
208/**
209 * @usage: check the type of device is valid on this board
210 *
211 * @type: the device type that you want to check
212 *
213 * @return: is the device valid
214 * 0 = invalid
215 * 1 = valid
216 */
217u8 store_device_valid(enum boot_type_e type);
218
219/**
220 * @usage: set the 'type' device as current device, and you can operates it
221 *
222 * @type: the device type that you want to set
223 *
224 * @return: result of the operation
225 * 0 = success
226 * other = fail
227 */
228int store_set_device(enum boot_type_e type);
229
230/**
231 * @usage: get the type of current storage device
232 *
233 * @return: storage device type
234 */
235enum boot_type_e store_get_type(void);
236
237/**
238 * @usage: get information about the current device
239 *
240 * @info: the pointer for the information
241 *
242 * @return: result of the operation
243 * 0 = success
244 * other = fail
245 */
246int store_get_device_info(struct storage_info_t *info);
247
248/**
249 * @usage: read data from storage device
250 *
251 * @name: partition name, when it's null the target
252 * will regards as whole device.
253 * @off: offset to the 0 address of partition/device
254 * @size: the amount of bytes to read
255 * @buf: pointer of target buffer
256 *
257 * @return: result of the operation
258 * 0 = success
259 * other = fail
260 */
261int store_read(const char *name, loff_t off, size_t size, void *buf);
262
263/**
264 * @usage: write data to storage device
265 *
266 * @name: partition name, when it's null the target
267 * will regards as whole device.
268 * @off: offset to the 0 address of partition/device
269 * @size: the amount of bytes to write
270 * @buf: pointer of source buffer
271 *
272 * @return: result of the operation
273 * 0 = success
274 * other = fail
275 */
276int store_write(const char *name, loff_t off, size_t size, void *buf);
277
278/**
279 * @usage: erase the storage device
280 *
281 * @name: partition name, when it's null the target
282 * will regards as whole device.
283 * @off: offset to the 0 address of partition/device
284 * @size: the amount of bytes to erase
285 * @scrub: scrub flag(scrub operates will works only when the device support)
286 * 0 = no scrub, just erase
287 * 1 = use scrub operates instead of erase
288 * @return: result of the operation
289 * 0 = success
290 * other = fail
291 */
292int store_erase(const char *name, loff_t off, size_t size, int scrub);
293
294/**
295 * @usage: get the partition size or capacity of device
296 *
297 * @name: partition name, when it's null the target
298 * will regards as whole device.
299 *
300 * @return: the amount of bytes to the partition or device size
301 */
302u64 store_part_size(const char *name);
303
304/**
305 * @usage: get the copy number of [name]
306 *
307 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
308 * be "bootloader" in compact mode
309 * @return: the copy number of the "bootloader" or "tpl"
310 */
311u8 store_boot_copy_num(const char *name);
312
313/**
314 * @usage: get the 1st boot copy number of current device.
315 * for eMMC: 0 -> user partition; 1 -> boot0; 2 -> boot1
316 */
317u8 store_boot_copy_start(void);
318
319/**
320 * @usage: get the bootup index of [name]
321 *
322 * @name: do not care discrete mode or compact mode
323 * "bl2" "spl" could be used as the one romboot loaded
324 * "fip" "devfip" "tpl" or "bootloader" would be the main u-boot.
325 * @return: the copy number of the "bootloader" or "tpl"
326 */
327u8 store_bootup_bootidx(const char *name);
328
329/**
330 * @usage: restore the bootidx/bootdev etc.
331 */
332void store_restore_bootidx(void);
333
334/**
335 * @usage: get the copy size of [name]
336 *
337 * @name: name: only can be "bl2" or "tpl"/"fip" in discrete mode
338 * be "bootloader" in compact mode
339 *
340 * @return: the size of every copy
341 */
342u64 store_boot_copy_size(const char *name);
343
344/**
345 * @usage: read the [name] data from storage device
346 *
347 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
348 * be "bootloader" in compact mode
349 * @copy: which copy you want read
350 * @size: the amount of bytes to read
351 * @buf: pointer of the target buffer
352 *
353 * @return: result of the operation
354 * 0 = success
355 * other = fail
356 */
357int store_boot_read(const char *name, u8 copy, size_t size, void *buf);
358
359/**
360 * @usage: write the [name] data into storage device
361 *
362 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
363 * be "bootloader" in compact mode
364 * @copy: which copy you want write,
365 * it will write to all copies when copy = BOOT_OPS_ALL
366 * @size: the amount of bytes to write
367 * @buf: pointer of the source buffer
368 *
369 * @return: result of the operation
370 * 0 = success
371 * other = fail
372 */
373int store_boot_write(const char *name, u8 copy, size_t size, void *buf);
374
375/**
376 * @usage: erase the [name] data
377 *
378 * @name: only can be "bl2" or "tpl"/"fip" in discrete mode
379 * be "bootloader" in compact mode
380 * @copy: which copy you want erase,
381 * it will erase all copies when copy = BOOT_OPS_ALL
382 *
383 * @return: result of the operation
384 * 0 = success
385 * other = fail
386 */
387int store_boot_erase(const char *name, u8 copy);
388
389/**
390 * @usage: get the rsv info size
391 *
392 * @name: rsv info name, please refer to
393 * RSV_KEY "key"
394 * RSV_ENV "env"
395 * RSV_DTB "dtb"
396 * RSV_BBT "bbt"
397 *
398 * @return: the amount bytes of the rsv info
399 */
400u32 store_rsv_size(const char *name);
401
402/**
403 * @usage: read the rsv info from storage device
404 *
405 * @name: rsv info name, please refer to
406 * RSV_KEY "key"
407 * RSV_ENV "env"
408 * RSV_DTB "dtb"
409 * RSV_BBT "bbt"
410 * @size: the amount of bytes to read
411 * @buf: pointer of the target buffer
412 *
413 * @return: result of the operation
414 * 0 = success
415 * other = fail
416 */
417int store_rsv_read(const char *name, size_t size, void *buf);
418
419/**
420 * @usage: write the rsv info to the storage device
421 *
422 * @name: rsv info name, please refer to
423 * RSV_KEY "key"
424 * RSV_ENV "env"
425 * RSV_DTB "dtb"
426 * RSV_BBT "bbt"
427 * @size: the amount of bytes to write
428 * @buf: pointer of the source buffer
429 *
430 * @return: result of the operation
431 * 0 = success
432 * other = fail
433 */
434int store_rsv_write(const char *name, size_t size, void *buf);
435
436/**
437 * @usage: erase the rsv info
438 *
439 * @name: rsv info name, please refer to
440 * RSV_KEY "key"
441 * RSV_ENV "env"
442 * RSV_DTB "dtb"
443 * RSV_BBT "bbt"
444 * it will erase all reserve information
445 * when name is null
446 *
447 * @return: result of the operation
448 * 0 = success
449 * other = fail
450 */
451int store_rsv_erase(const char *name);
452
453/**
454 * @usage: turn on/off the protection of rsv info
455 *
456 * @name: rsv info name, please refer to
457 * RSV_KEY "key"
458 * RSV_ENV "env"
459 * RSV_DTB "dtb"
460 * RSV_BBT "bbt"
461 * it will operates all reserve information
462 * when name is null
463 * @ops: turn on/off the rsv info protection
464 * true = turn on the protection
465 * flase = turn off the protection
466 *
467 * @return: result of the operation
468 * 0 = success
469 * other = fail
470 */
471int store_rsv_protect(const char *name, bool ops);
472
473/**
474 * @usage: get bootloader mode for current storage
475 *
476 * @return: result of the operation
477 * 0 = COMPACT_BOOTLOADER
478 * 1 = DISCRETE_BOOTLOADER
479 */
480int store_get_device_bootloader_mode(void);
481
482int sheader_need(void);
483void sheader_load(void *addr);
484
485int store_gpt_read(void *buf);
486int store_gpt_write(void *buf);
487int store_gpt_erase(void);
488
Sam Wu9b53f962023-08-01 19:46:50 +0800489int check_valid_dts(unsigned char *buffer);
490
Bo Lv72d0e902023-01-02 14:27:34 +0000491#endif/* __STORAGE_H__ */