blob: c285aeb9ddb0355a86976ebb6f164f234de02a96 [file] [log] [blame]
developer8d16ac22021-05-26 15:32:12 +08001/* 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_PRIVATE_H_
11#define _NMBM_PRIVATE_H_
12
13#include <nmbm/nmbm.h>
14
15#define NMBM_MAGIC_SIGNATURE 0x304d4d4e /* NMM0 */
16#define NMBM_MAGIC_INFO_TABLE 0x314d4d4e /* NMM1 */
17
18#define NMBM_VERSION_MAJOR_S 0
19#define NMBM_VERSION_MAJOR_M 0xffff
20#define NMBM_VERSION_MINOR_S 16
21#define NMBM_VERSION_MINOR_M 0xffff
22#define NMBM_VERSION_MAKE(major, minor) (((major) & NMBM_VERSION_MAJOR_M) | \
23 (((minor) & NMBM_VERSION_MINOR_M) << \
24 NMBM_VERSION_MINOR_S))
25#define NMBM_VERSION_MAJOR_GET(ver) (((ver) >> NMBM_VERSION_MAJOR_S) & \
26 NMBM_VERSION_MAJOR_M)
27#define NMBM_VERSION_MINOR_GET(ver) (((ver) >> NMBM_VERSION_MINOR_S) & \
28 NMBM_VERSION_MINOR_M)
29
30typedef uint32_t nmbm_bitmap_t;
31#define NMBM_BITMAP_UNIT_SIZE (sizeof(nmbm_bitmap_t))
32#define NMBM_BITMAP_BITS_PER_BLOCK 2
33#define NMBM_BITMAP_BITS_PER_UNIT (8 * sizeof(nmbm_bitmap_t))
34#define NMBM_BITMAP_BLOCKS_PER_UNIT (NMBM_BITMAP_BITS_PER_UNIT / \
35 NMBM_BITMAP_BITS_PER_BLOCK)
36
37#define NMBM_SPARE_BLOCK_MULTI 1
38#define NMBM_SPARE_BLOCK_DIV 2
39#define NMBM_SPARE_BLOCK_MIN 2
40
41#define NMBM_MGMT_DIV 16
42#define NMBM_MGMT_BLOCKS_MIN 32
43
44#define NMBM_TRY_COUNT 3
45
46#define BLOCK_ST_BAD 0
47#define BLOCK_ST_NEED_REMAP 2
48#define BLOCK_ST_GOOD 3
49#define BLOCK_ST_MASK 3
50
51struct nmbm_header {
52 uint32_t magic;
53 uint32_t version;
54 uint32_t size;
55 uint32_t checksum;
56};
57
58struct nmbm_signature {
59 struct nmbm_header header;
60 uint64_t nand_size;
61 uint32_t block_size;
62 uint32_t page_size;
63 uint32_t spare_size;
64 uint32_t mgmt_start_pb;
65 uint8_t max_try_count;
66 uint8_t padding[3];
67};
68
69struct nmbm_info_table_header {
70 struct nmbm_header header;
71 uint32_t write_count;
72 uint32_t state_table_off;
73 uint32_t mapping_table_off;
74 uint32_t padding;
75};
76
77struct nmbm_instance {
78 struct nmbm_lower_device lower;
79
80 uint32_t rawpage_size;
81 uint32_t rawblock_size;
82 uint32_t rawchip_size;
83
84 uint32_t writesize_mask;
85 uint32_t erasesize_mask;
86 uint16_t writesize_shift;
87 uint16_t erasesize_shift;
88
89 struct nmbm_signature signature;
90
91 uint8_t *info_table_cache;
92 uint32_t info_table_size;
93 uint32_t info_table_spare_blocks;
94 struct nmbm_info_table_header info_table;
95
96 nmbm_bitmap_t *block_state;
97 uint32_t block_state_changed;
98 uint32_t state_table_size;
99
100 int32_t *block_mapping;
101 uint32_t block_mapping_changed;
102 uint32_t mapping_table_size;
103
104 uint8_t *page_cache;
105
106 int protected;
107
108 uint32_t block_count;
109 uint32_t data_block_count;
110
111 uint32_t mgmt_start_ba;
112 uint32_t main_table_ba;
113 uint32_t backup_table_ba;
114 uint32_t mapping_blocks_ba;
115 uint32_t mapping_blocks_top_ba;
116 uint32_t signature_ba;
117
118 enum nmbm_log_category log_display_level;
119};
120
121/* Log utilities */
122#define nlog_debug(ni, fmt, ...) \
123 nmbm_log(ni, NMBM_LOG_DEBUG, fmt, ##__VA_ARGS__)
124
125#define nlog_info(ni, fmt, ...) \
126 nmbm_log(ni, NMBM_LOG_INFO, fmt, ##__VA_ARGS__)
127
128#define nlog_warn(ni, fmt, ...) \
129 nmbm_log(ni, NMBM_LOG_WARN, fmt, ##__VA_ARGS__)
130
131#define nlog_err(ni, fmt, ...) \
132 nmbm_log(ni, NMBM_LOG_ERR, fmt, ##__VA_ARGS__)
133
134#define nlog_emerg(ni, fmt, ...) \
135 nmbm_log(ni, NMBM_LOG_EMERG, fmt, ##__VA_ARGS__)
136
137#endif /* _NMBM_PRIVATE_H_ */