1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 Macronix
5 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/mtd/spinand.h>
12 #define SPINAND_MFR_MACRONIX 0xC2
13 #define MACRONIX_ECCSR_MASK 0x0F
15 static SPINAND_OP_VARIANTS(read_cache_variants,
16 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
17 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
18 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
19 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
21 static SPINAND_OP_VARIANTS(write_cache_variants,
22 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
23 SPINAND_PROG_LOAD(true, 0, NULL, 0));
25 static SPINAND_OP_VARIANTS(update_cache_variants,
26 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
27 SPINAND_PROG_LOAD(false, 0, NULL, 0));
29 static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
30 struct mtd_oob_region *region)
35 static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
36 struct mtd_oob_region *region)
42 region->length = mtd->oobsize - 2;
47 static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
48 .ecc = mx35lfxge4ab_ooblayout_ecc,
49 .free = mx35lfxge4ab_ooblayout_free,
52 static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
54 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
56 SPI_MEM_OP_DUMMY(1, 1),
57 SPI_MEM_OP_DATA_IN(1, eccsr, 1));
59 int ret = spi_mem_exec_op(spinand->spimem, &op);
63 *eccsr &= MACRONIX_ECCSR_MASK;
67 static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
70 struct nand_device *nand = spinand_to_nand(spinand);
73 switch (status & STATUS_ECC_MASK) {
74 case STATUS_ECC_NO_BITFLIPS:
77 case STATUS_ECC_UNCOR_ERROR:
80 case STATUS_ECC_HAS_BITFLIPS:
82 * Let's try to retrieve the real maximum number of bitflips
83 * in order to avoid forcing the wear-leveling layer to move
84 * data around if it's not necessary.
86 if (mx35lf1ge4ab_get_eccsr(spinand, &eccsr))
87 return nand->eccreq.strength;
89 if (WARN_ON(eccsr > nand->eccreq.strength || !eccsr))
90 return nand->eccreq.strength;
101 static const struct spinand_info macronix_spinand_table[] = {
102 SPINAND_INFO("MX35LF1GE4AB", 0x12,
103 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
105 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
106 &write_cache_variants,
107 &update_cache_variants),
109 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
110 mx35lf1ge4ab_ecc_get_status)),
111 SPINAND_INFO("MX35LF2GE4AB", 0x22,
112 NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1),
114 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
115 &write_cache_variants,
116 &update_cache_variants),
118 SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
121 static int macronix_spinand_detect(struct spinand_device *spinand)
123 u8 *id = spinand->id.data;
127 * Macronix SPI NAND read ID needs a dummy byte, so the first byte in
130 if (id[1] != SPINAND_MFR_MACRONIX)
133 ret = spinand_match_and_init(spinand, macronix_spinand_table,
134 ARRAY_SIZE(macronix_spinand_table),
142 static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
143 .detect = macronix_spinand_detect,
146 const struct spinand_manufacturer macronix_spinand_manufacturer = {
147 .id = SPINAND_MFR_MACRONIX,
149 .ops = ¯onix_spinand_manuf_ops,