a48e02fc083bca74acbed67f10b4308293368106
[openwrt/staging/noltari.git] / target / linux / generic / pending-5.15 / 487-mtd-spinand-Add-support-for-Etron-EM73D044VCx.patch
1 diff --git a/drivers/mtd/nand/spi/Makefile b/drivers/mtd/nand/spi/Makefile
2 index 9c64d9fc..5f99ea72 100644
3 --- a/drivers/mtd/nand/spi/Makefile
4 +++ b/drivers/mtd/nand/spi/Makefile
5 @@ -1,3 +1,3 @@
6 # SPDX-License-Identifier: GPL-2.0
7 -spinand-objs := core.o esmt.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.o xtx.o
8 +spinand-objs := core.o esmt.o etron.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.o xtx.o
9 obj-$(CONFIG_MTD_SPI_NAND) += spinand.o
10 diff --git a/drivers/mtd/nand/spi/core.c b/drivers/mtd/nand/spi/core.c
11 index 9839ee44..9ab44217 100644
12 --- a/drivers/mtd/nand/spi/core.c
13 +++ b/drivers/mtd/nand/spi/core.c
14 @@ -898,6 +898,7 @@ static const struct nand_ops spinand_ops = {
15 static const struct spinand_manufacturer *spinand_manufacturers[] = {
16 &esmt_c8_spinand_manufacturer,
17 &gigadevice_spinand_manufacturer,
18 + &etron_spinand_manufacturer,
19 &macronix_spinand_manufacturer,
20 &micron_spinand_manufacturer,
21 &paragon_spinand_manufacturer,
22 diff --git a/drivers/mtd/nand/spi/etron.c b/drivers/mtd/nand/spi/etron.c
23 new file mode 100644
24 index 00000000..653092be
25 --- /dev/null
26 +++ b/drivers/mtd/nand/spi/etron.c
27 @@ -0,0 +1,98 @@
28 +// SPDX-License-Identifier: GPL-2.0
29 +
30 +#include <linux/device.h>
31 +#include <linux/kernel.h>
32 +#include <linux/mtd/spinand.h>
33 +
34 +#define SPINAND_MFR_ETRON 0xd5
35 +
36 +
37 +static SPINAND_OP_VARIANTS(read_cache_variants,
38 + SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
39 + SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
40 + SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
41 + SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
42 + SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
43 + SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
44 +
45 +static SPINAND_OP_VARIANTS(write_cache_variants,
46 + SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
47 + SPINAND_PROG_LOAD(true, 0, NULL, 0));
48 +
49 +static SPINAND_OP_VARIANTS(update_cache_variants,
50 + SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
51 + SPINAND_PROG_LOAD(false, 0, NULL, 0));
52 +
53 +static int etron_ooblayout_ecc(struct mtd_info *mtd, int section,
54 + struct mtd_oob_region *oobregion)
55 +{
56 + if (section)
57 + return -ERANGE;
58 +
59 + oobregion->offset = 72;
60 + oobregion->length = 56;
61 +
62 + return 0;
63 +}
64 +
65 +static int etron_ooblayout_free(struct mtd_info *mtd, int section,
66 + struct mtd_oob_region *oobregion)
67 +{
68 + if (section)
69 + return -ERANGE;
70 +
71 + oobregion->offset = 1;
72 + oobregion->length = 71;
73 +
74 + return 0;
75 +}
76 +
77 +static int etron_ecc_get_status(struct spinand_device *spinand, u8 status)
78 +{
79 + switch (status & STATUS_ECC_MASK) {
80 + case STATUS_ECC_NO_BITFLIPS:
81 + return 0;
82 +
83 + case STATUS_ECC_HAS_BITFLIPS:
84 + /* Between 1-7 bitflips were corrected */
85 + return 7;
86 +
87 + case STATUS_ECC_MASK:
88 + /* Maximum bitflips were corrected */
89 + return 8;
90 +
91 + case STATUS_ECC_UNCOR_ERROR:
92 + return -EBADMSG;
93 + }
94 +
95 + return -EINVAL;
96 +}
97 +
98 +static const struct mtd_ooblayout_ops etron_ooblayout = {
99 + .ecc = etron_ooblayout_ecc,
100 + .free = etron_ooblayout_free,
101 +};
102 +
103 +static const struct spinand_info etron_spinand_table[] = {
104 + SPINAND_INFO("EM73D044VCx",
105 + SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0x1f),
106 + // bpc, pagesize, oobsize, pagesperblock, bperlun, maxbadplun, ppl, lpt, #t
107 + NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
108 + NAND_ECCREQ(8, 512),
109 + SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
110 + &write_cache_variants,
111 + &update_cache_variants),
112 + SPINAND_HAS_QE_BIT,
113 + SPINAND_ECCINFO(&etron_ooblayout, etron_ecc_get_status)),
114 +};
115 +
116 +static const struct spinand_manufacturer_ops etron_spinand_manuf_ops = {
117 +};
118 +
119 +const struct spinand_manufacturer etron_spinand_manufacturer = {
120 + .id = SPINAND_MFR_ETRON,
121 + .name = "Etron",
122 + .chips = etron_spinand_table,
123 + .nchips = ARRAY_SIZE(etron_spinand_table),
124 + .ops = &etron_spinand_manuf_ops,
125 +};
126 diff --git a/include/linux/mtd/spinand.h b/include/linux/mtd/spinand.h
127 index 2066962d..11d38d2f 100644
128 --- a/include/linux/mtd/spinand.h
129 +++ b/include/linux/mtd/spinand.h
130 @@ -261,6 +261,7 @@ struct spinand_manufacturer {
131
132 /* SPI NAND manufacturers */
133 extern const struct spinand_manufacturer esmt_c8_spinand_manufacturer;
134 +extern const struct spinand_manufacturer etron_spinand_manufacturer;
135 extern const struct spinand_manufacturer gigadevice_spinand_manufacturer;
136 extern const struct spinand_manufacturer macronix_spinand_manufacturer;
137 extern const struct spinand_manufacturer micron_spinand_manufacturer;