• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hinfc_common.c
3  *
4  * The Flash Memory Controller Device Driver for hisilicon
5  *
6  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include <hinfc_common.h>
24 #include <match_table.h>
25 
26 /*****************************************************************************/
27 struct nand_flash_dev *(*get_flash_type)(
28 	struct mtd_info *mtd,
29 	struct nand_chip *chip,
30 	unsigned char *id) = NULL;
31 
32 int (*nand_oob_resize)(struct mtd_info *mtd) = NULL;
33 
34 /*****************************************************************************/
35 #if defined(CONFIG_NAND_FLASH_HISNFC100) ||\
36 	defined(CONFIG_NAND_FLASH_HINFC610) ||\
37 	defined(CONFIG_HIFMC_SPI_NAND) ||\
38 	defined(CONFIG_HIFMC_NAND)
39 static struct match_type_str ecc2name[] = {
40 	{NAND_ECC_0BIT,  "none"     },
41 	{NAND_ECC_8BIT,  "4bit/512" },
42 	{NAND_ECC_16BIT, "8bit/512" },
43 	{NAND_ECC_24BIT, "24bit/1K" },
44 	{NAND_ECC_28BIT, "28bit/1K" },
45 	{NAND_ECC_40BIT, "40bit/1K" },
46 	{NAND_ECC_42BIT, "42bit/1K" },
47 	{NAND_ECC_64BIT, "64bit/1K" },
48 };
49 
nand_ecc_name(int type)50 const char *nand_ecc_name(int type)
51 {
52 	return type2str(ecc2name, ARRAY_SIZE(ecc2name), type, "unknown");
53 }
54 
55 /*****************************************************************************/
56 static struct match_type_str page2name[] = {
57 	{ NAND_PAGE_512B, "512" },
58 	{ NAND_PAGE_2K,   "2K" },
59 	{ NAND_PAGE_4K,   "4K" },
60 	{ NAND_PAGE_8K,   "8K" },
61 	{ NAND_PAGE_16K,  "16K" },
62 	{ NAND_PAGE_32K,  "32K" },
63 };
64 
nand_page_name(int type)65 const char *nand_page_name(int type)
66 {
67 	return type2str(page2name, ARRAY_SIZE(page2name), type, "unknown");
68 }
69 
70 /*****************************************************************************/
71 static struct match_reg_type page2size[] = {
72 	{ _512B, NAND_PAGE_512B },
73 	{ _2K, NAND_PAGE_2K },
74 	{ _4K, NAND_PAGE_4K },
75 	{ _8K, NAND_PAGE_8K },
76 	{ _16K, NAND_PAGE_16K },
77 	{ _32K, NAND_PAGE_32K },
78 };
79 
nandpage_size2type(int size)80 int nandpage_size2type(int size)
81 {
82 	return reg2type(page2size, ARRAY_SIZE(page2size), size, NAND_PAGE_2K);
83 }
84 
nandpage_type2size(int size)85 int nandpage_type2size(int size)
86 {
87 	return type2reg(page2size, ARRAY_SIZE(page2size), size, NAND_PAGE_2K);
88 }
89 #endif
90 
91 /*****************************************************************************/
92 #define ET_ECC_NONE	0x00
93 #define ET_ECC_4BIT	0x02
94 #define ET_ECC_8BIT	0x03
95 #define ET_ECC_24BIT	0x04
96 #define ET_ECC_40BIT1K	0x05
97 #define ET_ECC_64BIT1K	0x06
98 
99 static struct match_reg_type ecc_yaffs_type_t[] = {
100 	{ET_ECC_NONE,		NAND_ECC_0BIT},
101 	{ET_ECC_4BIT,		NAND_ECC_8BIT},
102 	{ET_ECC_8BIT,		NAND_ECC_16BIT},
103 	{ET_ECC_24BIT,		NAND_ECC_24BIT},
104 	{ET_ECC_40BIT1K,	NAND_ECC_40BIT},
105 	{ET_ECC_64BIT1K,	NAND_ECC_64BIT}
106 };
107 
match_ecc_type_to_yaffs(unsigned char type)108 unsigned char match_ecc_type_to_yaffs(unsigned char type)
109 {
110 	return type2reg(ecc_yaffs_type_t, ARRAY_SIZE(ecc_yaffs_type_t), type,
111 			ET_ECC_4BIT);
112 }
113 
114 /*****************************************************************************/
115 static struct match_t page_table[] = {
116 	{NAND_PAGE_2K,	PAGE_SIZE_2KB,	"2K"},
117 	{NAND_PAGE_4K,	PAGE_SIZE_4KB,	"4K"},
118 	{NAND_PAGE_8K,	PAGE_SIZE_8KB,	"8K"},
119 	{NAND_PAGE_16K,	PAGE_SIZE_16KB,	"16K"},
120 };
121 
match_page_reg_to_type(unsigned char reg)122 unsigned char match_page_reg_to_type(unsigned char reg)
123 {
124 	return match_reg_to_type(page_table, ARRAY_SIZE(page_table), reg,
125 				 NAND_PAGE_2K);
126 }
127 
match_page_type_to_reg(unsigned char type)128 unsigned char match_page_type_to_reg(unsigned char type)
129 {
130 	return match_type_to_reg(page_table, ARRAY_SIZE(page_table), type,
131 				 PAGE_SIZE_2KB);
132 }
133 
match_page_type_to_str(unsigned char type)134 const char *match_page_type_to_str(unsigned char type)
135 {
136 	return match_type_to_data(page_table, ARRAY_SIZE(page_table), type,
137 				  "unknown");
138 }
139 
140 /*****************************************************************************/
141 static struct match_t ecc_table[] = {
142 	{NAND_ECC_0BIT,		ECC_TYPE_0BIT,	"none"},
143 	{NAND_ECC_8BIT,		ECC_TYPE_8BIT,	"4bit/512"},
144 	{NAND_ECC_16BIT,	ECC_TYPE_16BIT,	"8bit/512"},
145 	{NAND_ECC_24BIT,	ECC_TYPE_24BIT,	"24bit/1K"},
146 	{NAND_ECC_28BIT,	ECC_TYPE_28BIT,	"28bit/1K"},
147 	{NAND_ECC_40BIT,	ECC_TYPE_40BIT,	"40bit/1K"},
148 	{NAND_ECC_64BIT,	ECC_TYPE_64BIT,	"64bit/1K"},
149 };
150 
match_ecc_reg_to_type(unsigned char reg)151 unsigned char match_ecc_reg_to_type(unsigned char reg)
152 {
153 	return match_reg_to_type(ecc_table, ARRAY_SIZE(ecc_table), reg,
154 				 NAND_ECC_8BIT);
155 }
156 
match_ecc_type_to_reg(unsigned char type)157 unsigned char match_ecc_type_to_reg(unsigned char type)
158 {
159 	return match_type_to_reg(ecc_table, ARRAY_SIZE(ecc_table), type,
160 				 ECC_TYPE_8BIT);
161 }
162 
match_ecc_type_to_str(unsigned char type)163 const char *match_ecc_type_to_str(unsigned char type)
164 {
165 	return match_type_to_data(ecc_table, ARRAY_SIZE(ecc_table), type,
166 				  "unknown");
167 }
168 
169 /*****************************************************************************/
170 static struct match_t page_type_size_table[] = {
171 	{NAND_PAGE_2K,	_2K,	NULL},
172 	{NAND_PAGE_4K,	_4K,	NULL},
173 	{NAND_PAGE_8K,	_8K,	NULL},
174 	{NAND_PAGE_16K,	_16K,	NULL},
175 };
176 
match_page_size_to_type(unsigned int size)177 unsigned char match_page_size_to_type(unsigned int size)
178 {
179 	return match_reg_to_type(page_type_size_table,
180 			ARRAY_SIZE(page_type_size_table), size, NAND_PAGE_2K);
181 }
182 
match_page_type_to_size(unsigned char type)183 unsigned int match_page_type_to_size(unsigned char type)
184 {
185 	return match_type_to_reg(page_type_size_table,
186 				 ARRAY_SIZE(page_type_size_table), type, _2K);
187 }
188