kernel: bump 4.19 to 4.19.122
[oweals/openwrt.git] / target / linux / generic / backport-4.19 / 463-v5.3-mtd-spinand-Add-initial-support-for-Paragon-PN26G0xA.patch
1 From 3552691616c940a7c4125c2678ba816653cd725e Mon Sep 17 00:00:00 2001
2 From: Jeff Kletsky <git-commits@allycomm.com>
3 Date: Tue, 18 Jun 2019 10:08:05 -0700
4 Subject: [PATCH] mtd: spinand: Add initial support for Paragon PN26G0xA
5
6 Add initial support for Paragon Technology
7 PN26G01Axxxxx and PN26G02Axxxxx SPI NAND
8
9 Datasheets available at
10 http://www.xtxtech.com/upfile/2016082517274590.pdf
11 http://www.xtxtech.com/upfile/2016082517282329.pdf
12
13 Signed-off-by: Jeff Kletsky <git-commits@allycomm.com>
14 Reviewed-by: Frieder Schrempf <frieder.schrempf@kontron.de>
15 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
16
17 ADOPTED FROM UPSTREAM   due to upstream commit 377e517b5fa5 in Linux 5.2
18                         which added another parameter to NAND_MEMORG
19 ---
20  drivers/mtd/nand/spi/Makefile  |   2 +-
21  drivers/mtd/nand/spi/core.c    |   1 +
22  drivers/mtd/nand/spi/paragon.c | 147 +++++++++++++++++++++++++++++++++
23  include/linux/mtd/spinand.h    |   1 +
24  4 files changed, 150 insertions(+), 1 deletion(-)
25  create mode 100644 drivers/mtd/nand/spi/paragon.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 toshiba.o winbond.o
32 +spinand-objs := core.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.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 @@ -763,6 +763,7 @@ static const struct spinand_manufacturer
37         &gigadevice_spinand_manufacturer,
38         &macronix_spinand_manufacturer,
39         &micron_spinand_manufacturer,
40 +       &paragon_spinand_manufacturer,
41         &toshiba_spinand_manufacturer,
42         &winbond_spinand_manufacturer,
43  };
44 --- /dev/null
45 +++ b/drivers/mtd/nand/spi/paragon.c
46 @@ -0,0 +1,147 @@
47 +// SPDX-License-Identifier: GPL-2.0
48 +/*
49 + * Copyright (C) 2019 Jeff Kletsky
50 + *
51 + * Author: Jeff Kletsky <git-commits@allycomm.com>
52 + */
53 +
54 +#include <linux/device.h>
55 +#include <linux/kernel.h>
56 +#include <linux/mtd/spinand.h>
57 +
58 +
59 +#define SPINAND_MFR_PARAGON    0xa1
60 +
61 +
62 +#define PN26G0XA_STATUS_ECC_BITMASK            (3 << 4)
63 +
64 +#define PN26G0XA_STATUS_ECC_NONE_DETECTED      (0 << 4)
65 +#define PN26G0XA_STATUS_ECC_1_7_CORRECTED      (1 << 4)
66 +#define PN26G0XA_STATUS_ECC_ERRORED            (2 << 4)
67 +#define PN26G0XA_STATUS_ECC_8_CORRECTED                (3 << 4)
68 +
69 +
70 +static SPINAND_OP_VARIANTS(read_cache_variants,
71 +               SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
72 +               SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
73 +               SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
74 +               SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
75 +               SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
76 +               SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
77 +
78 +static SPINAND_OP_VARIANTS(write_cache_variants,
79 +               SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
80 +               SPINAND_PROG_LOAD(true, 0, NULL, 0));
81 +
82 +static SPINAND_OP_VARIANTS(update_cache_variants,
83 +               SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
84 +               SPINAND_PROG_LOAD(false, 0, NULL, 0));
85 +
86 +
87 +static int pn26g0xa_ooblayout_ecc(struct mtd_info *mtd, int section,
88 +                                  struct mtd_oob_region *region)
89 +{
90 +       if (section > 3)
91 +               return -ERANGE;
92 +
93 +       region->offset = 6 + (15 * section); /* 4 BBM + 2 user bytes */
94 +       region->length = 13;
95 +
96 +       return 0;
97 +}
98 +
99 +static int pn26g0xa_ooblayout_free(struct mtd_info *mtd, int section,
100 +                                  struct mtd_oob_region *region)
101 +{
102 +       if (section > 4)
103 +               return -ERANGE;
104 +
105 +       if (section == 4) {
106 +               region->offset = 64;
107 +               region->length = 64;
108 +       } else {
109 +               region->offset = 4 + (15 * section);
110 +               region->length = 2;
111 +       }
112 +
113 +       return 0;
114 +}
115 +
116 +static int pn26g0xa_ecc_get_status(struct spinand_device *spinand,
117 +                                  u8 status)
118 +{
119 +       switch (status & PN26G0XA_STATUS_ECC_BITMASK) {
120 +       case PN26G0XA_STATUS_ECC_NONE_DETECTED:
121 +               return 0;
122 +
123 +       case PN26G0XA_STATUS_ECC_1_7_CORRECTED:
124 +               return 7;       /* Return upper limit by convention */
125 +
126 +       case PN26G0XA_STATUS_ECC_8_CORRECTED:
127 +               return 8;
128 +
129 +       case PN26G0XA_STATUS_ECC_ERRORED:
130 +               return -EBADMSG;
131 +
132 +       default:
133 +               break;
134 +       }
135 +
136 +       return -EINVAL;
137 +}
138 +
139 +static const struct mtd_ooblayout_ops pn26g0xa_ooblayout = {
140 +       .ecc = pn26g0xa_ooblayout_ecc,
141 +       .free = pn26g0xa_ooblayout_free,
142 +};
143 +
144 +
145 +static const struct spinand_info paragon_spinand_table[] = {
146 +       SPINAND_INFO("PN26G01A", 0xe1,
147 +                    NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
148 +                    NAND_ECCREQ(8, 512),
149 +                    SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
150 +                                             &write_cache_variants,
151 +                                             &update_cache_variants),
152 +                    0,
153 +                    SPINAND_ECCINFO(&pn26g0xa_ooblayout,
154 +                                    pn26g0xa_ecc_get_status)),
155 +       SPINAND_INFO("PN26G02A", 0xe2,
156 +                    NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
157 +                    NAND_ECCREQ(8, 512),
158 +                    SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
159 +                                             &write_cache_variants,
160 +                                             &update_cache_variants),
161 +                    0,
162 +                    SPINAND_ECCINFO(&pn26g0xa_ooblayout,
163 +                                    pn26g0xa_ecc_get_status)),
164 +};
165 +
166 +static int paragon_spinand_detect(struct spinand_device *spinand)
167 +{
168 +       u8 *id = spinand->id.data;
169 +       int ret;
170 +
171 +       /* Read ID returns [0][MID][DID] */
172 +
173 +       if (id[1] != SPINAND_MFR_PARAGON)
174 +               return 0;
175 +
176 +       ret = spinand_match_and_init(spinand, paragon_spinand_table,
177 +                                    ARRAY_SIZE(paragon_spinand_table),
178 +                                    id[2]);
179 +       if (ret)
180 +               return ret;
181 +
182 +       return 1;
183 +}
184 +
185 +static const struct spinand_manufacturer_ops paragon_spinand_manuf_ops = {
186 +       .detect = paragon_spinand_detect,
187 +};
188 +
189 +const struct spinand_manufacturer paragon_spinand_manufacturer = {
190 +       .id = SPINAND_MFR_PARAGON,
191 +       .name = "Paragon",
192 +       .ops = &paragon_spinand_manuf_ops,
193 +};
194 --- a/include/linux/mtd/spinand.h
195 +++ b/include/linux/mtd/spinand.h
196 @@ -227,6 +227,7 @@ struct spinand_manufacturer {
197  extern const struct spinand_manufacturer gigadevice_spinand_manufacturer;
198  extern const struct spinand_manufacturer macronix_spinand_manufacturer;
199  extern const struct spinand_manufacturer micron_spinand_manufacturer;
200 +extern const struct spinand_manufacturer paragon_spinand_manufacturer;
201  extern const struct spinand_manufacturer toshiba_spinand_manufacturer;
202  extern const struct spinand_manufacturer winbond_spinand_manufacturer;
203