x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / drivers / mtd / nand / nand_micron.c
blobabf6a3c376e888de0b0cc43154fcd5afdd04576a
1 /*
2 * Copyright (C) 2017 Free Electrons
3 * Copyright (C) 2017 NextThing Co
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
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.
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.
18 #include <linux/mtd/rawnand.h>
21 * Special Micron status bit that indicates when the block has been
22 * corrected by on-die ECC and should be rewritten
24 #define NAND_STATUS_WRITE_RECOMMENDED BIT(3)
26 struct 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;
46 static int micron_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
48 struct nand_chip *chip = mtd_to_nand(mtd);
49 u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
51 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
52 feature);
56 * Configure chip properties from Micron vendor-specific ONFI table
58 static int micron_nand_onfi_init(struct nand_chip *chip)
60 struct nand_onfi_params *p = &chip->onfi_params;
61 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
63 if (!chip->onfi_version)
64 return 0;
66 if (le16_to_cpu(p->vendor_revision) < 1)
67 return 0;
69 chip->read_retries = micron->read_retry_options;
70 chip->setup_read_retry = micron_nand_setup_read_retry;
72 return 0;
75 static int micron_nand_on_die_ooblayout_ecc(struct mtd_info *mtd, int section,
76 struct mtd_oob_region *oobregion)
78 if (section >= 4)
79 return -ERANGE;
81 oobregion->offset = (section * 16) + 8;
82 oobregion->length = 8;
84 return 0;
87 static int micron_nand_on_die_ooblayout_free(struct mtd_info *mtd, int section,
88 struct mtd_oob_region *oobregion)
90 if (section >= 4)
91 return -ERANGE;
93 oobregion->offset = (section * 16) + 2;
94 oobregion->length = 6;
96 return 0;
99 static 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,
104 static int micron_nand_on_die_ecc_setup(struct nand_chip *chip, bool enable)
106 u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
108 if (enable)
109 feature[0] |= ONFI_FEATURE_ON_DIE_ECC_EN;
111 return chip->onfi_set_features(nand_to_mtd(chip), chip,
112 ONFI_FEATURE_ON_DIE_ECC, feature);
115 static int
116 micron_nand_read_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
117 uint8_t *buf, int oob_required,
118 int page)
120 int status;
121 int max_bitflips = 0;
123 micron_nand_on_die_ecc_setup(chip, true);
125 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
126 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
127 status = chip->read_byte(mtd);
128 if (status & NAND_STATUS_FAIL)
129 mtd->ecc_stats.failed++;
131 * The internal ECC doesn't tell us the number of bitflips
132 * that have been corrected, but tells us if it recommends to
133 * rewrite the block. If it's the case, then we pretend we had
134 * a number of bitflips equal to the ECC strength, which will
135 * hint the NAND core to rewrite the block.
137 else if (status & NAND_STATUS_WRITE_RECOMMENDED)
138 max_bitflips = chip->ecc.strength;
140 chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1);
142 nand_read_page_raw(mtd, chip, buf, oob_required, page);
144 micron_nand_on_die_ecc_setup(chip, false);
146 return max_bitflips;
149 static int
150 micron_nand_write_page_on_die_ecc(struct mtd_info *mtd, struct nand_chip *chip,
151 const uint8_t *buf, int oob_required,
152 int page)
154 int status;
156 micron_nand_on_die_ecc_setup(chip, true);
158 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
159 nand_write_page_raw(mtd, chip, buf, oob_required, page);
160 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
161 status = chip->waitfunc(mtd, chip);
163 micron_nand_on_die_ecc_setup(chip, false);
165 return status & NAND_STATUS_FAIL ? -EIO : 0;
168 static int
169 micron_nand_read_page_raw_on_die_ecc(struct mtd_info *mtd,
170 struct nand_chip *chip,
171 uint8_t *buf, int oob_required,
172 int page)
174 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
175 nand_read_page_raw(mtd, chip, buf, oob_required, page);
177 return 0;
180 static int
181 micron_nand_write_page_raw_on_die_ecc(struct mtd_info *mtd,
182 struct nand_chip *chip,
183 const uint8_t *buf, int oob_required,
184 int page)
186 int status;
188 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
189 nand_write_page_raw(mtd, chip, buf, oob_required, page);
190 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
191 status = chip->waitfunc(mtd, chip);
193 return status & NAND_STATUS_FAIL ? -EIO : 0;
196 enum {
197 /* The NAND flash doesn't support on-die ECC */
198 MICRON_ON_DIE_UNSUPPORTED,
201 * The NAND flash supports on-die ECC and it can be
202 * enabled/disabled by a set features command.
204 MICRON_ON_DIE_SUPPORTED,
207 * The NAND flash supports on-die ECC, and it cannot be
208 * disabled.
210 MICRON_ON_DIE_MANDATORY,
214 * Try to detect if the NAND support on-die ECC. To do this, we enable
215 * the feature, and read back if it has been enabled as expected. We
216 * also check if it can be disabled, because some Micron NANDs do not
217 * allow disabling the on-die ECC and we don't support such NANDs for
218 * now.
220 * This function also has the side effect of disabling on-die ECC if
221 * it had been left enabled by the firmware/bootloader.
223 static int micron_supports_on_die_ecc(struct nand_chip *chip)
225 u8 feature[ONFI_SUBFEATURE_PARAM_LEN] = { 0, };
226 int ret;
228 if (chip->onfi_version == 0)
229 return MICRON_ON_DIE_UNSUPPORTED;
231 if (chip->bits_per_cell != 1)
232 return MICRON_ON_DIE_UNSUPPORTED;
234 ret = micron_nand_on_die_ecc_setup(chip, true);
235 if (ret)
236 return MICRON_ON_DIE_UNSUPPORTED;
238 chip->onfi_get_features(nand_to_mtd(chip), chip,
239 ONFI_FEATURE_ON_DIE_ECC, feature);
240 if ((feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN) == 0)
241 return MICRON_ON_DIE_UNSUPPORTED;
243 ret = micron_nand_on_die_ecc_setup(chip, false);
244 if (ret)
245 return MICRON_ON_DIE_UNSUPPORTED;
247 chip->onfi_get_features(nand_to_mtd(chip), chip,
248 ONFI_FEATURE_ON_DIE_ECC, feature);
249 if (feature[0] & ONFI_FEATURE_ON_DIE_ECC_EN)
250 return MICRON_ON_DIE_MANDATORY;
253 * Some Micron NANDs have an on-die ECC of 4/512, some other
254 * 8/512. We only support the former.
256 if (chip->onfi_params.ecc_bits != 4)
257 return MICRON_ON_DIE_UNSUPPORTED;
259 return MICRON_ON_DIE_SUPPORTED;
262 static int micron_nand_init(struct nand_chip *chip)
264 struct mtd_info *mtd = nand_to_mtd(chip);
265 int ondie;
266 int ret;
268 ret = micron_nand_onfi_init(chip);
269 if (ret)
270 return ret;
272 if (mtd->writesize == 2048)
273 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
275 ondie = micron_supports_on_die_ecc(chip);
277 if (ondie == MICRON_ON_DIE_MANDATORY) {
278 pr_err("On-die ECC forcefully enabled, not supported\n");
279 return -EINVAL;
282 if (chip->ecc.mode == NAND_ECC_ON_DIE) {
283 if (ondie == MICRON_ON_DIE_UNSUPPORTED) {
284 pr_err("On-die ECC selected but not supported\n");
285 return -EINVAL;
288 chip->ecc.options = NAND_ECC_CUSTOM_PAGE_ACCESS;
289 chip->ecc.bytes = 8;
290 chip->ecc.size = 512;
291 chip->ecc.strength = 4;
292 chip->ecc.algo = NAND_ECC_BCH;
293 chip->ecc.read_page = micron_nand_read_page_on_die_ecc;
294 chip->ecc.write_page = micron_nand_write_page_on_die_ecc;
295 chip->ecc.read_page_raw =
296 micron_nand_read_page_raw_on_die_ecc;
297 chip->ecc.write_page_raw =
298 micron_nand_write_page_raw_on_die_ecc;
300 mtd_set_ooblayout(mtd, &micron_nand_on_die_ooblayout_ops);
303 return 0;
306 const struct nand_manufacturer_ops micron_nand_manuf_ops = {
307 .init = micron_nand_init,