blob: bd2006854dffc722125345c8f96db66272e214ab [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2017 exceet electronics GmbH
4 *
5 * Authors:
6 * Frieder Schrempf <frieder.schrempf@exceet.de>
7 * Boris Brezillon <boris.brezillon@bootlin.com>
8 */
9
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/mtd/spinand.h>
13
14#define SPINAND_MFR_WINBOND 0xEF
15
16#define WINBOND_CFG_BUF_READ BIT(3)
17
18#define W25N01KW_STATUS_ECC_1_4_BITFLIPS (1 << 4)
19#define W25N01KW_STATUS_ECC_4_BITFLIPS (3 << 4)
20
21static SPINAND_OP_VARIANTS(read_cache_variants,
22 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
23 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
24 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
25 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
26 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
27 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
28
29static SPINAND_OP_VARIANTS(write_cache_variants,
30 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
31 SPINAND_PROG_LOAD(true, 0, NULL, 0));
32
33static SPINAND_OP_VARIANTS(update_cache_variants,
34 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
35 SPINAND_PROG_LOAD(false, 0, NULL, 0));
36
37static int w25m02gv_ooblayout_ecc(struct mtd_info *mtd, int section,
38 struct mtd_oob_region *region)
39{
40 if (section > 3)
41 return -ERANGE;
42
43 region->offset = (16 * section) + 8;
44 region->length = 8;
45
46 return 0;
47}
48
49static int w25m02gv_ooblayout_free(struct mtd_info *mtd, int section,
50 struct mtd_oob_region *region)
51{
52 if (section > 3)
53 return -ERANGE;
54
55 region->offset = (16 * section) + 2;
56 region->length = 6;
57
58 return 0;
59}
60
61static const struct mtd_ooblayout_ops w25m02gv_ooblayout = {
62 .ecc = w25m02gv_ooblayout_ecc,
63 .free = w25m02gv_ooblayout_free,
64};
65
66static int w25m02gv_select_target(struct spinand_device *spinand,
67 unsigned int target)
68{
69 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0xc2, 1),
70 SPI_MEM_OP_NO_ADDR,
71 SPI_MEM_OP_NO_DUMMY,
72 SPI_MEM_OP_DATA_OUT(1,
73 spinand->scratchbuf,
74 1));
75
76 *spinand->scratchbuf = target;
77 return spi_mem_exec_op(spinand->spimem, &op);
78}
79
80static int w25n01kw_ooblayout_ecc(struct mtd_info *mtd, int section,
81 struct mtd_oob_region *region)
82{
83 return -ERANGE;
84}
85
86static int w25n01kw_ooblayout_free(struct mtd_info *mtd, int section,
87 struct mtd_oob_region *region)
88{
89 if (section)
90 return -ERANGE;
91
92 region->offset = 2;
93 region->length = mtd->oobsize - 2;
94
95 return 0;
96}
97
98static const struct mtd_ooblayout_ops w25n01kw_ooblayout = {
99 .ecc = w25n01kw_ooblayout_ecc,
100 .free = w25n01kw_ooblayout_free,
101};
102
103static int w25n01kw_ecc_get_status(struct spinand_device *spinand,
104 u8 status)
105{
106 u8 status2;
107 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &status2);
108 int ret;
109
110 switch (status & STATUS_ECC_MASK) {
111 case STATUS_ECC_NO_BITFLIPS:
112 return 0;
113
114 case W25N01KW_STATUS_ECC_1_4_BITFLIPS:
115 /*
116 * Read status2 register to determine a more fine grained
117 * bit error status
118 */
119 ret = spi_mem_exec_op(spinand->spimem, &op);
120 if (ret)
121 return ret;
122
123 status2 = (status2 >> 4) & 0x7;
124 if (status2 == 0x7)
125 return -EBADMSG;
126
127 return status2;
128
129 case W25N01KW_STATUS_ECC_4_BITFLIPS:
130 return 4;
131
132 case STATUS_ECC_UNCOR_ERROR:
133 pr_warn("w25n01kw: exceed 4bit errors and not corrected\n");
134 return -EBADMSG;
135
136 default:
137 break;
138 }
139
140 return -EINVAL;
141}
142
143static int w25n02kw_ecc_get_status(struct spinand_device *spinand,
144 u8 status)
145{
146 u8 status2;
147 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &status2);
148 int ret;
149
150 switch (status & STATUS_ECC_MASK) {
151 case STATUS_ECC_NO_BITFLIPS:
152 return 0;
153
154 case W25N01KW_STATUS_ECC_1_4_BITFLIPS:
155 /*
156 * Read status2 register to determine a more fine grained
157 * bit error status
158 */
159 ret = spi_mem_exec_op(spinand->spimem, &op);
160 if (ret)
161 return ret;
162
163 status2 = (status2 >> 4) & 0xf;
164 if (status2 == 0xf)
165 return -EBADMSG;
166
167 return status2;
168
169 case W25N01KW_STATUS_ECC_4_BITFLIPS:
170 return 4;
171
172 case STATUS_ECC_UNCOR_ERROR:
173 pr_warn("w25n02kw: exceed 4bit errors and not corrected\n");
174 return -EBADMSG;
175
176 default:
177 break;
178 }
179
180 return -EINVAL;
181}
182
183static const struct spinand_info winbond_spinand_table[] = {
184 SPINAND_INFO("W25N01GW", 0x21BA,
185 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
186 NAND_ECCREQ(1, 512),
187 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
188 &write_cache_variants,
189 &update_cache_variants),
190 0,
191 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
192 SPINAND_INFO("W25N512GWxxR/T", 0x20BA,
193 NAND_MEMORG(1, 2048, 64, 64, 512, 20, 1, 1, 1),
194 NAND_ECCREQ(1, 512),
195 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
196 &write_cache_variants,
197 &update_cache_variants),
198 0,
199 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
200 SPINAND_INFO("W25N01KW", 0x21BE,
201 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
202 NAND_ECCREQ(4, 512),
203 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
204 &write_cache_variants,
205 &update_cache_variants),
206 0,
207 SPINAND_ECCINFO(&w25n01kw_ooblayout,
208 w25n01kw_ecc_get_status)),
209 SPINAND_INFO("W25N02KW", 0x22BA,
210 NAND_MEMORG(1, 2048, 64, 64, 2048, 20, 1, 1, 1),
211 NAND_ECCREQ(8, 512),
212 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
213 &write_cache_variants,
214 &update_cache_variants),
215 0,
216 SPINAND_ECCINFO(&w25n01kw_ooblayout,
217 w25n02kw_ecc_get_status)),
218 SPINAND_INFO("W25N02GW", 0xBB,
219 NAND_MEMORG(1, 2048, 64, 64, 2048, 20, 1, 1, 1),
220 NAND_ECCREQ(1, 512),
221 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
222 &write_cache_variants,
223 &update_cache_variants),
224 0,
225 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
226 SPINAND_INFO("W25M02GV", 0xAB,
227 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 2),
228 NAND_ECCREQ(1, 512),
229 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
230 &write_cache_variants,
231 &update_cache_variants),
232 0,
233 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
234 SPINAND_SELECT_TARGET(w25m02gv_select_target)),
235 SPINAND_INFO("W25N01GV", 0xAA,
236 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
237 NAND_ECCREQ(1, 512),
238 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
239 &write_cache_variants,
240 &update_cache_variants),
241 0,
242 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
243 SPINAND_INFO("W25N02JW", 0xBF,
244 NAND_MEMORG(1, 2048, 64, 64, 2048, 20, 1, 1, 1),
245 NAND_ECCREQ(1, 512),
246 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
247 &write_cache_variants,
248 &update_cache_variants),
249 0,
250 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
251 SPINAND_INFO("W25N01JW", 0x21BC,
252 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
253 NAND_ECCREQ(1, 512),
254 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
255 &write_cache_variants,
256 &update_cache_variants),
257 0,
258 SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL)),
259};
260
261/**
262 * winbond_spinand_detect - initialize device related part in spinand_device
263 * struct if it is a Winbond device.
264 * @spinand: SPI NAND device structure
265 */
266static int winbond_spinand_detect(struct spinand_device *spinand)
267{
268 u8 *id = spinand->id.data;
269 u16 devid;
270 int ret;
271
272 /*
273 * Winbond SPI NAND read ID need a dummy byte,
274 * so the first byte in raw_id is dummy.
275 */
276 if (id[1] != SPINAND_MFR_WINBOND)
277 return 0;
278
279 devid = id[2] | id[3] << 8;
280 ret = spinand_match_and_init(spinand, winbond_spinand_table,
281 ARRAY_SIZE(winbond_spinand_table), devid);
282 if (ret)
283 return ret;
284
285 return 1;
286}
287
288static int winbond_spinand_init(struct spinand_device *spinand)
289{
290 struct nand_device *nand = spinand_to_nand(spinand);
291 unsigned int i;
292
293 /*
294 * Make sure all dies are in buffer read mode and not continuous read
295 * mode.
296 */
297 for (i = 0; i < nand->memorg.ntargets; i++) {
298 spinand_select_target(spinand, i);
299 spinand_upd_cfg(spinand, WINBOND_CFG_BUF_READ,
300 WINBOND_CFG_BUF_READ);
301 }
302
303 return 0;
304}
305
306static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = {
307 .detect = winbond_spinand_detect,
308 .init = winbond_spinand_init,
309};
310
311const struct spinand_manufacturer winbond_spinand_manufacturer = {
312 .id = SPINAND_MFR_WINBOND,
313 .name = "Winbond",
314 .ops = &winbond_spinand_manuf_ops,
315};