blob: 0af45b134c0cf859902f3d138b305bf5836d526d [file] [log] [blame]
Boris Brezillon10d4e752016-06-08 10:38:57 +02001/*
2 * Copyright (C) 2017 Free Electrons
3 * Copyright (C) 2017 NextThing Co
4 *
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Boris Brezillond4092d72017-08-04 17:29:10 +020018#include <linux/mtd/rawnand.h>
Boris Brezillon10d4e752016-06-08 10:38:57 +020019
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +020020/*
21 * Special Micron status bit that indicates when the block has been
22 * corrected by on-die ECC and should be rewritten
23 */
24#define NAND_STATUS_WRITE_RECOMMENDED BIT(3)
25
Boris Brezillon10d4e752016-06-08 10:38:57 +020026struct nand_onfi_vendor_micron {
27 u8 two_plane_read;
28 u8 read_cache;
29 u8 read_unique_id;
30 u8 dq_imped;
31 u8 dq_imped_num_settings;
32 u8 dq_imped_feat_addr;
33 u8 rb_pulldown_strength;
34 u8 rb_pulldown_strength_feat_addr;
35 u8 rb_pulldown_strength_num_settings;
36 u8 otp_mode;
37 u8 otp_page_start;
38 u8 otp_data_prot_addr;
39 u8 otp_num_pages;
40 u8 otp_feat_addr;
41 u8 read_retry_options;
42 u8 reserved[72];
43 u8 param_revision;
44} __packed;
45
46static int micron_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
47{
48 struct nand_chip *chip = mtd_to_nand(mtd);
49 u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
50
Miquel Raynal97baea12018-03-19 14:47:20 +010051 return nand_set_features(chip, ONFI_FEATURE_ADDR_READ_RETRY, feature);
Boris Brezillon10d4e752016-06-08 10:38:57 +020052}
53
54/*
55 * Configure chip properties from Micron vendor-specific ONFI table
56 */
57static int micron_nand_onfi_init(struct nand_chip *chip)
58{
Miquel Raynala97421c2018-03-19 14:47:27 +010059 struct nand_parameters *p = &chip->parameters;
60 struct nand_onfi_vendor_micron *micron = (void *)p->onfi.vendor;
Boris Brezillon10d4e752016-06-08 10:38:57 +020061
Miquel Raynala97421c2018-03-19 14:47:27 +010062 if (chip->parameters.onfi.version && p->onfi.vendor_revision) {
63 chip->read_retries = micron->read_retry_options;
64 chip->setup_read_retry = micron_nand_setup_read_retry;
65 }
Boris Brezillon10d4e752016-06-08 10:38:57 +020066
Miquel Raynal789157e2018-03-19 14:47:28 +010067 if (p->supports_set_get_features) {
68 set_bit(ONFI_FEATURE_ADDR_READ_RETRY, p->set_feature_list);
69 set_bit(ONFI_FEATURE_ADDR_READ_RETRY, p->get_feature_list);
70 }
Boris Brezillon10d4e752016-06-08 10:38:57 +020071
72 return 0;
73}
74
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +020075static int micron_nand_on_die_ooblayout_ecc(struct mtd_info *mtd, int section,
76 struct mtd_oob_region *oobregion)
77{
78 if (section >= 4)
79 return -ERANGE;
80
81 oobregion->offset = (section * 16) + 8;
82 oobregion->length = 8;
83
84 return 0;
85}
86
87static int micron_nand_on_die_ooblayout_free(struct mtd_info *mtd, int section,
88 struct mtd_oob_region *oobregion)
89{
90 if (section >= 4)
91 return -ERANGE;
92
93 oobregion->offset = (section * 16) + 2;
94 oobregion->length = 6;
95
96 return 0;
97}
98
99static const struct mtd_ooblayout_ops micron_nand_on_die_ooblayout_ops = {
100 .ecc = micron_nand_on_die_ooblayout_ecc,
101 .free = micron_nand_on_die_ooblayout_free,
102};
103
104static int micron_nand_on_die_ecc_setup(struct nand_chip *chip, bool enable)
105{
106 u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
107
108 if (enable)
109 feature[0] |= ONFI_FEATURE_ON_DIE_ECC_EN;
110
Miquel Raynal97baea12018-03-19 14:47:20 +0100111 return nand_set_features(chip, ONFI_FEATURE_ON_DIE_ECC, feature);
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200112}
113
114static int
115micron_nand_read_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
116 uint8_t *buf, int oob_required,
117 int page)
118{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100119 u8 status;
120 int ret, max_bitflips = 0;
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200121
Boris Brezillon97d90da2017-11-30 18:01:29 +0100122 ret = micron_nand_on_die_ecc_setup(chip, true);
123 if (ret)
124 return ret;
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200125
Boris Brezillon97d90da2017-11-30 18:01:29 +0100126 ret = nand_read_page_op(chip, page, 0, NULL, 0);
127 if (ret)
128 goto out;
129
130 ret = nand_status_op(chip, &status);
131 if (ret)
132 goto out;
133
134 ret = nand_exit_status_op(chip);
135 if (ret)
136 goto out;
137
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200138 if (status & NAND_STATUS_FAIL)
139 mtd->ecc_stats.failed++;
Boris Brezillon97d90da2017-11-30 18:01:29 +0100140
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200141 /*
142 * The internal ECC doesn't tell us the number of bitflips
143 * that have been corrected, but tells us if it recommends to
144 * rewrite the block. If it's the case, then we pretend we had
145 * a number of bitflips equal to the ECC strength, which will
146 * hint the NAND core to rewrite the block.
147 */
148 else if (status & NAND_STATUS_WRITE_RECOMMENDED)
149 max_bitflips = chip->ecc.strength;
150
Boris Brezillon25f815f2017-11-30 18:01:30 +0100151 ret = nand_read_data_op(chip, buf, mtd->writesize, false);
152 if (!ret && oob_required)
153 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
154 false);
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200155
Boris Brezillon97d90da2017-11-30 18:01:29 +0100156out:
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200157 micron_nand_on_die_ecc_setup(chip, false);
158
Boris Brezillon97d90da2017-11-30 18:01:29 +0100159 return ret ? ret : max_bitflips;
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200160}
161
162static int
163micron_nand_write_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
164 const uint8_t *buf, int oob_required,
165 int page)
166{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100167 int ret;
Boris Brezillon41145642017-05-16 18:27:49 +0200168
Boris Brezillon97d90da2017-11-30 18:01:29 +0100169 ret = micron_nand_on_die_ecc_setup(chip, true);
170 if (ret)
171 return ret;
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200172
Boris Brezillon97d90da2017-11-30 18:01:29 +0100173 ret = nand_write_page_raw(mtd, chip, buf, oob_required, page);
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200174 micron_nand_on_die_ecc_setup(chip, false);
175
Boris Brezillon97d90da2017-11-30 18:01:29 +0100176 return ret;
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200177}
178
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200179enum {
180 /* The NAND flash doesn't support on-die ECC */
181 MICRON_ON_DIE_UNSUPPORTED,
182
183 /*
184 * The NAND flash supports on-die ECC and it can be
185 * enabled/disabled by a set features command.
186 */
187 MICRON_ON_DIE_SUPPORTED,
188
189 /*
190 * The NAND flash supports on-die ECC, and it cannot be
191 * disabled.
192 */
193 MICRON_ON_DIE_MANDATORY,
194};
195
196/*
197 * Try to detect if the NAND support on-die ECC. To do this, we enable
198 * the feature, and read back if it has been enabled as expected. We
199 * also check if it can be disabled, because some Micron NANDs do not
200 * allow disabling the on-die ECC and we don't support such NANDs for
201 * now.
202 *
203 * This function also has the side effect of disabling on-die ECC if
204 * it had been left enabled by the firmware/bootloader.
205 */
206static int micron_supports_on_die_ecc(struct nand_chip *chip)
207{
208 u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
209 int ret;
210
Miquel Raynala97421c2018-03-19 14:47:27 +0100211 if (!chip->parameters.onfi.version)
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200212 return MICRON_ON_DIE_UNSUPPORTED;
213
214 if (chip->bits_per_cell != 1)
215 return MICRON_ON_DIE_UNSUPPORTED;
216
217 ret = micron_nand_on_die_ecc_setup(chip, true);
218 if (ret)
219 return MICRON_ON_DIE_UNSUPPORTED;
220
Miquel Raynal97baea12018-03-19 14:47:20 +0100221 ret = nand_get_features(chip, ONFI_FEATURE_ON_DIE_ECC, feature);
222 if (ret < 0)
223 return ret;
224
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200225 if ((feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) == 0)
226 return MICRON_ON_DIE_UNSUPPORTED;
227
228 ret = micron_nand_on_die_ecc_setup(chip, false);
229 if (ret)
230 return MICRON_ON_DIE_UNSUPPORTED;
231
Miquel Raynal97baea12018-03-19 14:47:20 +0100232 ret = nand_get_features(chip, ONFI_FEATURE_ON_DIE_ECC, feature);
233 if (ret < 0)
234 return ret;
235
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200236 if (feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN)
237 return MICRON_ON_DIE_MANDATORY;
238
239 /*
240 * Some Micron NANDs have an on-die ECC of 4/512, some other
241 * 8/512. We only support the former.
242 */
Miquel Raynala97421c2018-03-19 14:47:27 +0100243 if (chip->ecc_strength_ds != 4)
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200244 return MICRON_ON_DIE_UNSUPPORTED;
245
246 return MICRON_ON_DIE_SUPPORTED;
247}
248
Boris Brezillon10d4e752016-06-08 10:38:57 +0200249static int micron_nand_init(struct nand_chip *chip)
250{
251 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200252 int ondie;
Boris Brezillon10d4e752016-06-08 10:38:57 +0200253 int ret;
254
255 ret = micron_nand_onfi_init(chip);
256 if (ret)
257 return ret;
258
259 if (mtd->writesize == 2048)
260 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
261
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200262 ondie = micron_supports_on_die_ecc(chip);
263
264 if (ondie == MICRON_ON_DIE_MANDATORY) {
265 pr_err("On-die ECC forcefully enabled, not supported\n");
266 return -EINVAL;
267 }
268
269 if (chip->ecc.mode == NAND_ECC_ON_DIE) {
270 if (ondie == MICRON_ON_DIE_UNSUPPORTED) {
271 pr_err("On-die ECC selected but not supported\n");
272 return -EINVAL;
273 }
274
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200275 chip->ecc.bytes = 8;
276 chip->ecc.size = 512;
277 chip->ecc.strength = 4;
278 chip->ecc.algo = NAND_ECC_BCH;
279 chip->ecc.read_page = micron_nand_read_page_on_die_ecc;
280 chip->ecc.write_page = micron_nand_write_page_on_die_ecc;
Boris Brezillon25f815f2017-11-30 18:01:30 +0100281 chip->ecc.read_page_raw = nand_read_page_raw;
282 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Petazzoni9748e1d2017-04-29 11:06:45 +0200283
284 mtd_set_ooblayout(mtd, &micron_nand_on_die_ooblayout_ops);
285 }
286
Boris Brezillon10d4e752016-06-08 10:38:57 +0200287 return 0;
288}
289
290const struct nand_manufacturer_ops micron_nand_manuf_ops = {
291 .init = micron_nand_init,
292};