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/nand.h>
19 #include <linux/sizes.h>
21 static bool hynix_nand_has_valid_jedecid(struct nand_chip *chip)
23 struct mtd_info *mtd = nand_to_mtd(chip);
27 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
28 for (i = 0; i < 5; i++)
29 jedecid[i] = chip->read_byte(mtd);
31 return !strcmp("JEDEC", jedecid);
34 static void hynix_nand_extract_oobsize(struct nand_chip *chip,
37 struct mtd_info *mtd = nand_to_mtd(chip);
40 oobsize = ((chip->id.data[3] >> 2) & 0x3) |
41 ((chip->id.data[3] >> 4) & 0x4);
59 * We should never reach this case, but if that
60 * happens, this probably means Hynix decided to use
61 * a different extended ID format, and we should find
62 * a way to support it.
64 WARN(1, "Invalid OOB size");
92 * We should never reach this case, but if that
93 * happens, this probably means Hynix decided to use
94 * a different extended ID format, and we should find
95 * a way to support it.
97 WARN(1, "Invalid OOB size");
103 static void hynix_nand_extract_ecc_requirements(struct nand_chip *chip,
106 u8 ecc_level = (chip->id.data[4] >> 4) & 0x7;
109 /* Reference: H27UCG8T2E datasheet */
110 chip->ecc_step_ds = 1024;
114 chip->ecc_step_ds = 0;
115 chip->ecc_strength_ds = 0;
118 chip->ecc_strength_ds = 4;
121 chip->ecc_strength_ds = 24;
124 chip->ecc_strength_ds = 32;
127 chip->ecc_strength_ds = 40;
130 chip->ecc_strength_ds = 50;
133 chip->ecc_strength_ds = 60;
137 * We should never reach this case, but if that
138 * happens, this probably means Hynix decided to use
139 * a different extended ID format, and we should find
140 * a way to support it.
142 WARN(1, "Invalid ECC requirements");
146 * The ECC requirements field meaning depends on the
149 u8 nand_tech = chip->id.data[5] & 0x3;
152 /* > 26nm, reference: H27UBG8T2A datasheet */
154 chip->ecc_step_ds = 512;
155 chip->ecc_strength_ds = 1 << ecc_level;
156 } else if (ecc_level < 7) {
158 chip->ecc_step_ds = 2048;
160 chip->ecc_step_ds = 1024;
161 chip->ecc_strength_ds = 24;
164 * We should never reach this case, but if that
165 * happens, this probably means Hynix decided
166 * to use a different extended ID format, and
167 * we should find a way to support it.
169 WARN(1, "Invalid ECC requirements");
172 /* <= 26nm, reference: H27UBG8T2B datasheet */
174 chip->ecc_step_ds = 0;
175 chip->ecc_strength_ds = 0;
176 } else if (ecc_level < 5) {
177 chip->ecc_step_ds = 512;
178 chip->ecc_strength_ds = 1 << (ecc_level - 1);
180 chip->ecc_step_ds = 1024;
181 chip->ecc_strength_ds = 24 +
182 (8 * (ecc_level - 5));
188 static void hynix_nand_extract_scrambling_requirements(struct nand_chip *chip,
193 /* We need scrambling on all TLC NANDs*/
194 if (chip->bits_per_cell > 2)
195 chip->options |= NAND_NEED_SCRAMBLING;
197 /* And on MLC NANDs with sub-3xnm process */
199 nand_tech = chip->id.data[5] >> 4;
203 chip->options |= NAND_NEED_SCRAMBLING;
205 nand_tech = chip->id.data[5] & 0x3;
209 chip->options |= NAND_NEED_SCRAMBLING;
213 static void hynix_nand_decode_id(struct nand_chip *chip)
215 struct mtd_info *mtd = nand_to_mtd(chip);
220 * Exclude all SLC NANDs from this advanced detection scheme.
221 * According to the ranges defined in several datasheets, it might
222 * appear that even SLC NANDs could fall in this extended ID scheme.
223 * If that the case rework the test to let SLC NANDs go through the
226 if (chip->id.len < 6 || nand_is_slc(chip)) {
227 nand_decode_ext_id(chip);
231 /* Extract pagesize */
232 mtd->writesize = 2048 << (chip->id.data[3] & 0x03);
234 tmp = (chip->id.data[3] >> 4) & 0x3;
236 * When bit7 is set that means we start counting at 1MiB, otherwise
237 * we start counting at 128KiB and shift this value the content of
239 * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in
240 * this case the erasesize is set to 768KiB.
242 if (chip->id.data[3] & 0x80)
243 mtd->erasesize = SZ_1M << tmp;
245 mtd->erasesize = SZ_512K + SZ_256K;
247 mtd->erasesize = SZ_128K << tmp;
250 * Modern Toggle DDR NANDs have a valid JEDECID even though they are
251 * not exposing a valid JEDEC parameter table.
252 * These NANDs use a different NAND ID scheme.
254 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
256 hynix_nand_extract_oobsize(chip, valid_jedecid);
257 hynix_nand_extract_ecc_requirements(chip, valid_jedecid);
258 hynix_nand_extract_scrambling_requirements(chip, valid_jedecid);
261 static int hynix_nand_init(struct nand_chip *chip)
263 if (!nand_is_slc(chip))
264 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
266 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
271 const struct nand_manufacturer_ops hynix_nand_manuf_ops = {
272 .detect = hynix_nand_decode_id,
273 .init = hynix_nand_init,