1
2
3
4
5
6
7
8
9#include "internals.h"
10
11
12#define TOSHIBA_NAND_ID4_IS_BENAND BIT(7)
13
14
15#define TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED BIT(3)
16
17
18#define TOSHIBA_NAND_CMD_ECC_STATUS_READ 0x7A
19
20
21#define TOSHIBA_NAND_ECC_STATUS_MASK 0x0F
22
23
24#define TOSHIBA_NAND_ECC_STATUS_UNCORR 0x0F
25
26
27#define TOSHIBA_NAND_MAX_ECC_STEPS 8
28
29static int toshiba_nand_benand_read_eccstatus_op(struct nand_chip *chip,
30 u8 *buf)
31{
32 u8 *ecc_status = buf;
33
34 if (nand_has_exec_op(chip)) {
35 const struct nand_sdr_timings *sdr =
36 nand_get_sdr_timings(&chip->data_interface);
37 struct nand_op_instr instrs[] = {
38 NAND_OP_CMD(TOSHIBA_NAND_CMD_ECC_STATUS_READ,
39 PSEC_TO_NSEC(sdr->tADL_min)),
40 NAND_OP_8BIT_DATA_IN(chip->ecc.steps, ecc_status, 0),
41 };
42 struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
43
44 return nand_exec_op(chip, &op);
45 }
46
47 return -ENOTSUPP;
48}
49
50static int toshiba_nand_benand_eccstatus(struct nand_chip *chip)
51{
52 struct mtd_info *mtd = nand_to_mtd(chip);
53 int ret;
54 unsigned int max_bitflips = 0;
55 u8 status, ecc_status[TOSHIBA_NAND_MAX_ECC_STEPS];
56
57
58 ret = toshiba_nand_benand_read_eccstatus_op(chip, ecc_status);
59 if (!ret) {
60 unsigned int i, bitflips = 0;
61
62 for (i = 0; i < chip->ecc.steps; i++) {
63 bitflips = ecc_status[i] & TOSHIBA_NAND_ECC_STATUS_MASK;
64 if (bitflips == TOSHIBA_NAND_ECC_STATUS_UNCORR) {
65 mtd->ecc_stats.failed++;
66 } else {
67 mtd->ecc_stats.corrected += bitflips;
68 max_bitflips = max(max_bitflips, bitflips);
69 }
70 }
71
72 return max_bitflips;
73 }
74
75
76
77
78
79 ret = nand_status_op(chip, &status);
80 if (ret)
81 return ret;
82
83 if (status & NAND_STATUS_FAIL) {
84
85 mtd->ecc_stats.failed++;
86 } else if (status & TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED) {
87
88 max_bitflips = mtd->bitflip_threshold;
89 mtd->ecc_stats.corrected += max_bitflips;
90 }
91
92 return max_bitflips;
93}
94
95static int
96toshiba_nand_read_page_benand(struct nand_chip *chip, uint8_t *buf,
97 int oob_required, int page)
98{
99 int ret;
100
101 ret = nand_read_page_raw(chip, buf, oob_required, page);
102 if (ret)
103 return ret;
104
105 return toshiba_nand_benand_eccstatus(chip);
106}
107
108static int
109toshiba_nand_read_subpage_benand(struct nand_chip *chip, uint32_t data_offs,
110 uint32_t readlen, uint8_t *bufpoi, int page)
111{
112 int ret;
113
114 ret = nand_read_page_op(chip, page, data_offs,
115 bufpoi + data_offs, readlen);
116 if (ret)
117 return ret;
118
119 return toshiba_nand_benand_eccstatus(chip);
120}
121
122static void toshiba_nand_benand_init(struct nand_chip *chip)
123{
124 struct mtd_info *mtd = nand_to_mtd(chip);
125
126
127
128
129
130
131
132 chip->ecc.bytes = 0;
133 chip->ecc.size = 512;
134 chip->ecc.strength = 8;
135 chip->ecc.read_page = toshiba_nand_read_page_benand;
136 chip->ecc.read_subpage = toshiba_nand_read_subpage_benand;
137 chip->ecc.write_page = nand_write_page_raw;
138 chip->ecc.read_page_raw = nand_read_page_raw_notsupp;
139 chip->ecc.write_page_raw = nand_write_page_raw_notsupp;
140
141 chip->options |= NAND_SUBPAGE_READ;
142
143 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
144}
145
146static void toshiba_nand_decode_id(struct nand_chip *chip)
147{
148 struct mtd_info *mtd = nand_to_mtd(chip);
149 struct nand_memory_organization *memorg;
150
151 memorg = nanddev_get_memorg(&chip->base);
152
153 nand_decode_ext_id(chip);
154
155
156
157
158
159
160
161
162
163 if (chip->id.len >= 6 && nand_is_slc(chip) &&
164 (chip->id.data[5] & 0x7) == 0x6 &&
165 !(chip->id.data[4] & TOSHIBA_NAND_ID4_IS_BENAND) ) {
166 memorg->oobsize = 32 * memorg->pagesize >> 9;
167 mtd->oobsize = memorg->oobsize;
168 }
169
170
171
172
173
174
175
176
177 if (chip->id.len >= 6 && nand_is_slc(chip)) {
178 chip->base.eccreq.step_size = 512;
179 switch (chip->id.data[5] & 0x7) {
180 case 0x4:
181 chip->base.eccreq.strength = 1;
182 break;
183 case 0x5:
184 chip->base.eccreq.strength = 4;
185 break;
186 case 0x6:
187 chip->base.eccreq.strength = 8;
188 break;
189 default:
190 WARN(1, "Could not get ECC info");
191 chip->base.eccreq.step_size = 0;
192 break;
193 }
194 }
195}
196
197static int tc58teg5dclta00_init(struct nand_chip *chip)
198{
199 struct mtd_info *mtd = nand_to_mtd(chip);
200
201 chip->onfi_timing_mode_default = 5;
202 chip->options |= NAND_NEED_SCRAMBLING;
203 mtd_set_pairing_scheme(mtd, &dist3_pairing_scheme);
204
205 return 0;
206}
207
208static int toshiba_nand_init(struct nand_chip *chip)
209{
210 if (nand_is_slc(chip))
211 chip->options |= NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE;
212
213
214 if (nand_is_slc(chip) && chip->ecc.mode == NAND_ECC_ON_DIE &&
215 chip->id.data[4] & TOSHIBA_NAND_ID4_IS_BENAND)
216 toshiba_nand_benand_init(chip);
217
218 if (!strcmp("TC58TEG5DCLTA00", chip->parameters.model))
219 tc58teg5dclta00_init(chip);
220
221 return 0;
222}
223
224const struct nand_manufacturer_ops toshiba_nand_manuf_ops = {
225 .detect = toshiba_nand_decode_id,
226 .init = toshiba_nand_init,
227};
228