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