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