mediatek: Add support for Xiaomi Redmi Router AX6S
[openwrt/openwrt.git] / target / linux / generic / pending-5.4 / 483-mtd-spinand-add-support-for-xtx-xt26g0xa.patch
1 From 8ffe91b6b429277bd7bd79267ba836edf951dce2 Mon Sep 17 00:00:00 2001
2 From: Felix Matouschek <felix@matouschek.org>
3 Date: Fri, 2 Jul 2021 20:31:23 +0200
4 Subject: [PATCH] mtd: spinand: Add support for XTX XT26G0xA
5
6 Add support for XTX Technology XT26G01AXXXXX, XTX26G02AXXXXX and
7 XTX26G04AXXXXX SPI NAND.
8
9 These are 3V, 1G/2G/4Gbit serial SLC NAND flash devices with on-die ECC
10 (8bit strength per 512bytes).
11
12 Tested on Teltonika RUTX10 flashed with OpenWrt.
13
14 Datasheets available at
15 http://www.xtxtech.com/download/?AId=225
16 https://datasheet.lcsc.com/szlcsc/2005251034_XTX-XT26G01AWSEGA_C558841.pdf
17
18 Signed-off-by: Felix Matouschek <felix@matouschek.org>
19 ---
20 drivers/mtd/nand/spi/Makefile | 2 +-
21 drivers/mtd/nand/spi/core.c | 1 +
22 drivers/mtd/nand/spi/xtx.c | 139 ++++++++++++++++++++++++++++++++++
23 include/linux/mtd/spinand.h | 1 +
24 4 files changed, 142 insertions(+), 1 deletion(-)
25 create mode 100644 drivers/mtd/nand/spi/xtx.c
26
27 --- a/drivers/mtd/nand/spi/Makefile
28 +++ b/drivers/mtd/nand/spi/Makefile
29 @@ -1,3 +1,3 @@
30 # SPDX-License-Identifier: GPL-2.0
31 -spinand-objs := core.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.o
32 +spinand-objs := core.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.o xtx.o
33 obj-$(CONFIG_MTD_SPI_NAND) += spinand.o
34 --- a/drivers/mtd/nand/spi/core.c
35 +++ b/drivers/mtd/nand/spi/core.c
36 @@ -758,6 +758,7 @@ static const struct spinand_manufacturer
37 &paragon_spinand_manufacturer,
38 &toshiba_spinand_manufacturer,
39 &winbond_spinand_manufacturer,
40 + &xtx_spinand_manufacturer,
41 };
42
43 static int spinand_manufacturer_detect(struct spinand_device *spinand)
44 --- /dev/null
45 +++ b/drivers/mtd/nand/spi/xtx.c
46 @@ -0,0 +1,139 @@
47 +// SPDX-License-Identifier: GPL-2.0
48 +/*
49 + * Author:
50 + * Felix Matouschek <felix@matouschek.org>
51 + */
52 +
53 +#include <linux/device.h>
54 +#include <linux/kernel.h>
55 +#include <linux/mtd/spinand.h>
56 +
57 +#define SPINAND_MFR_XTX 0x0B
58 +
59 +#define XT26G0XA_STATUS_ECC_MASK GENMASK(5, 2)
60 +#define XT26G0XA_STATUS_ECC_NO_DETECTED (0 << 2)
61 +#define XT26G0XA_STATUS_ECC_8_CORRECTED (3 << 4)
62 +#define XT26G0XA_STATUS_ECC_UNCOR_ERROR (2 << 4)
63 +
64 +static SPINAND_OP_VARIANTS(read_cache_variants,
65 + SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
66 + SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
67 + SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
68 + SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
69 + SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
70 + SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
71 +
72 +static SPINAND_OP_VARIANTS(write_cache_variants,
73 + SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
74 + SPINAND_PROG_LOAD(true, 0, NULL, 0));
75 +
76 +static SPINAND_OP_VARIANTS(update_cache_variants,
77 + SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
78 + SPINAND_PROG_LOAD(false, 0, NULL, 0));
79 +
80 +static int xt26g0xa_ooblayout_ecc(struct mtd_info *mtd, int section,
81 + struct mtd_oob_region *region)
82 +{
83 + if (section)
84 + return -ERANGE;
85 +
86 + region->offset = 8;
87 + region->length = 40;
88 +
89 + return 0;
90 +}
91 +
92 +static int xt26g0xa_ooblayout_free(struct mtd_info *mtd, int section,
93 + struct mtd_oob_region *region)
94 +{
95 + if (section)
96 + return -ERANGE;
97 +
98 + region->offset = 1;
99 + region->length = 7;
100 +
101 + return 0;
102 +}
103 +
104 +static const struct mtd_ooblayout_ops xt26g0xa_ooblayout = {
105 + .ecc = xt26g0xa_ooblayout_ecc,
106 + .free = xt26g0xa_ooblayout_free,
107 +};
108 +
109 +static int xt26g0xa_ecc_get_status(struct spinand_device *spinand,
110 + u8 status)
111 +{
112 + switch (status & XT26G0XA_STATUS_ECC_MASK) {
113 + case XT26G0XA_STATUS_ECC_NO_DETECTED:
114 + return 0;
115 + case XT26G0XA_STATUS_ECC_8_CORRECTED:
116 + return 8;
117 + case XT26G0XA_STATUS_ECC_UNCOR_ERROR:
118 + return -EBADMSG;
119 + default: /* (1 << 2) through (7 << 2) are 1-7 corrected errors */
120 + return (status & XT26G0XA_STATUS_ECC_MASK) >> 2;
121 + }
122 +
123 + return -EINVAL;
124 +}
125 +
126 +static const struct spinand_info xtx_spinand_table[] = {
127 + SPINAND_INFO("XT26G01A", 0xE1,
128 + NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
129 + NAND_ECCREQ(8, 512),
130 + SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
131 + &write_cache_variants,
132 + &update_cache_variants),
133 + SPINAND_HAS_QE_BIT,
134 + SPINAND_ECCINFO(&xt26g0xa_ooblayout,
135 + xt26g0xa_ecc_get_status)),
136 + SPINAND_INFO("XT26G02A", 0xE2,
137 + NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
138 + NAND_ECCREQ(8, 512),
139 + SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
140 + &write_cache_variants,
141 + &update_cache_variants),
142 + SPINAND_HAS_QE_BIT,
143 + SPINAND_ECCINFO(&xt26g0xa_ooblayout,
144 + xt26g0xa_ecc_get_status)),
145 + SPINAND_INFO("XT26G04A", 0xE3,
146 + NAND_MEMORG(1, 2048, 64, 128, 2048, 40, 1, 1, 1),
147 + NAND_ECCREQ(8, 512),
148 + SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
149 + &write_cache_variants,
150 + &update_cache_variants),
151 + SPINAND_HAS_QE_BIT,
152 + SPINAND_ECCINFO(&xt26g0xa_ooblayout,
153 + xt26g0xa_ecc_get_status)),
154 +};
155 +
156 +static int xtx_spinand_detect(struct spinand_device *spinand)
157 +{
158 + u8 *id = spinand->id.data;
159 + int ret;
160 +
161 + /*
162 + * XTX SPI NAND read ID needs a dummy byte, so the first byte in
163 + * raw_id is garbage.
164 + */
165 + if (id[1] != SPINAND_MFR_XTX)
166 + return 0;
167 +
168 + ret = spinand_match_and_init(spinand, xtx_spinand_table,
169 + ARRAY_SIZE(xtx_spinand_table),
170 + id[2]);
171 + if (ret)
172 + return ret;
173 +
174 + return 1;
175 +}
176 +
177 +static const struct spinand_manufacturer_ops xtx_spinand_manuf_ops = {
178 + .detect = xtx_spinand_detect,
179 +};
180 +
181 +const struct spinand_manufacturer xtx_spinand_manufacturer = {
182 + .id = SPINAND_MFR_XTX,
183 + .name = "XTX",
184 + .ops = &xtx_spinand_manuf_ops,
185 +};
186 --- a/include/linux/mtd/spinand.h
187 +++ b/include/linux/mtd/spinand.h
188 @@ -230,6 +230,7 @@ extern const struct spinand_manufacturer
189 extern const struct spinand_manufacturer paragon_spinand_manufacturer;
190 extern const struct spinand_manufacturer toshiba_spinand_manufacturer;
191 extern const struct spinand_manufacturer winbond_spinand_manufacturer;
192 +extern const struct spinand_manufacturer xtx_spinand_manufacturer;
193
194 /**
195 * struct spinand_op_variants - SPI NAND operation variants