288fdbf267b5a2a06de4218c752152d3c354d8e1
[openwrt/staging/thess.git] /
1 From 10949af1681d5bb5cdbcc012815c6e40eec17d02 Mon Sep 17 00:00:00 2001
2 From: Schrempf Frieder <frieder.schrempf@kontron.De>
3 Date: Thu, 8 Nov 2018 08:32:11 +0000
4 Subject: [PATCH 2/8] mtd: spinand: Add initial support for Toshiba TC58CVG2S0H
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add minimal support for the Toshiba TC58CVG2S0H SPI NAND chip.
10
11 Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
12 Acked-by: Clément Péron <peron.clem@gmail.com>
13 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
14 ---
15 drivers/mtd/nand/spi/Makefile | 2 +-
16 drivers/mtd/nand/spi/core.c | 1 +
17 drivers/mtd/nand/spi/toshiba.c | 137 +++++++++++++++++++++++++++++++++
18 include/linux/mtd/spinand.h | 1 +
19 4 files changed, 140 insertions(+), 1 deletion(-)
20 create mode 100644 drivers/mtd/nand/spi/toshiba.c
21
22 --- a/drivers/mtd/nand/spi/Makefile
23 +++ b/drivers/mtd/nand/spi/Makefile
24 @@ -1,3 +1,3 @@
25 # SPDX-License-Identifier: GPL-2.0
26 -spinand-objs := core.o macronix.o micron.o winbond.o
27 +spinand-objs := core.o macronix.o micron.o toshiba.o winbond.o
28 obj-$(CONFIG_MTD_SPI_NAND) += spinand.o
29 --- a/drivers/mtd/nand/spi/core.c
30 +++ b/drivers/mtd/nand/spi/core.c
31 @@ -762,6 +762,7 @@ static const struct nand_ops spinand_ops
32 static const struct spinand_manufacturer *spinand_manufacturers[] = {
33 &macronix_spinand_manufacturer,
34 &micron_spinand_manufacturer,
35 + &toshiba_spinand_manufacturer,
36 &winbond_spinand_manufacturer,
37 };
38
39 --- /dev/null
40 +++ b/drivers/mtd/nand/spi/toshiba.c
41 @@ -0,0 +1,137 @@
42 +// SPDX-License-Identifier: GPL-2.0
43 +/*
44 + * Copyright (c) 2018 exceet electronics GmbH
45 + * Copyright (c) 2018 Kontron Electronics GmbH
46 + *
47 + * Author: Frieder Schrempf <frieder.schrempf@kontron.de>
48 + */
49 +
50 +#include <linux/device.h>
51 +#include <linux/kernel.h>
52 +#include <linux/mtd/spinand.h>
53 +
54 +#define SPINAND_MFR_TOSHIBA 0x98
55 +#define TOSH_STATUS_ECC_HAS_BITFLIPS_T (3 << 4)
56 +
57 +static SPINAND_OP_VARIANTS(read_cache_variants,
58 + SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
59 + SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
60 + SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
61 + SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
62 +
63 +static SPINAND_OP_VARIANTS(write_cache_variants,
64 + SPINAND_PROG_LOAD(true, 0, NULL, 0));
65 +
66 +static SPINAND_OP_VARIANTS(update_cache_variants,
67 + SPINAND_PROG_LOAD(false, 0, NULL, 0));
68 +
69 +static int tc58cvg2s0h_ooblayout_ecc(struct mtd_info *mtd, int section,
70 + struct mtd_oob_region *region)
71 +{
72 + if (section > 7)
73 + return -ERANGE;
74 +
75 + region->offset = 128 + 16 * section;
76 + region->length = 16;
77 +
78 + return 0;
79 +}
80 +
81 +static int tc58cvg2s0h_ooblayout_free(struct mtd_info *mtd, int section,
82 + struct mtd_oob_region *region)
83 +{
84 + if (section > 0)
85 + return -ERANGE;
86 +
87 + /* 2 bytes reserved for BBM */
88 + region->offset = 2;
89 + region->length = 126;
90 +
91 + return 0;
92 +}
93 +
94 +static const struct mtd_ooblayout_ops tc58cvg2s0h_ooblayout = {
95 + .ecc = tc58cvg2s0h_ooblayout_ecc,
96 + .free = tc58cvg2s0h_ooblayout_free,
97 +};
98 +
99 +static int tc58cvg2s0h_ecc_get_status(struct spinand_device *spinand,
100 + u8 status)
101 +{
102 + struct nand_device *nand = spinand_to_nand(spinand);
103 + u8 mbf = 0;
104 + struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &mbf);
105 +
106 + switch (status & STATUS_ECC_MASK) {
107 + case STATUS_ECC_NO_BITFLIPS:
108 + return 0;
109 +
110 + case STATUS_ECC_UNCOR_ERROR:
111 + return -EBADMSG;
112 +
113 + case STATUS_ECC_HAS_BITFLIPS:
114 + case TOSH_STATUS_ECC_HAS_BITFLIPS_T:
115 + /*
116 + * Let's try to retrieve the real maximum number of bitflips
117 + * in order to avoid forcing the wear-leveling layer to move
118 + * data around if it's not necessary.
119 + */
120 + if (spi_mem_exec_op(spinand->spimem, &op))
121 + return nand->eccreq.strength;
122 +
123 + mbf >>= 4;
124 +
125 + if (WARN_ON(mbf > nand->eccreq.strength || !mbf))
126 + return nand->eccreq.strength;
127 +
128 + return mbf;
129 +
130 + default:
131 + break;
132 + }
133 +
134 + return -EINVAL;
135 +}
136 +
137 +static const struct spinand_info toshiba_spinand_table[] = {
138 + SPINAND_INFO("TC58CVG2S0H", 0xCD,
139 + NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
140 + NAND_ECCREQ(8, 512),
141 + SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
142 + &write_cache_variants,
143 + &update_cache_variants),
144 + SPINAND_HAS_QE_BIT,
145 + SPINAND_ECCINFO(&tc58cvg2s0h_ooblayout,
146 + tc58cvg2s0h_ecc_get_status)),
147 +};
148 +
149 +static int toshiba_spinand_detect(struct spinand_device *spinand)
150 +{
151 + u8 *id = spinand->id.data;
152 + int ret;
153 +
154 + /*
155 + * Toshiba SPI NAND read ID needs a dummy byte,
156 + * so the first byte in id is garbage.
157 + */
158 + if (id[1] != SPINAND_MFR_TOSHIBA)
159 + return 0;
160 +
161 + ret = spinand_match_and_init(spinand, toshiba_spinand_table,
162 + ARRAY_SIZE(toshiba_spinand_table),
163 + id[2]);
164 + if (ret)
165 + return ret;
166 +
167 + return 1;
168 +}
169 +
170 +static const struct spinand_manufacturer_ops toshiba_spinand_manuf_ops = {
171 + .detect = toshiba_spinand_detect,
172 +};
173 +
174 +const struct spinand_manufacturer toshiba_spinand_manufacturer = {
175 + .id = SPINAND_MFR_TOSHIBA,
176 + .name = "Toshiba",
177 + .ops = &toshiba_spinand_manuf_ops,
178 +};
179 --- a/include/linux/mtd/spinand.h
180 +++ b/include/linux/mtd/spinand.h
181 @@ -196,6 +196,7 @@ struct spinand_manufacturer {
182 /* SPI NAND manufacturers */
183 extern const struct spinand_manufacturer macronix_spinand_manufacturer;
184 extern const struct spinand_manufacturer micron_spinand_manufacturer;
185 +extern const struct spinand_manufacturer toshiba_spinand_manufacturer;
186 extern const struct spinand_manufacturer winbond_spinand_manufacturer;
187
188 /**