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