05138d984e5bbef85404141219f69d2124598886
[openwrt/staging/rmilecki.git] /
1 From f7704275957852cd4c4632d6da126979ef24b83a Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Tue, 2 Mar 2021 16:58:01 +0800
4 Subject: [PATCH 36/71] drivers: mtd: add support for MediaTek SPI-NAND flash
5 controller
6
7 Add mtd driver for MediaTek SPI-NAND flash controller
8
9 This driver is written from scratch, and uses standard mtd framework, not
10 the nand framework which only applies for raw parallel nand flashes so that
11 this driver can have a smaller size in binary.
12
13 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
14 ---
15 drivers/mtd/Kconfig | 2 +
16 drivers/mtd/Makefile | 2 +
17 drivers/mtd/mtk-snand/Kconfig | 21 +
18 drivers/mtd/mtk-snand/Makefile | 11 +
19 drivers/mtd/mtk-snand/mtk-snand-def.h | 271 ++++
20 drivers/mtd/mtk-snand/mtk-snand-ecc.c | 411 ++++++
21 drivers/mtd/mtk-snand/mtk-snand-ids.c | 515 +++++++
22 drivers/mtd/mtk-snand/mtk-snand-mtd.c | 535 +++++++
23 drivers/mtd/mtk-snand/mtk-snand-os.c | 39 +
24 drivers/mtd/mtk-snand/mtk-snand-os.h | 120 ++
25 drivers/mtd/mtk-snand/mtk-snand.c | 1933 +++++++++++++++++++++++++
26 drivers/mtd/mtk-snand/mtk-snand.h | 77 +
27 12 files changed, 3937 insertions(+)
28 create mode 100644 drivers/mtd/mtk-snand/Kconfig
29 create mode 100644 drivers/mtd/mtk-snand/Makefile
30 create mode 100644 drivers/mtd/mtk-snand/mtk-snand-def.h
31 create mode 100644 drivers/mtd/mtk-snand/mtk-snand-ecc.c
32 create mode 100644 drivers/mtd/mtk-snand/mtk-snand-ids.c
33 create mode 100644 drivers/mtd/mtk-snand/mtk-snand-mtd.c
34 create mode 100644 drivers/mtd/mtk-snand/mtk-snand-os.c
35 create mode 100644 drivers/mtd/mtk-snand/mtk-snand-os.h
36 create mode 100644 drivers/mtd/mtk-snand/mtk-snand.c
37 create mode 100644 drivers/mtd/mtk-snand/mtk-snand.h
38
39 --- a/drivers/mtd/Kconfig
40 +++ b/drivers/mtd/Kconfig
41 @@ -238,6 +238,8 @@ config SYS_MAX_FLASH_BANKS_DETECT
42 to reduce the effective number of flash bank, between 0 and
43 CONFIG_SYS_MAX_FLASH_BANKS
44
45 +source "drivers/mtd/mtk-snand/Kconfig"
46 +
47 source "drivers/mtd/nand/Kconfig"
48
49 config SYS_NAND_MAX_OOBFREE
50 --- a/drivers/mtd/Makefile
51 +++ b/drivers/mtd/Makefile
52 @@ -39,3 +39,5 @@ obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_SUPPOR
53 obj-$(CONFIG_SPL_UBI) += ubispl/
54
55 endif
56 +
57 +obj-$(CONFIG_MTK_SPI_NAND) += mtk-snand/
58 --- /dev/null
59 +++ b/drivers/mtd/mtk-snand/Kconfig
60 @@ -0,0 +1,21 @@
61 +#
62 +# Copyright (C) 2020 MediaTek Inc. All rights reserved.
63 +# Author: Weijie Gao <weijie.gao@mediatek.com>
64 +#
65 +# SPDX-License-Identifier: GPL-2.0
66 +#
67 +
68 +config MTK_SPI_NAND
69 + tristate "MediaTek SPI NAND flash controller driver"
70 + depends on !MTD_SPI_NAND
71 + help
72 + This option enables access to SPI-NAND flashes through the
73 + MediaTek SPI NAND Flash Controller
74 +
75 +config MTK_SPI_NAND_MTD
76 + tristate "MTD support for MediaTek SPI NAND flash controller"
77 + depends on DM_MTD
78 + depends on MTK_SPI_NAND
79 + help
80 + This option enables access to SPI-NAND flashes through the
81 + MTD interface of MediaTek SPI NAND Flash Controller
82 --- /dev/null
83 +++ b/drivers/mtd/mtk-snand/Makefile
84 @@ -0,0 +1,11 @@
85 +#
86 +# Copyright (C) 2020 MediaTek Inc. All rights reserved.
87 +# Author: Weijie Gao <weijie.gao@mediatek.com>
88 +#
89 +# SPDX-License-Identifier: GPL-2.0
90 +#
91 +
92 +obj-y += mtk-snand.o mtk-snand-ecc.o mtk-snand-ids.o mtk-snand-os.o
93 +obj-$(CONFIG_MTK_SPI_NAND_MTD) += mtk-snand-mtd.o
94 +
95 +ccflags-y += -DPRIVATE_MTK_SNAND_HEADER
96 --- /dev/null
97 +++ b/drivers/mtd/mtk-snand/mtk-snand-def.h
98 @@ -0,0 +1,271 @@
99 +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
100 +/*
101 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
102 + *
103 + * Author: Weijie Gao <weijie.gao@mediatek.com>
104 + */
105 +
106 +#ifndef _MTK_SNAND_DEF_H_
107 +#define _MTK_SNAND_DEF_H_
108 +
109 +#include "mtk-snand-os.h"
110 +
111 +#ifdef PRIVATE_MTK_SNAND_HEADER
112 +#include "mtk-snand.h"
113 +#else
114 +#include <mtk-snand.h>
115 +#endif
116 +
117 +struct mtk_snand_plat_dev;
118 +
119 +enum snand_flash_io {
120 + SNAND_IO_1_1_1,
121 + SNAND_IO_1_1_2,
122 + SNAND_IO_1_2_2,
123 + SNAND_IO_1_1_4,
124 + SNAND_IO_1_4_4,
125 +
126 + __SNAND_IO_MAX
127 +};
128 +
129 +#define SPI_IO_1_1_1 BIT(SNAND_IO_1_1_1)
130 +#define SPI_IO_1_1_2 BIT(SNAND_IO_1_1_2)
131 +#define SPI_IO_1_2_2 BIT(SNAND_IO_1_2_2)
132 +#define SPI_IO_1_1_4 BIT(SNAND_IO_1_1_4)
133 +#define SPI_IO_1_4_4 BIT(SNAND_IO_1_4_4)
134 +
135 +struct snand_opcode {
136 + uint8_t opcode;
137 + uint8_t dummy;
138 +};
139 +
140 +struct snand_io_cap {
141 + uint8_t caps;
142 + struct snand_opcode opcodes[__SNAND_IO_MAX];
143 +};
144 +
145 +#define SNAND_OP(_io, _opcode, _dummy) [_io] = { .opcode = (_opcode), \
146 + .dummy = (_dummy) }
147 +
148 +#define SNAND_IO_CAP(_name, _caps, ...) \
149 + struct snand_io_cap _name = { .caps = (_caps), \
150 + .opcodes = { __VA_ARGS__ } }
151 +
152 +#define SNAND_MAX_ID_LEN 4
153 +
154 +enum snand_id_type {
155 + SNAND_ID_DYMMY,
156 + SNAND_ID_ADDR = SNAND_ID_DYMMY,
157 + SNAND_ID_DIRECT,
158 +
159 + __SNAND_ID_TYPE_MAX
160 +};
161 +
162 +struct snand_id {
163 + uint8_t type; /* enum snand_id_type */
164 + uint8_t len;
165 + uint8_t id[SNAND_MAX_ID_LEN];
166 +};
167 +
168 +#define SNAND_ID(_type, ...) \
169 + { .type = (_type), .id = { __VA_ARGS__ }, \
170 + .len = sizeof((uint8_t[]) { __VA_ARGS__ }) }
171 +
172 +struct snand_mem_org {
173 + uint16_t pagesize;
174 + uint16_t sparesize;
175 + uint16_t pages_per_block;
176 + uint16_t blocks_per_die;
177 + uint16_t planes_per_die;
178 + uint16_t ndies;
179 +};
180 +
181 +#define SNAND_MEMORG(_ps, _ss, _ppb, _bpd, _ppd, _nd) \
182 + { .pagesize = (_ps), .sparesize = (_ss), .pages_per_block = (_ppb), \
183 + .blocks_per_die = (_bpd), .planes_per_die = (_ppd), .ndies = (_nd) }
184 +
185 +typedef int (*snand_select_die_t)(struct mtk_snand *snf, uint32_t dieidx);
186 +
187 +struct snand_flash_info {
188 + const char *model;
189 + struct snand_id id;
190 + const struct snand_mem_org memorg;
191 + const struct snand_io_cap *cap_rd;
192 + const struct snand_io_cap *cap_pl;
193 + snand_select_die_t select_die;
194 +};
195 +
196 +#define SNAND_INFO(_model, _id, _memorg, _cap_rd, _cap_pl, ...) \
197 + { .model = (_model), .id = _id, .memorg = _memorg, \
198 + .cap_rd = (_cap_rd), .cap_pl = (_cap_pl), __VA_ARGS__ }
199 +
200 +const struct snand_flash_info *snand_flash_id_lookup(enum snand_id_type type,
201 + const uint8_t *id);
202 +
203 +struct mtk_snand_soc_data {
204 + uint16_t sector_size;
205 + uint16_t max_sectors;
206 + uint16_t fdm_size;
207 + uint16_t fdm_ecc_size;
208 + uint16_t fifo_size;
209 +
210 + bool bbm_swap;
211 + bool empty_page_check;
212 + uint32_t mastersta_mask;
213 +
214 + const uint8_t *spare_sizes;
215 + uint32_t num_spare_size;
216 +
217 + uint16_t latch_lat;
218 + uint16_t sample_delay;
219 +};
220 +
221 +enum mtk_ecc_regs {
222 + ECC_DECDONE,
223 +};
224 +
225 +struct mtk_ecc_soc_data {
226 + const uint8_t *ecc_caps;
227 + uint32_t num_ecc_cap;
228 + const uint32_t *regs;
229 + uint16_t mode_shift;
230 + uint8_t errnum_bits;
231 + uint8_t errnum_shift;
232 +};
233 +
234 +struct mtk_snand {
235 + struct mtk_snand_plat_dev *pdev;
236 +
237 + void __iomem *nfi_base;
238 + void __iomem *ecc_base;
239 +
240 + enum mtk_snand_soc soc;
241 + const struct mtk_snand_soc_data *nfi_soc;
242 + const struct mtk_ecc_soc_data *ecc_soc;
243 + bool snfi_quad_spi;
244 + bool quad_spi_op;
245 +
246 + const char *model;
247 + uint64_t size;
248 + uint64_t die_size;
249 + uint32_t erasesize;
250 + uint32_t writesize;
251 + uint32_t oobsize;
252 +
253 + uint32_t num_dies;
254 + snand_select_die_t select_die;
255 +
256 + uint8_t opcode_rfc;
257 + uint8_t opcode_pl;
258 + uint8_t dummy_rfc;
259 + uint8_t mode_rfc;
260 + uint8_t mode_pl;
261 +
262 + uint32_t writesize_mask;
263 + uint32_t writesize_shift;
264 + uint32_t erasesize_mask;
265 + uint32_t erasesize_shift;
266 + uint64_t die_mask;
267 + uint32_t die_shift;
268 +
269 + uint32_t spare_per_sector;
270 + uint32_t raw_sector_size;
271 + uint32_t ecc_strength;
272 + uint32_t ecc_steps;
273 + uint32_t ecc_bytes;
274 + uint32_t ecc_parity_bits;
275 +
276 + uint8_t *page_cache; /* Used by read/write page */
277 + uint8_t *buf_cache; /* Used by block bad/markbad & auto_oob */
278 + int *sect_bf; /* Used by ECC correction */
279 +};
280 +
281 +enum mtk_snand_log_category {
282 + SNAND_LOG_NFI,
283 + SNAND_LOG_SNFI,
284 + SNAND_LOG_ECC,
285 + SNAND_LOG_CHIP,
286 +
287 + __SNAND_LOG_CAT_MAX
288 +};
289 +
290 +int mtk_ecc_setup(struct mtk_snand *snf, void *fmdaddr, uint32_t max_ecc_bytes,
291 + uint32_t msg_size);
292 +int mtk_snand_ecc_encoder_start(struct mtk_snand *snf);
293 +void mtk_snand_ecc_encoder_stop(struct mtk_snand *snf);
294 +int mtk_snand_ecc_decoder_start(struct mtk_snand *snf);
295 +void mtk_snand_ecc_decoder_stop(struct mtk_snand *snf);
296 +int mtk_ecc_wait_decoder_done(struct mtk_snand *snf);
297 +int mtk_ecc_check_decode_error(struct mtk_snand *snf);
298 +int mtk_ecc_fixup_empty_sector(struct mtk_snand *snf, uint32_t sect);
299 +
300 +int mtk_snand_mac_io(struct mtk_snand *snf, const uint8_t *out, uint32_t outlen,
301 + uint8_t *in, uint32_t inlen);
302 +int mtk_snand_set_feature(struct mtk_snand *snf, uint32_t addr, uint32_t val);
303 +
304 +int mtk_snand_log(struct mtk_snand_plat_dev *pdev,
305 + enum mtk_snand_log_category cat, const char *fmt, ...);
306 +
307 +#define snand_log_nfi(pdev, fmt, ...) \
308 + mtk_snand_log(pdev, SNAND_LOG_NFI, fmt, ##__VA_ARGS__)
309 +
310 +#define snand_log_snfi(pdev, fmt, ...) \
311 + mtk_snand_log(pdev, SNAND_LOG_SNFI, fmt, ##__VA_ARGS__)
312 +
313 +#define snand_log_ecc(pdev, fmt, ...) \
314 + mtk_snand_log(pdev, SNAND_LOG_ECC, fmt, ##__VA_ARGS__)
315 +
316 +#define snand_log_chip(pdev, fmt, ...) \
317 + mtk_snand_log(pdev, SNAND_LOG_CHIP, fmt, ##__VA_ARGS__)
318 +
319 +/* ffs64 */
320 +static inline int mtk_snand_ffs64(uint64_t x)
321 +{
322 + if (!x)
323 + return 0;
324 +
325 + if (!(x & 0xffffffff))
326 + return ffs((uint32_t)(x >> 32)) + 32;
327 +
328 + return ffs((uint32_t)(x & 0xffffffff));
329 +}
330 +
331 +/* NFI dummy commands */
332 +#define NFI_CMD_DUMMY_READ 0x00
333 +#define NFI_CMD_DUMMY_WRITE 0x80
334 +
335 +/* SPI-NAND opcodes */
336 +#define SNAND_CMD_RESET 0xff
337 +#define SNAND_CMD_BLOCK_ERASE 0xd8
338 +#define SNAND_CMD_READ_FROM_CACHE_QUAD 0xeb
339 +#define SNAND_CMD_WINBOND_SELECT_DIE 0xc2
340 +#define SNAND_CMD_READ_FROM_CACHE_DUAL 0xbb
341 +#define SNAND_CMD_READID 0x9f
342 +#define SNAND_CMD_READ_FROM_CACHE_X4 0x6b
343 +#define SNAND_CMD_READ_FROM_CACHE_X2 0x3b
344 +#define SNAND_CMD_PROGRAM_LOAD_X4 0x32
345 +#define SNAND_CMD_SET_FEATURE 0x1f
346 +#define SNAND_CMD_READ_TO_CACHE 0x13
347 +#define SNAND_CMD_PROGRAM_EXECUTE 0x10
348 +#define SNAND_CMD_GET_FEATURE 0x0f
349 +#define SNAND_CMD_READ_FROM_CACHE 0x0b
350 +#define SNAND_CMD_WRITE_ENABLE 0x06
351 +#define SNAND_CMD_PROGRAM_LOAD 0x02
352 +
353 +/* SPI-NAND feature addresses */
354 +#define SNAND_FEATURE_MICRON_DIE_ADDR 0xd0
355 +#define SNAND_MICRON_DIE_SEL_1 BIT(6)
356 +
357 +#define SNAND_FEATURE_STATUS_ADDR 0xc0
358 +#define SNAND_STATUS_OIP BIT(0)
359 +#define SNAND_STATUS_WEL BIT(1)
360 +#define SNAND_STATUS_ERASE_FAIL BIT(2)
361 +#define SNAND_STATUS_PROGRAM_FAIL BIT(3)
362 +
363 +#define SNAND_FEATURE_CONFIG_ADDR 0xb0
364 +#define SNAND_FEATURE_QUAD_ENABLE BIT(0)
365 +#define SNAND_FEATURE_ECC_EN BIT(4)
366 +
367 +#define SNAND_FEATURE_PROTECT_ADDR 0xa0
368 +
369 +#endif /* _MTK_SNAND_DEF_H_ */
370 --- /dev/null
371 +++ b/drivers/mtd/mtk-snand/mtk-snand-ecc.c
372 @@ -0,0 +1,411 @@
373 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
374 +/*
375 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
376 + *
377 + * Author: Weijie Gao <weijie.gao@mediatek.com>
378 + */
379 +
380 +#include "mtk-snand-def.h"
381 +
382 +/* ECC registers */
383 +#define ECC_ENCCON 0x000
384 +#define ENC_EN BIT(0)
385 +
386 +#define ECC_ENCCNFG 0x004
387 +#define ENC_MS_S 16
388 +#define ENC_BURST_EN BIT(8)
389 +#define ENC_TNUM_S 0
390 +
391 +#define ECC_ENCIDLE 0x00c
392 +#define ENC_IDLE BIT(0)
393 +
394 +#define ECC_DECCON 0x100
395 +#define DEC_EN BIT(0)
396 +
397 +#define ECC_DECCNFG 0x104
398 +#define DEC_EMPTY_EN BIT(31)
399 +#define DEC_CS_S 16
400 +#define DEC_CON_S 12
401 +#define DEC_CON_CORRECT 3
402 +#define DEC_BURST_EN BIT(8)
403 +#define DEC_TNUM_S 0
404 +
405 +#define ECC_DECIDLE 0x10c
406 +#define DEC_IDLE BIT(0)
407 +
408 +#define ECC_DECENUM0 0x114
409 +#define ECC_DECENUM(n) (ECC_DECENUM0 + (n) * 4)
410 +
411 +/* ECC_ENCIDLE & ECC_DECIDLE */
412 +#define ECC_IDLE BIT(0)
413 +
414 +/* ENC_MODE & DEC_MODE */
415 +#define ECC_MODE_NFI 1
416 +
417 +#define ECC_TIMEOUT 500000
418 +
419 +static const uint8_t mt7622_ecc_caps[] = { 4, 6, 8, 10, 12 };
420 +
421 +static const uint8_t mt7981_ecc_caps[] = {
422 + 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24
423 +};
424 +
425 +static const uint8_t mt7986_ecc_caps[] = {
426 + 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24
427 +};
428 +
429 +static const uint32_t mt7622_ecc_regs[] = {
430 + [ECC_DECDONE] = 0x11c,
431 +};
432 +
433 +static const uint32_t mt7981_ecc_regs[] = {
434 + [ECC_DECDONE] = 0x124,
435 +};
436 +
437 +static const uint32_t mt7986_ecc_regs[] = {
438 + [ECC_DECDONE] = 0x124,
439 +};
440 +
441 +static const struct mtk_ecc_soc_data mtk_ecc_socs[__SNAND_SOC_MAX] = {
442 + [SNAND_SOC_MT7622] = {
443 + .ecc_caps = mt7622_ecc_caps,
444 + .num_ecc_cap = ARRAY_SIZE(mt7622_ecc_caps),
445 + .regs = mt7622_ecc_regs,
446 + .mode_shift = 4,
447 + .errnum_bits = 5,
448 + .errnum_shift = 5,
449 + },
450 + [SNAND_SOC_MT7629] = {
451 + .ecc_caps = mt7622_ecc_caps,
452 + .num_ecc_cap = ARRAY_SIZE(mt7622_ecc_caps),
453 + .regs = mt7622_ecc_regs,
454 + .mode_shift = 4,
455 + .errnum_bits = 5,
456 + .errnum_shift = 5,
457 + },
458 + [SNAND_SOC_MT7981] = {
459 + .ecc_caps = mt7981_ecc_caps,
460 + .num_ecc_cap = ARRAY_SIZE(mt7981_ecc_caps),
461 + .regs = mt7981_ecc_regs,
462 + .mode_shift = 5,
463 + .errnum_bits = 5,
464 + .errnum_shift = 8,
465 + },
466 + [SNAND_SOC_MT7986] = {
467 + .ecc_caps = mt7986_ecc_caps,
468 + .num_ecc_cap = ARRAY_SIZE(mt7986_ecc_caps),
469 + .regs = mt7986_ecc_regs,
470 + .mode_shift = 5,
471 + .errnum_bits = 5,
472 + .errnum_shift = 8,
473 + },
474 +};
475 +
476 +static inline uint32_t ecc_read32(struct mtk_snand *snf, uint32_t reg)
477 +{
478 + return readl(snf->ecc_base + reg);
479 +}
480 +
481 +static inline void ecc_write32(struct mtk_snand *snf, uint32_t reg,
482 + uint32_t val)
483 +{
484 + writel(val, snf->ecc_base + reg);
485 +}
486 +
487 +static inline void ecc_write16(struct mtk_snand *snf, uint32_t reg,
488 + uint16_t val)
489 +{
490 + writew(val, snf->ecc_base + reg);
491 +}
492 +
493 +static int mtk_ecc_poll(struct mtk_snand *snf, uint32_t reg, uint32_t bits)
494 +{
495 + uint32_t val;
496 +
497 + return read16_poll_timeout(snf->ecc_base + reg, val, (val & bits), 0,
498 + ECC_TIMEOUT);
499 +}
500 +
501 +static int mtk_ecc_wait_idle(struct mtk_snand *snf, uint32_t reg)
502 +{
503 + int ret;
504 +
505 + ret = mtk_ecc_poll(snf, reg, ECC_IDLE);
506 + if (ret) {
507 + snand_log_ecc(snf->pdev, "ECC engine is busy\n");
508 + return -EBUSY;
509 + }
510 +
511 + return 0;
512 +}
513 +
514 +int mtk_ecc_setup(struct mtk_snand *snf, void *fmdaddr, uint32_t max_ecc_bytes,
515 + uint32_t msg_size)
516 +{
517 + uint32_t i, val, ecc_msg_bits, ecc_strength;
518 + int ret;
519 +
520 + snf->ecc_soc = &mtk_ecc_socs[snf->soc];
521 +
522 + snf->ecc_parity_bits = fls(1 + 8 * msg_size);
523 + ecc_strength = max_ecc_bytes * 8 / snf->ecc_parity_bits;
524 +
525 + for (i = snf->ecc_soc->num_ecc_cap - 1; i >= 0; i--) {
526 + if (snf->ecc_soc->ecc_caps[i] <= ecc_strength)
527 + break;
528 + }
529 +
530 + if (unlikely(i < 0)) {
531 + snand_log_ecc(snf->pdev, "Page size %u+%u is not supported\n",
532 + snf->writesize, snf->oobsize);
533 + return -ENOTSUPP;
534 + }
535 +
536 + snf->ecc_strength = snf->ecc_soc->ecc_caps[i];
537 + snf->ecc_bytes = DIV_ROUND_UP(snf->ecc_strength * snf->ecc_parity_bits,
538 + 8);
539 +
540 + /* Encoder config */
541 + ecc_write16(snf, ECC_ENCCON, 0);
542 + ret = mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
543 + if (ret)
544 + return ret;
545 +
546 + ecc_msg_bits = msg_size * 8;
547 + val = (ecc_msg_bits << ENC_MS_S) |
548 + (ECC_MODE_NFI << snf->ecc_soc->mode_shift) | i;
549 + ecc_write32(snf, ECC_ENCCNFG, val);
550 +
551 + /* Decoder config */
552 + ecc_write16(snf, ECC_DECCON, 0);
553 + ret = mtk_ecc_wait_idle(snf, ECC_DECIDLE);
554 + if (ret)
555 + return ret;
556 +
557 + ecc_msg_bits += snf->ecc_strength * snf->ecc_parity_bits;
558 + val = DEC_EMPTY_EN | (ecc_msg_bits << DEC_CS_S) |
559 + (DEC_CON_CORRECT << DEC_CON_S) |
560 + (ECC_MODE_NFI << snf->ecc_soc->mode_shift) | i;
561 + ecc_write32(snf, ECC_DECCNFG, val);
562 +
563 + return 0;
564 +}
565 +
566 +int mtk_snand_ecc_encoder_start(struct mtk_snand *snf)
567 +{
568 + int ret;
569 +
570 + ret = mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
571 + if (ret) {
572 + ecc_write16(snf, ECC_ENCCON, 0);
573 + mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
574 + }
575 +
576 + ecc_write16(snf, ECC_ENCCON, ENC_EN);
577 +
578 + return 0;
579 +}
580 +
581 +void mtk_snand_ecc_encoder_stop(struct mtk_snand *snf)
582 +{
583 + mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
584 + ecc_write16(snf, ECC_ENCCON, 0);
585 +}
586 +
587 +int mtk_snand_ecc_decoder_start(struct mtk_snand *snf)
588 +{
589 + int ret;
590 +
591 + ret = mtk_ecc_wait_idle(snf, ECC_DECIDLE);
592 + if (ret) {
593 + ecc_write16(snf, ECC_DECCON, 0);
594 + mtk_ecc_wait_idle(snf, ECC_DECIDLE);
595 + }
596 +
597 + ecc_write16(snf, ECC_DECCON, DEC_EN);
598 +
599 + return 0;
600 +}
601 +
602 +void mtk_snand_ecc_decoder_stop(struct mtk_snand *snf)
603 +{
604 + mtk_ecc_wait_idle(snf, ECC_DECIDLE);
605 + ecc_write16(snf, ECC_DECCON, 0);
606 +}
607 +
608 +int mtk_ecc_wait_decoder_done(struct mtk_snand *snf)
609 +{
610 + uint16_t val, step_mask = (1 << snf->ecc_steps) - 1;
611 + uint32_t reg = snf->ecc_soc->regs[ECC_DECDONE];
612 + int ret;
613 +
614 + ret = read16_poll_timeout(snf->ecc_base + reg, val,
615 + (val & step_mask) == step_mask, 0,
616 + ECC_TIMEOUT);
617 + if (ret)
618 + snand_log_ecc(snf->pdev, "ECC decoder is busy\n");
619 +
620 + return ret;
621 +}
622 +
623 +int mtk_ecc_check_decode_error(struct mtk_snand *snf)
624 +{
625 + uint32_t i, regi, fi, errnum;
626 + uint32_t errnum_shift = snf->ecc_soc->errnum_shift;
627 + uint32_t errnum_mask = (1 << snf->ecc_soc->errnum_bits) - 1;
628 + int ret = 0;
629 +
630 + for (i = 0; i < snf->ecc_steps; i++) {
631 + regi = i / 4;
632 + fi = i % 4;
633 +
634 + errnum = ecc_read32(snf, ECC_DECENUM(regi));
635 + errnum = (errnum >> (fi * errnum_shift)) & errnum_mask;
636 +
637 + if (errnum <= snf->ecc_strength) {
638 + snf->sect_bf[i] = errnum;
639 + } else {
640 + snf->sect_bf[i] = -1;
641 + ret = -EBADMSG;
642 + }
643 + }
644 +
645 + return ret;
646 +}
647 +
648 +static int mtk_ecc_check_buf_bitflips(struct mtk_snand *snf, const void *buf,
649 + size_t len, uint32_t bitflips)
650 +{
651 + const uint8_t *buf8 = buf;
652 + const uint32_t *buf32;
653 + uint32_t d, weight;
654 +
655 + while (len && ((uintptr_t)buf8) % sizeof(uint32_t)) {
656 + weight = hweight8(*buf8);
657 + bitflips += BITS_PER_BYTE - weight;
658 + buf8++;
659 + len--;
660 +
661 + if (bitflips > snf->ecc_strength)
662 + return -EBADMSG;
663 + }
664 +
665 + buf32 = (const uint32_t *)buf8;
666 + while (len >= sizeof(uint32_t)) {
667 + d = *buf32;
668 +
669 + if (d != ~0) {
670 + weight = hweight32(d);
671 + bitflips += sizeof(uint32_t) * BITS_PER_BYTE - weight;
672 + }
673 +
674 + buf32++;
675 + len -= sizeof(uint32_t);
676 +
677 + if (bitflips > snf->ecc_strength)
678 + return -EBADMSG;
679 + }
680 +
681 + buf8 = (const uint8_t *)buf32;
682 + while (len) {
683 + weight = hweight8(*buf8);
684 + bitflips += BITS_PER_BYTE - weight;
685 + buf8++;
686 + len--;
687 +
688 + if (bitflips > snf->ecc_strength)
689 + return -EBADMSG;
690 + }
691 +
692 + return bitflips;
693 +}
694 +
695 +static int mtk_ecc_check_parity_bitflips(struct mtk_snand *snf, const void *buf,
696 + uint32_t bits, uint32_t bitflips)
697 +{
698 + uint32_t len, i;
699 + uint8_t b;
700 + int rc;
701 +
702 + len = bits >> 3;
703 + bits &= 7;
704 +
705 + rc = mtk_ecc_check_buf_bitflips(snf, buf, len, bitflips);
706 + if (!bits || rc < 0)
707 + return rc;
708 +
709 + bitflips = rc;
710 +
711 + /* We want a precise count of bits */
712 + b = ((const uint8_t *)buf)[len];
713 + for (i = 0; i < bits; i++) {
714 + if (!(b & BIT(i)))
715 + bitflips++;
716 + }
717 +
718 + if (bitflips > snf->ecc_strength)
719 + return -EBADMSG;
720 +
721 + return bitflips;
722 +}
723 +
724 +static void mtk_ecc_reset_parity(void *buf, uint32_t bits)
725 +{
726 + uint32_t len;
727 +
728 + len = bits >> 3;
729 + bits &= 7;
730 +
731 + memset(buf, 0xff, len);
732 +
733 + /* Only reset bits protected by ECC to 1 */
734 + if (bits)
735 + ((uint8_t *)buf)[len] |= GENMASK(bits - 1, 0);
736 +}
737 +
738 +int mtk_ecc_fixup_empty_sector(struct mtk_snand *snf, uint32_t sect)
739 +{
740 + uint32_t ecc_bytes = snf->spare_per_sector - snf->nfi_soc->fdm_size;
741 + uint8_t *oob = snf->page_cache + snf->writesize;
742 + uint8_t *data_ptr, *fdm_ptr, *ecc_ptr;
743 + int bitflips = 0, ecc_bits, parity_bits;
744 +
745 + parity_bits = fls(snf->nfi_soc->sector_size * 8);
746 + ecc_bits = snf->ecc_strength * parity_bits;
747 +
748 + data_ptr = snf->page_cache + sect * snf->nfi_soc->sector_size;
749 + fdm_ptr = oob + sect * snf->nfi_soc->fdm_size;
750 + ecc_ptr = oob + snf->ecc_steps * snf->nfi_soc->fdm_size +
751 + sect * ecc_bytes;
752 +
753 + /*
754 + * Check whether DATA + FDM + ECC of a sector contains correctable
755 + * bitflips
756 + */
757 + bitflips = mtk_ecc_check_buf_bitflips(snf, data_ptr,
758 + snf->nfi_soc->sector_size,
759 + bitflips);
760 + if (bitflips < 0)
761 + return -EBADMSG;
762 +
763 + bitflips = mtk_ecc_check_buf_bitflips(snf, fdm_ptr,
764 + snf->nfi_soc->fdm_ecc_size,
765 + bitflips);
766 + if (bitflips < 0)
767 + return -EBADMSG;
768 +
769 + bitflips = mtk_ecc_check_parity_bitflips(snf, ecc_ptr, ecc_bits,
770 + bitflips);
771 + if (bitflips < 0)
772 + return -EBADMSG;
773 +
774 + if (!bitflips)
775 + return 0;
776 +
777 + /* Reset the data of this sector to 0xff */
778 + memset(data_ptr, 0xff, snf->nfi_soc->sector_size);
779 + memset(fdm_ptr, 0xff, snf->nfi_soc->fdm_ecc_size);
780 + mtk_ecc_reset_parity(ecc_ptr, ecc_bits);
781 +
782 + return bitflips;
783 +}
784 --- /dev/null
785 +++ b/drivers/mtd/mtk-snand/mtk-snand-ids.c
786 @@ -0,0 +1,515 @@
787 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
788 +/*
789 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
790 + *
791 + * Author: Weijie Gao <weijie.gao@mediatek.com>
792 + */
793 +
794 +#include "mtk-snand-def.h"
795 +
796 +static int mtk_snand_winbond_select_die(struct mtk_snand *snf, uint32_t dieidx);
797 +static int mtk_snand_micron_select_die(struct mtk_snand *snf, uint32_t dieidx);
798 +
799 +#define SNAND_MEMORG_512M_2K_64 SNAND_MEMORG(2048, 64, 64, 512, 1, 1)
800 +#define SNAND_MEMORG_1G_2K_64 SNAND_MEMORG(2048, 64, 64, 1024, 1, 1)
801 +#define SNAND_MEMORG_2G_2K_64 SNAND_MEMORG(2048, 64, 64, 2048, 1, 1)
802 +#define SNAND_MEMORG_2G_2K_120 SNAND_MEMORG(2048, 120, 64, 2048, 1, 1)
803 +#define SNAND_MEMORG_4G_2K_64 SNAND_MEMORG(2048, 64, 64, 4096, 1, 1)
804 +#define SNAND_MEMORG_1G_2K_120 SNAND_MEMORG(2048, 120, 64, 1024, 1, 1)
805 +#define SNAND_MEMORG_1G_2K_128 SNAND_MEMORG(2048, 128, 64, 1024, 1, 1)
806 +#define SNAND_MEMORG_2G_2K_128 SNAND_MEMORG(2048, 128, 64, 2048, 1, 1)
807 +#define SNAND_MEMORG_4G_2K_128 SNAND_MEMORG(2048, 128, 64, 4096, 1, 1)
808 +#define SNAND_MEMORG_4G_4K_240 SNAND_MEMORG(4096, 240, 64, 2048, 1, 1)
809 +#define SNAND_MEMORG_4G_4K_256 SNAND_MEMORG(4096, 256, 64, 2048, 1, 1)
810 +#define SNAND_MEMORG_8G_4K_256 SNAND_MEMORG(4096, 256, 64, 4096, 1, 1)
811 +#define SNAND_MEMORG_2G_2K_64_2P SNAND_MEMORG(2048, 64, 64, 2048, 2, 1)
812 +#define SNAND_MEMORG_2G_2K_64_2D SNAND_MEMORG(2048, 64, 64, 1024, 1, 2)
813 +#define SNAND_MEMORG_2G_2K_128_2P SNAND_MEMORG(2048, 128, 64, 2048, 2, 1)
814 +#define SNAND_MEMORG_4G_2K_64_2P SNAND_MEMORG(2048, 64, 64, 4096, 2, 1)
815 +#define SNAND_MEMORG_4G_2K_128_2P_2D SNAND_MEMORG(2048, 128, 64, 2048, 2, 2)
816 +#define SNAND_MEMORG_8G_4K_256_2D SNAND_MEMORG(4096, 256, 64, 2048, 1, 2)
817 +
818 +static const SNAND_IO_CAP(snand_cap_read_from_cache_quad,
819 + SPI_IO_1_1_1 | SPI_IO_1_1_2 | SPI_IO_1_2_2 | SPI_IO_1_1_4 |
820 + SPI_IO_1_4_4,
821 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_READ_FROM_CACHE, 8),
822 + SNAND_OP(SNAND_IO_1_1_2, SNAND_CMD_READ_FROM_CACHE_X2, 8),
823 + SNAND_OP(SNAND_IO_1_2_2, SNAND_CMD_READ_FROM_CACHE_DUAL, 4),
824 + SNAND_OP(SNAND_IO_1_1_4, SNAND_CMD_READ_FROM_CACHE_X4, 8),
825 + SNAND_OP(SNAND_IO_1_4_4, SNAND_CMD_READ_FROM_CACHE_QUAD, 4));
826 +
827 +static const SNAND_IO_CAP(snand_cap_read_from_cache_quad_q2d,
828 + SPI_IO_1_1_1 | SPI_IO_1_1_2 | SPI_IO_1_2_2 | SPI_IO_1_1_4 |
829 + SPI_IO_1_4_4,
830 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_READ_FROM_CACHE, 8),
831 + SNAND_OP(SNAND_IO_1_1_2, SNAND_CMD_READ_FROM_CACHE_X2, 8),
832 + SNAND_OP(SNAND_IO_1_2_2, SNAND_CMD_READ_FROM_CACHE_DUAL, 4),
833 + SNAND_OP(SNAND_IO_1_1_4, SNAND_CMD_READ_FROM_CACHE_X4, 8),
834 + SNAND_OP(SNAND_IO_1_4_4, SNAND_CMD_READ_FROM_CACHE_QUAD, 2));
835 +
836 +static const SNAND_IO_CAP(snand_cap_read_from_cache_quad_a8d,
837 + SPI_IO_1_1_1 | SPI_IO_1_1_2 | SPI_IO_1_2_2 | SPI_IO_1_1_4 |
838 + SPI_IO_1_4_4,
839 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_READ_FROM_CACHE, 8),
840 + SNAND_OP(SNAND_IO_1_1_2, SNAND_CMD_READ_FROM_CACHE_X2, 8),
841 + SNAND_OP(SNAND_IO_1_2_2, SNAND_CMD_READ_FROM_CACHE_DUAL, 8),
842 + SNAND_OP(SNAND_IO_1_1_4, SNAND_CMD_READ_FROM_CACHE_X4, 8),
843 + SNAND_OP(SNAND_IO_1_4_4, SNAND_CMD_READ_FROM_CACHE_QUAD, 8));
844 +
845 +static const SNAND_IO_CAP(snand_cap_read_from_cache_x4,
846 + SPI_IO_1_1_1 | SPI_IO_1_1_2 | SPI_IO_1_1_4,
847 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_READ_FROM_CACHE, 8),
848 + SNAND_OP(SNAND_IO_1_1_2, SNAND_CMD_READ_FROM_CACHE_X2, 8),
849 + SNAND_OP(SNAND_IO_1_1_4, SNAND_CMD_READ_FROM_CACHE_X4, 8));
850 +
851 +static const SNAND_IO_CAP(snand_cap_read_from_cache_x4_only,
852 + SPI_IO_1_1_1 | SPI_IO_1_1_4,
853 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_READ_FROM_CACHE, 8),
854 + SNAND_OP(SNAND_IO_1_1_4, SNAND_CMD_READ_FROM_CACHE_X4, 8));
855 +
856 +static const SNAND_IO_CAP(snand_cap_program_load_x1,
857 + SPI_IO_1_1_1,
858 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_PROGRAM_LOAD, 0));
859 +
860 +static const SNAND_IO_CAP(snand_cap_program_load_x4,
861 + SPI_IO_1_1_1 | SPI_IO_1_1_4,
862 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_PROGRAM_LOAD, 0),
863 + SNAND_OP(SNAND_IO_1_1_4, SNAND_CMD_PROGRAM_LOAD_X4, 0));
864 +
865 +static const struct snand_flash_info snand_flash_ids[] = {
866 + SNAND_INFO("W25N512GV", SNAND_ID(SNAND_ID_DYMMY, 0xef, 0xaa, 0x20),
867 + SNAND_MEMORG_512M_2K_64,
868 + &snand_cap_read_from_cache_quad,
869 + &snand_cap_program_load_x4),
870 + SNAND_INFO("W25N01GV", SNAND_ID(SNAND_ID_DYMMY, 0xef, 0xaa, 0x21),
871 + SNAND_MEMORG_1G_2K_64,
872 + &snand_cap_read_from_cache_quad,
873 + &snand_cap_program_load_x4),
874 + SNAND_INFO("W25M02GV", SNAND_ID(SNAND_ID_DYMMY, 0xef, 0xab, 0x21),
875 + SNAND_MEMORG_2G_2K_64_2D,
876 + &snand_cap_read_from_cache_quad,
877 + &snand_cap_program_load_x4,
878 + mtk_snand_winbond_select_die),
879 + SNAND_INFO("W25N01KV", SNAND_ID(SNAND_ID_DYMMY, 0xef, 0xae, 0x21),
880 + SNAND_MEMORG_1G_2K_64,
881 + &snand_cap_read_from_cache_quad,
882 + &snand_cap_program_load_x4),
883 + SNAND_INFO("W25N02KV", SNAND_ID(SNAND_ID_DYMMY, 0xef, 0xaa, 0x22),
884 + SNAND_MEMORG_2G_2K_128,
885 + &snand_cap_read_from_cache_quad,
886 + &snand_cap_program_load_x4),
887 +
888 + SNAND_INFO("GD5F1GQ4UAWxx", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0x10),
889 + SNAND_MEMORG_1G_2K_64,
890 + &snand_cap_read_from_cache_quad_q2d,
891 + &snand_cap_program_load_x4),
892 + SNAND_INFO("GD5F1GQ4UExIG", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xd1),
893 + SNAND_MEMORG_1G_2K_128,
894 + &snand_cap_read_from_cache_quad_q2d,
895 + &snand_cap_program_load_x4),
896 + SNAND_INFO("GD5F1GQ4UExxH", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xd9),
897 + SNAND_MEMORG_1G_2K_64,
898 + &snand_cap_read_from_cache_quad_q2d,
899 + &snand_cap_program_load_x4),
900 + SNAND_INFO("GD5F1GQ4xAYIG", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xf1),
901 + SNAND_MEMORG_1G_2K_64,
902 + &snand_cap_read_from_cache_quad_q2d,
903 + &snand_cap_program_load_x4),
904 + SNAND_INFO("GD5F2GQ4UExIG", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xd2),
905 + SNAND_MEMORG_2G_2K_128,
906 + &snand_cap_read_from_cache_quad_q2d,
907 + &snand_cap_program_load_x4),
908 + SNAND_INFO("GD5F2GQ5UExxH", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0x32),
909 + SNAND_MEMORG_2G_2K_64,
910 + &snand_cap_read_from_cache_quad_a8d,
911 + &snand_cap_program_load_x4),
912 + SNAND_INFO("GD5F2GQ4xAYIG", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xf2),
913 + SNAND_MEMORG_2G_2K_64,
914 + &snand_cap_read_from_cache_quad_q2d,
915 + &snand_cap_program_load_x4),
916 + SNAND_INFO("GD5F4GQ4UBxIG", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xd4),
917 + SNAND_MEMORG_4G_4K_256,
918 + &snand_cap_read_from_cache_quad_q2d,
919 + &snand_cap_program_load_x4),
920 + SNAND_INFO("GD5F4GQ4xAYIG", SNAND_ID(SNAND_ID_ADDR, 0xc8, 0xf4),
921 + SNAND_MEMORG_4G_2K_64,
922 + &snand_cap_read_from_cache_quad_q2d,
923 + &snand_cap_program_load_x4),
924 + SNAND_INFO("GD5F2GQ5UExxG", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0x52),
925 + SNAND_MEMORG_2G_2K_128,
926 + &snand_cap_read_from_cache_quad_a8d,
927 + &snand_cap_program_load_x4),
928 + SNAND_INFO("GD5F4GQ4UCxIG", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0xb4),
929 + SNAND_MEMORG_4G_4K_256,
930 + &snand_cap_read_from_cache_quad_q2d,
931 + &snand_cap_program_load_x4),
932 +
933 + SNAND_INFO("MX35LF1GE4AB", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x12),
934 + SNAND_MEMORG_1G_2K_64,
935 + &snand_cap_read_from_cache_x4,
936 + &snand_cap_program_load_x4),
937 + SNAND_INFO("MX35LF1G24AD", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x14),
938 + SNAND_MEMORG_1G_2K_128,
939 + &snand_cap_read_from_cache_quad,
940 + &snand_cap_program_load_x4),
941 + SNAND_INFO("MX31LF1GE4BC", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x1e),
942 + SNAND_MEMORG_1G_2K_64,
943 + &snand_cap_read_from_cache_x4,
944 + &snand_cap_program_load_x4),
945 + SNAND_INFO("MX35LF2GE4AB", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x22),
946 + SNAND_MEMORG_2G_2K_64,
947 + &snand_cap_read_from_cache_x4,
948 + &snand_cap_program_load_x4),
949 + SNAND_INFO("MX35LF2G24AD", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x24),
950 + SNAND_MEMORG_2G_2K_128,
951 + &snand_cap_read_from_cache_quad,
952 + &snand_cap_program_load_x4),
953 + SNAND_INFO("MX35LF2GE4AD", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x26),
954 + SNAND_MEMORG_2G_2K_128,
955 + &snand_cap_read_from_cache_x4,
956 + &snand_cap_program_load_x4),
957 + SNAND_INFO("MX35LF2G14AC", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x20),
958 + SNAND_MEMORG_2G_2K_64,
959 + &snand_cap_read_from_cache_x4,
960 + &snand_cap_program_load_x4),
961 + SNAND_INFO("MX35LF4G24AD", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x35),
962 + SNAND_MEMORG_4G_4K_256,
963 + &snand_cap_read_from_cache_quad,
964 + &snand_cap_program_load_x4),
965 + SNAND_INFO("MX35LF4GE4AD", SNAND_ID(SNAND_ID_DYMMY, 0xc2, 0x37),
966 + SNAND_MEMORG_4G_4K_256,
967 + &snand_cap_read_from_cache_x4,
968 + &snand_cap_program_load_x4),
969 +
970 + SNAND_INFO("MT29F1G01AAADD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x12),
971 + SNAND_MEMORG_1G_2K_64,
972 + &snand_cap_read_from_cache_x4,
973 + &snand_cap_program_load_x1),
974 + SNAND_INFO("MT29F1G01ABAFD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x14),
975 + SNAND_MEMORG_1G_2K_128,
976 + &snand_cap_read_from_cache_quad,
977 + &snand_cap_program_load_x4),
978 + SNAND_INFO("MT29F2G01AAAED", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x9f),
979 + SNAND_MEMORG_2G_2K_64_2P,
980 + &snand_cap_read_from_cache_x4,
981 + &snand_cap_program_load_x1),
982 + SNAND_INFO("MT29F2G01ABAGD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x24),
983 + SNAND_MEMORG_2G_2K_128_2P,
984 + &snand_cap_read_from_cache_quad,
985 + &snand_cap_program_load_x4),
986 + SNAND_INFO("MT29F4G01AAADD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x32),
987 + SNAND_MEMORG_4G_2K_64_2P,
988 + &snand_cap_read_from_cache_x4,
989 + &snand_cap_program_load_x1),
990 + SNAND_INFO("MT29F4G01ABAFD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x34),
991 + SNAND_MEMORG_4G_4K_256,
992 + &snand_cap_read_from_cache_quad,
993 + &snand_cap_program_load_x4),
994 + SNAND_INFO("MT29F4G01ADAGD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x36),
995 + SNAND_MEMORG_4G_2K_128_2P_2D,
996 + &snand_cap_read_from_cache_quad,
997 + &snand_cap_program_load_x4,
998 + mtk_snand_micron_select_die),
999 + SNAND_INFO("MT29F8G01ADAFD", SNAND_ID(SNAND_ID_DYMMY, 0x2c, 0x46),
1000 + SNAND_MEMORG_8G_4K_256_2D,
1001 + &snand_cap_read_from_cache_quad,
1002 + &snand_cap_program_load_x4,
1003 + mtk_snand_micron_select_die),
1004 +
1005 + SNAND_INFO("TC58CVG0S3HRAIG", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xc2),
1006 + SNAND_MEMORG_1G_2K_128,
1007 + &snand_cap_read_from_cache_x4,
1008 + &snand_cap_program_load_x1),
1009 + SNAND_INFO("TC58CVG1S3HRAIG", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xcb),
1010 + SNAND_MEMORG_2G_2K_128,
1011 + &snand_cap_read_from_cache_x4,
1012 + &snand_cap_program_load_x1),
1013 + SNAND_INFO("TC58CVG2S0HRAIG", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xcd),
1014 + SNAND_MEMORG_4G_4K_256,
1015 + &snand_cap_read_from_cache_x4,
1016 + &snand_cap_program_load_x1),
1017 + SNAND_INFO("TC58CVG0S3HRAIJ", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xe2),
1018 + SNAND_MEMORG_1G_2K_128,
1019 + &snand_cap_read_from_cache_x4,
1020 + &snand_cap_program_load_x4),
1021 + SNAND_INFO("TC58CVG1S3HRAIJ", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xeb),
1022 + SNAND_MEMORG_2G_2K_128,
1023 + &snand_cap_read_from_cache_x4,
1024 + &snand_cap_program_load_x4),
1025 + SNAND_INFO("TC58CVG2S0HRAIJ", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xed),
1026 + SNAND_MEMORG_4G_4K_256,
1027 + &snand_cap_read_from_cache_x4,
1028 + &snand_cap_program_load_x4),
1029 + SNAND_INFO("TH58CVG3S0HRAIJ", SNAND_ID(SNAND_ID_DYMMY, 0x98, 0xe4),
1030 + SNAND_MEMORG_8G_4K_256,
1031 + &snand_cap_read_from_cache_x4,
1032 + &snand_cap_program_load_x4),
1033 +
1034 + SNAND_INFO("F50L512M41A", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0x20),
1035 + SNAND_MEMORG_512M_2K_64,
1036 + &snand_cap_read_from_cache_x4,
1037 + &snand_cap_program_load_x4),
1038 + SNAND_INFO("F50L1G41A", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0x21),
1039 + SNAND_MEMORG_1G_2K_64,
1040 + &snand_cap_read_from_cache_x4,
1041 + &snand_cap_program_load_x4),
1042 + SNAND_INFO("F50L1G41LB", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0x01),
1043 + SNAND_MEMORG_1G_2K_64,
1044 + &snand_cap_read_from_cache_quad,
1045 + &snand_cap_program_load_x4),
1046 + SNAND_INFO("F50L2G41LB", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0x0a),
1047 + SNAND_MEMORG_2G_2K_64_2D,
1048 + &snand_cap_read_from_cache_quad,
1049 + &snand_cap_program_load_x4,
1050 + mtk_snand_winbond_select_die),
1051 +
1052 + SNAND_INFO("CS11G0T0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x00),
1053 + SNAND_MEMORG_1G_2K_128,
1054 + &snand_cap_read_from_cache_quad_q2d,
1055 + &snand_cap_program_load_x4),
1056 + SNAND_INFO("CS11G0G0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x10),
1057 + SNAND_MEMORG_1G_2K_128,
1058 + &snand_cap_read_from_cache_quad_q2d,
1059 + &snand_cap_program_load_x4),
1060 + SNAND_INFO("CS11G0S0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x20),
1061 + SNAND_MEMORG_1G_2K_64,
1062 + &snand_cap_read_from_cache_quad_q2d,
1063 + &snand_cap_program_load_x4),
1064 + SNAND_INFO("CS11G1T0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x01),
1065 + SNAND_MEMORG_2G_2K_128,
1066 + &snand_cap_read_from_cache_quad_q2d,
1067 + &snand_cap_program_load_x4),
1068 + SNAND_INFO("CS11G1S0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x21),
1069 + SNAND_MEMORG_2G_2K_64,
1070 + &snand_cap_read_from_cache_quad_q2d,
1071 + &snand_cap_program_load_x4),
1072 + SNAND_INFO("CS11G2T0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x02),
1073 + SNAND_MEMORG_4G_2K_128,
1074 + &snand_cap_read_from_cache_quad_q2d,
1075 + &snand_cap_program_load_x4),
1076 + SNAND_INFO("CS11G2S0A0AA", SNAND_ID(SNAND_ID_DYMMY, 0x6b, 0x22),
1077 + SNAND_MEMORG_4G_2K_64,
1078 + &snand_cap_read_from_cache_quad_q2d,
1079 + &snand_cap_program_load_x4),
1080 +
1081 + SNAND_INFO("EM73B044VCA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x01),
1082 + SNAND_MEMORG_512M_2K_64,
1083 + &snand_cap_read_from_cache_quad_q2d,
1084 + &snand_cap_program_load_x4),
1085 + SNAND_INFO("EM73C044SNB", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x11),
1086 + SNAND_MEMORG_1G_2K_120,
1087 + &snand_cap_read_from_cache_quad_q2d,
1088 + &snand_cap_program_load_x4),
1089 + SNAND_INFO("EM73C044SNF", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x09),
1090 + SNAND_MEMORG_1G_2K_128,
1091 + &snand_cap_read_from_cache_quad_q2d,
1092 + &snand_cap_program_load_x4),
1093 + SNAND_INFO("EM73C044VCA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x18),
1094 + SNAND_MEMORG_1G_2K_64,
1095 + &snand_cap_read_from_cache_quad_q2d,
1096 + &snand_cap_program_load_x4),
1097 + SNAND_INFO("EM73C044SNA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x19),
1098 + SNAND_MEMORG(2048, 64, 128, 512, 1, 1),
1099 + &snand_cap_read_from_cache_quad_q2d,
1100 + &snand_cap_program_load_x4),
1101 + SNAND_INFO("EM73C044VCD", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x1c),
1102 + SNAND_MEMORG_1G_2K_64,
1103 + &snand_cap_read_from_cache_quad_q2d,
1104 + &snand_cap_program_load_x4),
1105 + SNAND_INFO("EM73C044SND", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x1d),
1106 + SNAND_MEMORG_1G_2K_64,
1107 + &snand_cap_read_from_cache_quad_q2d,
1108 + &snand_cap_program_load_x4),
1109 + SNAND_INFO("EM73D044SND", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x1e),
1110 + SNAND_MEMORG_2G_2K_64,
1111 + &snand_cap_read_from_cache_quad_q2d,
1112 + &snand_cap_program_load_x4),
1113 + SNAND_INFO("EM73C044VCC", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x22),
1114 + SNAND_MEMORG_1G_2K_64,
1115 + &snand_cap_read_from_cache_quad_q2d,
1116 + &snand_cap_program_load_x4),
1117 + SNAND_INFO("EM73C044VCF", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x25),
1118 + SNAND_MEMORG_1G_2K_64,
1119 + &snand_cap_read_from_cache_quad_q2d,
1120 + &snand_cap_program_load_x4),
1121 + SNAND_INFO("EM73C044SNC", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x31),
1122 + SNAND_MEMORG_1G_2K_128,
1123 + &snand_cap_read_from_cache_quad_q2d,
1124 + &snand_cap_program_load_x4),
1125 + SNAND_INFO("EM73D044SNC", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x0a),
1126 + SNAND_MEMORG_2G_2K_120,
1127 + &snand_cap_read_from_cache_quad_q2d,
1128 + &snand_cap_program_load_x4),
1129 + SNAND_INFO("EM73D044SNA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x12),
1130 + SNAND_MEMORG_2G_2K_128,
1131 + &snand_cap_read_from_cache_quad_q2d,
1132 + &snand_cap_program_load_x4),
1133 + SNAND_INFO("EM73D044SNF", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x10),
1134 + SNAND_MEMORG_2G_2K_128,
1135 + &snand_cap_read_from_cache_quad_q2d,
1136 + &snand_cap_program_load_x4),
1137 + SNAND_INFO("EM73D044VCA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x13),
1138 + SNAND_MEMORG_2G_2K_128,
1139 + &snand_cap_read_from_cache_quad_q2d,
1140 + &snand_cap_program_load_x4),
1141 + SNAND_INFO("EM73D044VCB", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x14),
1142 + SNAND_MEMORG_2G_2K_64,
1143 + &snand_cap_read_from_cache_quad_q2d,
1144 + &snand_cap_program_load_x4),
1145 + SNAND_INFO("EM73D044VCD", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x17),
1146 + SNAND_MEMORG_2G_2K_128,
1147 + &snand_cap_read_from_cache_quad_q2d,
1148 + &snand_cap_program_load_x4),
1149 + SNAND_INFO("EM73D044VCH", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x1b),
1150 + SNAND_MEMORG_2G_2K_64,
1151 + &snand_cap_read_from_cache_quad_q2d,
1152 + &snand_cap_program_load_x4),
1153 + SNAND_INFO("EM73D044SND", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x1d),
1154 + SNAND_MEMORG_2G_2K_64,
1155 + &snand_cap_read_from_cache_quad_q2d,
1156 + &snand_cap_program_load_x4),
1157 + SNAND_INFO("EM73D044VCG", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x1f),
1158 + SNAND_MEMORG_2G_2K_64,
1159 + &snand_cap_read_from_cache_quad_q2d,
1160 + &snand_cap_program_load_x4),
1161 + SNAND_INFO("EM73D044VCE", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x20),
1162 + SNAND_MEMORG_2G_2K_64,
1163 + &snand_cap_read_from_cache_quad_q2d,
1164 + &snand_cap_program_load_x4),
1165 + SNAND_INFO("EM73D044VCL", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x2e),
1166 + SNAND_MEMORG_2G_2K_128,
1167 + &snand_cap_read_from_cache_quad_q2d,
1168 + &snand_cap_program_load_x4),
1169 + SNAND_INFO("EM73D044SNB", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x32),
1170 + SNAND_MEMORG_2G_2K_128,
1171 + &snand_cap_read_from_cache_quad_q2d,
1172 + &snand_cap_program_load_x4),
1173 + SNAND_INFO("EM73E044SNA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x03),
1174 + SNAND_MEMORG_4G_4K_256,
1175 + &snand_cap_read_from_cache_quad_q2d,
1176 + &snand_cap_program_load_x4),
1177 + SNAND_INFO("EM73E044SND", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x0b),
1178 + SNAND_MEMORG_4G_4K_240,
1179 + &snand_cap_read_from_cache_quad_q2d,
1180 + &snand_cap_program_load_x4),
1181 + SNAND_INFO("EM73E044SNB", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x23),
1182 + SNAND_MEMORG_4G_4K_256,
1183 + &snand_cap_read_from_cache_quad_q2d,
1184 + &snand_cap_program_load_x4),
1185 + SNAND_INFO("EM73E044VCA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x2c),
1186 + SNAND_MEMORG_4G_4K_256,
1187 + &snand_cap_read_from_cache_quad_q2d,
1188 + &snand_cap_program_load_x4),
1189 + SNAND_INFO("EM73E044VCB", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x2f),
1190 + SNAND_MEMORG_4G_2K_128,
1191 + &snand_cap_read_from_cache_quad_q2d,
1192 + &snand_cap_program_load_x4),
1193 + SNAND_INFO("EM73F044SNA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x24),
1194 + SNAND_MEMORG_8G_4K_256,
1195 + &snand_cap_read_from_cache_quad_q2d,
1196 + &snand_cap_program_load_x4),
1197 + SNAND_INFO("EM73F044VCA", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x2d),
1198 + SNAND_MEMORG_8G_4K_256,
1199 + &snand_cap_read_from_cache_quad_q2d,
1200 + &snand_cap_program_load_x4),
1201 + SNAND_INFO("EM73E044SNE", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x0e),
1202 + SNAND_MEMORG_8G_4K_256,
1203 + &snand_cap_read_from_cache_quad_q2d,
1204 + &snand_cap_program_load_x4),
1205 + SNAND_INFO("EM73C044SNG", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x0c),
1206 + SNAND_MEMORG_1G_2K_120,
1207 + &snand_cap_read_from_cache_quad_q2d,
1208 + &snand_cap_program_load_x4),
1209 + SNAND_INFO("EM73D044VCN", SNAND_ID(SNAND_ID_DYMMY, 0xd5, 0x0f),
1210 + SNAND_MEMORG_2G_2K_64,
1211 + &snand_cap_read_from_cache_quad_q2d,
1212 + &snand_cap_program_load_x4),
1213 +
1214 + SNAND_INFO("FM35Q1GA", SNAND_ID(SNAND_ID_DYMMY, 0xe5, 0x71),
1215 + SNAND_MEMORG_1G_2K_64,
1216 + &snand_cap_read_from_cache_x4,
1217 + &snand_cap_program_load_x4),
1218 +
1219 + SNAND_INFO("PN26G01A", SNAND_ID(SNAND_ID_DYMMY, 0xa1, 0xe1),
1220 + SNAND_MEMORG_1G_2K_128,
1221 + &snand_cap_read_from_cache_quad_q2d,
1222 + &snand_cap_program_load_x4),
1223 + SNAND_INFO("PN26G02A", SNAND_ID(SNAND_ID_DYMMY, 0xa1, 0xe2),
1224 + SNAND_MEMORG_2G_2K_128,
1225 + &snand_cap_read_from_cache_quad_q2d,
1226 + &snand_cap_program_load_x4),
1227 +
1228 + SNAND_INFO("IS37SML01G1", SNAND_ID(SNAND_ID_DYMMY, 0xc8, 0x21),
1229 + SNAND_MEMORG_1G_2K_64,
1230 + &snand_cap_read_from_cache_x4,
1231 + &snand_cap_program_load_x4),
1232 +
1233 + SNAND_INFO("ATO25D1GA", SNAND_ID(SNAND_ID_DYMMY, 0x9b, 0x12),
1234 + SNAND_MEMORG_1G_2K_64,
1235 + &snand_cap_read_from_cache_x4_only,
1236 + &snand_cap_program_load_x4),
1237 +
1238 + SNAND_INFO("HYF1GQ4U", SNAND_ID(SNAND_ID_DYMMY, 0xc9, 0x51),
1239 + SNAND_MEMORG_1G_2K_128,
1240 + &snand_cap_read_from_cache_quad_q2d,
1241 + &snand_cap_program_load_x4),
1242 + SNAND_INFO("HYF2GQ4U", SNAND_ID(SNAND_ID_DYMMY, 0xc9, 0x52),
1243 + SNAND_MEMORG_2G_2K_128,
1244 + &snand_cap_read_from_cache_quad_q2d,
1245 + &snand_cap_program_load_x4),
1246 +};
1247 +
1248 +static int mtk_snand_winbond_select_die(struct mtk_snand *snf, uint32_t dieidx)
1249 +{
1250 + uint8_t op[2];
1251 +
1252 + if (dieidx > 1) {
1253 + snand_log_chip(snf->pdev, "Invalid die index %u\n", dieidx);
1254 + return -EINVAL;
1255 + }
1256 +
1257 + op[0] = SNAND_CMD_WINBOND_SELECT_DIE;
1258 + op[1] = (uint8_t)dieidx;
1259 +
1260 + return mtk_snand_mac_io(snf, op, sizeof(op), NULL, 0);
1261 +}
1262 +
1263 +static int mtk_snand_micron_select_die(struct mtk_snand *snf, uint32_t dieidx)
1264 +{
1265 + int ret;
1266 +
1267 + if (dieidx > 1) {
1268 + snand_log_chip(snf->pdev, "Invalid die index %u\n", dieidx);
1269 + return -EINVAL;
1270 + }
1271 +
1272 + ret = mtk_snand_set_feature(snf, SNAND_FEATURE_MICRON_DIE_ADDR,
1273 + SNAND_MICRON_DIE_SEL_1);
1274 + if (ret) {
1275 + snand_log_chip(snf->pdev,
1276 + "Failed to set die selection feature\n");
1277 + return ret;
1278 + }
1279 +
1280 + return 0;
1281 +}
1282 +
1283 +const struct snand_flash_info *snand_flash_id_lookup(enum snand_id_type type,
1284 + const uint8_t *id)
1285 +{
1286 + const struct snand_id *fid;
1287 + uint32_t i;
1288 +
1289 + for (i = 0; i < ARRAY_SIZE(snand_flash_ids); i++) {
1290 + if (snand_flash_ids[i].id.type != type)
1291 + continue;
1292 +
1293 + fid = &snand_flash_ids[i].id;
1294 + if (memcmp(fid->id, id, fid->len))
1295 + continue;
1296 +
1297 + return &snand_flash_ids[i];
1298 + }
1299 +
1300 + return NULL;
1301 +}
1302 --- /dev/null
1303 +++ b/drivers/mtd/mtk-snand/mtk-snand-mtd.c
1304 @@ -0,0 +1,535 @@
1305 +// SPDX-License-Identifier: GPL-2.0
1306 +/*
1307 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1308 + *
1309 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1310 + */
1311 +
1312 +#include <common.h>
1313 +#include <dm.h>
1314 +#include <malloc.h>
1315 +#include <mapmem.h>
1316 +#include <linux/mtd/mtd.h>
1317 +#include <watchdog.h>
1318 +
1319 +#include "mtk-snand.h"
1320 +
1321 +struct mtk_snand_mtd {
1322 + struct udevice *dev;
1323 + struct mtk_snand *snf;
1324 + struct mtk_snand_chip_info cinfo;
1325 + uint8_t *page_cache;
1326 +};
1327 +
1328 +static const char snand_mtd_name_prefix[] = "spi-nand";
1329 +
1330 +static u32 snandidx;
1331 +
1332 +static inline struct mtk_snand_mtd *mtd_to_msm(struct mtd_info *mtd)
1333 +{
1334 + return mtd->priv;
1335 +}
1336 +
1337 +static int mtk_snand_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1338 +{
1339 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1340 + u64 start_addr, end_addr;
1341 + int ret;
1342 +
1343 + /* Do not allow write past end of device */
1344 + if ((instr->addr + instr->len) > mtd->size) {
1345 + pr_debug("%s: attempt to erase beyond end of device\n",
1346 + __func__);
1347 + return -EINVAL;
1348 + }
1349 +
1350 + start_addr = instr->addr & (~mtd->erasesize_mask);
1351 + end_addr = instr->addr + instr->len;
1352 + if (end_addr & mtd->erasesize_mask) {
1353 + end_addr = (end_addr + mtd->erasesize_mask) &
1354 + (~mtd->erasesize_mask);
1355 + }
1356 +
1357 + instr->state = MTD_ERASING;
1358 +
1359 + while (start_addr < end_addr) {
1360 + schedule();
1361 +
1362 + if (mtk_snand_block_isbad(msm->snf, start_addr)) {
1363 + if (!instr->scrub) {
1364 + instr->fail_addr = start_addr;
1365 + ret = -EIO;
1366 + break;
1367 + }
1368 + }
1369 +
1370 + ret = mtk_snand_erase_block(msm->snf, start_addr);
1371 + if (ret) {
1372 + instr->fail_addr = start_addr;
1373 + break;
1374 + }
1375 +
1376 + start_addr += mtd->erasesize;
1377 + }
1378 +
1379 + if (!ret) {
1380 + instr->state = MTD_ERASE_DONE;
1381 + } else {
1382 + instr->state = MTD_ERASE_FAILED;
1383 + ret = -EIO;
1384 + }
1385 +
1386 + return ret;
1387 +}
1388 +
1389 +static int mtk_snand_mtd_read_data(struct mtk_snand_mtd *msm, uint64_t addr,
1390 + struct mtd_oob_ops *ops)
1391 +{
1392 + struct mtd_info *mtd = dev_get_uclass_priv(msm->dev);
1393 + size_t len, ooblen, maxooblen, chklen;
1394 + uint32_t col, ooboffs;
1395 + uint8_t *datcache, *oobcache;
1396 + bool ecc_failed = false, raw = ops->mode == MTD_OPS_RAW ? true : false;
1397 + int ret, max_bitflips = 0;
1398 +
1399 + col = addr & mtd->writesize_mask;
1400 + addr &= ~mtd->writesize_mask;
1401 + maxooblen = mtd_oobavail(mtd, ops);
1402 + ooboffs = ops->ooboffs;
1403 + ooblen = ops->ooblen;
1404 + len = ops->len;
1405 +
1406 + datcache = len ? msm->page_cache : NULL;
1407 + oobcache = ooblen ? msm->page_cache + mtd->writesize : NULL;
1408 +
1409 + ops->oobretlen = 0;
1410 + ops->retlen = 0;
1411 +
1412 + while (len || ooblen) {
1413 + schedule();
1414 +
1415 + if (ops->mode == MTD_OPS_AUTO_OOB)
1416 + ret = mtk_snand_read_page_auto_oob(msm->snf, addr,
1417 + datcache, oobcache, maxooblen, NULL, raw);
1418 + else
1419 + ret = mtk_snand_read_page(msm->snf, addr, datcache,
1420 + oobcache, raw);
1421 +
1422 + if (ret < 0 && ret != -EBADMSG)
1423 + return ret;
1424 +
1425 + if (ret == -EBADMSG) {
1426 + mtd->ecc_stats.failed++;
1427 + ecc_failed = true;
1428 + } else {
1429 + mtd->ecc_stats.corrected += ret;
1430 + max_bitflips = max_t(int, ret, max_bitflips);
1431 + }
1432 +
1433 + mtd->ecc_stats.corrected += ret;
1434 + max_bitflips = max_t(int, ret, max_bitflips);
1435 +
1436 + if (len) {
1437 + /* Move data */
1438 + chklen = mtd->writesize - col;
1439 + if (chklen > len)
1440 + chklen = len;
1441 +
1442 + memcpy(ops->datbuf + ops->retlen, datcache + col,
1443 + chklen);
1444 + len -= chklen;
1445 + col = 0; /* (col + chklen) % */
1446 + ops->retlen += chklen;
1447 + }
1448 +
1449 + if (ooblen) {
1450 + /* Move oob */
1451 + chklen = maxooblen - ooboffs;
1452 + if (chklen > ooblen)
1453 + chklen = ooblen;
1454 +
1455 + memcpy(ops->oobbuf + ops->oobretlen, oobcache + ooboffs,
1456 + chklen);
1457 + ooblen -= chklen;
1458 + ooboffs = 0; /* (ooboffs + chklen) % maxooblen; */
1459 + ops->oobretlen += chklen;
1460 + }
1461 +
1462 + addr += mtd->writesize;
1463 + }
1464 +
1465 + return ecc_failed ? -EBADMSG : max_bitflips;
1466 +}
1467 +
1468 +static int mtk_snand_mtd_read_oob(struct mtd_info *mtd, loff_t from,
1469 + struct mtd_oob_ops *ops)
1470 +{
1471 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1472 + uint32_t maxooblen;
1473 +
1474 + if (!ops->oobbuf && !ops->datbuf) {
1475 + if (ops->ooblen || ops->len)
1476 + return -EINVAL;
1477 +
1478 + return 0;
1479 + }
1480 +
1481 + switch (ops->mode) {
1482 + case MTD_OPS_PLACE_OOB:
1483 + case MTD_OPS_AUTO_OOB:
1484 + case MTD_OPS_RAW:
1485 + break;
1486 + default:
1487 + pr_debug("%s: unsupported oob mode: %u\n", __func__, ops->mode);
1488 + return -EINVAL;
1489 + }
1490 +
1491 + maxooblen = mtd_oobavail(mtd, ops);
1492 +
1493 + /* Do not allow read past end of device */
1494 + if (ops->datbuf && (from + ops->len) > mtd->size) {
1495 + pr_debug("%s: attempt to read beyond end of device\n",
1496 + __func__);
1497 + return -EINVAL;
1498 + }
1499 +
1500 + if (unlikely(ops->ooboffs >= maxooblen)) {
1501 + pr_debug("%s: attempt to start read outside oob\n",
1502 + __func__);
1503 + return -EINVAL;
1504 + }
1505 +
1506 + if (unlikely(from >= mtd->size ||
1507 + ops->ooboffs + ops->ooblen > ((mtd->size >> mtd->writesize_shift) -
1508 + (from >> mtd->writesize_shift)) * maxooblen)) {
1509 + pr_debug("%s: attempt to read beyond end of device\n",
1510 + __func__);
1511 + return -EINVAL;
1512 + }
1513 +
1514 + return mtk_snand_mtd_read_data(msm, from, ops);
1515 +}
1516 +
1517 +static int mtk_snand_mtd_write_data(struct mtk_snand_mtd *msm, uint64_t addr,
1518 + struct mtd_oob_ops *ops)
1519 +{
1520 + struct mtd_info *mtd = dev_get_uclass_priv(msm->dev);
1521 + size_t len, ooblen, maxooblen, chklen, oobwrlen;
1522 + uint32_t col, ooboffs;
1523 + uint8_t *datcache, *oobcache;
1524 + bool raw = ops->mode == MTD_OPS_RAW ? true : false;
1525 + int ret;
1526 +
1527 + col = addr & mtd->writesize_mask;
1528 + addr &= ~mtd->writesize_mask;
1529 + maxooblen = mtd_oobavail(mtd, ops);
1530 + ooboffs = ops->ooboffs;
1531 + ooblen = ops->ooblen;
1532 + len = ops->len;
1533 +
1534 + datcache = len ? msm->page_cache : NULL;
1535 + oobcache = ooblen ? msm->page_cache + mtd->writesize : NULL;
1536 +
1537 + ops->oobretlen = 0;
1538 + ops->retlen = 0;
1539 +
1540 + while (len || ooblen) {
1541 + schedule();
1542 +
1543 + if (len) {
1544 + /* Move data */
1545 + chklen = mtd->writesize - col;
1546 + if (chklen > len)
1547 + chklen = len;
1548 +
1549 + memset(datcache, 0xff, col);
1550 + memcpy(datcache + col, ops->datbuf + ops->retlen,
1551 + chklen);
1552 + memset(datcache + col + chklen, 0xff,
1553 + mtd->writesize - col - chklen);
1554 + len -= chklen;
1555 + col = 0; /* (col + chklen) % */
1556 + ops->retlen += chklen;
1557 + }
1558 +
1559 + oobwrlen = 0;
1560 + if (ooblen) {
1561 + /* Move oob */
1562 + chklen = maxooblen - ooboffs;
1563 + if (chklen > ooblen)
1564 + chklen = ooblen;
1565 +
1566 + memset(oobcache, 0xff, ooboffs);
1567 + memcpy(oobcache + ooboffs,
1568 + ops->oobbuf + ops->oobretlen, chklen);
1569 + memset(oobcache + ooboffs + chklen, 0xff,
1570 + mtd->oobsize - ooboffs - chklen);
1571 + oobwrlen = chklen + ooboffs;
1572 + ooblen -= chklen;
1573 + ooboffs = 0; /* (ooboffs + chklen) % maxooblen; */
1574 + ops->oobretlen += chklen;
1575 + }
1576 +
1577 + if (ops->mode == MTD_OPS_AUTO_OOB)
1578 + ret = mtk_snand_write_page_auto_oob(msm->snf, addr,
1579 + datcache, oobcache, oobwrlen, NULL, raw);
1580 + else
1581 + ret = mtk_snand_write_page(msm->snf, addr, datcache,
1582 + oobcache, raw);
1583 +
1584 + if (ret)
1585 + return ret;
1586 +
1587 + addr += mtd->writesize;
1588 + }
1589 +
1590 + return 0;
1591 +}
1592 +
1593 +static int mtk_snand_mtd_write_oob(struct mtd_info *mtd, loff_t to,
1594 + struct mtd_oob_ops *ops)
1595 +{
1596 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1597 + uint32_t maxooblen;
1598 +
1599 + if (!ops->oobbuf && !ops->datbuf) {
1600 + if (ops->ooblen || ops->len)
1601 + return -EINVAL;
1602 +
1603 + return 0;
1604 + }
1605 +
1606 + switch (ops->mode) {
1607 + case MTD_OPS_PLACE_OOB:
1608 + case MTD_OPS_AUTO_OOB:
1609 + case MTD_OPS_RAW:
1610 + break;
1611 + default:
1612 + pr_debug("%s: unsupported oob mode: %u\n", __func__, ops->mode);
1613 + return -EINVAL;
1614 + }
1615 +
1616 + maxooblen = mtd_oobavail(mtd, ops);
1617 +
1618 + /* Do not allow write past end of device */
1619 + if (ops->datbuf && (to + ops->len) > mtd->size) {
1620 + pr_debug("%s: attempt to write beyond end of device\n",
1621 + __func__);
1622 + return -EINVAL;
1623 + }
1624 +
1625 + if (unlikely(ops->ooboffs >= maxooblen)) {
1626 + pr_debug("%s: attempt to start write outside oob\n",
1627 + __func__);
1628 + return -EINVAL;
1629 + }
1630 +
1631 + if (unlikely(to >= mtd->size ||
1632 + ops->ooboffs + ops->ooblen > ((mtd->size >> mtd->writesize_shift) -
1633 + (to >> mtd->writesize_shift)) * maxooblen)) {
1634 + pr_debug("%s: attempt to write beyond end of device\n",
1635 + __func__);
1636 + return -EINVAL;
1637 + }
1638 +
1639 + return mtk_snand_mtd_write_data(msm, to, ops);
1640 +}
1641 +
1642 +static int mtk_snand_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1643 + size_t *retlen, u_char *buf)
1644 +{
1645 + struct mtd_oob_ops ops = {
1646 + .mode = MTD_OPS_PLACE_OOB,
1647 + .datbuf = buf,
1648 + .len = len,
1649 + };
1650 + int ret;
1651 +
1652 + ret = mtk_snand_mtd_read_oob(mtd, from, &ops);
1653 +
1654 + if (retlen)
1655 + *retlen = ops.retlen;
1656 +
1657 + return ret;
1658 +}
1659 +
1660 +static int mtk_snand_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1661 + size_t *retlen, const u_char *buf)
1662 +{
1663 + struct mtd_oob_ops ops = {
1664 + .mode = MTD_OPS_PLACE_OOB,
1665 + .datbuf = (void *)buf,
1666 + .len = len,
1667 + };
1668 + int ret;
1669 +
1670 + ret = mtk_snand_mtd_write_oob(mtd, to, &ops);
1671 +
1672 + if (retlen)
1673 + *retlen = ops.retlen;
1674 +
1675 + return ret;
1676 +}
1677 +
1678 +static int mtk_snand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
1679 +{
1680 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1681 +
1682 + return mtk_snand_block_isbad(msm->snf, offs);
1683 +}
1684 +
1685 +static int mtk_snand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
1686 +{
1687 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1688 +
1689 + return mtk_snand_block_markbad(msm->snf, offs);
1690 +}
1691 +
1692 +static int mtk_snand_ooblayout_ecc(struct mtd_info *mtd, int section,
1693 + struct mtd_oob_region *oobecc)
1694 +{
1695 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1696 +
1697 + if (section)
1698 + return -ERANGE;
1699 +
1700 + oobecc->offset = msm->cinfo.fdm_size * msm->cinfo.num_sectors;
1701 + oobecc->length = mtd->oobsize - oobecc->offset;
1702 +
1703 + return 0;
1704 +}
1705 +
1706 +static int mtk_snand_ooblayout_free(struct mtd_info *mtd, int section,
1707 + struct mtd_oob_region *oobfree)
1708 +{
1709 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1710 +
1711 + if (section >= msm->cinfo.num_sectors)
1712 + return -ERANGE;
1713 +
1714 + oobfree->length = msm->cinfo.fdm_size - 1;
1715 + oobfree->offset = section * msm->cinfo.fdm_size + 1;
1716 +
1717 + return 0;
1718 +}
1719 +
1720 +static const struct mtd_ooblayout_ops mtk_snand_ooblayout = {
1721 + .ecc = mtk_snand_ooblayout_ecc,
1722 + .rfree = mtk_snand_ooblayout_free,
1723 +};
1724 +
1725 +static int mtk_snand_mtd_probe(struct udevice *dev)
1726 +{
1727 + struct mtk_snand_mtd *msm = dev_get_priv(dev);
1728 + struct mtd_info *mtd = dev_get_uclass_priv(dev);
1729 + struct mtk_snand_platdata mtk_snand_pdata = {};
1730 + size_t namelen;
1731 + fdt_addr_t base;
1732 + int ret;
1733 +
1734 + base = dev_read_addr_name(dev, "nfi");
1735 + if (base == FDT_ADDR_T_NONE)
1736 + return -EINVAL;
1737 + mtk_snand_pdata.nfi_base = map_sysmem(base, 0);
1738 +
1739 + base = dev_read_addr_name(dev, "ecc");
1740 + if (base == FDT_ADDR_T_NONE)
1741 + return -EINVAL;
1742 + mtk_snand_pdata.ecc_base = map_sysmem(base, 0);
1743 +
1744 + mtk_snand_pdata.soc = dev_get_driver_data(dev);
1745 + mtk_snand_pdata.quad_spi = dev_read_bool(dev, "quad-spi");
1746 +
1747 + ret = mtk_snand_init(NULL, &mtk_snand_pdata, &msm->snf);
1748 + if (ret)
1749 + return ret;
1750 +
1751 + mtk_snand_get_chip_info(msm->snf, &msm->cinfo);
1752 +
1753 + msm->page_cache = malloc(msm->cinfo.pagesize + msm->cinfo.sparesize);
1754 + if (!msm->page_cache) {
1755 + printf("%s: failed to allocate memory for page cache\n",
1756 + __func__);
1757 + ret = -ENOMEM;
1758 + goto errout1;
1759 + }
1760 +
1761 + namelen = sizeof(snand_mtd_name_prefix) + 12;
1762 +
1763 + mtd->name = malloc(namelen);
1764 + if (!mtd->name) {
1765 + printf("%s: failed to allocate memory for MTD name\n",
1766 + __func__);
1767 + ret = -ENOMEM;
1768 + goto errout2;
1769 + }
1770 +
1771 + msm->dev = dev;
1772 +
1773 + snprintf(mtd->name, namelen, "%s%u", snand_mtd_name_prefix, snandidx++);
1774 +
1775 + mtd->priv = msm;
1776 + mtd->dev = dev;
1777 + mtd->type = MTD_NANDFLASH;
1778 + mtd->flags = MTD_CAP_NANDFLASH;
1779 +
1780 + mtd->size = msm->cinfo.chipsize;
1781 + mtd->erasesize = msm->cinfo.blocksize;
1782 + mtd->writesize = msm->cinfo.pagesize;
1783 + mtd->writebufsize = mtd->writesize;
1784 + mtd->oobsize = msm->cinfo.sparesize;
1785 + mtd->oobavail = msm->cinfo.num_sectors * (msm->cinfo.fdm_size - 1);
1786 +
1787 + mtd->ooblayout = &mtk_snand_ooblayout;
1788 +
1789 + mtd->ecc_strength = msm->cinfo.ecc_strength;
1790 + mtd->bitflip_threshold = (mtd->ecc_strength * 3) / 4;
1791 + mtd->ecc_step_size = msm->cinfo.sector_size;
1792 +
1793 + mtd->_read = mtk_snand_mtd_read;
1794 + mtd->_write = mtk_snand_mtd_write;
1795 + mtd->_erase = mtk_snand_mtd_erase;
1796 + mtd->_read_oob = mtk_snand_mtd_read_oob;
1797 + mtd->_write_oob = mtk_snand_mtd_write_oob;
1798 + mtd->_block_isbad = mtk_snand_mtd_block_isbad;
1799 + mtd->_block_markbad = mtk_snand_mtd_block_markbad;
1800 +
1801 + ret = add_mtd_device(mtd);
1802 + if (ret) {
1803 + printf("%s: failed to add SPI-NAND MTD device\n", __func__);
1804 + ret = -ENODEV;
1805 + goto errout3;
1806 + }
1807 +
1808 + printf("SPI-NAND: %s (%lluMB)\n", msm->cinfo.model,
1809 + msm->cinfo.chipsize >> 20);
1810 +
1811 + return 0;
1812 +
1813 +errout3:
1814 + free(mtd->name);
1815 +
1816 +errout2:
1817 + free(msm->page_cache);
1818 +
1819 +errout1:
1820 + mtk_snand_cleanup(msm->snf);
1821 +
1822 + return ret;
1823 +}
1824 +
1825 +static const struct udevice_id mtk_snand_ids[] = {
1826 + { .compatible = "mediatek,mt7622-snand", .data = SNAND_SOC_MT7622 },
1827 + { .compatible = "mediatek,mt7629-snand", .data = SNAND_SOC_MT7629 },
1828 + { .compatible = "mediatek,mt7981-snand", .data = SNAND_SOC_MT7981 },
1829 + { .compatible = "mediatek,mt7986-snand", .data = SNAND_SOC_MT7986 },
1830 + { /* sentinel */ },
1831 +};
1832 +
1833 +U_BOOT_DRIVER(spinand) = {
1834 + .name = "mtk-snand",
1835 + .id = UCLASS_MTD,
1836 + .of_match = mtk_snand_ids,
1837 + .priv_auto = sizeof(struct mtk_snand_mtd),
1838 + .probe = mtk_snand_mtd_probe,
1839 +};
1840 --- /dev/null
1841 +++ b/drivers/mtd/mtk-snand/mtk-snand-os.c
1842 @@ -0,0 +1,39 @@
1843 +// SPDX-License-Identifier: GPL-2.0
1844 +/*
1845 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1846 + *
1847 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1848 + */
1849 +
1850 +#include "mtk-snand-def.h"
1851 +
1852 +int mtk_snand_log(struct mtk_snand_plat_dev *pdev,
1853 + enum mtk_snand_log_category cat, const char *fmt, ...)
1854 +{
1855 + const char *catname = "";
1856 + va_list ap;
1857 + int ret;
1858 +
1859 + switch (cat) {
1860 + case SNAND_LOG_NFI:
1861 + catname = "NFI: ";
1862 + break;
1863 + case SNAND_LOG_SNFI:
1864 + catname = "SNFI: ";
1865 + break;
1866 + case SNAND_LOG_ECC:
1867 + catname = "ECC: ";
1868 + break;
1869 + default:
1870 + break;
1871 + }
1872 +
1873 + puts("SPI-NAND: ");
1874 + puts(catname);
1875 +
1876 + va_start(ap, fmt);
1877 + ret = vprintf(fmt, ap);
1878 + va_end(ap);
1879 +
1880 + return ret;
1881 +}
1882 --- /dev/null
1883 +++ b/drivers/mtd/mtk-snand/mtk-snand-os.h
1884 @@ -0,0 +1,120 @@
1885 +/* SPDX-License-Identifier: GPL-2.0 */
1886 +/*
1887 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1888 + *
1889 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1890 + */
1891 +
1892 +#ifndef _MTK_SNAND_OS_H_
1893 +#define _MTK_SNAND_OS_H_
1894 +
1895 +#include <common.h>
1896 +#include <cpu_func.h>
1897 +#include <errno.h>
1898 +#include <div64.h>
1899 +#include <malloc.h>
1900 +#include <stdbool.h>
1901 +#include <stdarg.h>
1902 +#include <linux/types.h>
1903 +#include <asm/io.h>
1904 +#include <linux/bitops.h>
1905 +#include <linux/sizes.h>
1906 +#include <linux/iopoll.h>
1907 +
1908 +#ifndef ARCH_DMA_MINALIGN
1909 +#define ARCH_DMA_MINALIGN 64
1910 +#endif
1911 +
1912 +struct mtk_snand_plat_dev {
1913 + ulong unused;
1914 +};
1915 +
1916 +/* Polling helpers */
1917 +#define read16_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
1918 + readw_poll_timeout((addr), (val), (cond), (timeout_us))
1919 +
1920 +#define read32_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
1921 + readl_poll_timeout((addr), (val), (cond), (timeout_us))
1922 +
1923 +/* Timer helpers */
1924 +typedef uint64_t mtk_snand_time_t;
1925 +
1926 +static inline mtk_snand_time_t timer_get_ticks(void)
1927 +{
1928 + return get_ticks();
1929 +}
1930 +
1931 +static inline mtk_snand_time_t timer_time_to_tick(uint32_t timeout_us)
1932 +{
1933 + return usec_to_tick(timeout_us);
1934 +}
1935 +
1936 +static inline bool timer_is_timeout(mtk_snand_time_t start_tick,
1937 + mtk_snand_time_t timeout_tick)
1938 +{
1939 + return get_ticks() - start_tick > timeout_tick;
1940 +}
1941 +
1942 +/* Memory helpers */
1943 +static inline void *generic_mem_alloc(struct mtk_snand_plat_dev *pdev,
1944 + size_t size)
1945 +{
1946 + return calloc(1, size);
1947 +}
1948 +
1949 +static inline void generic_mem_free(struct mtk_snand_plat_dev *pdev, void *ptr)
1950 +{
1951 + free(ptr);
1952 +}
1953 +
1954 +static inline void *dma_mem_alloc(struct mtk_snand_plat_dev *pdev, size_t size)
1955 +{
1956 + return memalign(ARCH_DMA_MINALIGN, size);
1957 +}
1958 +
1959 +static inline void dma_mem_free(struct mtk_snand_plat_dev *pdev, void *ptr)
1960 +{
1961 + free(ptr);
1962 +}
1963 +
1964 +static inline int dma_mem_map(struct mtk_snand_plat_dev *pdev, void *vaddr,
1965 + uintptr_t *dma_addr, size_t size, bool to_device)
1966 +{
1967 + size_t cachelen = roundup(size, ARCH_DMA_MINALIGN);
1968 + uintptr_t endaddr = (uintptr_t)vaddr + cachelen;
1969 +
1970 + if (to_device)
1971 + flush_dcache_range((uintptr_t)vaddr, endaddr);
1972 + else
1973 + invalidate_dcache_range((uintptr_t)vaddr, endaddr);
1974 +
1975 + *dma_addr = (uintptr_t)vaddr;
1976 +
1977 + return 0;
1978 +}
1979 +
1980 +static inline void dma_mem_unmap(struct mtk_snand_plat_dev *pdev,
1981 + uintptr_t dma_addr, size_t size,
1982 + bool to_device)
1983 +{
1984 +}
1985 +
1986 +/* Interrupt helpers */
1987 +static inline void irq_completion_done(struct mtk_snand_plat_dev *pdev)
1988 +{
1989 +}
1990 +
1991 +static inline void irq_completion_init(struct mtk_snand_plat_dev *pdev)
1992 +{
1993 +}
1994 +
1995 +static inline int irq_completion_wait(struct mtk_snand_plat_dev *pdev,
1996 + void __iomem *reg, uint32_t bit,
1997 + uint32_t timeout_us)
1998 +{
1999 + uint32_t val;
2000 +
2001 + return read32_poll_timeout(reg, val, val & bit, 0, timeout_us);
2002 +}
2003 +
2004 +#endif /* _MTK_SNAND_OS_H_ */
2005 --- /dev/null
2006 +++ b/drivers/mtd/mtk-snand/mtk-snand.c
2007 @@ -0,0 +1,1933 @@
2008 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2009 +/*
2010 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
2011 + *
2012 + * Author: Weijie Gao <weijie.gao@mediatek.com>
2013 + */
2014 +
2015 +#include "mtk-snand-def.h"
2016 +
2017 +/* NFI registers */
2018 +#define NFI_CNFG 0x000
2019 +#define CNFG_OP_MODE_S 12
2020 +#define CNFG_OP_MODE_CUST 6
2021 +#define CNFG_OP_MODE_PROGRAM 3
2022 +#define CNFG_AUTO_FMT_EN BIT(9)
2023 +#define CNFG_HW_ECC_EN BIT(8)
2024 +#define CNFG_DMA_BURST_EN BIT(2)
2025 +#define CNFG_READ_MODE BIT(1)
2026 +#define CNFG_DMA_MODE BIT(0)
2027 +
2028 +#define NFI_PAGEFMT 0x0004
2029 +#define NFI_SPARE_SIZE_LS_S 16
2030 +#define NFI_FDM_ECC_NUM_S 12
2031 +#define NFI_FDM_NUM_S 8
2032 +#define NFI_SPARE_SIZE_S 4
2033 +#define NFI_SEC_SEL_512 BIT(2)
2034 +#define NFI_PAGE_SIZE_S 0
2035 +#define NFI_PAGE_SIZE_512_2K 0
2036 +#define NFI_PAGE_SIZE_2K_4K 1
2037 +#define NFI_PAGE_SIZE_4K_8K 2
2038 +#define NFI_PAGE_SIZE_8K_16K 3
2039 +
2040 +#define NFI_CON 0x008
2041 +#define CON_SEC_NUM_S 12
2042 +#define CON_BWR BIT(9)
2043 +#define CON_BRD BIT(8)
2044 +#define CON_NFI_RST BIT(1)
2045 +#define CON_FIFO_FLUSH BIT(0)
2046 +
2047 +#define NFI_INTR_EN 0x010
2048 +#define NFI_INTR_STA 0x014
2049 +#define NFI_IRQ_INTR_EN BIT(31)
2050 +#define NFI_IRQ_CUS_READ BIT(8)
2051 +#define NFI_IRQ_CUS_PG BIT(7)
2052 +
2053 +#define NFI_CMD 0x020
2054 +
2055 +#define NFI_STRDATA 0x040
2056 +#define STR_DATA BIT(0)
2057 +
2058 +#define NFI_STA 0x060
2059 +#define NFI_NAND_FSM GENMASK(28, 24)
2060 +#define NFI_FSM GENMASK(19, 16)
2061 +#define READ_EMPTY BIT(12)
2062 +
2063 +#define NFI_FIFOSTA 0x064
2064 +#define FIFO_WR_REMAIN_S 8
2065 +#define FIFO_RD_REMAIN_S 0
2066 +
2067 +#define NFI_ADDRCNTR 0x070
2068 +#define SEC_CNTR GENMASK(16, 12)
2069 +#define SEC_CNTR_S 12
2070 +#define NFI_SEC_CNTR(val) (((val) & SEC_CNTR) >> SEC_CNTR_S)
2071 +
2072 +#define NFI_STRADDR 0x080
2073 +
2074 +#define NFI_BYTELEN 0x084
2075 +#define BUS_SEC_CNTR(val) (((val) & SEC_CNTR) >> SEC_CNTR_S)
2076 +
2077 +#define NFI_FDM0L 0x0a0
2078 +#define NFI_FDM0M 0x0a4
2079 +#define NFI_FDML(n) (NFI_FDM0L + (n) * 8)
2080 +#define NFI_FDMM(n) (NFI_FDM0M + (n) * 8)
2081 +
2082 +#define NFI_DEBUG_CON1 0x220
2083 +#define WBUF_EN BIT(2)
2084 +
2085 +#define NFI_MASTERSTA 0x224
2086 +#define MAS_ADDR GENMASK(11, 9)
2087 +#define MAS_RD GENMASK(8, 6)
2088 +#define MAS_WR GENMASK(5, 3)
2089 +#define MAS_RDDLY GENMASK(2, 0)
2090 +#define NFI_MASTERSTA_MASK_7622 (MAS_ADDR | MAS_RD | MAS_WR | MAS_RDDLY)
2091 +#define AHB_BUS_BUSY BIT(1)
2092 +#define BUS_BUSY BIT(0)
2093 +#define NFI_MASTERSTA_MASK_7981 (AHB_BUS_BUSY | BUS_BUSY)
2094 +#define NFI_MASTERSTA_MASK_7986 (AHB_BUS_BUSY | BUS_BUSY)
2095 +
2096 +/* SNFI registers */
2097 +#define SNF_MAC_CTL 0x500
2098 +#define MAC_XIO_SEL BIT(4)
2099 +#define SF_MAC_EN BIT(3)
2100 +#define SF_TRIG BIT(2)
2101 +#define WIP_READY BIT(1)
2102 +#define WIP BIT(0)
2103 +
2104 +#define SNF_MAC_OUTL 0x504
2105 +#define SNF_MAC_INL 0x508
2106 +
2107 +#define SNF_RD_CTL2 0x510
2108 +#define DATA_READ_DUMMY_S 8
2109 +#define DATA_READ_CMD_S 0
2110 +
2111 +#define SNF_RD_CTL3 0x514
2112 +
2113 +#define SNF_PG_CTL1 0x524
2114 +#define PG_LOAD_CMD_S 8
2115 +
2116 +#define SNF_PG_CTL2 0x528
2117 +
2118 +#define SNF_MISC_CTL 0x538
2119 +#define SW_RST BIT(28)
2120 +#define FIFO_RD_LTC_S 25
2121 +#define PG_LOAD_X4_EN BIT(20)
2122 +#define DATA_READ_MODE_S 16
2123 +#define DATA_READ_MODE GENMASK(18, 16)
2124 +#define DATA_READ_MODE_X1 0
2125 +#define DATA_READ_MODE_X2 1
2126 +#define DATA_READ_MODE_X4 2
2127 +#define DATA_READ_MODE_DUAL 5
2128 +#define DATA_READ_MODE_QUAD 6
2129 +#define LATCH_LAT_S 8
2130 +#define LATCH_LAT GENMASK(9, 8)
2131 +#define PG_LOAD_CUSTOM_EN BIT(7)
2132 +#define DATARD_CUSTOM_EN BIT(6)
2133 +#define CS_DESELECT_CYC_S 0
2134 +
2135 +#define SNF_MISC_CTL2 0x53c
2136 +#define PROGRAM_LOAD_BYTE_NUM_S 16
2137 +#define READ_DATA_BYTE_NUM_S 11
2138 +
2139 +#define SNF_DLY_CTL3 0x548
2140 +#define SFCK_SAM_DLY_S 0
2141 +
2142 +#define SNF_STA_CTL1 0x550
2143 +#define CUS_PG_DONE BIT(28)
2144 +#define CUS_READ_DONE BIT(27)
2145 +#define SPI_STATE_S 0
2146 +#define SPI_STATE GENMASK(3, 0)
2147 +
2148 +#define SNF_CFG 0x55c
2149 +#define SPI_MODE BIT(0)
2150 +
2151 +#define SNF_GPRAM 0x800
2152 +#define SNF_GPRAM_SIZE 0xa0
2153 +
2154 +#define SNFI_POLL_INTERVAL 1000000
2155 +
2156 +static const uint8_t mt7622_spare_sizes[] = { 16, 26, 27, 28 };
2157 +
2158 +static const uint8_t mt7981_spare_sizes[] = {
2159 + 16, 26, 27, 28, 32, 36, 40, 44, 48, 49, 50, 51, 52, 62, 61, 63, 64,
2160 + 67, 74
2161 +};
2162 +
2163 +static const uint8_t mt7986_spare_sizes[] = {
2164 + 16, 26, 27, 28, 32, 36, 40, 44, 48, 49, 50, 51, 52, 62, 61, 63, 64,
2165 + 67, 74
2166 +};
2167 +
2168 +static const struct mtk_snand_soc_data mtk_snand_socs[__SNAND_SOC_MAX] = {
2169 + [SNAND_SOC_MT7622] = {
2170 + .sector_size = 512,
2171 + .max_sectors = 8,
2172 + .fdm_size = 8,
2173 + .fdm_ecc_size = 1,
2174 + .fifo_size = 32,
2175 + .bbm_swap = false,
2176 + .empty_page_check = false,
2177 + .mastersta_mask = NFI_MASTERSTA_MASK_7622,
2178 + .spare_sizes = mt7622_spare_sizes,
2179 + .num_spare_size = ARRAY_SIZE(mt7622_spare_sizes),
2180 + .latch_lat = 0,
2181 + .sample_delay = 40
2182 + },
2183 + [SNAND_SOC_MT7629] = {
2184 + .sector_size = 512,
2185 + .max_sectors = 8,
2186 + .fdm_size = 8,
2187 + .fdm_ecc_size = 1,
2188 + .fifo_size = 32,
2189 + .bbm_swap = true,
2190 + .empty_page_check = false,
2191 + .mastersta_mask = NFI_MASTERSTA_MASK_7622,
2192 + .spare_sizes = mt7622_spare_sizes,
2193 + .num_spare_size = ARRAY_SIZE(mt7622_spare_sizes),
2194 + .latch_lat = 0,
2195 + .sample_delay = 40
2196 + },
2197 + [SNAND_SOC_MT7981] = {
2198 + .sector_size = 1024,
2199 + .max_sectors = 16,
2200 + .fdm_size = 8,
2201 + .fdm_ecc_size = 1,
2202 + .fifo_size = 64,
2203 + .bbm_swap = true,
2204 + .empty_page_check = true,
2205 + .mastersta_mask = NFI_MASTERSTA_MASK_7981,
2206 + .spare_sizes = mt7981_spare_sizes,
2207 + .num_spare_size = ARRAY_SIZE(mt7981_spare_sizes),
2208 + .latch_lat = 0,
2209 + .sample_delay = 40
2210 + },
2211 + [SNAND_SOC_MT7986] = {
2212 + .sector_size = 1024,
2213 + .max_sectors = 16,
2214 + .fdm_size = 8,
2215 + .fdm_ecc_size = 1,
2216 + .fifo_size = 64,
2217 + .bbm_swap = true,
2218 + .empty_page_check = true,
2219 + .mastersta_mask = NFI_MASTERSTA_MASK_7986,
2220 + .spare_sizes = mt7986_spare_sizes,
2221 + .num_spare_size = ARRAY_SIZE(mt7986_spare_sizes),
2222 + .latch_lat = 0,
2223 + .sample_delay = 40
2224 + },
2225 +};
2226 +
2227 +static inline uint32_t nfi_read32(struct mtk_snand *snf, uint32_t reg)
2228 +{
2229 + return readl(snf->nfi_base + reg);
2230 +}
2231 +
2232 +static inline void nfi_write32(struct mtk_snand *snf, uint32_t reg,
2233 + uint32_t val)
2234 +{
2235 + writel(val, snf->nfi_base + reg);
2236 +}
2237 +
2238 +static inline void nfi_write16(struct mtk_snand *snf, uint32_t reg,
2239 + uint16_t val)
2240 +{
2241 + writew(val, snf->nfi_base + reg);
2242 +}
2243 +
2244 +static inline void nfi_rmw32(struct mtk_snand *snf, uint32_t reg, uint32_t clr,
2245 + uint32_t set)
2246 +{
2247 + uint32_t val;
2248 +
2249 + val = readl(snf->nfi_base + reg);
2250 + val &= ~clr;
2251 + val |= set;
2252 + writel(val, snf->nfi_base + reg);
2253 +}
2254 +
2255 +static void nfi_write_data(struct mtk_snand *snf, uint32_t reg,
2256 + const uint8_t *data, uint32_t len)
2257 +{
2258 + uint32_t i, val = 0, es = sizeof(uint32_t);
2259 +
2260 + for (i = reg; i < reg + len; i++) {
2261 + val |= ((uint32_t)*data++) << (8 * (i % es));
2262 +
2263 + if (i % es == es - 1 || i == reg + len - 1) {
2264 + nfi_write32(snf, i & ~(es - 1), val);
2265 + val = 0;
2266 + }
2267 + }
2268 +}
2269 +
2270 +static void nfi_read_data(struct mtk_snand *snf, uint32_t reg, uint8_t *data,
2271 + uint32_t len)
2272 +{
2273 + uint32_t i, val = 0, es = sizeof(uint32_t);
2274 +
2275 + for (i = reg; i < reg + len; i++) {
2276 + if (i == reg || i % es == 0)
2277 + val = nfi_read32(snf, i & ~(es - 1));
2278 +
2279 + *data++ = (uint8_t)(val >> (8 * (i % es)));
2280 + }
2281 +}
2282 +
2283 +static inline void do_bm_swap(uint8_t *bm1, uint8_t *bm2)
2284 +{
2285 + uint8_t tmp = *bm1;
2286 + *bm1 = *bm2;
2287 + *bm2 = tmp;
2288 +}
2289 +
2290 +static void mtk_snand_bm_swap_raw(struct mtk_snand *snf)
2291 +{
2292 + uint32_t fdm_bbm_pos;
2293 +
2294 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
2295 + return;
2296 +
2297 + fdm_bbm_pos = (snf->ecc_steps - 1) * snf->raw_sector_size +
2298 + snf->nfi_soc->sector_size;
2299 + do_bm_swap(&snf->page_cache[fdm_bbm_pos],
2300 + &snf->page_cache[snf->writesize]);
2301 +}
2302 +
2303 +static void mtk_snand_bm_swap(struct mtk_snand *snf)
2304 +{
2305 + uint32_t buf_bbm_pos, fdm_bbm_pos;
2306 +
2307 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
2308 + return;
2309 +
2310 + buf_bbm_pos = snf->writesize -
2311 + (snf->ecc_steps - 1) * snf->spare_per_sector;
2312 + fdm_bbm_pos = snf->writesize +
2313 + (snf->ecc_steps - 1) * snf->nfi_soc->fdm_size;
2314 + do_bm_swap(&snf->page_cache[fdm_bbm_pos],
2315 + &snf->page_cache[buf_bbm_pos]);
2316 +}
2317 +
2318 +static void mtk_snand_fdm_bm_swap_raw(struct mtk_snand *snf)
2319 +{
2320 + uint32_t fdm_bbm_pos1, fdm_bbm_pos2;
2321 +
2322 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
2323 + return;
2324 +
2325 + fdm_bbm_pos1 = snf->nfi_soc->sector_size;
2326 + fdm_bbm_pos2 = (snf->ecc_steps - 1) * snf->raw_sector_size +
2327 + snf->nfi_soc->sector_size;
2328 + do_bm_swap(&snf->page_cache[fdm_bbm_pos1],
2329 + &snf->page_cache[fdm_bbm_pos2]);
2330 +}
2331 +
2332 +static void mtk_snand_fdm_bm_swap(struct mtk_snand *snf)
2333 +{
2334 + uint32_t fdm_bbm_pos1, fdm_bbm_pos2;
2335 +
2336 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
2337 + return;
2338 +
2339 + fdm_bbm_pos1 = snf->writesize;
2340 + fdm_bbm_pos2 = snf->writesize +
2341 + (snf->ecc_steps - 1) * snf->nfi_soc->fdm_size;
2342 + do_bm_swap(&snf->page_cache[fdm_bbm_pos1],
2343 + &snf->page_cache[fdm_bbm_pos2]);
2344 +}
2345 +
2346 +static int mtk_nfi_reset(struct mtk_snand *snf)
2347 +{
2348 + uint32_t val, fifo_mask;
2349 + int ret;
2350 +
2351 + nfi_write32(snf, NFI_CON, CON_FIFO_FLUSH | CON_NFI_RST);
2352 +
2353 + ret = read16_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
2354 + !(val & snf->nfi_soc->mastersta_mask), 0,
2355 + SNFI_POLL_INTERVAL);
2356 + if (ret) {
2357 + snand_log_nfi(snf->pdev,
2358 + "NFI master is still busy after reset\n");
2359 + return ret;
2360 + }
2361 +
2362 + ret = read32_poll_timeout(snf->nfi_base + NFI_STA, val,
2363 + !(val & (NFI_FSM | NFI_NAND_FSM)), 0,
2364 + SNFI_POLL_INTERVAL);
2365 + if (ret) {
2366 + snand_log_nfi(snf->pdev, "Failed to reset NFI\n");
2367 + return ret;
2368 + }
2369 +
2370 + fifo_mask = ((snf->nfi_soc->fifo_size - 1) << FIFO_RD_REMAIN_S) |
2371 + ((snf->nfi_soc->fifo_size - 1) << FIFO_WR_REMAIN_S);
2372 + ret = read16_poll_timeout(snf->nfi_base + NFI_FIFOSTA, val,
2373 + !(val & fifo_mask), 0, SNFI_POLL_INTERVAL);
2374 + if (ret) {
2375 + snand_log_nfi(snf->pdev, "NFI FIFOs are not empty\n");
2376 + return ret;
2377 + }
2378 +
2379 + return 0;
2380 +}
2381 +
2382 +static int mtk_snand_mac_reset(struct mtk_snand *snf)
2383 +{
2384 + int ret;
2385 + uint32_t val;
2386 +
2387 + nfi_rmw32(snf, SNF_MISC_CTL, 0, SW_RST);
2388 +
2389 + ret = read32_poll_timeout(snf->nfi_base + SNF_STA_CTL1, val,
2390 + !(val & SPI_STATE), 0, SNFI_POLL_INTERVAL);
2391 + if (ret)
2392 + snand_log_snfi(snf->pdev, "Failed to reset SNFI MAC\n");
2393 +
2394 + nfi_write32(snf, SNF_MISC_CTL, (2 << FIFO_RD_LTC_S) |
2395 + (10 << CS_DESELECT_CYC_S) | (snf->nfi_soc->latch_lat << LATCH_LAT_S));
2396 +
2397 + return ret;
2398 +}
2399 +
2400 +static int mtk_snand_mac_trigger(struct mtk_snand *snf, uint32_t outlen,
2401 + uint32_t inlen)
2402 +{
2403 + int ret;
2404 + uint32_t val;
2405 +
2406 + nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN);
2407 + nfi_write32(snf, SNF_MAC_OUTL, outlen);
2408 + nfi_write32(snf, SNF_MAC_INL, inlen);
2409 +
2410 + nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN | SF_TRIG);
2411 +
2412 + ret = read32_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val,
2413 + val & WIP_READY, 0, SNFI_POLL_INTERVAL);
2414 + if (ret) {
2415 + snand_log_snfi(snf->pdev, "Timed out waiting for WIP_READY\n");
2416 + goto cleanup;
2417 + }
2418 +
2419 + ret = read32_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val,
2420 + !(val & WIP), 0, SNFI_POLL_INTERVAL);
2421 + if (ret) {
2422 + snand_log_snfi(snf->pdev,
2423 + "Timed out waiting for WIP cleared\n");
2424 + }
2425 +
2426 +cleanup:
2427 + nfi_write32(snf, SNF_MAC_CTL, 0);
2428 +
2429 + return ret;
2430 +}
2431 +
2432 +int mtk_snand_mac_io(struct mtk_snand *snf, const uint8_t *out, uint32_t outlen,
2433 + uint8_t *in, uint32_t inlen)
2434 +{
2435 + int ret;
2436 +
2437 + if (outlen + inlen > SNF_GPRAM_SIZE)
2438 + return -EINVAL;
2439 +
2440 + mtk_snand_mac_reset(snf);
2441 +
2442 + nfi_write_data(snf, SNF_GPRAM, out, outlen);
2443 +
2444 + ret = mtk_snand_mac_trigger(snf, outlen, inlen);
2445 + if (ret)
2446 + return ret;
2447 +
2448 + if (!inlen)
2449 + return 0;
2450 +
2451 + nfi_read_data(snf, SNF_GPRAM + outlen, in, inlen);
2452 +
2453 + return 0;
2454 +}
2455 +
2456 +static int mtk_snand_get_feature(struct mtk_snand *snf, uint32_t addr)
2457 +{
2458 + uint8_t op[2], val;
2459 + int ret;
2460 +
2461 + op[0] = SNAND_CMD_GET_FEATURE;
2462 + op[1] = (uint8_t)addr;
2463 +
2464 + ret = mtk_snand_mac_io(snf, op, sizeof(op), &val, 1);
2465 + if (ret)
2466 + return ret;
2467 +
2468 + return val;
2469 +}
2470 +
2471 +int mtk_snand_set_feature(struct mtk_snand *snf, uint32_t addr, uint32_t val)
2472 +{
2473 + uint8_t op[3];
2474 +
2475 + op[0] = SNAND_CMD_SET_FEATURE;
2476 + op[1] = (uint8_t)addr;
2477 + op[2] = (uint8_t)val;
2478 +
2479 + return mtk_snand_mac_io(snf, op, sizeof(op), NULL, 0);
2480 +}
2481 +
2482 +static int mtk_snand_poll_status(struct mtk_snand *snf, uint32_t wait_us)
2483 +{
2484 + int val;
2485 + mtk_snand_time_t time_start, tmo;
2486 +
2487 + time_start = timer_get_ticks();
2488 + tmo = timer_time_to_tick(wait_us);
2489 +
2490 + do {
2491 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_STATUS_ADDR);
2492 + if (!(val & SNAND_STATUS_OIP))
2493 + return val & (SNAND_STATUS_ERASE_FAIL |
2494 + SNAND_STATUS_PROGRAM_FAIL);
2495 + } while (!timer_is_timeout(time_start, tmo));
2496 +
2497 + return -ETIMEDOUT;
2498 +}
2499 +
2500 +int mtk_snand_chip_reset(struct mtk_snand *snf)
2501 +{
2502 + uint8_t op = SNAND_CMD_RESET;
2503 + int ret;
2504 +
2505 + ret = mtk_snand_mac_io(snf, &op, 1, NULL, 0);
2506 + if (ret)
2507 + return ret;
2508 +
2509 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
2510 + if (ret < 0)
2511 + return ret;
2512 +
2513 + return 0;
2514 +}
2515 +
2516 +static int mtk_snand_config_feature(struct mtk_snand *snf, uint8_t clr,
2517 + uint8_t set)
2518 +{
2519 + int val, newval;
2520 + int ret;
2521 +
2522 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_CONFIG_ADDR);
2523 + if (val < 0) {
2524 + snand_log_chip(snf->pdev,
2525 + "Failed to get configuration feature\n");
2526 + return val;
2527 + }
2528 +
2529 + newval = (val & (~clr)) | set;
2530 +
2531 + if (newval == val)
2532 + return 0;
2533 +
2534 + ret = mtk_snand_set_feature(snf, SNAND_FEATURE_CONFIG_ADDR,
2535 + (uint8_t)newval);
2536 + if (val < 0) {
2537 + snand_log_chip(snf->pdev,
2538 + "Failed to set configuration feature\n");
2539 + return ret;
2540 + }
2541 +
2542 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_CONFIG_ADDR);
2543 + if (val < 0) {
2544 + snand_log_chip(snf->pdev,
2545 + "Failed to get configuration feature\n");
2546 + return val;
2547 + }
2548 +
2549 + if (newval != val)
2550 + return -ENOTSUPP;
2551 +
2552 + return 0;
2553 +}
2554 +
2555 +static int mtk_snand_ondie_ecc_control(struct mtk_snand *snf, bool enable)
2556 +{
2557 + int ret;
2558 +
2559 + if (enable)
2560 + ret = mtk_snand_config_feature(snf, 0, SNAND_FEATURE_ECC_EN);
2561 + else
2562 + ret = mtk_snand_config_feature(snf, SNAND_FEATURE_ECC_EN, 0);
2563 +
2564 + if (ret) {
2565 + snand_log_chip(snf->pdev, "Failed to %s On-Die ECC engine\n",
2566 + enable ? "enable" : "disable");
2567 + }
2568 +
2569 + return ret;
2570 +}
2571 +
2572 +static int mtk_snand_qspi_control(struct mtk_snand *snf, bool enable)
2573 +{
2574 + int ret;
2575 +
2576 + if (enable) {
2577 + ret = mtk_snand_config_feature(snf, 0,
2578 + SNAND_FEATURE_QUAD_ENABLE);
2579 + } else {
2580 + ret = mtk_snand_config_feature(snf,
2581 + SNAND_FEATURE_QUAD_ENABLE, 0);
2582 + }
2583 +
2584 + if (ret) {
2585 + snand_log_chip(snf->pdev, "Failed to %s quad spi\n",
2586 + enable ? "enable" : "disable");
2587 + }
2588 +
2589 + return ret;
2590 +}
2591 +
2592 +static int mtk_snand_unlock(struct mtk_snand *snf)
2593 +{
2594 + int ret;
2595 +
2596 + ret = mtk_snand_set_feature(snf, SNAND_FEATURE_PROTECT_ADDR, 0);
2597 + if (ret) {
2598 + snand_log_chip(snf->pdev, "Failed to set protection feature\n");
2599 + return ret;
2600 + }
2601 +
2602 + return 0;
2603 +}
2604 +
2605 +static int mtk_snand_write_enable(struct mtk_snand *snf)
2606 +{
2607 + uint8_t op = SNAND_CMD_WRITE_ENABLE;
2608 + int ret, val;
2609 +
2610 + ret = mtk_snand_mac_io(snf, &op, 1, NULL, 0);
2611 + if (ret)
2612 + return ret;
2613 +
2614 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_STATUS_ADDR);
2615 + if (val < 0)
2616 + return ret;
2617 +
2618 + if (val & SNAND_STATUS_WEL)
2619 + return 0;
2620 +
2621 + snand_log_chip(snf->pdev, "Failed to send write-enable command\n");
2622 +
2623 + return -ENOTSUPP;
2624 +}
2625 +
2626 +static int mtk_snand_select_die(struct mtk_snand *snf, uint32_t dieidx)
2627 +{
2628 + if (!snf->select_die)
2629 + return 0;
2630 +
2631 + return snf->select_die(snf, dieidx);
2632 +}
2633 +
2634 +static uint64_t mtk_snand_select_die_address(struct mtk_snand *snf,
2635 + uint64_t addr)
2636 +{
2637 + uint32_t dieidx;
2638 +
2639 + if (!snf->select_die)
2640 + return addr;
2641 +
2642 + dieidx = addr >> snf->die_shift;
2643 +
2644 + mtk_snand_select_die(snf, dieidx);
2645 +
2646 + return addr & snf->die_mask;
2647 +}
2648 +
2649 +static uint32_t mtk_snand_get_plane_address(struct mtk_snand *snf,
2650 + uint32_t page)
2651 +{
2652 + uint32_t pages_per_block;
2653 +
2654 + pages_per_block = 1 << (snf->erasesize_shift - snf->writesize_shift);
2655 +
2656 + if (page & pages_per_block)
2657 + return 1 << (snf->writesize_shift + 1);
2658 +
2659 + return 0;
2660 +}
2661 +
2662 +static int mtk_snand_page_op(struct mtk_snand *snf, uint32_t page, uint8_t cmd)
2663 +{
2664 + uint8_t op[4];
2665 +
2666 + op[0] = cmd;
2667 + op[1] = (page >> 16) & 0xff;
2668 + op[2] = (page >> 8) & 0xff;
2669 + op[3] = page & 0xff;
2670 +
2671 + return mtk_snand_mac_io(snf, op, sizeof(op), NULL, 0);
2672 +}
2673 +
2674 +static void mtk_snand_read_fdm(struct mtk_snand *snf, uint8_t *buf)
2675 +{
2676 + uint32_t vall, valm;
2677 + uint8_t *oobptr = buf;
2678 + int i, j;
2679 +
2680 + for (i = 0; i < snf->ecc_steps; i++) {
2681 + vall = nfi_read32(snf, NFI_FDML(i));
2682 + valm = nfi_read32(snf, NFI_FDMM(i));
2683 +
2684 + for (j = 0; j < snf->nfi_soc->fdm_size; j++)
2685 + oobptr[j] = (j >= 4 ? valm : vall) >> ((j % 4) * 8);
2686 +
2687 + oobptr += snf->nfi_soc->fdm_size;
2688 + }
2689 +}
2690 +
2691 +static int mtk_snand_read_ecc_parity(struct mtk_snand *snf, uint32_t page,
2692 + uint32_t sect, uint8_t *oob)
2693 +{
2694 + uint32_t ecc_bytes = snf->spare_per_sector - snf->nfi_soc->fdm_size;
2695 + uint32_t coladdr, raw_offs, offs;
2696 + uint8_t op[4];
2697 +
2698 + if (sizeof(op) + ecc_bytes > SNF_GPRAM_SIZE) {
2699 + snand_log_snfi(snf->pdev,
2700 + "ECC parity size does not fit the GPRAM\n");
2701 + return -ENOTSUPP;
2702 + }
2703 +
2704 + raw_offs = sect * snf->raw_sector_size + snf->nfi_soc->sector_size +
2705 + snf->nfi_soc->fdm_size;
2706 + offs = snf->ecc_steps * snf->nfi_soc->fdm_size + sect * ecc_bytes;
2707 +
2708 + /* Column address with plane bit */
2709 + coladdr = raw_offs | mtk_snand_get_plane_address(snf, page);
2710 +
2711 + op[0] = SNAND_CMD_READ_FROM_CACHE;
2712 + op[1] = (coladdr >> 8) & 0xff;
2713 + op[2] = coladdr & 0xff;
2714 + op[3] = 0;
2715 +
2716 + return mtk_snand_mac_io(snf, op, sizeof(op), oob + offs, ecc_bytes);
2717 +}
2718 +
2719 +static int mtk_snand_check_ecc_result(struct mtk_snand *snf, uint32_t page)
2720 +{
2721 + uint8_t *oob = snf->page_cache + snf->writesize;
2722 + int i, rc, ret = 0, max_bitflips = 0;
2723 +
2724 + for (i = 0; i < snf->ecc_steps; i++) {
2725 + if (snf->sect_bf[i] >= 0) {
2726 + if (snf->sect_bf[i] > max_bitflips)
2727 + max_bitflips = snf->sect_bf[i];
2728 + continue;
2729 + }
2730 +
2731 + rc = mtk_snand_read_ecc_parity(snf, page, i, oob);
2732 + if (rc)
2733 + return rc;
2734 +
2735 + rc = mtk_ecc_fixup_empty_sector(snf, i);
2736 + if (rc < 0) {
2737 + ret = -EBADMSG;
2738 +
2739 + snand_log_ecc(snf->pdev,
2740 + "Uncorrectable bitflips in page %u sect %u\n",
2741 + page, i);
2742 + } else if (rc) {
2743 + snf->sect_bf[i] = rc;
2744 +
2745 + if (snf->sect_bf[i] > max_bitflips)
2746 + max_bitflips = snf->sect_bf[i];
2747 +
2748 + snand_log_ecc(snf->pdev,
2749 + "%u bitflip%s corrected in page %u sect %u\n",
2750 + rc, rc > 1 ? "s" : "", page, i);
2751 + } else {
2752 + snf->sect_bf[i] = 0;
2753 + }
2754 + }
2755 +
2756 + return ret ? ret : max_bitflips;
2757 +}
2758 +
2759 +static int mtk_snand_read_cache(struct mtk_snand *snf, uint32_t page, bool raw)
2760 +{
2761 + uint32_t coladdr, rwbytes, mode, len, val;
2762 + uintptr_t dma_addr;
2763 + int ret;
2764 +
2765 + /* Column address with plane bit */
2766 + coladdr = mtk_snand_get_plane_address(snf, page);
2767 +
2768 + mtk_snand_mac_reset(snf);
2769 + mtk_nfi_reset(snf);
2770 +
2771 + /* Command and dummy cycles */
2772 + nfi_write32(snf, SNF_RD_CTL2,
2773 + ((uint32_t)snf->dummy_rfc << DATA_READ_DUMMY_S) |
2774 + (snf->opcode_rfc << DATA_READ_CMD_S));
2775 +
2776 + /* Column address */
2777 + nfi_write32(snf, SNF_RD_CTL3, coladdr);
2778 +
2779 + /* Set read mode */
2780 + mode = (uint32_t)snf->mode_rfc << DATA_READ_MODE_S;
2781 + nfi_rmw32(snf, SNF_MISC_CTL, DATA_READ_MODE,
2782 + mode | DATARD_CUSTOM_EN | (snf->nfi_soc->latch_lat << LATCH_LAT_S));
2783 +
2784 + /* Set bytes to read */
2785 + rwbytes = snf->ecc_steps * snf->raw_sector_size;
2786 + nfi_write32(snf, SNF_MISC_CTL2, (rwbytes << PROGRAM_LOAD_BYTE_NUM_S) |
2787 + rwbytes);
2788 +
2789 + /* NFI read prepare */
2790 + mode = raw ? 0 : CNFG_HW_ECC_EN | CNFG_AUTO_FMT_EN;
2791 + nfi_write16(snf, NFI_CNFG, (CNFG_OP_MODE_CUST << CNFG_OP_MODE_S) |
2792 + CNFG_DMA_BURST_EN | CNFG_READ_MODE | CNFG_DMA_MODE | mode);
2793 +
2794 + nfi_write32(snf, NFI_CON, (snf->ecc_steps << CON_SEC_NUM_S));
2795 +
2796 + /* Prepare for DMA read */
2797 + len = snf->writesize + snf->oobsize;
2798 + ret = dma_mem_map(snf->pdev, snf->page_cache, &dma_addr, len, false);
2799 + if (ret) {
2800 + snand_log_nfi(snf->pdev,
2801 + "DMA map from device failed with %d\n", ret);
2802 + return ret;
2803 + }
2804 +
2805 + nfi_write32(snf, NFI_STRADDR, (uint32_t)dma_addr);
2806 +
2807 + if (!raw)
2808 + mtk_snand_ecc_decoder_start(snf);
2809 +
2810 + /* Prepare for custom read interrupt */
2811 + nfi_write32(snf, NFI_INTR_EN, NFI_IRQ_INTR_EN | NFI_IRQ_CUS_READ);
2812 + irq_completion_init(snf->pdev);
2813 +
2814 + /* Trigger NFI into custom mode */
2815 + nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_READ);
2816 +
2817 + /* Start DMA read */
2818 + nfi_rmw32(snf, NFI_CON, 0, CON_BRD);
2819 + nfi_write16(snf, NFI_STRDATA, STR_DATA);
2820 +
2821 + /* Wait for operation finished */
2822 + ret = irq_completion_wait(snf->pdev, snf->nfi_base + SNF_STA_CTL1,
2823 + CUS_READ_DONE, SNFI_POLL_INTERVAL);
2824 + if (ret) {
2825 + snand_log_nfi(snf->pdev,
2826 + "DMA timed out for reading from cache\n");
2827 + goto cleanup;
2828 + }
2829 +
2830 + /* Wait for BUS_SEC_CNTR returning expected value */
2831 + ret = read32_poll_timeout(snf->nfi_base + NFI_BYTELEN, val,
2832 + BUS_SEC_CNTR(val) >= snf->ecc_steps,
2833 + 0, SNFI_POLL_INTERVAL);
2834 + if (ret) {
2835 + snand_log_nfi(snf->pdev,
2836 + "Timed out waiting for BUS_SEC_CNTR\n");
2837 + goto cleanup;
2838 + }
2839 +
2840 + /* Wait for bus becoming idle */
2841 + ret = read32_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
2842 + !(val & snf->nfi_soc->mastersta_mask),
2843 + 0, SNFI_POLL_INTERVAL);
2844 + if (ret) {
2845 + snand_log_nfi(snf->pdev,
2846 + "Timed out waiting for bus becoming idle\n");
2847 + goto cleanup;
2848 + }
2849 +
2850 + if (!raw) {
2851 + ret = mtk_ecc_wait_decoder_done(snf);
2852 + if (ret)
2853 + goto cleanup;
2854 +
2855 + mtk_snand_read_fdm(snf, snf->page_cache + snf->writesize);
2856 +
2857 + mtk_ecc_check_decode_error(snf);
2858 + mtk_snand_ecc_decoder_stop(snf);
2859 +
2860 + ret = mtk_snand_check_ecc_result(snf, page);
2861 + }
2862 +
2863 +cleanup:
2864 + /* DMA cleanup */
2865 + dma_mem_unmap(snf->pdev, dma_addr, len, false);
2866 +
2867 + /* Stop read */
2868 + nfi_write32(snf, NFI_CON, 0);
2869 + nfi_write16(snf, NFI_CNFG, 0);
2870 +
2871 + /* Clear SNF done flag */
2872 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_READ_DONE);
2873 + nfi_write32(snf, SNF_STA_CTL1, 0);
2874 +
2875 + /* Disable interrupt */
2876 + nfi_read32(snf, NFI_INTR_STA);
2877 + nfi_write32(snf, NFI_INTR_EN, 0);
2878 +
2879 + nfi_rmw32(snf, SNF_MISC_CTL, DATARD_CUSTOM_EN | LATCH_LAT, 0);
2880 +
2881 + return ret;
2882 +}
2883 +
2884 +static void mtk_snand_from_raw_page(struct mtk_snand *snf, void *buf, void *oob)
2885 +{
2886 + uint32_t i, ecc_bytes = snf->spare_per_sector - snf->nfi_soc->fdm_size;
2887 + uint8_t *eccptr = oob + snf->ecc_steps * snf->nfi_soc->fdm_size;
2888 + uint8_t *bufptr = buf, *oobptr = oob, *raw_sector;
2889 +
2890 + for (i = 0; i < snf->ecc_steps; i++) {
2891 + raw_sector = snf->page_cache + i * snf->raw_sector_size;
2892 +
2893 + if (buf) {
2894 + memcpy(bufptr, raw_sector, snf->nfi_soc->sector_size);
2895 + bufptr += snf->nfi_soc->sector_size;
2896 + }
2897 +
2898 + raw_sector += snf->nfi_soc->sector_size;
2899 +
2900 + if (oob) {
2901 + memcpy(oobptr, raw_sector, snf->nfi_soc->fdm_size);
2902 + oobptr += snf->nfi_soc->fdm_size;
2903 + raw_sector += snf->nfi_soc->fdm_size;
2904 +
2905 + memcpy(eccptr, raw_sector, ecc_bytes);
2906 + eccptr += ecc_bytes;
2907 + }
2908 + }
2909 +}
2910 +
2911 +static int mtk_snand_do_read_page(struct mtk_snand *snf, uint64_t addr,
2912 + void *buf, void *oob, bool raw, bool format)
2913 +{
2914 + uint64_t die_addr;
2915 + uint32_t page, dly_ctrl3;
2916 + int ret, retry_cnt = 0;
2917 +
2918 + die_addr = mtk_snand_select_die_address(snf, addr);
2919 + page = die_addr >> snf->writesize_shift;
2920 +
2921 + dly_ctrl3 = nfi_read32(snf, SNF_DLY_CTL3);
2922 +
2923 + ret = mtk_snand_page_op(snf, page, SNAND_CMD_READ_TO_CACHE);
2924 + if (ret)
2925 + return ret;
2926 +
2927 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
2928 + if (ret < 0) {
2929 + snand_log_chip(snf->pdev, "Read to cache command timed out\n");
2930 + return ret;
2931 + }
2932 +
2933 +retry:
2934 + ret = mtk_snand_read_cache(snf, page, raw);
2935 + if (ret < 0 && ret != -EBADMSG)
2936 + return ret;
2937 +
2938 + if (ret == -EBADMSG && retry_cnt < 16) {
2939 + nfi_write32(snf, SNF_DLY_CTL3, retry_cnt * 2);
2940 + retry_cnt++;
2941 + goto retry;
2942 + }
2943 +
2944 + if (retry_cnt) {
2945 + if(ret == -EBADMSG) {
2946 + nfi_write32(snf, SNF_DLY_CTL3, dly_ctrl3);
2947 + snand_log_chip(snf->pdev,
2948 + "NFI calibration failed. Original sample delay: 0x%x\n",
2949 + dly_ctrl3);
2950 + } else {
2951 + snand_log_chip(snf->pdev,
2952 + "NFI calibration passed. New sample delay: 0x%x\n",
2953 + nfi_read32(snf, SNF_DLY_CTL3));
2954 + }
2955 + }
2956 +
2957 + if (raw) {
2958 + if (format) {
2959 + mtk_snand_bm_swap_raw(snf);
2960 + mtk_snand_fdm_bm_swap_raw(snf);
2961 + mtk_snand_from_raw_page(snf, buf, oob);
2962 + } else {
2963 + if (buf)
2964 + memcpy(buf, snf->page_cache, snf->writesize);
2965 +
2966 + if (oob) {
2967 + memset(oob, 0xff, snf->oobsize);
2968 + memcpy(oob, snf->page_cache + snf->writesize,
2969 + snf->ecc_steps * snf->spare_per_sector);
2970 + }
2971 + }
2972 + } else {
2973 + mtk_snand_bm_swap(snf);
2974 + mtk_snand_fdm_bm_swap(snf);
2975 +
2976 + if (buf)
2977 + memcpy(buf, snf->page_cache, snf->writesize);
2978 +
2979 + if (oob) {
2980 + memset(oob, 0xff, snf->oobsize);
2981 + memcpy(oob, snf->page_cache + snf->writesize,
2982 + snf->ecc_steps * snf->nfi_soc->fdm_size);
2983 + }
2984 + }
2985 +
2986 + return ret;
2987 +}
2988 +
2989 +int mtk_snand_read_page(struct mtk_snand *snf, uint64_t addr, void *buf,
2990 + void *oob, bool raw)
2991 +{
2992 + if (!snf || (!buf && !oob))
2993 + return -EINVAL;
2994 +
2995 + if (addr >= snf->size)
2996 + return -EINVAL;
2997 +
2998 + return mtk_snand_do_read_page(snf, addr, buf, oob, raw, true);
2999 +}
3000 +
3001 +static void mtk_snand_write_fdm(struct mtk_snand *snf, const uint8_t *buf)
3002 +{
3003 + uint32_t vall, valm, fdm_size = snf->nfi_soc->fdm_size;
3004 + const uint8_t *oobptr = buf;
3005 + int i, j;
3006 +
3007 + for (i = 0; i < snf->ecc_steps; i++) {
3008 + vall = 0;
3009 + valm = 0;
3010 +
3011 + for (j = 0; j < 8; j++) {
3012 + if (j < 4)
3013 + vall |= (j < fdm_size ? oobptr[j] : 0xff)
3014 + << (j * 8);
3015 + else
3016 + valm |= (j < fdm_size ? oobptr[j] : 0xff)
3017 + << ((j - 4) * 8);
3018 + }
3019 +
3020 + nfi_write32(snf, NFI_FDML(i), vall);
3021 + nfi_write32(snf, NFI_FDMM(i), valm);
3022 +
3023 + oobptr += fdm_size;
3024 + }
3025 +}
3026 +
3027 +static int mtk_snand_program_load(struct mtk_snand *snf, uint32_t page,
3028 + bool raw)
3029 +{
3030 + uint32_t coladdr, rwbytes, mode, len, val;
3031 + uintptr_t dma_addr;
3032 + int ret;
3033 +
3034 + /* Column address with plane bit */
3035 + coladdr = mtk_snand_get_plane_address(snf, page);
3036 +
3037 + mtk_snand_mac_reset(snf);
3038 + mtk_nfi_reset(snf);
3039 +
3040 + /* Write FDM registers if necessary */
3041 + if (!raw)
3042 + mtk_snand_write_fdm(snf, snf->page_cache + snf->writesize);
3043 +
3044 + /* Command */
3045 + nfi_write32(snf, SNF_PG_CTL1, (snf->opcode_pl << PG_LOAD_CMD_S));
3046 +
3047 + /* Column address */
3048 + nfi_write32(snf, SNF_PG_CTL2, coladdr);
3049 +
3050 + /* Set write mode */
3051 + mode = snf->mode_pl ? PG_LOAD_X4_EN : 0;
3052 + nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_X4_EN, mode | PG_LOAD_CUSTOM_EN);
3053 +
3054 + /* Set bytes to write */
3055 + rwbytes = snf->ecc_steps * snf->raw_sector_size;
3056 + nfi_write32(snf, SNF_MISC_CTL2, (rwbytes << PROGRAM_LOAD_BYTE_NUM_S) |
3057 + rwbytes);
3058 +
3059 + /* NFI write prepare */
3060 + mode = raw ? 0 : CNFG_HW_ECC_EN | CNFG_AUTO_FMT_EN;
3061 + nfi_write16(snf, NFI_CNFG, (CNFG_OP_MODE_PROGRAM << CNFG_OP_MODE_S) |
3062 + CNFG_DMA_BURST_EN | CNFG_DMA_MODE | mode);
3063 +
3064 + nfi_write32(snf, NFI_CON, (snf->ecc_steps << CON_SEC_NUM_S));
3065 +
3066 + /* Prepare for DMA write */
3067 + len = snf->writesize + snf->oobsize;
3068 + ret = dma_mem_map(snf->pdev, snf->page_cache, &dma_addr, len, true);
3069 + if (ret) {
3070 + snand_log_nfi(snf->pdev,
3071 + "DMA map to device failed with %d\n", ret);
3072 + return ret;
3073 + }
3074 +
3075 + nfi_write32(snf, NFI_STRADDR, (uint32_t)dma_addr);
3076 +
3077 + if (!raw)
3078 + mtk_snand_ecc_encoder_start(snf);
3079 +
3080 + /* Prepare for custom write interrupt */
3081 + nfi_write32(snf, NFI_INTR_EN, NFI_IRQ_INTR_EN | NFI_IRQ_CUS_PG);
3082 + irq_completion_init(snf->pdev);
3083 +
3084 + /* Trigger NFI into custom mode */
3085 + nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_WRITE);
3086 +
3087 + /* Start DMA write */
3088 + nfi_rmw32(snf, NFI_CON, 0, CON_BWR);
3089 + nfi_write16(snf, NFI_STRDATA, STR_DATA);
3090 +
3091 + /* Wait for operation finished */
3092 + ret = irq_completion_wait(snf->pdev, snf->nfi_base + SNF_STA_CTL1,
3093 + CUS_PG_DONE, SNFI_POLL_INTERVAL);
3094 + if (ret) {
3095 + snand_log_nfi(snf->pdev,
3096 + "DMA timed out for program load\n");
3097 + goto cleanup;
3098 + }
3099 +
3100 + /* Wait for NFI_SEC_CNTR returning expected value */
3101 + ret = read32_poll_timeout(snf->nfi_base + NFI_ADDRCNTR, val,
3102 + NFI_SEC_CNTR(val) >= snf->ecc_steps,
3103 + 0, SNFI_POLL_INTERVAL);
3104 + if (ret) {
3105 + snand_log_nfi(snf->pdev,
3106 + "Timed out waiting for BUS_SEC_CNTR\n");
3107 + goto cleanup;
3108 + }
3109 +
3110 + if (!raw)
3111 + mtk_snand_ecc_encoder_stop(snf);
3112 +
3113 +cleanup:
3114 + /* DMA cleanup */
3115 + dma_mem_unmap(snf->pdev, dma_addr, len, true);
3116 +
3117 + /* Stop write */
3118 + nfi_write32(snf, NFI_CON, 0);
3119 + nfi_write16(snf, NFI_CNFG, 0);
3120 +
3121 + /* Clear SNF done flag */
3122 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_PG_DONE);
3123 + nfi_write32(snf, SNF_STA_CTL1, 0);
3124 +
3125 + /* Disable interrupt */
3126 + nfi_read32(snf, NFI_INTR_STA);
3127 + nfi_write32(snf, NFI_INTR_EN, 0);
3128 +
3129 + nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_CUSTOM_EN, 0);
3130 +
3131 + return ret;
3132 +}
3133 +
3134 +static void mtk_snand_to_raw_page(struct mtk_snand *snf,
3135 + const void *buf, const void *oob,
3136 + bool empty_ecc)
3137 +{
3138 + uint32_t i, ecc_bytes = snf->spare_per_sector - snf->nfi_soc->fdm_size;
3139 + const uint8_t *eccptr = oob + snf->ecc_steps * snf->nfi_soc->fdm_size;
3140 + const uint8_t *bufptr = buf, *oobptr = oob;
3141 + uint8_t *raw_sector;
3142 +
3143 + memset(snf->page_cache, 0xff, snf->writesize + snf->oobsize);
3144 + for (i = 0; i < snf->ecc_steps; i++) {
3145 + raw_sector = snf->page_cache + i * snf->raw_sector_size;
3146 +
3147 + if (buf) {
3148 + memcpy(raw_sector, bufptr, snf->nfi_soc->sector_size);
3149 + bufptr += snf->nfi_soc->sector_size;
3150 + }
3151 +
3152 + raw_sector += snf->nfi_soc->sector_size;
3153 +
3154 + if (oob) {
3155 + memcpy(raw_sector, oobptr, snf->nfi_soc->fdm_size);
3156 + oobptr += snf->nfi_soc->fdm_size;
3157 + raw_sector += snf->nfi_soc->fdm_size;
3158 +
3159 + if (empty_ecc)
3160 + memset(raw_sector, 0xff, ecc_bytes);
3161 + else
3162 + memcpy(raw_sector, eccptr, ecc_bytes);
3163 + eccptr += ecc_bytes;
3164 + }
3165 + }
3166 +}
3167 +
3168 +static bool mtk_snand_is_empty_page(struct mtk_snand *snf, const void *buf,
3169 + const void *oob)
3170 +{
3171 + const uint8_t *p = buf;
3172 + uint32_t i, j;
3173 +
3174 + if (buf) {
3175 + for (i = 0; i < snf->writesize; i++) {
3176 + if (p[i] != 0xff)
3177 + return false;
3178 + }
3179 + }
3180 +
3181 + if (oob) {
3182 + for (j = 0; j < snf->ecc_steps; j++) {
3183 + p = oob + j * snf->nfi_soc->fdm_size;
3184 +
3185 + for (i = 0; i < snf->nfi_soc->fdm_ecc_size; i++) {
3186 + if (p[i] != 0xff)
3187 + return false;
3188 + }
3189 + }
3190 + }
3191 +
3192 + return true;
3193 +}
3194 +
3195 +static int mtk_snand_do_write_page(struct mtk_snand *snf, uint64_t addr,
3196 + const void *buf, const void *oob,
3197 + bool raw, bool format)
3198 +{
3199 + uint64_t die_addr;
3200 + bool empty_ecc = false;
3201 + uint32_t page;
3202 + int ret;
3203 +
3204 + die_addr = mtk_snand_select_die_address(snf, addr);
3205 + page = die_addr >> snf->writesize_shift;
3206 +
3207 + if (!raw && mtk_snand_is_empty_page(snf, buf, oob)) {
3208 + /*
3209 + * If the data in the page to be ecc-ed is full 0xff,
3210 + * change to raw write mode
3211 + */
3212 + raw = true;
3213 + format = true;
3214 +
3215 + /* fill ecc parity code region with 0xff */
3216 + empty_ecc = true;
3217 + }
3218 +
3219 + if (raw) {
3220 + if (format) {
3221 + mtk_snand_to_raw_page(snf, buf, oob, empty_ecc);
3222 + mtk_snand_fdm_bm_swap_raw(snf);
3223 + mtk_snand_bm_swap_raw(snf);
3224 + } else {
3225 + memset(snf->page_cache, 0xff,
3226 + snf->writesize + snf->oobsize);
3227 +
3228 + if (buf)
3229 + memcpy(snf->page_cache, buf, snf->writesize);
3230 +
3231 + if (oob) {
3232 + memcpy(snf->page_cache + snf->writesize, oob,
3233 + snf->ecc_steps * snf->spare_per_sector);
3234 + }
3235 + }
3236 + } else {
3237 + memset(snf->page_cache, 0xff, snf->writesize + snf->oobsize);
3238 + if (buf)
3239 + memcpy(snf->page_cache, buf, snf->writesize);
3240 +
3241 + if (oob) {
3242 + memcpy(snf->page_cache + snf->writesize, oob,
3243 + snf->ecc_steps * snf->nfi_soc->fdm_size);
3244 + }
3245 +
3246 + mtk_snand_fdm_bm_swap(snf);
3247 + mtk_snand_bm_swap(snf);
3248 + }
3249 +
3250 + ret = mtk_snand_write_enable(snf);
3251 + if (ret)
3252 + return ret;
3253 +
3254 + ret = mtk_snand_program_load(snf, page, raw);
3255 + if (ret)
3256 + return ret;
3257 +
3258 + ret = mtk_snand_page_op(snf, page, SNAND_CMD_PROGRAM_EXECUTE);
3259 + if (ret)
3260 + return ret;
3261 +
3262 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
3263 + if (ret < 0) {
3264 + snand_log_chip(snf->pdev,
3265 + "Page program command timed out on page %u\n",
3266 + page);
3267 + return ret;
3268 + }
3269 +
3270 + if (ret & SNAND_STATUS_PROGRAM_FAIL) {
3271 + snand_log_chip(snf->pdev,
3272 + "Page program failed on page %u\n", page);
3273 + return -EIO;
3274 + }
3275 +
3276 + return 0;
3277 +}
3278 +
3279 +int mtk_snand_write_page(struct mtk_snand *snf, uint64_t addr, const void *buf,
3280 + const void *oob, bool raw)
3281 +{
3282 + if (!snf || (!buf && !oob))
3283 + return -EINVAL;
3284 +
3285 + if (addr >= snf->size)
3286 + return -EINVAL;
3287 +
3288 + return mtk_snand_do_write_page(snf, addr, buf, oob, raw, true);
3289 +}
3290 +
3291 +int mtk_snand_erase_block(struct mtk_snand *snf, uint64_t addr)
3292 +{
3293 + uint64_t die_addr;
3294 + uint32_t page, block;
3295 + int ret;
3296 +
3297 + if (!snf)
3298 + return -EINVAL;
3299 +
3300 + if (addr >= snf->size)
3301 + return -EINVAL;
3302 +
3303 + die_addr = mtk_snand_select_die_address(snf, addr);
3304 + block = die_addr >> snf->erasesize_shift;
3305 + page = block << (snf->erasesize_shift - snf->writesize_shift);
3306 +
3307 + ret = mtk_snand_write_enable(snf);
3308 + if (ret)
3309 + return ret;
3310 +
3311 + ret = mtk_snand_page_op(snf, page, SNAND_CMD_BLOCK_ERASE);
3312 + if (ret)
3313 + return ret;
3314 +
3315 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
3316 + if (ret < 0) {
3317 + snand_log_chip(snf->pdev,
3318 + "Block erase command timed out on block %u\n",
3319 + block);
3320 + return ret;
3321 + }
3322 +
3323 + if (ret & SNAND_STATUS_ERASE_FAIL) {
3324 + snand_log_chip(snf->pdev,
3325 + "Block erase failed on block %u\n", block);
3326 + return -EIO;
3327 + }
3328 +
3329 + return 0;
3330 +}
3331 +
3332 +static int mtk_snand_block_isbad_std(struct mtk_snand *snf, uint64_t addr)
3333 +{
3334 + int ret;
3335 +
3336 + ret = mtk_snand_do_read_page(snf, addr, NULL, snf->buf_cache, true,
3337 + false);
3338 + if (ret && ret != -EBADMSG)
3339 + return ret;
3340 +
3341 + return snf->buf_cache[0] != 0xff;
3342 +}
3343 +
3344 +static int mtk_snand_block_isbad_mtk(struct mtk_snand *snf, uint64_t addr)
3345 +{
3346 + int ret;
3347 +
3348 + ret = mtk_snand_do_read_page(snf, addr, NULL, snf->buf_cache, true,
3349 + true);
3350 + if (ret && ret != -EBADMSG)
3351 + return ret;
3352 +
3353 + return snf->buf_cache[0] != 0xff;
3354 +}
3355 +
3356 +int mtk_snand_block_isbad(struct mtk_snand *snf, uint64_t addr)
3357 +{
3358 + if (!snf)
3359 + return -EINVAL;
3360 +
3361 + if (addr >= snf->size)
3362 + return -EINVAL;
3363 +
3364 + addr &= ~snf->erasesize_mask;
3365 +
3366 + if (snf->nfi_soc->bbm_swap)
3367 + return mtk_snand_block_isbad_std(snf, addr);
3368 +
3369 + return mtk_snand_block_isbad_mtk(snf, addr);
3370 +}
3371 +
3372 +static int mtk_snand_block_markbad_std(struct mtk_snand *snf, uint64_t addr)
3373 +{
3374 + /* Standard BBM position */
3375 + memset(snf->buf_cache, 0xff, snf->oobsize);
3376 + snf->buf_cache[0] = 0;
3377 +
3378 + return mtk_snand_do_write_page(snf, addr, NULL, snf->buf_cache, true,
3379 + false);
3380 +}
3381 +
3382 +static int mtk_snand_block_markbad_mtk(struct mtk_snand *snf, uint64_t addr)
3383 +{
3384 + /* Write the whole page with zeros */
3385 + memset(snf->buf_cache, 0, snf->writesize + snf->oobsize);
3386 +
3387 + return mtk_snand_do_write_page(snf, addr, snf->buf_cache,
3388 + snf->buf_cache + snf->writesize, true,
3389 + true);
3390 +}
3391 +
3392 +int mtk_snand_block_markbad(struct mtk_snand *snf, uint64_t addr)
3393 +{
3394 + if (!snf)
3395 + return -EINVAL;
3396 +
3397 + if (addr >= snf->size)
3398 + return -EINVAL;
3399 +
3400 + addr &= ~snf->erasesize_mask;
3401 +
3402 + if (snf->nfi_soc->bbm_swap)
3403 + return mtk_snand_block_markbad_std(snf, addr);
3404 +
3405 + return mtk_snand_block_markbad_mtk(snf, addr);
3406 +}
3407 +
3408 +int mtk_snand_fill_oob(struct mtk_snand *snf, uint8_t *oobraw,
3409 + const uint8_t *oobbuf, size_t ooblen)
3410 +{
3411 + size_t len = ooblen, sect_fdm_len;
3412 + const uint8_t *oob = oobbuf;
3413 + uint32_t step = 0;
3414 +
3415 + if (!snf || !oobraw || !oob)
3416 + return -EINVAL;
3417 +
3418 + while (len && step < snf->ecc_steps) {
3419 + sect_fdm_len = snf->nfi_soc->fdm_size - 1;
3420 + if (sect_fdm_len > len)
3421 + sect_fdm_len = len;
3422 +
3423 + memcpy(oobraw + step * snf->nfi_soc->fdm_size + 1, oob,
3424 + sect_fdm_len);
3425 +
3426 + len -= sect_fdm_len;
3427 + oob += sect_fdm_len;
3428 + step++;
3429 + }
3430 +
3431 + return len;
3432 +}
3433 +
3434 +int mtk_snand_transfer_oob(struct mtk_snand *snf, uint8_t *oobbuf,
3435 + size_t ooblen, const uint8_t *oobraw)
3436 +{
3437 + size_t len = ooblen, sect_fdm_len;
3438 + uint8_t *oob = oobbuf;
3439 + uint32_t step = 0;
3440 +
3441 + if (!snf || !oobraw || !oob)
3442 + return -EINVAL;
3443 +
3444 + while (len && step < snf->ecc_steps) {
3445 + sect_fdm_len = snf->nfi_soc->fdm_size - 1;
3446 + if (sect_fdm_len > len)
3447 + sect_fdm_len = len;
3448 +
3449 + memcpy(oob, oobraw + step * snf->nfi_soc->fdm_size + 1,
3450 + sect_fdm_len);
3451 +
3452 + len -= sect_fdm_len;
3453 + oob += sect_fdm_len;
3454 + step++;
3455 + }
3456 +
3457 + return len;
3458 +}
3459 +
3460 +int mtk_snand_read_page_auto_oob(struct mtk_snand *snf, uint64_t addr,
3461 + void *buf, void *oob, size_t ooblen,
3462 + size_t *actualooblen, bool raw)
3463 +{
3464 + int ret, oobremain;
3465 +
3466 + if (!snf)
3467 + return -EINVAL;
3468 +
3469 + if (!oob)
3470 + return mtk_snand_read_page(snf, addr, buf, NULL, raw);
3471 +
3472 + ret = mtk_snand_read_page(snf, addr, buf, snf->buf_cache, raw);
3473 + if (ret && ret != -EBADMSG) {
3474 + if (actualooblen)
3475 + *actualooblen = 0;
3476 + return ret;
3477 + }
3478 +
3479 + oobremain = mtk_snand_transfer_oob(snf, oob, ooblen, snf->buf_cache);
3480 + if (actualooblen)
3481 + *actualooblen = ooblen - oobremain;
3482 +
3483 + return ret;
3484 +}
3485 +
3486 +int mtk_snand_write_page_auto_oob(struct mtk_snand *snf, uint64_t addr,
3487 + const void *buf, const void *oob,
3488 + size_t ooblen, size_t *actualooblen, bool raw)
3489 +{
3490 + int oobremain;
3491 +
3492 + if (!snf)
3493 + return -EINVAL;
3494 +
3495 + if (!oob)
3496 + return mtk_snand_write_page(snf, addr, buf, NULL, raw);
3497 +
3498 + memset(snf->buf_cache, 0xff, snf->oobsize);
3499 + oobremain = mtk_snand_fill_oob(snf, snf->buf_cache, oob, ooblen);
3500 + if (actualooblen)
3501 + *actualooblen = ooblen - oobremain;
3502 +
3503 + return mtk_snand_write_page(snf, addr, buf, snf->buf_cache, raw);
3504 +}
3505 +
3506 +int mtk_snand_get_chip_info(struct mtk_snand *snf,
3507 + struct mtk_snand_chip_info *info)
3508 +{
3509 + if (!snf || !info)
3510 + return -EINVAL;
3511 +
3512 + info->model = snf->model;
3513 + info->chipsize = snf->size;
3514 + info->blocksize = snf->erasesize;
3515 + info->pagesize = snf->writesize;
3516 + info->sparesize = snf->oobsize;
3517 + info->spare_per_sector = snf->spare_per_sector;
3518 + info->fdm_size = snf->nfi_soc->fdm_size;
3519 + info->fdm_ecc_size = snf->nfi_soc->fdm_ecc_size;
3520 + info->num_sectors = snf->ecc_steps;
3521 + info->sector_size = snf->nfi_soc->sector_size;
3522 + info->ecc_strength = snf->ecc_strength;
3523 + info->ecc_bytes = snf->ecc_bytes;
3524 +
3525 + return 0;
3526 +}
3527 +
3528 +int mtk_snand_irq_process(struct mtk_snand *snf)
3529 +{
3530 + uint32_t sta, ien;
3531 +
3532 + if (!snf)
3533 + return -EINVAL;
3534 +
3535 + sta = nfi_read32(snf, NFI_INTR_STA);
3536 + ien = nfi_read32(snf, NFI_INTR_EN);
3537 +
3538 + if (!(sta & ien))
3539 + return 0;
3540 +
3541 + nfi_write32(snf, NFI_INTR_EN, 0);
3542 + irq_completion_done(snf->pdev);
3543 +
3544 + return 1;
3545 +}
3546 +
3547 +static int mtk_snand_select_spare_per_sector(struct mtk_snand *snf)
3548 +{
3549 + uint32_t spare_per_step = snf->oobsize / snf->ecc_steps;
3550 + int i, mul = 1;
3551 +
3552 + /*
3553 + * If we're using the 1KB sector size, HW will automatically
3554 + * double the spare size. So we should only use half of the value.
3555 + */
3556 + if (snf->nfi_soc->sector_size == 1024)
3557 + mul = 2;
3558 +
3559 + spare_per_step /= mul;
3560 +
3561 + for (i = snf->nfi_soc->num_spare_size - 1; i >= 0; i--) {
3562 + if (snf->nfi_soc->spare_sizes[i] <= spare_per_step) {
3563 + snf->spare_per_sector = snf->nfi_soc->spare_sizes[i];
3564 + snf->spare_per_sector *= mul;
3565 + return i;
3566 + }
3567 + }
3568 +
3569 + snand_log_nfi(snf->pdev,
3570 + "Page size %u+%u is not supported\n", snf->writesize,
3571 + snf->oobsize);
3572 +
3573 + return -1;
3574 +}
3575 +
3576 +static int mtk_snand_pagefmt_setup(struct mtk_snand *snf)
3577 +{
3578 + uint32_t spare_size_idx, spare_size_shift, pagesize_idx;
3579 + uint32_t sector_size_512;
3580 +
3581 + if (snf->nfi_soc->sector_size == 512) {
3582 + sector_size_512 = NFI_SEC_SEL_512;
3583 + spare_size_shift = NFI_SPARE_SIZE_S;
3584 + } else {
3585 + sector_size_512 = 0;
3586 + spare_size_shift = NFI_SPARE_SIZE_LS_S;
3587 + }
3588 +
3589 + switch (snf->writesize) {
3590 + case SZ_512:
3591 + pagesize_idx = NFI_PAGE_SIZE_512_2K;
3592 + break;
3593 + case SZ_2K:
3594 + if (snf->nfi_soc->sector_size == 512)
3595 + pagesize_idx = NFI_PAGE_SIZE_2K_4K;
3596 + else
3597 + pagesize_idx = NFI_PAGE_SIZE_512_2K;
3598 + break;
3599 + case SZ_4K:
3600 + if (snf->nfi_soc->sector_size == 512)
3601 + pagesize_idx = NFI_PAGE_SIZE_4K_8K;
3602 + else
3603 + pagesize_idx = NFI_PAGE_SIZE_2K_4K;
3604 + break;
3605 + case SZ_8K:
3606 + if (snf->nfi_soc->sector_size == 512)
3607 + pagesize_idx = NFI_PAGE_SIZE_8K_16K;
3608 + else
3609 + pagesize_idx = NFI_PAGE_SIZE_4K_8K;
3610 + break;
3611 + case SZ_16K:
3612 + pagesize_idx = NFI_PAGE_SIZE_8K_16K;
3613 + break;
3614 + default:
3615 + snand_log_nfi(snf->pdev, "Page size %u is not supported\n",
3616 + snf->writesize);
3617 + return -ENOTSUPP;
3618 + }
3619 +
3620 + spare_size_idx = mtk_snand_select_spare_per_sector(snf);
3621 + if (unlikely(spare_size_idx < 0))
3622 + return -ENOTSUPP;
3623 +
3624 + snf->raw_sector_size = snf->nfi_soc->sector_size +
3625 + snf->spare_per_sector;
3626 +
3627 + /* Setup page format */
3628 + nfi_write32(snf, NFI_PAGEFMT,
3629 + (snf->nfi_soc->fdm_ecc_size << NFI_FDM_ECC_NUM_S) |
3630 + (snf->nfi_soc->fdm_size << NFI_FDM_NUM_S) |
3631 + (spare_size_idx << spare_size_shift) |
3632 + (pagesize_idx << NFI_PAGE_SIZE_S) |
3633 + sector_size_512);
3634 +
3635 + return 0;
3636 +}
3637 +
3638 +static enum snand_flash_io mtk_snand_select_opcode(struct mtk_snand *snf,
3639 + uint32_t snfi_caps, uint8_t *opcode,
3640 + uint8_t *dummy,
3641 + const struct snand_io_cap *op_cap)
3642 +{
3643 + uint32_t i, caps;
3644 +
3645 + caps = snfi_caps & op_cap->caps;
3646 +
3647 + i = fls(caps);
3648 + if (i > 0) {
3649 + *opcode = op_cap->opcodes[i - 1].opcode;
3650 + if (dummy)
3651 + *dummy = op_cap->opcodes[i - 1].dummy;
3652 + return i - 1;
3653 + }
3654 +
3655 + return __SNAND_IO_MAX;
3656 +}
3657 +
3658 +static int mtk_snand_select_opcode_rfc(struct mtk_snand *snf,
3659 + uint32_t snfi_caps,
3660 + const struct snand_io_cap *op_cap)
3661 +{
3662 + enum snand_flash_io idx;
3663 +
3664 + static const uint8_t rfc_modes[__SNAND_IO_MAX] = {
3665 + [SNAND_IO_1_1_1] = DATA_READ_MODE_X1,
3666 + [SNAND_IO_1_1_2] = DATA_READ_MODE_X2,
3667 + [SNAND_IO_1_2_2] = DATA_READ_MODE_DUAL,
3668 + [SNAND_IO_1_1_4] = DATA_READ_MODE_X4,
3669 + [SNAND_IO_1_4_4] = DATA_READ_MODE_QUAD,
3670 + };
3671 +
3672 + idx = mtk_snand_select_opcode(snf, snfi_caps, &snf->opcode_rfc,
3673 + &snf->dummy_rfc, op_cap);
3674 + if (idx >= __SNAND_IO_MAX) {
3675 + snand_log_snfi(snf->pdev,
3676 + "No capable opcode for read from cache\n");
3677 + return -ENOTSUPP;
3678 + }
3679 +
3680 + snf->mode_rfc = rfc_modes[idx];
3681 +
3682 + if (idx == SNAND_IO_1_1_4 || idx == SNAND_IO_1_4_4)
3683 + snf->quad_spi_op = true;
3684 +
3685 + return 0;
3686 +}
3687 +
3688 +static int mtk_snand_select_opcode_pl(struct mtk_snand *snf, uint32_t snfi_caps,
3689 + const struct snand_io_cap *op_cap)
3690 +{
3691 + enum snand_flash_io idx;
3692 +
3693 + static const uint8_t pl_modes[__SNAND_IO_MAX] = {
3694 + [SNAND_IO_1_1_1] = 0,
3695 + [SNAND_IO_1_1_4] = 1,
3696 + };
3697 +
3698 + idx = mtk_snand_select_opcode(snf, snfi_caps, &snf->opcode_pl,
3699 + NULL, op_cap);
3700 + if (idx >= __SNAND_IO_MAX) {
3701 + snand_log_snfi(snf->pdev,
3702 + "No capable opcode for program load\n");
3703 + return -ENOTSUPP;
3704 + }
3705 +
3706 + snf->mode_pl = pl_modes[idx];
3707 +
3708 + if (idx == SNAND_IO_1_1_4)
3709 + snf->quad_spi_op = true;
3710 +
3711 + return 0;
3712 +}
3713 +
3714 +static int mtk_snand_setup(struct mtk_snand *snf,
3715 + const struct snand_flash_info *snand_info)
3716 +{
3717 + const struct snand_mem_org *memorg = &snand_info->memorg;
3718 + uint32_t i, msg_size, snfi_caps;
3719 + int ret;
3720 +
3721 + /* Calculate flash memory organization */
3722 + snf->model = snand_info->model;
3723 + snf->writesize = memorg->pagesize;
3724 + snf->oobsize = memorg->sparesize;
3725 + snf->erasesize = snf->writesize * memorg->pages_per_block;
3726 + snf->die_size = (uint64_t)snf->erasesize * memorg->blocks_per_die;
3727 + snf->size = snf->die_size * memorg->ndies;
3728 + snf->num_dies = memorg->ndies;
3729 +
3730 + snf->writesize_mask = snf->writesize - 1;
3731 + snf->erasesize_mask = snf->erasesize - 1;
3732 + snf->die_mask = snf->die_size - 1;
3733 +
3734 + snf->writesize_shift = ffs(snf->writesize) - 1;
3735 + snf->erasesize_shift = ffs(snf->erasesize) - 1;
3736 + snf->die_shift = mtk_snand_ffs64(snf->die_size) - 1;
3737 +
3738 + snf->select_die = snand_info->select_die;
3739 +
3740 + /* Determine opcodes for read from cache/program load */
3741 + snfi_caps = SPI_IO_1_1_1 | SPI_IO_1_1_2 | SPI_IO_1_2_2;
3742 + if (snf->snfi_quad_spi)
3743 + snfi_caps |= SPI_IO_1_1_4 | SPI_IO_1_4_4;
3744 +
3745 + ret = mtk_snand_select_opcode_rfc(snf, snfi_caps, snand_info->cap_rd);
3746 + if (ret)
3747 + return ret;
3748 +
3749 + ret = mtk_snand_select_opcode_pl(snf, snfi_caps, snand_info->cap_pl);
3750 + if (ret)
3751 + return ret;
3752 +
3753 + /* ECC and page format */
3754 + snf->ecc_steps = snf->writesize / snf->nfi_soc->sector_size;
3755 + if (snf->ecc_steps > snf->nfi_soc->max_sectors) {
3756 + snand_log_nfi(snf->pdev, "Page size %u is not supported\n",
3757 + snf->writesize);
3758 + return -ENOTSUPP;
3759 + }
3760 +
3761 + ret = mtk_snand_pagefmt_setup(snf);
3762 + if (ret)
3763 + return ret;
3764 +
3765 + msg_size = snf->nfi_soc->sector_size + snf->nfi_soc->fdm_ecc_size;
3766 + ret = mtk_ecc_setup(snf, snf->nfi_base + NFI_FDM0L,
3767 + snf->spare_per_sector - snf->nfi_soc->fdm_size,
3768 + msg_size);
3769 + if (ret)
3770 + return ret;
3771 +
3772 + nfi_write16(snf, NFI_CNFG, 0);
3773 +
3774 + /* Tuning options */
3775 + nfi_write16(snf, NFI_DEBUG_CON1, WBUF_EN);
3776 + nfi_write32(snf, SNF_DLY_CTL3, (snf->nfi_soc->sample_delay << SFCK_SAM_DLY_S));
3777 +
3778 + /* Interrupts */
3779 + nfi_read32(snf, NFI_INTR_STA);
3780 + nfi_write32(snf, NFI_INTR_EN, 0);
3781 +
3782 + /* Clear SNF done flag */
3783 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_READ_DONE | CUS_PG_DONE);
3784 + nfi_write32(snf, SNF_STA_CTL1, 0);
3785 +
3786 + /* Initialization on all dies */
3787 + for (i = 0; i < snf->num_dies; i++) {
3788 + mtk_snand_select_die(snf, i);
3789 +
3790 + /* Disable On-Die ECC engine */
3791 + ret = mtk_snand_ondie_ecc_control(snf, false);
3792 + if (ret)
3793 + return ret;
3794 +
3795 + /* Disable block protection */
3796 + mtk_snand_unlock(snf);
3797 +
3798 + /* Enable/disable quad-spi */
3799 + mtk_snand_qspi_control(snf, snf->quad_spi_op);
3800 + }
3801 +
3802 + mtk_snand_select_die(snf, 0);
3803 +
3804 + return 0;
3805 +}
3806 +
3807 +static int mtk_snand_id_probe(struct mtk_snand *snf,
3808 + const struct snand_flash_info **snand_info)
3809 +{
3810 + uint8_t id[4], op[2];
3811 + int ret;
3812 +
3813 + /* Read SPI-NAND JEDEC ID, OP + dummy/addr + ID */
3814 + op[0] = SNAND_CMD_READID;
3815 + op[1] = 0;
3816 + ret = mtk_snand_mac_io(snf, op, 2, id, sizeof(id));
3817 + if (ret)
3818 + return ret;
3819 +
3820 + *snand_info = snand_flash_id_lookup(SNAND_ID_DYMMY, id);
3821 + if (*snand_info)
3822 + return 0;
3823 +
3824 + /* Read SPI-NAND JEDEC ID, OP + ID */
3825 + op[0] = SNAND_CMD_READID;
3826 + ret = mtk_snand_mac_io(snf, op, 1, id, sizeof(id));
3827 + if (ret)
3828 + return ret;
3829 +
3830 + *snand_info = snand_flash_id_lookup(SNAND_ID_DYMMY, id);
3831 + if (*snand_info)
3832 + return 0;
3833 +
3834 + snand_log_chip(snf->pdev,
3835 + "Unrecognized SPI-NAND ID: %02x %02x %02x %02x\n",
3836 + id[0], id[1], id[2], id[3]);
3837 +
3838 + return -EINVAL;
3839 +}
3840 +
3841 +int mtk_snand_init(void *dev, const struct mtk_snand_platdata *pdata,
3842 + struct mtk_snand **psnf)
3843 +{
3844 + const struct snand_flash_info *snand_info;
3845 + uint32_t rawpage_size, sect_bf_size;
3846 + struct mtk_snand tmpsnf, *snf;
3847 + int ret;
3848 +
3849 + if (!pdata || !psnf)
3850 + return -EINVAL;
3851 +
3852 + if (pdata->soc >= __SNAND_SOC_MAX) {
3853 + snand_log_chip(dev, "Invalid SOC %u for MTK-SNAND\n",
3854 + pdata->soc);
3855 + return -EINVAL;
3856 + }
3857 +
3858 + /* Dummy instance only for initial reset and id probe */
3859 + tmpsnf.nfi_base = pdata->nfi_base;
3860 + tmpsnf.ecc_base = pdata->ecc_base;
3861 + tmpsnf.soc = pdata->soc;
3862 + tmpsnf.nfi_soc = &mtk_snand_socs[pdata->soc];
3863 + tmpsnf.pdev = dev;
3864 +
3865 + /* Switch to SNFI mode */
3866 + writel(SPI_MODE, tmpsnf.nfi_base + SNF_CFG);
3867 +
3868 + /* Reset SNFI & NFI */
3869 + mtk_snand_mac_reset(&tmpsnf);
3870 + mtk_nfi_reset(&tmpsnf);
3871 +
3872 + /* Reset SPI-NAND chip */
3873 + ret = mtk_snand_chip_reset(&tmpsnf);
3874 + if (ret) {
3875 + snand_log_chip(dev, "Failed to reset SPI-NAND chip\n");
3876 + return ret;
3877 + }
3878 +
3879 + /* Probe SPI-NAND flash by JEDEC ID */
3880 + ret = mtk_snand_id_probe(&tmpsnf, &snand_info);
3881 + if (ret)
3882 + return ret;
3883 +
3884 + rawpage_size = snand_info->memorg.pagesize +
3885 + snand_info->memorg.sparesize;
3886 +
3887 + sect_bf_size = mtk_snand_socs[pdata->soc].max_sectors *
3888 + sizeof(*snf->sect_bf);
3889 +
3890 + /* Allocate memory for instance and cache */
3891 + snf = generic_mem_alloc(dev,
3892 + sizeof(*snf) + rawpage_size + sect_bf_size);
3893 + if (!snf) {
3894 + snand_log_chip(dev, "Failed to allocate memory for instance\n");
3895 + return -ENOMEM;
3896 + }
3897 +
3898 + snf->sect_bf = (int *)((uintptr_t)snf + sizeof(*snf));
3899 + snf->buf_cache = (uint8_t *)((uintptr_t)snf->sect_bf + sect_bf_size);
3900 +
3901 + /* Allocate memory for DMA buffer */
3902 + snf->page_cache = dma_mem_alloc(dev, rawpage_size);
3903 + if (!snf->page_cache) {
3904 + generic_mem_free(dev, snf);
3905 + snand_log_chip(dev,
3906 + "Failed to allocate memory for DMA buffer\n");
3907 + return -ENOMEM;
3908 + }
3909 +
3910 + /* Fill up instance */
3911 + snf->pdev = dev;
3912 + snf->nfi_base = pdata->nfi_base;
3913 + snf->ecc_base = pdata->ecc_base;
3914 + snf->soc = pdata->soc;
3915 + snf->nfi_soc = &mtk_snand_socs[pdata->soc];
3916 + snf->snfi_quad_spi = pdata->quad_spi;
3917 +
3918 + /* Initialize SNFI & ECC engine */
3919 + ret = mtk_snand_setup(snf, snand_info);
3920 + if (ret) {
3921 + dma_mem_free(dev, snf->page_cache);
3922 + generic_mem_free(dev, snf);
3923 + return ret;
3924 + }
3925 +
3926 + *psnf = snf;
3927 +
3928 + return 0;
3929 +}
3930 +
3931 +int mtk_snand_cleanup(struct mtk_snand *snf)
3932 +{
3933 + if (!snf)
3934 + return 0;
3935 +
3936 + dma_mem_free(snf->pdev, snf->page_cache);
3937 + generic_mem_free(snf->pdev, snf);
3938 +
3939 + return 0;
3940 +}
3941 --- /dev/null
3942 +++ b/drivers/mtd/mtk-snand/mtk-snand.h
3943 @@ -0,0 +1,77 @@
3944 +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
3945 +/*
3946 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
3947 + *
3948 + * Author: Weijie Gao <weijie.gao@mediatek.com>
3949 + */
3950 +
3951 +#ifndef _MTK_SNAND_H_
3952 +#define _MTK_SNAND_H_
3953 +
3954 +#ifndef PRIVATE_MTK_SNAND_HEADER
3955 +#include <stddef.h>
3956 +#include <stdint.h>
3957 +#include <stdbool.h>
3958 +#endif
3959 +
3960 +enum mtk_snand_soc {
3961 + SNAND_SOC_MT7622,
3962 + SNAND_SOC_MT7629,
3963 + SNAND_SOC_MT7981,
3964 + SNAND_SOC_MT7986,
3965 + __SNAND_SOC_MAX
3966 +};
3967 +
3968 +struct mtk_snand_platdata {
3969 + void *nfi_base;
3970 + void *ecc_base;
3971 + enum mtk_snand_soc soc;
3972 + bool quad_spi;
3973 +};
3974 +
3975 +struct mtk_snand_chip_info {
3976 + const char *model;
3977 + uint64_t chipsize;
3978 + uint32_t blocksize;
3979 + uint32_t pagesize;
3980 + uint32_t sparesize;
3981 + uint32_t spare_per_sector;
3982 + uint32_t fdm_size;
3983 + uint32_t fdm_ecc_size;
3984 + uint32_t num_sectors;
3985 + uint32_t sector_size;
3986 + uint32_t ecc_strength;
3987 + uint32_t ecc_bytes;
3988 +};
3989 +
3990 +struct mtk_snand;
3991 +struct snand_flash_info;
3992 +
3993 +int mtk_snand_init(void *dev, const struct mtk_snand_platdata *pdata,
3994 + struct mtk_snand **psnf);
3995 +int mtk_snand_cleanup(struct mtk_snand *snf);
3996 +
3997 +int mtk_snand_chip_reset(struct mtk_snand *snf);
3998 +int mtk_snand_read_page(struct mtk_snand *snf, uint64_t addr, void *buf,
3999 + void *oob, bool raw);
4000 +int mtk_snand_write_page(struct mtk_snand *snf, uint64_t addr, const void *buf,
4001 + const void *oob, bool raw);
4002 +int mtk_snand_erase_block(struct mtk_snand *snf, uint64_t addr);
4003 +int mtk_snand_block_isbad(struct mtk_snand *snf, uint64_t addr);
4004 +int mtk_snand_block_markbad(struct mtk_snand *snf, uint64_t addr);
4005 +int mtk_snand_fill_oob(struct mtk_snand *snf, uint8_t *oobraw,
4006 + const uint8_t *oobbuf, size_t ooblen);
4007 +int mtk_snand_transfer_oob(struct mtk_snand *snf, uint8_t *oobbuf,
4008 + size_t ooblen, const uint8_t *oobraw);
4009 +int mtk_snand_read_page_auto_oob(struct mtk_snand *snf, uint64_t addr,
4010 + void *buf, void *oob, size_t ooblen,
4011 + size_t *actualooblen, bool raw);
4012 +int mtk_snand_write_page_auto_oob(struct mtk_snand *snf, uint64_t addr,
4013 + const void *buf, const void *oob,
4014 + size_t ooblen, size_t *actualooblen,
4015 + bool raw);
4016 +int mtk_snand_get_chip_info(struct mtk_snand *snf,
4017 + struct mtk_snand_chip_info *info);
4018 +int mtk_snand_irq_process(struct mtk_snand *snf);
4019 +
4020 +#endif /* _MTK_SNAND_H_ */