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
7 Add mtd driver for MediaTek SPI-NAND flash controller
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.
13 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
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
39 --- a/drivers/mtd/Kconfig
40 +++ b/drivers/mtd/Kconfig
41 @@ -116,6 +116,8 @@ config STM32_FLASH
42 This is the driver of embedded flash for some STMicroelectronics
45 +source "drivers/mtd/mtk-snand/Kconfig"
47 source "drivers/mtd/nand/Kconfig"
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/
57 +obj-$(CONFIG_MTK_SPI_NAND) += mtk-snand/
59 +++ b/drivers/mtd/mtk-snand/Kconfig
62 +# Copyright (C) 2020 MediaTek Inc. All rights reserved.
63 +# Author: Weijie Gao <weijie.gao@mediatek.com>
65 +# SPDX-License-Identifier: GPL-2.0
69 + tristate "MediaTek SPI NAND flash controller driver"
70 + depends on !MTD_SPI_NAND
72 + This option enables access to SPI-NAND flashes through the
73 + MediaTek SPI NAND Flash Controller
75 +config MTK_SPI_NAND_MTD
76 + tristate "MTD support for MediaTek SPI NAND flash controller"
78 + depends on MTK_SPI_NAND
80 + This option enables access to SPI-NAND flashes through the
81 + MTD interface of MediaTek SPI NAND Flash Controller
83 +++ b/drivers/mtd/mtk-snand/Makefile
86 +# Copyright (C) 2020 MediaTek Inc. All rights reserved.
87 +# Author: Weijie Gao <weijie.gao@mediatek.com>
89 +# SPDX-License-Identifier: GPL-2.0
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
95 +ccflags-y += -DPRIVATE_MTK_SNAND_HEADER
97 +++ b/drivers/mtd/mtk-snand/mtk-snand-def.h
99 +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
101 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
103 + * Author: Weijie Gao <weijie.gao@mediatek.com>
106 +#ifndef _MTK_SNAND_DEF_H_
107 +#define _MTK_SNAND_DEF_H_
109 +#include "mtk-snand-os.h"
111 +#ifdef PRIVATE_MTK_SNAND_HEADER
112 +#include "mtk-snand.h"
114 +#include <mtk-snand.h>
117 +struct mtk_snand_plat_dev;
119 +enum snand_flash_io {
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)
135 +struct snand_opcode {
140 +struct snand_io_cap {
142 + struct snand_opcode opcodes[__SNAND_IO_MAX];
145 +#define SNAND_OP(_io, _opcode, _dummy) [_io] = { .opcode = (_opcode), \
146 + .dummy = (_dummy) }
148 +#define SNAND_IO_CAP(_name, _caps, ...) \
149 + struct snand_io_cap _name = { .caps = (_caps), \
150 + .opcodes = { __VA_ARGS__ } }
152 +#define SNAND_MAX_ID_LEN 4
154 +enum snand_id_type {
156 + SNAND_ID_ADDR = SNAND_ID_DYMMY,
159 + __SNAND_ID_TYPE_MAX
163 + uint8_t type; /* enum snand_id_type */
165 + uint8_t id[SNAND_MAX_ID_LEN];
168 +#define SNAND_ID(_type, ...) \
169 + { .type = (_type), .id = { __VA_ARGS__ }, \
170 + .len = sizeof((uint8_t[]) { __VA_ARGS__ }) }
172 +struct snand_mem_org {
174 + uint16_t sparesize;
175 + uint16_t pages_per_block;
176 + uint16_t blocks_per_die;
177 + uint16_t planes_per_die;
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) }
185 +typedef int (*snand_select_die_t)(struct mtk_snand *snf, uint32_t dieidx);
187 +struct snand_flash_info {
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;
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__ }
200 +const struct snand_flash_info *snand_flash_id_lookup(enum snand_id_type type,
201 + const uint8_t *id);
203 +struct mtk_snand_soc_data {
204 + uint16_t sector_size;
205 + uint16_t max_sectors;
207 + uint16_t fdm_ecc_size;
208 + uint16_t fifo_size;
211 + bool empty_page_check;
212 + uint32_t mastersta_mask;
214 + const uint8_t *spare_sizes;
215 + uint32_t num_spare_size;
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;
232 + struct mtk_snand_plat_dev *pdev;
234 + void __iomem *nfi_base;
235 + void __iomem *ecc_base;
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;
246 + uint32_t erasesize;
247 + uint32_t writesize;
251 + snand_select_die_t select_die;
253 + uint8_t opcode_rfc;
259 + uint32_t writesize_mask;
260 + uint32_t writesize_shift;
261 + uint32_t erasesize_mask;
262 + uint32_t erasesize_shift;
264 + uint32_t die_shift;
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;
273 + uint8_t *page_cache; /* Used by read/write page */
274 + uint8_t *buf_cache; /* Used by block bad/markbad & auto_oob */
277 +enum mtk_snand_log_category {
283 + __SNAND_LOG_CAT_MAX
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);
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);
299 +int mtk_snand_log(struct mtk_snand_plat_dev *pdev,
300 + enum mtk_snand_log_category cat, const char *fmt, ...);
302 +#define snand_log_nfi(pdev, fmt, ...) \
303 + mtk_snand_log(pdev, SNAND_LOG_NFI, fmt, ##__VA_ARGS__)
305 +#define snand_log_snfi(pdev, fmt, ...) \
306 + mtk_snand_log(pdev, SNAND_LOG_SNFI, fmt, ##__VA_ARGS__)
308 +#define snand_log_ecc(pdev, fmt, ...) \
309 + mtk_snand_log(pdev, SNAND_LOG_ECC, fmt, ##__VA_ARGS__)
311 +#define snand_log_chip(pdev, fmt, ...) \
312 + mtk_snand_log(pdev, SNAND_LOG_CHIP, fmt, ##__VA_ARGS__)
315 +static inline int mtk_snand_ffs64(uint64_t x)
320 + if (!(x & 0xffffffff))
321 + return ffs((uint32_t)(x >> 32)) + 32;
323 + return ffs((uint32_t)(x & 0xffffffff));
326 +/* NFI dummy commands */
327 +#define NFI_CMD_DUMMY_READ 0x00
328 +#define NFI_CMD_DUMMY_WRITE 0x80
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
348 +/* SPI-NAND feature addresses */
349 +#define SNAND_FEATURE_MICRON_DIE_ADDR 0xd0
350 +#define SNAND_MICRON_DIE_SEL_1 BIT(6)
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)
358 +#define SNAND_FEATURE_CONFIG_ADDR 0xb0
359 +#define SNAND_FEATURE_QUAD_ENABLE BIT(0)
360 +#define SNAND_FEATURE_ECC_EN BIT(4)
362 +#define SNAND_FEATURE_PROTECT_ADDR 0xa0
364 +#endif /* _MTK_SNAND_DEF_H_ */
366 +++ b/drivers/mtd/mtk-snand/mtk-snand-ecc.c
368 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
370 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
372 + * Author: Weijie Gao <weijie.gao@mediatek.com>
375 +#include "mtk-snand-def.h"
378 +#define ECC_ENCCON 0x000
379 +#define ENC_EN BIT(0)
381 +#define ECC_ENCCNFG 0x004
383 +#define ENC_BURST_EN BIT(8)
384 +#define ENC_TNUM_S 0
386 +#define ECC_ENCIDLE 0x00c
387 +#define ENC_IDLE BIT(0)
389 +#define ECC_DECCON 0x100
390 +#define DEC_EN BIT(0)
392 +#define ECC_DECCNFG 0x104
393 +#define DEC_EMPTY_EN BIT(31)
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
400 +#define ECC_DECIDLE 0x10c
401 +#define DEC_IDLE BIT(0)
403 +#define ECC_DECENUM0 0x114
404 +#define ECC_DECENUM(n) (ECC_DECENUM0 + (n) * 4)
406 +/* ECC_ENCIDLE & ECC_DECIDLE */
407 +#define ECC_IDLE BIT(0)
409 +/* ENC_MODE & DEC_MODE */
410 +#define ECC_MODE_NFI 1
412 +#define ECC_TIMEOUT 500000
414 +static const uint8_t mt7622_ecc_caps[] = { 4, 6, 8, 10, 12 };
416 +static const uint8_t mt7986_ecc_caps[] = {
417 + 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24
420 +static const uint32_t mt7622_ecc_regs[] = {
421 + [ECC_DECDONE] = 0x11c,
424 +static const uint32_t mt7986_ecc_regs[] = {
425 + [ECC_DECDONE] = 0x124,
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,
437 + [SNAND_SOC_MT7629] = {
438 + .ecc_caps = mt7622_ecc_caps,
439 + .num_ecc_cap = ARRAY_SIZE(mt7622_ecc_caps),
440 + .regs = mt7622_ecc_regs,
445 + [SNAND_SOC_MT7986] = {
446 + .ecc_caps = mt7986_ecc_caps,
447 + .num_ecc_cap = ARRAY_SIZE(mt7986_ecc_caps),
448 + .regs = mt7986_ecc_regs,
455 +static inline uint32_t ecc_read32(struct mtk_snand *snf, uint32_t reg)
457 + return readl(snf->ecc_base + reg);
460 +static inline void ecc_write32(struct mtk_snand *snf, uint32_t reg,
463 + writel(val, snf->ecc_base + reg);
466 +static inline void ecc_write16(struct mtk_snand *snf, uint32_t reg,
469 + writew(val, snf->ecc_base + reg);
472 +static int mtk_ecc_poll(struct mtk_snand *snf, uint32_t reg, uint32_t bits)
476 + return read16_poll_timeout(snf->ecc_base + reg, val, (val & bits), 0,
480 +static int mtk_ecc_wait_idle(struct mtk_snand *snf, uint32_t reg)
484 + ret = mtk_ecc_poll(snf, reg, ECC_IDLE);
486 + snand_log_ecc(snf->pdev, "ECC engine is busy\n");
493 +int mtk_ecc_setup(struct mtk_snand *snf, void *fmdaddr, uint32_t max_ecc_bytes,
496 + uint32_t i, val, ecc_msg_bits, ecc_strength;
499 + snf->ecc_soc = &mtk_ecc_socs[snf->soc];
501 + snf->ecc_parity_bits = fls(1 + 8 * msg_size);
502 + ecc_strength = max_ecc_bytes * 8 / snf->ecc_parity_bits;
504 + for (i = snf->ecc_soc->num_ecc_cap - 1; i >= 0; i--) {
505 + if (snf->ecc_soc->ecc_caps[i] <= ecc_strength)
509 + if (unlikely(i < 0)) {
510 + snand_log_ecc(snf->pdev, "Page size %u+%u is not supported\n",
511 + snf->writesize, snf->oobsize);
515 + snf->ecc_strength = snf->ecc_soc->ecc_caps[i];
516 + snf->ecc_bytes = DIV_ROUND_UP(snf->ecc_strength * snf->ecc_parity_bits,
519 + /* Encoder config */
520 + ecc_write16(snf, ECC_ENCCON, 0);
521 + ret = mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
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);
530 + /* Decoder config */
531 + ecc_write16(snf, ECC_DECCON, 0);
532 + ret = mtk_ecc_wait_idle(snf, ECC_DECIDLE);
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);
545 +int mtk_snand_ecc_encoder_start(struct mtk_snand *snf)
549 + ret = mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
551 + ecc_write16(snf, ECC_ENCCON, 0);
552 + mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
555 + ecc_write16(snf, ECC_ENCCON, ENC_EN);
560 +void mtk_snand_ecc_encoder_stop(struct mtk_snand *snf)
562 + mtk_ecc_wait_idle(snf, ECC_ENCIDLE);
563 + ecc_write16(snf, ECC_ENCCON, 0);
566 +int mtk_snand_ecc_decoder_start(struct mtk_snand *snf)
570 + ret = mtk_ecc_wait_idle(snf, ECC_DECIDLE);
572 + ecc_write16(snf, ECC_DECCON, 0);
573 + mtk_ecc_wait_idle(snf, ECC_DECIDLE);
576 + ecc_write16(snf, ECC_DECCON, DEC_EN);
581 +void mtk_snand_ecc_decoder_stop(struct mtk_snand *snf)
583 + mtk_ecc_wait_idle(snf, ECC_DECIDLE);
584 + ecc_write16(snf, ECC_DECCON, 0);
587 +int mtk_ecc_wait_decoder_done(struct mtk_snand *snf)
589 + uint16_t val, step_mask = (1 << snf->ecc_steps) - 1;
590 + uint32_t reg = snf->ecc_soc->regs[ECC_DECDONE];
593 + ret = read16_poll_timeout(snf->ecc_base + reg, val,
594 + (val & step_mask) == step_mask, 0,
597 + snand_log_ecc(snf->pdev, "ECC decoder is busy\n");
602 +int mtk_ecc_check_decode_error(struct mtk_snand *snf, uint32_t page)
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;
609 + for (i = 0; i < snf->ecc_steps; i++) {
613 + errnum = ecc_read32(snf, ECC_DECENUM(regi));
614 + errnum = (errnum >> (fi * errnum_shift)) & errnum_mask;
618 + if (errnum <= snf->ecc_strength) {
624 + snand_log_ecc(snf->pdev,
625 + "Uncorrectable bitflips in page %u sect %u\n",
633 +++ b/drivers/mtd/mtk-snand/mtk-snand-ids.c
635 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
637 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
639 + * Author: Weijie Gao <weijie.gao@mediatek.com>
642 +#include "mtk-snand-def.h"
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);
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)
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 |
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));
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 |
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));
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 |
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));
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));
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));
704 +static const SNAND_IO_CAP(snand_cap_program_load_x1,
706 + SNAND_OP(SNAND_IO_1_1_1, SNAND_CMD_PROGRAM_LOAD, 0));
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));
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),
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),
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),
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),
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),
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),
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),
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),
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),
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),
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),
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),
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),
1092 +static int mtk_snand_winbond_select_die(struct mtk_snand *snf, uint32_t dieidx)
1097 + snand_log_chip(snf->pdev, "Invalid die index %u\n", dieidx);
1101 + op[0] = SNAND_CMD_WINBOND_SELECT_DIE;
1102 + op[1] = (uint8_t)dieidx;
1104 + return mtk_snand_mac_io(snf, op, sizeof(op), NULL, 0);
1107 +static int mtk_snand_micron_select_die(struct mtk_snand *snf, uint32_t dieidx)
1112 + snand_log_chip(snf->pdev, "Invalid die index %u\n", dieidx);
1116 + ret = mtk_snand_set_feature(snf, SNAND_FEATURE_MICRON_DIE_ADDR,
1117 + SNAND_MICRON_DIE_SEL_1);
1119 + snand_log_chip(snf->pdev,
1120 + "Failed to set die selection feature\n");
1127 +const struct snand_flash_info *snand_flash_id_lookup(enum snand_id_type type,
1128 + const uint8_t *id)
1130 + const struct snand_id *fid;
1133 + for (i = 0; i < ARRAY_SIZE(snand_flash_ids); i++) {
1134 + if (snand_flash_ids[i].id.type != type)
1137 + fid = &snand_flash_ids[i].id;
1138 + if (memcmp(fid->id, id, fid->len))
1141 + return &snand_flash_ids[i];
1147 +++ b/drivers/mtd/mtk-snand/mtk-snand-mtd.c
1149 +// SPDX-License-Identifier: GPL-2.0
1151 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1153 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1156 +#include <common.h>
1158 +#include <malloc.h>
1159 +#include <mapmem.h>
1160 +#include <linux/mtd/mtd.h>
1161 +#include <watchdog.h>
1163 +#include "mtk-snand.h"
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;
1172 +static const char snand_mtd_name_prefix[] = "spi-nand";
1174 +static u32 snandidx;
1176 +static inline struct mtk_snand_mtd *mtd_to_msm(struct mtd_info *mtd)
1181 +static int mtk_snand_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1183 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1184 + u64 start_addr, end_addr;
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",
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);
1201 + instr->state = MTD_ERASING;
1203 + while (start_addr < end_addr) {
1206 + if (mtk_snand_block_isbad(msm->snf, start_addr)) {
1207 + if (!instr->scrub) {
1208 + instr->fail_addr = start_addr;
1214 + ret = mtk_snand_erase_block(msm->snf, start_addr);
1216 + instr->fail_addr = start_addr;
1220 + start_addr += mtd->erasesize;
1224 + instr->state = MTD_ERASE_FAILED;
1226 + instr->state = MTD_ERASE_DONE;
1234 +static int mtk_snand_mtd_read_data(struct mtk_snand_mtd *msm, uint64_t addr,
1235 + struct mtd_oob_ops *ops)
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;
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;
1251 + datcache = len ? msm->page_cache : NULL;
1252 + oobcache = ooblen ? msm->page_cache + mtd->writesize : NULL;
1254 + ops->oobretlen = 0;
1257 + while (len || ooblen) {
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);
1264 + ret = mtk_snand_read_page(msm->snf, addr, datcache,
1272 + chklen = mtd->writesize - col;
1276 + memcpy(ops->datbuf + ops->retlen, datcache + col,
1279 + col = 0; /* (col + chklen) % */
1280 + ops->retlen += chklen;
1285 + chklen = maxooblen - ooboffs;
1286 + if (chklen > ooblen)
1289 + memcpy(ops->oobbuf + ops->oobretlen, oobcache + ooboffs,
1292 + ooboffs = 0; /* (ooboffs + chklen) % maxooblen; */
1293 + ops->oobretlen += chklen;
1296 + addr += mtd->writesize;
1302 +static int mtk_snand_mtd_read_oob(struct mtd_info *mtd, loff_t from,
1303 + struct mtd_oob_ops *ops)
1305 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1306 + uint32_t maxooblen;
1308 + if (!ops->oobbuf && !ops->datbuf) {
1309 + if (ops->ooblen || ops->len)
1315 + switch (ops->mode) {
1316 + case MTD_OPS_PLACE_OOB:
1317 + case MTD_OPS_AUTO_OOB:
1321 + pr_debug("%s: unsupported oob mode: %u\n", __func__, ops->mode);
1325 + maxooblen = mtd_oobavail(mtd, ops);
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",
1334 + if (unlikely(ops->ooboffs >= maxooblen)) {
1335 + pr_debug("%s: attempt to start read outside oob\n",
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",
1348 + return mtk_snand_mtd_read_data(msm, from, ops);
1351 +static int mtk_snand_mtd_write_data(struct mtk_snand_mtd *msm, uint64_t addr,
1352 + struct mtd_oob_ops *ops)
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;
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;
1368 + datcache = len ? msm->page_cache : NULL;
1369 + oobcache = ooblen ? msm->page_cache + mtd->writesize : NULL;
1371 + ops->oobretlen = 0;
1374 + while (len || ooblen) {
1379 + chklen = mtd->writesize - col;
1383 + memset(datcache, 0xff, col);
1384 + memcpy(datcache + col, ops->datbuf + ops->retlen,
1386 + memset(datcache + col + chklen, 0xff,
1387 + mtd->writesize - col - chklen);
1389 + col = 0; /* (col + chklen) % */
1390 + ops->retlen += chklen;
1396 + chklen = maxooblen - ooboffs;
1397 + if (chklen > ooblen)
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;
1407 + ooboffs = 0; /* (ooboffs + chklen) % maxooblen; */
1408 + ops->oobretlen += chklen;
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);
1415 + ret = mtk_snand_write_page(msm->snf, addr, datcache,
1421 + addr += mtd->writesize;
1427 +static int mtk_snand_mtd_write_oob(struct mtd_info *mtd, loff_t to,
1428 + struct mtd_oob_ops *ops)
1430 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1431 + uint32_t maxooblen;
1433 + if (!ops->oobbuf && !ops->datbuf) {
1434 + if (ops->ooblen || ops->len)
1440 + switch (ops->mode) {
1441 + case MTD_OPS_PLACE_OOB:
1442 + case MTD_OPS_AUTO_OOB:
1446 + pr_debug("%s: unsupported oob mode: %u\n", __func__, ops->mode);
1450 + maxooblen = mtd_oobavail(mtd, ops);
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",
1459 + if (unlikely(ops->ooboffs >= maxooblen)) {
1460 + pr_debug("%s: attempt to start write outside oob\n",
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",
1473 + return mtk_snand_mtd_write_data(msm, to, ops);
1476 +static int mtk_snand_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1477 + size_t *retlen, u_char *buf)
1479 + struct mtd_oob_ops ops = {
1480 + .mode = MTD_OPS_PLACE_OOB,
1486 + ret = mtk_snand_mtd_read_oob(mtd, from, &ops);
1489 + *retlen = ops.retlen;
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)
1497 + struct mtd_oob_ops ops = {
1498 + .mode = MTD_OPS_PLACE_OOB,
1499 + .datbuf = (void *)buf,
1504 + ret = mtk_snand_mtd_write_oob(mtd, to, &ops);
1507 + *retlen = ops.retlen;
1512 +static int mtk_snand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
1514 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1516 + return mtk_snand_block_isbad(msm->snf, offs);
1519 +static int mtk_snand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
1521 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1523 + return mtk_snand_block_markbad(msm->snf, offs);
1526 +static int mtk_snand_ooblayout_ecc(struct mtd_info *mtd, int section,
1527 + struct mtd_oob_region *oobecc)
1529 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1534 + oobecc->offset = msm->cinfo.fdm_size * msm->cinfo.num_sectors;
1535 + oobecc->length = mtd->oobsize - oobecc->offset;
1540 +static int mtk_snand_ooblayout_free(struct mtd_info *mtd, int section,
1541 + struct mtd_oob_region *oobfree)
1543 + struct mtk_snand_mtd *msm = mtd_to_msm(mtd);
1545 + if (section >= msm->cinfo.num_sectors)
1548 + oobfree->length = msm->cinfo.fdm_size - 1;
1549 + oobfree->offset = section * msm->cinfo.fdm_size + 1;
1554 +static const struct mtd_ooblayout_ops mtk_snand_ooblayout = {
1555 + .ecc = mtk_snand_ooblayout_ecc,
1556 + .rfree = mtk_snand_ooblayout_free,
1559 +static int mtk_snand_mtd_probe(struct udevice *dev)
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 = {};
1568 + base = dev_read_addr_name(dev, "nfi");
1569 + if (base == FDT_ADDR_T_NONE)
1571 + mtk_snand_pdata.nfi_base = map_sysmem(base, 0);
1573 + base = dev_read_addr_name(dev, "ecc");
1574 + if (base == FDT_ADDR_T_NONE)
1576 + mtk_snand_pdata.ecc_base = map_sysmem(base, 0);
1578 + mtk_snand_pdata.soc = dev_get_driver_data(dev);
1579 + mtk_snand_pdata.quad_spi = dev_read_bool(dev, "quad-spi");
1581 + ret = mtk_snand_init(NULL, &mtk_snand_pdata, &msm->snf);
1585 + mtk_snand_get_chip_info(msm->snf, &msm->cinfo);
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",
1595 + namelen = sizeof(snand_mtd_name_prefix) + 12;
1597 + mtd->name = malloc(namelen);
1599 + printf("%s: failed to allocate memory for MTD name\n",
1607 + snprintf(mtd->name, namelen, "%s%u", snand_mtd_name_prefix, snandidx++);
1611 + mtd->type = MTD_NANDFLASH;
1612 + mtd->flags = MTD_CAP_NANDFLASH;
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);
1621 + mtd->ooblayout = &mtk_snand_ooblayout;
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;
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;
1635 + ret = add_mtd_device(mtd);
1637 + printf("%s: failed to add SPI-NAND MTD device\n", __func__);
1642 + printf("SPI-NAND: %s (%lluMB)\n", msm->cinfo.model,
1643 + msm->cinfo.chipsize >> 20);
1651 + free(msm->page_cache);
1654 + mtk_snand_cleanup(msm->snf);
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 */ },
1666 +U_BOOT_DRIVER(spinand) = {
1667 + .name = "mtk-snand",
1669 + .of_match = mtk_snand_ids,
1670 + .priv_auto = sizeof(struct mtk_snand_mtd),
1671 + .probe = mtk_snand_mtd_probe,
1674 +++ b/drivers/mtd/mtk-snand/mtk-snand-os.c
1676 +// SPDX-License-Identifier: GPL-2.0
1678 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1680 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1683 +#include "mtk-snand-def.h"
1685 +int mtk_snand_log(struct mtk_snand_plat_dev *pdev,
1686 + enum mtk_snand_log_category cat, const char *fmt, ...)
1688 + const char *catname = "";
1693 + case SNAND_LOG_NFI:
1694 + catname = "NFI: ";
1696 + case SNAND_LOG_SNFI:
1697 + catname = "SNFI: ";
1699 + case SNAND_LOG_ECC:
1700 + catname = "ECC: ";
1706 + puts("SPI-NAND: ");
1709 + va_start(ap, fmt);
1710 + ret = vprintf(fmt, ap);
1716 +++ b/drivers/mtd/mtk-snand/mtk-snand-os.h
1718 +/* SPDX-License-Identifier: GPL-2.0 */
1720 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1722 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1725 +#ifndef _MTK_SNAND_OS_H_
1726 +#define _MTK_SNAND_OS_H_
1728 +#include <common.h>
1729 +#include <cpu_func.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>
1741 +#ifndef ARCH_DMA_MINALIGN
1742 +#define ARCH_DMA_MINALIGN 64
1745 +struct mtk_snand_plat_dev {
1749 +/* Polling helpers */
1750 +#define read16_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
1751 + readw_poll_timeout((addr), (val), (cond), (timeout_us))
1753 +#define read32_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
1754 + readl_poll_timeout((addr), (val), (cond), (timeout_us))
1756 +/* Timer helpers */
1757 +typedef uint64_t mtk_snand_time_t;
1759 +static inline mtk_snand_time_t timer_get_ticks(void)
1761 + return get_ticks();
1764 +static inline mtk_snand_time_t timer_time_to_tick(uint32_t timeout_us)
1766 + return usec_to_tick(timeout_us);
1769 +static inline bool timer_is_timeout(mtk_snand_time_t start_tick,
1770 + mtk_snand_time_t timeout_tick)
1772 + return get_ticks() - start_tick > timeout_tick;
1775 +/* Memory helpers */
1776 +static inline void *generic_mem_alloc(struct mtk_snand_plat_dev *pdev,
1779 + return calloc(1, size);
1782 +static inline void generic_mem_free(struct mtk_snand_plat_dev *pdev, void *ptr)
1787 +static inline void *dma_mem_alloc(struct mtk_snand_plat_dev *pdev, size_t size)
1789 + return memalign(ARCH_DMA_MINALIGN, size);
1792 +static inline void dma_mem_free(struct mtk_snand_plat_dev *pdev, void *ptr)
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)
1800 + size_t cachelen = roundup(size, ARCH_DMA_MINALIGN);
1801 + uintptr_t endaddr = (uintptr_t)vaddr + cachelen;
1804 + flush_dcache_range((uintptr_t)vaddr, endaddr);
1806 + invalidate_dcache_range((uintptr_t)vaddr, endaddr);
1808 + *dma_addr = (uintptr_t)vaddr;
1813 +static inline void dma_mem_unmap(struct mtk_snand_plat_dev *pdev,
1814 + uintptr_t dma_addr, size_t size,
1819 +/* Interrupt helpers */
1820 +static inline void irq_completion_done(struct mtk_snand_plat_dev *pdev)
1824 +static inline void irq_completion_init(struct mtk_snand_plat_dev *pdev)
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)
1834 + return read32_poll_timeout(reg, val, val & bit, 0, timeout_us);
1837 +#endif /* _MTK_SNAND_OS_H_ */
1839 +++ b/drivers/mtd/mtk-snand/mtk-snand.c
1841 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
1843 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
1845 + * Author: Weijie Gao <weijie.gao@mediatek.com>
1848 +#include "mtk-snand-def.h"
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)
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
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)
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)
1886 +#define NFI_CMD 0x020
1888 +#define NFI_STRDATA 0x040
1889 +#define STR_DATA BIT(0)
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)
1896 +#define NFI_FIFOSTA 0x064
1897 +#define FIFO_WR_REMAIN_S 8
1898 +#define FIFO_RD_REMAIN_S 0
1900 +#define NFI_STRADDR 0x080
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)
1907 +#define NFI_DEBUG_CON1 0x220
1908 +#define WBUF_EN BIT(2)
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)
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)
1928 +#define SNF_MAC_OUTL 0x504
1929 +#define SNF_MAC_INL 0x508
1931 +#define SNF_RD_CTL2 0x510
1932 +#define DATA_READ_DUMMY_S 8
1933 +#define DATA_READ_CMD_S 0
1935 +#define SNF_RD_CTL3 0x514
1937 +#define SNF_PG_CTL1 0x524
1938 +#define PG_LOAD_CMD_S 8
1940 +#define SNF_PG_CTL2 0x528
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
1957 +#define SNF_MISC_CTL2 0x53c
1958 +#define PROGRAM_LOAD_BYTE_NUM_S 16
1959 +#define READ_DATA_BYTE_NUM_S 11
1961 +#define SNF_DLY_CTL3 0x548
1962 +#define SFCK_SAM_DLY_S 0
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)
1970 +#define SNF_CFG 0x55c
1971 +#define SPI_MODE BIT(0)
1973 +#define SNF_GPRAM 0x800
1974 +#define SNF_GPRAM_SIZE 0xa0
1976 +#define SNFI_POLL_INTERVAL 1000000
1978 +static const uint8_t mt7622_spare_sizes[] = { 16, 26, 27, 28 };
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,
1985 +static const struct mtk_snand_soc_data mtk_snand_socs[__SNAND_SOC_MAX] = {
1986 + [SNAND_SOC_MT7622] = {
1987 + .sector_size = 512,
1990 + .fdm_ecc_size = 1,
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)
1998 + [SNAND_SOC_MT7629] = {
1999 + .sector_size = 512,
2002 + .fdm_ecc_size = 1,
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)
2010 + [SNAND_SOC_MT7986] = {
2011 + .sector_size = 1024,
2012 + .max_sectors = 16,
2014 + .fdm_ecc_size = 1,
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)
2024 +static inline uint32_t nfi_read32(struct mtk_snand *snf, uint32_t reg)
2026 + return readl(snf->nfi_base + reg);
2029 +static inline void nfi_write32(struct mtk_snand *snf, uint32_t reg,
2032 + writel(val, snf->nfi_base + reg);
2035 +static inline void nfi_write16(struct mtk_snand *snf, uint32_t reg,
2038 + writew(val, snf->nfi_base + reg);
2041 +static inline void nfi_rmw32(struct mtk_snand *snf, uint32_t reg, uint32_t clr,
2046 + val = readl(snf->nfi_base + reg);
2049 + writel(val, snf->nfi_base + reg);
2052 +static void nfi_write_data(struct mtk_snand *snf, uint32_t reg,
2053 + const uint8_t *data, uint32_t len)
2055 + uint32_t i, val = 0, es = sizeof(uint32_t);
2057 + for (i = reg; i < reg + len; i++) {
2058 + val |= ((uint32_t)*data++) << (8 * (i % es));
2060 + if (i % es == es - 1 || i == reg + len - 1) {
2061 + nfi_write32(snf, i & ~(es - 1), val);
2067 +static void nfi_read_data(struct mtk_snand *snf, uint32_t reg, uint8_t *data,
2070 + uint32_t i, val = 0, es = sizeof(uint32_t);
2072 + for (i = reg; i < reg + len; i++) {
2073 + if (i == reg || i % es == 0)
2074 + val = nfi_read32(snf, i & ~(es - 1));
2076 + *data++ = (uint8_t)(val >> (8 * (i % es)));
2080 +static inline void do_bm_swap(uint8_t *bm1, uint8_t *bm2)
2082 + uint8_t tmp = *bm1;
2087 +static void mtk_snand_bm_swap_raw(struct mtk_snand *snf)
2089 + uint32_t fdm_bbm_pos;
2091 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
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]);
2100 +static void mtk_snand_bm_swap(struct mtk_snand *snf)
2102 + uint32_t buf_bbm_pos, fdm_bbm_pos;
2104 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
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]);
2115 +static void mtk_snand_fdm_bm_swap_raw(struct mtk_snand *snf)
2117 + uint32_t fdm_bbm_pos1, fdm_bbm_pos2;
2119 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
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]);
2129 +static void mtk_snand_fdm_bm_swap(struct mtk_snand *snf)
2131 + uint32_t fdm_bbm_pos1, fdm_bbm_pos2;
2133 + if (!snf->nfi_soc->bbm_swap || snf->ecc_steps == 1)
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]);
2143 +static int mtk_nfi_reset(struct mtk_snand *snf)
2145 + uint32_t val, fifo_mask;
2148 + nfi_write32(snf, NFI_CON, CON_FIFO_FLUSH | CON_NFI_RST);
2150 + ret = read16_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
2151 + !(val & snf->nfi_soc->mastersta_mask), 0,
2152 + SNFI_POLL_INTERVAL);
2154 + snand_log_nfi(snf->pdev,
2155 + "NFI master is still busy after reset\n");
2159 + ret = read32_poll_timeout(snf->nfi_base + NFI_STA, val,
2160 + !(val & (NFI_FSM | NFI_NAND_FSM)), 0,
2161 + SNFI_POLL_INTERVAL);
2163 + snand_log_nfi(snf->pdev, "Failed to reset NFI\n");
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);
2172 + snand_log_nfi(snf->pdev, "NFI FIFOs are not empty\n");
2179 +static int mtk_snand_mac_reset(struct mtk_snand *snf)
2184 + nfi_rmw32(snf, SNF_MISC_CTL, 0, SW_RST);
2186 + ret = read32_poll_timeout(snf->nfi_base + SNF_STA_CTL1, val,
2187 + !(val & SPI_STATE), 0, SNFI_POLL_INTERVAL);
2189 + snand_log_snfi(snf->pdev, "Failed to reset SNFI MAC\n");
2191 + nfi_write32(snf, SNF_MISC_CTL, (2 << FIFO_RD_LTC_S) |
2192 + (10 << CS_DESELECT_CYC_S));
2197 +static int mtk_snand_mac_trigger(struct mtk_snand *snf, uint32_t outlen,
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);
2207 + nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN | SF_TRIG);
2209 + ret = read32_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val,
2210 + val & WIP_READY, 0, SNFI_POLL_INTERVAL);
2212 + snand_log_snfi(snf->pdev, "Timed out waiting for WIP_READY\n");
2216 + ret = read32_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val,
2217 + !(val & WIP), 0, SNFI_POLL_INTERVAL);
2219 + snand_log_snfi(snf->pdev,
2220 + "Timed out waiting for WIP cleared\n");
2224 + nfi_write32(snf, SNF_MAC_CTL, 0);
2229 +int mtk_snand_mac_io(struct mtk_snand *snf, const uint8_t *out, uint32_t outlen,
2230 + uint8_t *in, uint32_t inlen)
2234 + if (outlen + inlen > SNF_GPRAM_SIZE)
2237 + mtk_snand_mac_reset(snf);
2239 + nfi_write_data(snf, SNF_GPRAM, out, outlen);
2241 + ret = mtk_snand_mac_trigger(snf, outlen, inlen);
2248 + nfi_read_data(snf, SNF_GPRAM + outlen, in, inlen);
2253 +static int mtk_snand_get_feature(struct mtk_snand *snf, uint32_t addr)
2255 + uint8_t op[2], val;
2258 + op[0] = SNAND_CMD_GET_FEATURE;
2259 + op[1] = (uint8_t)addr;
2261 + ret = mtk_snand_mac_io(snf, op, sizeof(op), &val, 1);
2268 +int mtk_snand_set_feature(struct mtk_snand *snf, uint32_t addr, uint32_t val)
2272 + op[0] = SNAND_CMD_SET_FEATURE;
2273 + op[1] = (uint8_t)addr;
2274 + op[2] = (uint8_t)val;
2276 + return mtk_snand_mac_io(snf, op, sizeof(op), NULL, 0);
2279 +static int mtk_snand_poll_status(struct mtk_snand *snf, uint32_t wait_us)
2282 + mtk_snand_time_t time_start, tmo;
2284 + time_start = timer_get_ticks();
2285 + tmo = timer_time_to_tick(wait_us);
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));
2294 + return -ETIMEDOUT;
2297 +int mtk_snand_chip_reset(struct mtk_snand *snf)
2299 + uint8_t op = SNAND_CMD_RESET;
2302 + ret = mtk_snand_mac_io(snf, &op, 1, NULL, 0);
2306 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
2313 +static int mtk_snand_config_feature(struct mtk_snand *snf, uint8_t clr,
2319 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_CONFIG_ADDR);
2321 + snand_log_chip(snf->pdev,
2322 + "Failed to get configuration feature\n");
2326 + newval = (val & (~clr)) | set;
2328 + if (newval == val)
2331 + ret = mtk_snand_set_feature(snf, SNAND_FEATURE_CONFIG_ADDR,
2334 + snand_log_chip(snf->pdev,
2335 + "Failed to set configuration feature\n");
2339 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_CONFIG_ADDR);
2341 + snand_log_chip(snf->pdev,
2342 + "Failed to get configuration feature\n");
2346 + if (newval != val)
2352 +static int mtk_snand_ondie_ecc_control(struct mtk_snand *snf, bool enable)
2357 + ret = mtk_snand_config_feature(snf, 0, SNAND_FEATURE_ECC_EN);
2359 + ret = mtk_snand_config_feature(snf, SNAND_FEATURE_ECC_EN, 0);
2362 + snand_log_chip(snf->pdev, "Failed to %s On-Die ECC engine\n",
2363 + enable ? "enable" : "disable");
2369 +static int mtk_snand_qspi_control(struct mtk_snand *snf, bool enable)
2374 + ret = mtk_snand_config_feature(snf, 0,
2375 + SNAND_FEATURE_QUAD_ENABLE);
2377 + ret = mtk_snand_config_feature(snf,
2378 + SNAND_FEATURE_QUAD_ENABLE, 0);
2382 + snand_log_chip(snf->pdev, "Failed to %s quad spi\n",
2383 + enable ? "enable" : "disable");
2389 +static int mtk_snand_unlock(struct mtk_snand *snf)
2393 + ret = mtk_snand_set_feature(snf, SNAND_FEATURE_PROTECT_ADDR, 0);
2395 + snand_log_chip(snf->pdev, "Failed to set protection feature\n");
2402 +static int mtk_snand_write_enable(struct mtk_snand *snf)
2404 + uint8_t op = SNAND_CMD_WRITE_ENABLE;
2407 + ret = mtk_snand_mac_io(snf, &op, 1, NULL, 0);
2411 + val = mtk_snand_get_feature(snf, SNAND_FEATURE_STATUS_ADDR);
2415 + if (val & SNAND_STATUS_WEL)
2418 + snand_log_chip(snf->pdev, "Failed to send write-enable command\n");
2423 +static int mtk_snand_select_die(struct mtk_snand *snf, uint32_t dieidx)
2425 + if (!snf->select_die)
2428 + return snf->select_die(snf, dieidx);
2431 +static uint64_t mtk_snand_select_die_address(struct mtk_snand *snf,
2436 + if (!snf->select_die)
2439 + dieidx = addr >> snf->die_shift;
2441 + mtk_snand_select_die(snf, dieidx);
2443 + return addr & snf->die_mask;
2446 +static uint32_t mtk_snand_get_plane_address(struct mtk_snand *snf,
2449 + uint32_t pages_per_block;
2451 + pages_per_block = 1 << (snf->erasesize_shift - snf->writesize_shift);
2453 + if (page & pages_per_block)
2454 + return 1 << (snf->writesize_shift + 1);
2459 +static int mtk_snand_page_op(struct mtk_snand *snf, uint32_t page, uint8_t cmd)
2464 + op[1] = (page >> 16) & 0xff;
2465 + op[2] = (page >> 8) & 0xff;
2466 + op[3] = page & 0xff;
2468 + return mtk_snand_mac_io(snf, op, sizeof(op), NULL, 0);
2471 +static void mtk_snand_read_fdm(struct mtk_snand *snf, uint8_t *buf)
2473 + uint32_t vall, valm;
2474 + uint8_t *oobptr = buf;
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));
2481 + for (j = 0; j < snf->nfi_soc->fdm_size; j++)
2482 + oobptr[j] = (j >= 4 ? valm : vall) >> ((j % 4) * 8);
2484 + oobptr += snf->nfi_soc->fdm_size;
2488 +static int mtk_snand_read_cache(struct mtk_snand *snf, uint32_t page, bool raw)
2490 + uint32_t coladdr, rwbytes, mode, len;
2491 + uintptr_t dma_addr;
2494 + /* Column address with plane bit */
2495 + coladdr = mtk_snand_get_plane_address(snf, page);
2497 + mtk_snand_mac_reset(snf);
2498 + mtk_nfi_reset(snf);
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));
2505 + /* Column address */
2506 + nfi_write32(snf, SNF_RD_CTL3, coladdr);
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);
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) |
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);
2522 + nfi_write32(snf, NFI_CON, (snf->ecc_steps << CON_SEC_NUM_S));
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);
2528 + snand_log_nfi(snf->pdev,
2529 + "DMA map from device failed with %d\n", ret);
2533 + nfi_write32(snf, NFI_STRADDR, (uint32_t)dma_addr);
2536 + mtk_snand_ecc_decoder_start(snf);
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);
2542 + /* Trigger NFI into custom mode */
2543 + nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_READ);
2545 + /* Start DMA read */
2546 + nfi_rmw32(snf, NFI_CON, 0, CON_BRD);
2547 + nfi_write16(snf, NFI_STRDATA, STR_DATA);
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);
2553 + snand_log_nfi(snf->pdev,
2554 + "DMA timed out for reading from cache\n");
2559 + ret = mtk_ecc_wait_decoder_done(snf);
2563 + mtk_snand_read_fdm(snf, snf->page_cache + snf->writesize);
2566 + * For new IPs, ecc error may occur on empty pages.
2567 + * Use an specific indication bit to check empty page.
2569 + if (snf->nfi_soc->empty_page_check &&
2570 + (nfi_read32(snf, NFI_STA) & READ_EMPTY))
2573 + ret = mtk_ecc_check_decode_error(snf, page);
2575 + mtk_snand_ecc_decoder_stop(snf);
2580 + dma_mem_unmap(snf->pdev, dma_addr, len, false);
2583 + nfi_write32(snf, NFI_CON, 0);
2585 + /* Clear SNF done flag */
2586 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_READ_DONE);
2587 + nfi_write32(snf, SNF_STA_CTL1, 0);
2589 + /* Disable interrupt */
2590 + nfi_read32(snf, NFI_INTR_STA);
2591 + nfi_write32(snf, NFI_INTR_EN, 0);
2593 + nfi_rmw32(snf, SNF_MISC_CTL, DATARD_CUSTOM_EN, 0);
2598 +static void mtk_snand_from_raw_page(struct mtk_snand *snf, void *buf, void *oob)
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;
2604 + for (i = 0; i < snf->ecc_steps; i++) {
2605 + raw_sector = snf->page_cache + i * snf->raw_sector_size;
2608 + memcpy(bufptr, raw_sector, snf->nfi_soc->sector_size);
2609 + bufptr += snf->nfi_soc->sector_size;
2612 + raw_sector += snf->nfi_soc->sector_size;
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;
2619 + memcpy(eccptr, raw_sector, ecc_bytes);
2620 + eccptr += ecc_bytes;
2625 +static int mtk_snand_do_read_page(struct mtk_snand *snf, uint64_t addr,
2626 + void *buf, void *oob, bool raw, bool format)
2628 + uint64_t die_addr;
2632 + die_addr = mtk_snand_select_die_address(snf, addr);
2633 + page = die_addr >> snf->writesize_shift;
2635 + ret = mtk_snand_page_op(snf, page, SNAND_CMD_READ_TO_CACHE);
2639 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
2641 + snand_log_chip(snf->pdev, "Read to cache command timed out\n");
2645 + ret = mtk_snand_read_cache(snf, page, raw);
2646 + if (ret < 0 && ret != -EBADMSG)
2651 + mtk_snand_bm_swap_raw(snf);
2652 + mtk_snand_fdm_bm_swap_raw(snf);
2653 + mtk_snand_from_raw_page(snf, buf, oob);
2656 + memcpy(buf, snf->page_cache, snf->writesize);
2659 + memset(oob, 0xff, snf->oobsize);
2660 + memcpy(oob, snf->page_cache + snf->writesize,
2661 + snf->ecc_steps * snf->spare_per_sector);
2665 + mtk_snand_bm_swap(snf);
2666 + mtk_snand_fdm_bm_swap(snf);
2669 + memcpy(buf, snf->page_cache, snf->writesize);
2672 + memset(oob, 0xff, snf->oobsize);
2673 + memcpy(oob, snf->page_cache + snf->writesize,
2674 + snf->ecc_steps * snf->nfi_soc->fdm_size);
2681 +int mtk_snand_read_page(struct mtk_snand *snf, uint64_t addr, void *buf,
2682 + void *oob, bool raw)
2684 + if (!snf || (!buf && !oob))
2687 + if (addr >= snf->size)
2690 + return mtk_snand_do_read_page(snf, addr, buf, oob, raw, true);
2693 +static void mtk_snand_write_fdm(struct mtk_snand *snf, const uint8_t *buf)
2695 + uint32_t vall, valm, fdm_size = snf->nfi_soc->fdm_size;
2696 + const uint8_t *oobptr = buf;
2699 + for (i = 0; i < snf->ecc_steps; i++) {
2703 + for (j = 0; j < 8; j++) {
2705 + vall |= (j < fdm_size ? oobptr[j] : 0xff)
2708 + valm |= (j < fdm_size ? oobptr[j] : 0xff)
2712 + nfi_write32(snf, NFI_FDML(i), vall);
2713 + nfi_write32(snf, NFI_FDMM(i), valm);
2715 + oobptr += fdm_size;
2719 +static int mtk_snand_program_load(struct mtk_snand *snf, uint32_t page,
2722 + uint32_t coladdr, rwbytes, mode, len;
2723 + uintptr_t dma_addr;
2726 + /* Column address with plane bit */
2727 + coladdr = mtk_snand_get_plane_address(snf, page);
2729 + mtk_snand_mac_reset(snf);
2730 + mtk_nfi_reset(snf);
2732 + /* Write FDM registers if necessary */
2734 + mtk_snand_write_fdm(snf, snf->page_cache + snf->writesize);
2737 + nfi_write32(snf, SNF_PG_CTL1, (snf->opcode_pl << PG_LOAD_CMD_S));
2739 + /* Column address */
2740 + nfi_write32(snf, SNF_PG_CTL2, coladdr);
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);
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) |
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);
2756 + nfi_write32(snf, NFI_CON, (snf->ecc_steps << CON_SEC_NUM_S));
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);
2762 + snand_log_nfi(snf->pdev,
2763 + "DMA map to device failed with %d\n", ret);
2767 + nfi_write32(snf, NFI_STRADDR, (uint32_t)dma_addr);
2770 + mtk_snand_ecc_encoder_start(snf);
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);
2776 + /* Trigger NFI into custom mode */
2777 + nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_WRITE);
2779 + /* Start DMA write */
2780 + nfi_rmw32(snf, NFI_CON, 0, CON_BWR);
2781 + nfi_write16(snf, NFI_STRDATA, STR_DATA);
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);
2787 + snand_log_nfi(snf->pdev,
2788 + "DMA timed out for program load\n");
2793 + mtk_snand_ecc_encoder_stop(snf);
2797 + dma_mem_unmap(snf->pdev, dma_addr, len, true);
2800 + nfi_write16(snf, NFI_CON, 0);
2802 + /* Clear SNF done flag */
2803 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_PG_DONE);
2804 + nfi_write32(snf, SNF_STA_CTL1, 0);
2806 + /* Disable interrupt */
2807 + nfi_read32(snf, NFI_INTR_STA);
2808 + nfi_write32(snf, NFI_INTR_EN, 0);
2810 + nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_CUSTOM_EN, 0);
2815 +static void mtk_snand_to_raw_page(struct mtk_snand *snf,
2816 + const void *buf, const void *oob,
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;
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;
2829 + memcpy(raw_sector, bufptr, snf->nfi_soc->sector_size);
2830 + bufptr += snf->nfi_soc->sector_size;
2833 + raw_sector += snf->nfi_soc->sector_size;
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;
2841 + memset(raw_sector, 0xff, ecc_bytes);
2843 + memcpy(raw_sector, eccptr, ecc_bytes);
2844 + eccptr += ecc_bytes;
2849 +static bool mtk_snand_is_empty_page(struct mtk_snand *snf, const void *buf,
2852 + const uint8_t *p = buf;
2856 + for (i = 0; i < snf->writesize; i++) {
2863 + for (j = 0; j < snf->ecc_steps; j++) {
2864 + p = oob + j * snf->nfi_soc->fdm_size;
2866 + for (i = 0; i < snf->nfi_soc->fdm_ecc_size; i++) {
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)
2880 + uint64_t die_addr;
2881 + bool empty_ecc = false;
2885 + die_addr = mtk_snand_select_die_address(snf, addr);
2886 + page = die_addr >> snf->writesize_shift;
2888 + if (!raw && mtk_snand_is_empty_page(snf, buf, oob)) {
2890 + * If the data in the page to be ecc-ed is full 0xff,
2891 + * change to raw write mode
2896 + /* fill ecc parity code region with 0xff */
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);
2906 + memset(snf->page_cache, 0xff,
2907 + snf->writesize + snf->oobsize);
2910 + memcpy(snf->page_cache, buf, snf->writesize);
2913 + memcpy(snf->page_cache + snf->writesize, oob,
2914 + snf->ecc_steps * snf->spare_per_sector);
2918 + memset(snf->page_cache, 0xff, snf->writesize + snf->oobsize);
2920 + memcpy(snf->page_cache, buf, snf->writesize);
2923 + memcpy(snf->page_cache + snf->writesize, oob,
2924 + snf->ecc_steps * snf->nfi_soc->fdm_size);
2927 + mtk_snand_fdm_bm_swap(snf);
2928 + mtk_snand_bm_swap(snf);
2931 + ret = mtk_snand_write_enable(snf);
2935 + ret = mtk_snand_program_load(snf, page, raw);
2939 + ret = mtk_snand_page_op(snf, page, SNAND_CMD_PROGRAM_EXECUTE);
2943 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
2945 + snand_log_chip(snf->pdev,
2946 + "Page program command timed out on page %u\n",
2951 + if (ret & SNAND_STATUS_PROGRAM_FAIL) {
2952 + snand_log_chip(snf->pdev,
2953 + "Page program failed on page %u\n", page);
2960 +int mtk_snand_write_page(struct mtk_snand *snf, uint64_t addr, const void *buf,
2961 + const void *oob, bool raw)
2963 + if (!snf || (!buf && !oob))
2966 + if (addr >= snf->size)
2969 + return mtk_snand_do_write_page(snf, addr, buf, oob, raw, true);
2972 +int mtk_snand_erase_block(struct mtk_snand *snf, uint64_t addr)
2974 + uint64_t die_addr;
2975 + uint32_t page, block;
2981 + if (addr >= snf->size)
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);
2988 + ret = mtk_snand_write_enable(snf);
2992 + ret = mtk_snand_page_op(snf, page, SNAND_CMD_BLOCK_ERASE);
2996 + ret = mtk_snand_poll_status(snf, SNFI_POLL_INTERVAL);
2998 + snand_log_chip(snf->pdev,
2999 + "Block erase command timed out on block %u\n",
3004 + if (ret & SNAND_STATUS_ERASE_FAIL) {
3005 + snand_log_chip(snf->pdev,
3006 + "Block erase failed on block %u\n", block);
3013 +static int mtk_snand_block_isbad_std(struct mtk_snand *snf, uint64_t addr)
3017 + ret = mtk_snand_do_read_page(snf, addr, NULL, snf->buf_cache, true,
3019 + if (ret && ret != -EBADMSG)
3022 + return snf->buf_cache[0] != 0xff;
3025 +static int mtk_snand_block_isbad_mtk(struct mtk_snand *snf, uint64_t addr)
3029 + ret = mtk_snand_do_read_page(snf, addr, NULL, snf->buf_cache, true,
3031 + if (ret && ret != -EBADMSG)
3034 + return snf->buf_cache[0] != 0xff;
3037 +int mtk_snand_block_isbad(struct mtk_snand *snf, uint64_t addr)
3042 + if (addr >= snf->size)
3045 + addr &= ~snf->erasesize_mask;
3047 + if (snf->nfi_soc->bbm_swap)
3048 + return mtk_snand_block_isbad_std(snf, addr);
3050 + return mtk_snand_block_isbad_mtk(snf, addr);
3053 +static int mtk_snand_block_markbad_std(struct mtk_snand *snf, uint64_t addr)
3055 + /* Standard BBM position */
3056 + memset(snf->buf_cache, 0xff, snf->oobsize);
3057 + snf->buf_cache[0] = 0;
3059 + return mtk_snand_do_write_page(snf, addr, NULL, snf->buf_cache, true,
3063 +static int mtk_snand_block_markbad_mtk(struct mtk_snand *snf, uint64_t addr)
3065 + /* Write the whole page with zeros */
3066 + memset(snf->buf_cache, 0, snf->writesize + snf->oobsize);
3068 + return mtk_snand_do_write_page(snf, addr, snf->buf_cache,
3069 + snf->buf_cache + snf->writesize, true,
3073 +int mtk_snand_block_markbad(struct mtk_snand *snf, uint64_t addr)
3078 + if (addr >= snf->size)
3081 + addr &= ~snf->erasesize_mask;
3083 + if (snf->nfi_soc->bbm_swap)
3084 + return mtk_snand_block_markbad_std(snf, addr);
3086 + return mtk_snand_block_markbad_mtk(snf, addr);
3089 +int mtk_snand_fill_oob(struct mtk_snand *snf, uint8_t *oobraw,
3090 + const uint8_t *oobbuf, size_t ooblen)
3092 + size_t len = ooblen, sect_fdm_len;
3093 + const uint8_t *oob = oobbuf;
3094 + uint32_t step = 0;
3096 + if (!snf || !oobraw || !oob)
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;
3104 + memcpy(oobraw + step * snf->nfi_soc->fdm_size + 1, oob,
3107 + len -= sect_fdm_len;
3108 + oob += sect_fdm_len;
3115 +int mtk_snand_transfer_oob(struct mtk_snand *snf, uint8_t *oobbuf,
3116 + size_t ooblen, const uint8_t *oobraw)
3118 + size_t len = ooblen, sect_fdm_len;
3119 + uint8_t *oob = oobbuf;
3120 + uint32_t step = 0;
3122 + if (!snf || !oobraw || !oob)
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;
3130 + memcpy(oob, oobraw + step * snf->nfi_soc->fdm_size + 1,
3133 + len -= sect_fdm_len;
3134 + oob += sect_fdm_len;
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)
3145 + int ret, oobremain;
3151 + return mtk_snand_read_page(snf, addr, buf, NULL, raw);
3153 + ret = mtk_snand_read_page(snf, addr, buf, snf->buf_cache, raw);
3154 + if (ret && ret != -EBADMSG) {
3156 + *actualooblen = 0;
3160 + oobremain = mtk_snand_transfer_oob(snf, oob, ooblen, snf->buf_cache);
3162 + *actualooblen = ooblen - oobremain;
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)
3177 + return mtk_snand_write_page(snf, addr, buf, NULL, raw);
3179 + memset(snf->buf_cache, 0xff, snf->oobsize);
3180 + oobremain = mtk_snand_fill_oob(snf, snf->buf_cache, oob, ooblen);
3182 + *actualooblen = ooblen - oobremain;
3184 + return mtk_snand_write_page(snf, addr, buf, snf->buf_cache, raw);
3187 +int mtk_snand_get_chip_info(struct mtk_snand *snf,
3188 + struct mtk_snand_chip_info *info)
3190 + if (!snf || !info)
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;
3209 +int mtk_snand_irq_process(struct mtk_snand *snf)
3211 + uint32_t sta, ien;
3216 + sta = nfi_read32(snf, NFI_INTR_STA);
3217 + ien = nfi_read32(snf, NFI_INTR_EN);
3222 + nfi_write32(snf, NFI_INTR_EN, 0);
3223 + irq_completion_done(snf->pdev);
3228 +static int mtk_snand_select_spare_per_sector(struct mtk_snand *snf)
3230 + uint32_t spare_per_step = snf->oobsize / snf->ecc_steps;
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.
3237 + if (snf->nfi_soc->sector_size == 1024)
3240 + spare_per_step /= mul;
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;
3250 + snand_log_nfi(snf->pdev,
3251 + "Page size %u+%u is not supported\n", snf->writesize,
3257 +static int mtk_snand_pagefmt_setup(struct mtk_snand *snf)
3259 + uint32_t spare_size_idx, spare_size_shift, pagesize_idx;
3260 + uint32_t sector_size_512;
3262 + if (snf->nfi_soc->sector_size == 512) {
3263 + sector_size_512 = NFI_SEC_SEL_512;
3264 + spare_size_shift = NFI_SPARE_SIZE_S;
3266 + sector_size_512 = 0;
3267 + spare_size_shift = NFI_SPARE_SIZE_LS_S;
3270 + switch (snf->writesize) {
3272 + pagesize_idx = NFI_PAGE_SIZE_512_2K;
3275 + if (snf->nfi_soc->sector_size == 512)
3276 + pagesize_idx = NFI_PAGE_SIZE_2K_4K;
3278 + pagesize_idx = NFI_PAGE_SIZE_512_2K;
3281 + if (snf->nfi_soc->sector_size == 512)
3282 + pagesize_idx = NFI_PAGE_SIZE_4K_8K;
3284 + pagesize_idx = NFI_PAGE_SIZE_2K_4K;
3287 + if (snf->nfi_soc->sector_size == 512)
3288 + pagesize_idx = NFI_PAGE_SIZE_8K_16K;
3290 + pagesize_idx = NFI_PAGE_SIZE_4K_8K;
3293 + pagesize_idx = NFI_PAGE_SIZE_8K_16K;
3296 + snand_log_nfi(snf->pdev, "Page size %u is not supported\n",
3301 + spare_size_idx = mtk_snand_select_spare_per_sector(snf);
3302 + if (unlikely(spare_size_idx < 0))
3305 + snf->raw_sector_size = snf->nfi_soc->sector_size +
3306 + snf->spare_per_sector;
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) |
3319 +static enum snand_flash_io mtk_snand_select_opcode(struct mtk_snand *snf,
3320 + uint32_t snfi_caps, uint8_t *opcode,
3322 + const struct snand_io_cap *op_cap)
3326 + caps = snfi_caps & op_cap->caps;
3330 + *opcode = op_cap->opcodes[i - 1].opcode;
3332 + *dummy = op_cap->opcodes[i - 1].dummy;
3336 + return __SNAND_IO_MAX;
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)
3343 + enum snand_flash_io idx;
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,
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");
3361 + snf->mode_rfc = rfc_modes[idx];
3363 + if (idx == SNAND_IO_1_1_4 || idx == SNAND_IO_1_4_4)
3364 + snf->quad_spi_op = true;
3369 +static int mtk_snand_select_opcode_pl(struct mtk_snand *snf, uint32_t snfi_caps,
3370 + const struct snand_io_cap *op_cap)
3372 + enum snand_flash_io idx;
3374 + static const uint8_t pl_modes[__SNAND_IO_MAX] = {
3375 + [SNAND_IO_1_1_1] = 0,
3376 + [SNAND_IO_1_1_4] = 1,
3379 + idx = mtk_snand_select_opcode(snf, snfi_caps, &snf->opcode_pl,
3381 + if (idx >= __SNAND_IO_MAX) {
3382 + snand_log_snfi(snf->pdev,
3383 + "No capable opcode for program load\n");
3387 + snf->mode_pl = pl_modes[idx];
3389 + if (idx == SNAND_IO_1_1_4)
3390 + snf->quad_spi_op = true;
3395 +static int mtk_snand_setup(struct mtk_snand *snf,
3396 + const struct snand_flash_info *snand_info)
3398 + const struct snand_mem_org *memorg = &snand_info->memorg;
3399 + uint32_t i, msg_size, snfi_caps;
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;
3411 + snf->writesize_mask = snf->writesize - 1;
3412 + snf->erasesize_mask = snf->erasesize - 1;
3413 + snf->die_mask = snf->die_size - 1;
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;
3419 + snf->select_die = snand_info->select_die;
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;
3426 + ret = mtk_snand_select_opcode_rfc(snf, snfi_caps, snand_info->cap_rd);
3430 + ret = mtk_snand_select_opcode_pl(snf, snfi_caps, snand_info->cap_pl);
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",
3442 + ret = mtk_snand_pagefmt_setup(snf);
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,
3453 + nfi_write16(snf, NFI_CNFG, 0);
3455 + /* Tuning options */
3456 + nfi_write16(snf, NFI_DEBUG_CON1, WBUF_EN);
3457 + nfi_write32(snf, SNF_DLY_CTL3, (40 << SFCK_SAM_DLY_S));
3460 + nfi_read32(snf, NFI_INTR_STA);
3461 + nfi_write32(snf, NFI_INTR_EN, 0);
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);
3467 + /* Initialization on all dies */
3468 + for (i = 0; i < snf->num_dies; i++) {
3469 + mtk_snand_select_die(snf, i);
3471 + /* Disable On-Die ECC engine */
3472 + ret = mtk_snand_ondie_ecc_control(snf, false);
3476 + /* Disable block protection */
3477 + mtk_snand_unlock(snf);
3479 + /* Enable/disable quad-spi */
3480 + mtk_snand_qspi_control(snf, snf->quad_spi_op);
3483 + mtk_snand_select_die(snf, 0);
3488 +static int mtk_snand_id_probe(struct mtk_snand *snf,
3489 + const struct snand_flash_info **snand_info)
3491 + uint8_t id[4], op[2];
3494 + /* Read SPI-NAND JEDEC ID, OP + dummy/addr + ID */
3495 + op[0] = SNAND_CMD_READID;
3497 + ret = mtk_snand_mac_io(snf, op, 2, id, sizeof(id));
3501 + *snand_info = snand_flash_id_lookup(SNAND_ID_DYMMY, id);
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));
3511 + *snand_info = snand_flash_id_lookup(SNAND_ID_DYMMY, id);
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]);
3522 +int mtk_snand_init(void *dev, const struct mtk_snand_platdata *pdata,
3523 + struct mtk_snand **psnf)
3525 + const struct snand_flash_info *snand_info;
3526 + struct mtk_snand tmpsnf, *snf;
3527 + uint32_t rawpage_size;
3530 + if (!pdata || !psnf)
3533 + if (pdata->soc >= __SNAND_SOC_MAX) {
3534 + snand_log_chip(dev, "Invalid SOC %u for MTK-SNAND\n",
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;
3546 + /* Switch to SNFI mode */
3547 + writel(SPI_MODE, tmpsnf.nfi_base + SNF_CFG);
3549 + /* Reset SNFI & NFI */
3550 + mtk_snand_mac_reset(&tmpsnf);
3551 + mtk_nfi_reset(&tmpsnf);
3553 + /* Reset SPI-NAND chip */
3554 + ret = mtk_snand_chip_reset(&tmpsnf);
3556 + snand_log_chip(dev, "Failed to reset SPI-NAND chip\n");
3560 + /* Probe SPI-NAND flash by JEDEC ID */
3561 + ret = mtk_snand_id_probe(&tmpsnf, &snand_info);
3565 + rawpage_size = snand_info->memorg.pagesize +
3566 + snand_info->memorg.sparesize;
3568 + /* Allocate memory for instance and cache */
3569 + snf = generic_mem_alloc(dev, sizeof(*snf) + rawpage_size);
3571 + snand_log_chip(dev, "Failed to allocate memory for instance\n");
3575 + snf->buf_cache = (uint8_t *)((uintptr_t)snf + sizeof(*snf));
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");
3586 + /* Fill up instance */
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;
3594 + /* Initialize SNFI & ECC engine */
3595 + ret = mtk_snand_setup(snf, snand_info);
3597 + dma_mem_free(dev, snf->page_cache);
3598 + generic_mem_free(dev, snf);
3607 +int mtk_snand_cleanup(struct mtk_snand *snf)
3612 + dma_mem_free(snf->pdev, snf->page_cache);
3613 + generic_mem_free(snf->pdev, snf);
3618 +++ b/drivers/mtd/mtk-snand/mtk-snand.h
3620 +/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
3622 + * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
3624 + * Author: Weijie Gao <weijie.gao@mediatek.com>
3627 +#ifndef _MTK_SNAND_H_
3628 +#define _MTK_SNAND_H_
3630 +#ifndef PRIVATE_MTK_SNAND_HEADER
3631 +#include <stddef.h>
3632 +#include <stdint.h>
3633 +#include <stdbool.h>
3636 +enum mtk_snand_soc {
3644 +struct mtk_snand_platdata {
3647 + enum mtk_snand_soc soc;
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;
3667 +struct snand_flash_info;
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);
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,
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);
3696 +#endif /* _MTK_SNAND_H_ */