blob: c4beefa61764272adfa9a0a681fa35c4c454a350 [file] [log] [blame]
Robert Marko24cb4092020-03-03 20:25:40 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018 exceet electronics GmbH
4 * Copyright (c) 2018 Kontron Electronics GmbH
5 *
6 * Author: Frieder Schrempf <frieder.schrempf@kontron.de>
7 */
8
9#ifndef __UBOOT__
10#include <malloc.h>
11#include <linux/device.h>
12#include <linux/kernel.h>
13#endif
Simon Glassc06c1be2020-05-10 11:40:08 -060014#include <linux/bug.h>
Robert Marko24cb4092020-03-03 20:25:40 +010015#include <linux/mtd/spinand.h>
16
17#define SPINAND_MFR_TOSHIBA 0x98
18#define TOSH_STATUS_ECC_HAS_BITFLIPS_T (3 << 4)
19
20static SPINAND_OP_VARIANTS(read_cache_variants,
21 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
22 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
23 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
24 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
25
26static SPINAND_OP_VARIANTS(write_cache_variants,
27 SPINAND_PROG_LOAD(true, 0, NULL, 0));
28
29static SPINAND_OP_VARIANTS(update_cache_variants,
30 SPINAND_PROG_LOAD(false, 0, NULL, 0));
31
32static int tc58cxgxsx_ooblayout_ecc(struct mtd_info *mtd, int section,
33 struct mtd_oob_region *region)
34{
35 if (section > 0)
36 return -ERANGE;
37
38 region->offset = mtd->oobsize / 2;
39 region->length = mtd->oobsize / 2;
40
41 return 0;
42}
43
44static int tc58cxgxsx_ooblayout_free(struct mtd_info *mtd, int section,
45 struct mtd_oob_region *region)
46{
47 if (section > 0)
48 return -ERANGE;
49
50 /* 2 bytes reserved for BBM */
51 region->offset = 2;
52 region->length = (mtd->oobsize / 2) - 2;
53
54 return 0;
55}
56
57static const struct mtd_ooblayout_ops tc58cxgxsx_ooblayout = {
58 .ecc = tc58cxgxsx_ooblayout_ecc,
59 .rfree = tc58cxgxsx_ooblayout_free,
60};
61
62static int tc58cxgxsx_ecc_get_status(struct spinand_device *spinand,
63 u8 status)
64{
65 struct nand_device *nand = spinand_to_nand(spinand);
66 u8 mbf = 0;
67 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &mbf);
68
69 switch (status & STATUS_ECC_MASK) {
70 case STATUS_ECC_NO_BITFLIPS:
71 return 0;
72
73 case STATUS_ECC_UNCOR_ERROR:
74 return -EBADMSG;
75
76 case STATUS_ECC_HAS_BITFLIPS:
77 case TOSH_STATUS_ECC_HAS_BITFLIPS_T:
78 /*
79 * Let's try to retrieve the real maximum number of bitflips
80 * in order to avoid forcing the wear-leveling layer to move
81 * data around if it's not necessary.
82 */
83 if (spi_mem_exec_op(spinand->slave, &op))
84 return nand->eccreq.strength;
85
86 mbf >>= 4;
87
88 if (WARN_ON(mbf > nand->eccreq.strength || !mbf))
89 return nand->eccreq.strength;
90
91 return mbf;
92
93 default:
94 break;
95 }
96
97 return -EINVAL;
98}
99
100static const struct spinand_info toshiba_spinand_table[] = {
101 /* 3.3V 1Gb */
102 SPINAND_INFO("TC58CVG0S3", 0xC2,
103 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
104 NAND_ECCREQ(8, 512),
105 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
106 &write_cache_variants,
107 &update_cache_variants),
108 0,
109 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
110 tc58cxgxsx_ecc_get_status)),
111 /* 3.3V 2Gb */
112 SPINAND_INFO("TC58CVG1S3", 0xCB,
113 NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
114 NAND_ECCREQ(8, 512),
115 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
116 &write_cache_variants,
117 &update_cache_variants),
118 0,
119 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
120 tc58cxgxsx_ecc_get_status)),
121 /* 3.3V 4Gb */
122 SPINAND_INFO("TC58CVG2S0", 0xCD,
123 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
124 NAND_ECCREQ(8, 512),
125 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
126 &write_cache_variants,
127 &update_cache_variants),
128 0,
129 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
130 tc58cxgxsx_ecc_get_status)),
Robert Marko872b9ce2020-03-03 20:25:42 +0100131 /* 3.3V 4Gb */
132 SPINAND_INFO("TC58CVG2S0", 0xED,
133 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
134 NAND_ECCREQ(8, 512),
135 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
136 &write_cache_variants,
137 &update_cache_variants),
138 0,
139 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
140 tc58cxgxsx_ecc_get_status)),
Robert Marko24cb4092020-03-03 20:25:40 +0100141 /* 1.8V 1Gb */
142 SPINAND_INFO("TC58CYG0S3", 0xB2,
143 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
144 NAND_ECCREQ(8, 512),
145 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
146 &write_cache_variants,
147 &update_cache_variants),
148 0,
149 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
150 tc58cxgxsx_ecc_get_status)),
151 /* 1.8V 2Gb */
152 SPINAND_INFO("TC58CYG1S3", 0xBB,
153 NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
154 NAND_ECCREQ(8, 512),
155 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
156 &write_cache_variants,
157 &update_cache_variants),
158 0,
159 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
160 tc58cxgxsx_ecc_get_status)),
161 /* 1.8V 4Gb */
162 SPINAND_INFO("TC58CYG2S0", 0xBD,
163 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
164 NAND_ECCREQ(8, 512),
165 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
166 &write_cache_variants,
167 &update_cache_variants),
168 0,
169 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
170 tc58cxgxsx_ecc_get_status)),
171};
172
173static int toshiba_spinand_detect(struct spinand_device *spinand)
174{
175 u8 *id = spinand->id.data;
176 int ret;
177
178 /*
179 * Toshiba SPI NAND read ID needs a dummy byte,
180 * so the first byte in id is garbage.
181 */
182 if (id[1] != SPINAND_MFR_TOSHIBA)
183 return 0;
184
185 ret = spinand_match_and_init(spinand, toshiba_spinand_table,
186 ARRAY_SIZE(toshiba_spinand_table),
187 id[2]);
188 if (ret)
189 return ret;
190
191 return 1;
192}
193
194static const struct spinand_manufacturer_ops toshiba_spinand_manuf_ops = {
195 .detect = toshiba_spinand_detect,
196};
197
198const struct spinand_manufacturer toshiba_spinand_manufacturer = {
199 .id = SPINAND_MFR_TOSHIBA,
200 .name = "Toshiba",
201 .ops = &toshiba_spinand_manuf_ops,
202};