developer | 8d16ac2 | 2021-05-26 15:32:12 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */ |
| 2 | /* |
| 3 | * Copyright (C) 2020 MediaTek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Definitions for NAND Mapped-block Management (NMBM) |
| 6 | * |
| 7 | * Author: Weijie Gao <weijie.gao@mediatek.com> |
| 8 | */ |
| 9 | |
| 10 | #ifndef _NMBM_H_ |
| 11 | #define _NMBM_H_ |
| 12 | |
| 13 | #include <nmbm/nmbm-os.h> |
| 14 | |
| 15 | enum nmbm_log_category { |
| 16 | NMBM_LOG_DEBUG, |
| 17 | NMBM_LOG_INFO, |
| 18 | NMBM_LOG_WARN, |
| 19 | NMBM_LOG_ERR, |
| 20 | NMBM_LOG_EMERG, |
| 21 | |
| 22 | __NMBM_LOG_MAX |
| 23 | }; |
| 24 | |
| 25 | enum nmbm_oob_mode { |
| 26 | NMBM_MODE_PLACE_OOB, |
| 27 | NMBM_MODE_AUTO_OOB, |
| 28 | NMBM_MODE_RAW, |
| 29 | |
| 30 | __NMBM_MODE_MAX |
| 31 | }; |
| 32 | |
| 33 | struct nmbm_lower_device { |
| 34 | uint32_t max_ratio; |
| 35 | uint32_t max_reserved_blocks; |
| 36 | int flags; |
| 37 | |
| 38 | uint64_t size; |
| 39 | uint32_t erasesize; |
| 40 | uint32_t writesize; |
| 41 | uint32_t oobsize; |
| 42 | uint32_t oobavail; |
| 43 | |
| 44 | void *arg; |
| 45 | int (*reset_chip)(void *arg); |
| 46 | |
| 47 | /* |
| 48 | * read_page: |
| 49 | * return 0 if succeeds |
| 50 | * return positive number for ecc error |
| 51 | * return negative number for other errors |
| 52 | */ |
| 53 | int (*read_page)(void *arg, uint64_t addr, void *buf, void *oob, enum nmbm_oob_mode mode); |
| 54 | int (*write_page)(void *arg, uint64_t addr, const void *buf, const void *oob, enum nmbm_oob_mode mode); |
developer | d8cf71c | 2023-06-20 19:10:04 +0800 | [diff] [blame^] | 55 | int (*panic_write_page)(void *arg, uint64_t addr, const void *buf); |
developer | 8d16ac2 | 2021-05-26 15:32:12 +0800 | [diff] [blame] | 56 | int (*erase_block)(void *arg, uint64_t addr); |
| 57 | |
| 58 | int (*is_bad_block)(void *arg, uint64_t addr); |
| 59 | int (*mark_bad_block)(void *arg, uint64_t addr); |
| 60 | |
| 61 | /* OS-dependent logging function */ |
| 62 | void (*logprint)(void *arg, enum nmbm_log_category level, const char *fmt, va_list ap); |
| 63 | }; |
| 64 | |
| 65 | struct nmbm_instance; |
| 66 | |
| 67 | /* Create NMBM if management area not found, or not complete */ |
| 68 | #define NMBM_F_CREATE 0x01 |
developer | 49f853a | 2021-06-23 17:22:02 +0800 | [diff] [blame] | 69 | |
| 70 | /* Empty page is also protected by ECC, and bitflip(s) can be corrected */ |
developer | 28a313b | 2021-06-16 17:23:34 +0800 | [diff] [blame] | 71 | #define NMBM_F_EMPTY_PAGE_ECC_OK 0x02 |
developer | 8d16ac2 | 2021-05-26 15:32:12 +0800 | [diff] [blame] | 72 | |
developer | 49f853a | 2021-06-23 17:22:02 +0800 | [diff] [blame] | 73 | /* Do not write anything back to flash */ |
| 74 | #define NMBM_F_READ_ONLY 0x04 |
| 75 | |
developer | 8d16ac2 | 2021-05-26 15:32:12 +0800 | [diff] [blame] | 76 | size_t nmbm_calc_structure_size(struct nmbm_lower_device *nld); |
| 77 | int nmbm_attach(struct nmbm_lower_device *nld, struct nmbm_instance *ni); |
| 78 | int nmbm_detach(struct nmbm_instance *ni); |
| 79 | |
| 80 | enum nmbm_log_category nmbm_set_log_level(struct nmbm_instance *ni, |
| 81 | enum nmbm_log_category level); |
| 82 | |
| 83 | int nmbm_erase_block_range(struct nmbm_instance *ni, uint64_t addr, |
| 84 | uint64_t size, uint64_t *failed_addr); |
| 85 | int nmbm_read_single_page(struct nmbm_instance *ni, uint64_t addr, void *data, |
| 86 | void *oob, enum nmbm_oob_mode mode); |
| 87 | int nmbm_read_range(struct nmbm_instance *ni, uint64_t addr, size_t size, |
| 88 | void *data, enum nmbm_oob_mode mode, size_t *retlen); |
| 89 | int nmbm_write_single_page(struct nmbm_instance *ni, uint64_t addr, |
| 90 | const void *data, const void *oob, |
| 91 | enum nmbm_oob_mode mode); |
developer | d8cf71c | 2023-06-20 19:10:04 +0800 | [diff] [blame^] | 92 | int nmbm_panic_write_single_page(struct nmbm_instance *ni, uint64_t addr, |
| 93 | const void *data); |
developer | 8d16ac2 | 2021-05-26 15:32:12 +0800 | [diff] [blame] | 94 | int nmbm_write_range(struct nmbm_instance *ni, uint64_t addr, size_t size, |
| 95 | const void *data, enum nmbm_oob_mode mode, |
| 96 | size_t *retlen); |
| 97 | |
| 98 | int nmbm_check_bad_block(struct nmbm_instance *ni, uint64_t addr); |
| 99 | int nmbm_mark_bad_block(struct nmbm_instance *ni, uint64_t addr); |
| 100 | |
| 101 | uint64_t nmbm_get_avail_size(struct nmbm_instance *ni); |
| 102 | |
| 103 | int nmbm_get_lower_device(struct nmbm_instance *ni, struct nmbm_lower_device *nld); |
| 104 | |
| 105 | #endif /* _NMBM_H_ */ |