blob: 2a5d0efea4984c9487c36675a898b696a1cc61b9 [file] [log] [blame]
Boris Brezillon01389b62016-06-08 10:30:18 +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
18#include <linux/mtd/nand.h>
Boris Brezillon78f34822016-05-27 14:36:36 +020019#include <linux/sizes.h>
Boris Brezillon626994e2016-05-27 10:15:03 +020020#include <linux/slab.h>
21
22#define NAND_HYNIX_CMD_SET_PARAMS 0x36
23#define NAND_HYNIX_CMD_APPLY_PARAMS 0x16
24
25#define NAND_HYNIX_1XNM_RR_REPEAT 8
26
27/**
28 * struct hynix_read_retry - read-retry data
29 * @nregs: number of register to set when applying a new read-retry mode
30 * @regs: register offsets (NAND chip dependent)
31 * @values: array of values to set in registers. The array size is equal to
32 * (nregs * nmodes)
33 */
34struct hynix_read_retry {
35 int nregs;
36 const u8 *regs;
37 u8 values[0];
38};
39
40/**
41 * struct hynix_nand - private Hynix NAND struct
42 * @nand_technology: manufacturing process expressed in picometer
43 * @read_retry: read-retry information
44 */
45struct hynix_nand {
46 const struct hynix_read_retry *read_retry;
47};
48
49/**
50 * struct hynix_read_retry_otp - structure describing how the read-retry OTP
51 * area
52 * @nregs: number of hynix private registers to set before reading the reading
53 * the OTP area
54 * @regs: registers that should be configured
55 * @values: values that should be set in regs
56 * @page: the address to pass to the READ_PAGE command. Depends on the NAND
57 * chip
58 * @size: size of the read-retry OTP section
59 */
60struct hynix_read_retry_otp {
61 int nregs;
62 const u8 *regs;
63 const u8 *values;
64 int page;
65 int size;
66};
Boris Brezillon01389b62016-06-08 10:30:18 +020067
Boris Brezillon78f34822016-05-27 14:36:36 +020068static bool hynix_nand_has_valid_jedecid(struct nand_chip *chip)
Boris Brezillon01389b62016-06-08 10:30:18 +020069{
70 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon78f34822016-05-27 14:36:36 +020071 u8 jedecid[6] = { };
72 int i = 0;
Boris Brezillon01389b62016-06-08 10:30:18 +020073
Boris Brezillon78f34822016-05-27 14:36:36 +020074 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
75 for (i = 0; i < 5; i++)
76 jedecid[i] = chip->read_byte(mtd);
Boris Brezillon01389b62016-06-08 10:30:18 +020077
Boris Brezillon78f34822016-05-27 14:36:36 +020078 return !strcmp("JEDEC", jedecid);
79}
Boris Brezillon01389b62016-06-08 10:30:18 +020080
Boris Brezillon626994e2016-05-27 10:15:03 +020081static int hynix_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
82{
83 struct nand_chip *chip = mtd_to_nand(mtd);
84 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
85 const u8 *values;
86 int status;
87 int i;
88
89 values = hynix->read_retry->values +
90 (retry_mode * hynix->read_retry->nregs);
91
92 /* Enter 'Set Hynix Parameters' mode */
93 chip->cmdfunc(mtd, NAND_HYNIX_CMD_SET_PARAMS, -1, -1);
94
95 /*
96 * Configure the NAND in the requested read-retry mode.
97 * This is done by setting pre-defined values in internal NAND
98 * registers.
99 *
100 * The set of registers is NAND specific, and the values are either
101 * predefined or extracted from an OTP area on the NAND (values are
102 * probably tweaked at production in this case).
103 */
104 for (i = 0; i < hynix->read_retry->nregs; i++) {
105 int column = hynix->read_retry->regs[i];
106
107 column |= column << 8;
108 chip->cmdfunc(mtd, NAND_CMD_NONE, column, -1);
109 chip->write_byte(mtd, values[i]);
110 }
111
112 /* Apply the new settings. */
113 chip->cmdfunc(mtd, NAND_HYNIX_CMD_APPLY_PARAMS, -1, -1);
114
115 status = chip->waitfunc(mtd, chip);
116 if (status & NAND_STATUS_FAIL)
117 return -EIO;
118
119 return 0;
120}
121
122/**
123 * hynix_get_majority - get the value that is occurring the most in a given
124 * set of values
125 * @in: the array of values to test
126 * @repeat: the size of the in array
127 * @out: pointer used to store the output value
128 *
129 * This function implements the 'majority check' logic that is supposed to
130 * overcome the unreliability of MLC NANDs when reading the OTP area storing
131 * the read-retry parameters.
132 *
133 * It's based on a pretty simple assumption: if we repeat the same value
134 * several times and then take the one that is occurring the most, we should
135 * find the correct value.
136 * Let's hope this dummy algorithm prevents us from losing the read-retry
137 * parameters.
138 */
139static int hynix_get_majority(const u8 *in, int repeat, u8 *out)
140{
141 int i, j, half = repeat / 2;
142
143 /*
144 * We only test the first half of the in array because we must ensure
145 * that the value is at least occurring repeat / 2 times.
146 *
147 * This loop is suboptimal since we may count the occurrences of the
148 * same value several time, but we are doing that on small sets, which
149 * makes it acceptable.
150 */
151 for (i = 0; i < half; i++) {
152 int cnt = 0;
153 u8 val = in[i];
154
155 /* Count all values that are matching the one at index i. */
156 for (j = i + 1; j < repeat; j++) {
157 if (in[j] == val)
158 cnt++;
159 }
160
161 /* We found a value occurring more than repeat / 2. */
162 if (cnt > half) {
163 *out = val;
164 return 0;
165 }
166 }
167
168 return -EIO;
169}
170
171static int hynix_read_rr_otp(struct nand_chip *chip,
172 const struct hynix_read_retry_otp *info,
173 void *buf)
174{
175 struct mtd_info *mtd = nand_to_mtd(chip);
176 int i;
177
178 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
179
180 chip->cmdfunc(mtd, NAND_HYNIX_CMD_SET_PARAMS, -1, -1);
181
182 for (i = 0; i < info->nregs; i++) {
183 int column = info->regs[i];
184
185 column |= column << 8;
186 chip->cmdfunc(mtd, NAND_CMD_NONE, column, -1);
187 chip->write_byte(mtd, info->values[i]);
188 }
189
190 chip->cmdfunc(mtd, NAND_HYNIX_CMD_APPLY_PARAMS, -1, -1);
191
192 /* Sequence to enter OTP mode? */
193 chip->cmdfunc(mtd, 0x17, -1, -1);
194 chip->cmdfunc(mtd, 0x04, -1, -1);
195 chip->cmdfunc(mtd, 0x19, -1, -1);
196
197 /* Now read the page */
198 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, info->page);
199 chip->read_buf(mtd, buf, info->size);
200
201 /* Put everything back to normal */
202 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
203 chip->cmdfunc(mtd, NAND_HYNIX_CMD_SET_PARAMS, 0x38, -1);
204 chip->write_byte(mtd, 0x0);
205 chip->cmdfunc(mtd, NAND_HYNIX_CMD_APPLY_PARAMS, -1, -1);
206 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, -1);
207
208 return 0;
209}
210
211#define NAND_HYNIX_1XNM_RR_COUNT_OFFS 0
212#define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS 8
213#define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv) \
214 (16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize)))
215
216static int hynix_mlc_1xnm_rr_value(const u8 *buf, int nmodes, int nregs,
217 int mode, int reg, bool inv, u8 *val)
218{
219 u8 tmp[NAND_HYNIX_1XNM_RR_REPEAT];
220 int val_offs = (mode * nregs) + reg;
221 int set_size = nmodes * nregs;
222 int i, ret;
223
224 for (i = 0; i < NAND_HYNIX_1XNM_RR_REPEAT; i++) {
225 int set_offs = NAND_HYNIX_1XNM_RR_SET_OFFS(i, set_size, inv);
226
227 tmp[i] = buf[val_offs + set_offs];
228 }
229
230 ret = hynix_get_majority(tmp, NAND_HYNIX_1XNM_RR_REPEAT, val);
231 if (ret)
232 return ret;
233
234 if (inv)
235 *val = ~*val;
236
237 return 0;
238}
239
240static u8 hynix_1xnm_mlc_read_retry_regs[] = {
241 0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf
242};
243
244static int hynix_mlc_1xnm_rr_init(struct nand_chip *chip,
245 const struct hynix_read_retry_otp *info)
246{
247 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
248 struct hynix_read_retry *rr = NULL;
249 int ret, i, j;
250 u8 nregs, nmodes;
251 u8 *buf;
252
253 buf = kmalloc(info->size, GFP_KERNEL);
254 if (!buf)
255 return -ENOMEM;
256
257 ret = hynix_read_rr_otp(chip, info, buf);
258 if (ret)
259 goto out;
260
261 ret = hynix_get_majority(buf, NAND_HYNIX_1XNM_RR_REPEAT,
262 &nmodes);
263 if (ret)
264 goto out;
265
266 ret = hynix_get_majority(buf + NAND_HYNIX_1XNM_RR_REPEAT,
267 NAND_HYNIX_1XNM_RR_REPEAT,
268 &nregs);
269 if (ret)
270 goto out;
271
272 rr = kzalloc(sizeof(*rr) + (nregs * nmodes), GFP_KERNEL);
273 if (!rr)
274 goto out;
275
276 for (i = 0; i < nmodes; i++) {
277 for (j = 0; j < nregs; j++) {
278 u8 *val = rr->values + (i * nregs);
279
280 ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
281 false, val);
282 if (!ret)
283 continue;
284
285 ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
286 true, val);
287 if (ret)
288 goto out;
289 }
290 }
291
292 rr->nregs = nregs;
293 rr->regs = hynix_1xnm_mlc_read_retry_regs;
294 hynix->read_retry = rr;
295 chip->setup_read_retry = hynix_nand_setup_read_retry;
296 chip->read_retries = nmodes;
297
298out:
299 kfree(buf);
300
301 if (ret)
302 kfree(rr);
303
304 return ret;
305}
306
307static const u8 hynix_mlc_1xnm_rr_otp_regs[] = { 0x38 };
308static const u8 hynix_mlc_1xnm_rr_otp_values[] = { 0x52 };
309
310static const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps[] = {
311 {
312 .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
313 .regs = hynix_mlc_1xnm_rr_otp_regs,
314 .values = hynix_mlc_1xnm_rr_otp_values,
315 .page = 0x21f,
316 .size = 784
317 },
318 {
319 .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
320 .regs = hynix_mlc_1xnm_rr_otp_regs,
321 .values = hynix_mlc_1xnm_rr_otp_values,
322 .page = 0x200,
323 .size = 528,
324 },
325};
326
327static int hynix_nand_rr_init(struct nand_chip *chip)
328{
329 int i, ret = 0;
330 bool valid_jedecid;
331
332 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
333
334 /*
335 * We only support read-retry for 1xnm NANDs, and those NANDs all
336 * expose a valid JEDEC ID.
337 */
338 if (valid_jedecid) {
339 u8 nand_tech = chip->id.data[5] >> 4;
340
341 /* 1xnm technology */
342 if (nand_tech == 4) {
343 for (i = 0; i < ARRAY_SIZE(hynix_mlc_1xnm_rr_otps);
344 i++) {
345 /*
346 * FIXME: Hynix recommend to copy the
347 * read-retry OTP area into a normal page.
348 */
349 ret = hynix_mlc_1xnm_rr_init(chip,
350 hynix_mlc_1xnm_rr_otps);
351 if (!ret)
352 break;
353 }
354 }
355 }
356
357 if (ret)
358 pr_warn("failed to initialize read-retry infrastructure");
359
360 return 0;
361}
362
Boris Brezillon78f34822016-05-27 14:36:36 +0200363static void hynix_nand_extract_oobsize(struct nand_chip *chip,
364 bool valid_jedecid)
365{
366 struct mtd_info *mtd = nand_to_mtd(chip);
367 u8 oobsize;
368
369 oobsize = ((chip->id.data[3] >> 2) & 0x3) |
370 ((chip->id.data[3] >> 4) & 0x4);
371
372 if (valid_jedecid) {
373 switch (oobsize) {
374 case 0:
375 mtd->oobsize = 2048;
376 break;
377 case 1:
378 mtd->oobsize = 1664;
379 break;
380 case 2:
381 mtd->oobsize = 1024;
382 break;
383 case 3:
384 mtd->oobsize = 640;
385 break;
386 default:
387 /*
388 * We should never reach this case, but if that
389 * happens, this probably means Hynix decided to use
390 * a different extended ID format, and we should find
391 * a way to support it.
392 */
393 WARN(1, "Invalid OOB size");
394 break;
395 }
396 } else {
397 switch (oobsize) {
Boris Brezillon01389b62016-06-08 10:30:18 +0200398 case 0:
399 mtd->oobsize = 128;
400 break;
401 case 1:
402 mtd->oobsize = 224;
403 break;
404 case 2:
405 mtd->oobsize = 448;
406 break;
407 case 3:
408 mtd->oobsize = 64;
409 break;
410 case 4:
411 mtd->oobsize = 32;
412 break;
413 case 5:
414 mtd->oobsize = 16;
415 break;
Boris Brezillon78f34822016-05-27 14:36:36 +0200416 case 6:
Boris Brezillon01389b62016-06-08 10:30:18 +0200417 mtd->oobsize = 640;
418 break;
Boris Brezillon78f34822016-05-27 14:36:36 +0200419 default:
420 /*
421 * We should never reach this case, but if that
422 * happens, this probably means Hynix decided to use
423 * a different extended ID format, and we should find
424 * a way to support it.
425 */
426 WARN(1, "Invalid OOB size");
427 break;
Boris Brezillon01389b62016-06-08 10:30:18 +0200428 }
Boris Brezillon01389b62016-06-08 10:30:18 +0200429 }
430}
431
Boris Brezillon78f34822016-05-27 14:36:36 +0200432static void hynix_nand_extract_ecc_requirements(struct nand_chip *chip,
433 bool valid_jedecid)
434{
435 u8 ecc_level = (chip->id.data[4] >> 4) & 0x7;
436
437 if (valid_jedecid) {
438 /* Reference: H27UCG8T2E datasheet */
439 chip->ecc_step_ds = 1024;
440
441 switch (ecc_level) {
442 case 0:
443 chip->ecc_step_ds = 0;
444 chip->ecc_strength_ds = 0;
445 break;
446 case 1:
447 chip->ecc_strength_ds = 4;
448 break;
449 case 2:
450 chip->ecc_strength_ds = 24;
451 break;
452 case 3:
453 chip->ecc_strength_ds = 32;
454 break;
455 case 4:
456 chip->ecc_strength_ds = 40;
457 break;
458 case 5:
459 chip->ecc_strength_ds = 50;
460 break;
461 case 6:
462 chip->ecc_strength_ds = 60;
463 break;
464 default:
465 /*
466 * We should never reach this case, but if that
467 * happens, this probably means Hynix decided to use
468 * a different extended ID format, and we should find
469 * a way to support it.
470 */
471 WARN(1, "Invalid ECC requirements");
472 }
473 } else {
474 /*
475 * The ECC requirements field meaning depends on the
476 * NAND technology.
477 */
478 u8 nand_tech = chip->id.data[5] & 0x3;
479
480 if (nand_tech < 3) {
481 /* > 26nm, reference: H27UBG8T2A datasheet */
482 if (ecc_level < 5) {
483 chip->ecc_step_ds = 512;
484 chip->ecc_strength_ds = 1 << ecc_level;
485 } else if (ecc_level < 7) {
486 if (ecc_level == 5)
487 chip->ecc_step_ds = 2048;
488 else
489 chip->ecc_step_ds = 1024;
490 chip->ecc_strength_ds = 24;
491 } else {
492 /*
493 * We should never reach this case, but if that
494 * happens, this probably means Hynix decided
495 * to use a different extended ID format, and
496 * we should find a way to support it.
497 */
498 WARN(1, "Invalid ECC requirements");
499 }
500 } else {
501 /* <= 26nm, reference: H27UBG8T2B datasheet */
502 if (!ecc_level) {
503 chip->ecc_step_ds = 0;
504 chip->ecc_strength_ds = 0;
505 } else if (ecc_level < 5) {
506 chip->ecc_step_ds = 512;
507 chip->ecc_strength_ds = 1 << (ecc_level - 1);
508 } else {
509 chip->ecc_step_ds = 1024;
510 chip->ecc_strength_ds = 24 +
511 (8 * (ecc_level - 5));
512 }
513 }
514 }
515}
516
517static void hynix_nand_extract_scrambling_requirements(struct nand_chip *chip,
518 bool valid_jedecid)
519{
520 u8 nand_tech;
521
522 /* We need scrambling on all TLC NANDs*/
523 if (chip->bits_per_cell > 2)
524 chip->options |= NAND_NEED_SCRAMBLING;
525
526 /* And on MLC NANDs with sub-3xnm process */
527 if (valid_jedecid) {
528 nand_tech = chip->id.data[5] >> 4;
529
530 /* < 3xnm */
531 if (nand_tech > 0)
532 chip->options |= NAND_NEED_SCRAMBLING;
533 } else {
534 nand_tech = chip->id.data[5] & 0x3;
535
536 /* < 32nm */
537 if (nand_tech > 2)
538 chip->options |= NAND_NEED_SCRAMBLING;
539 }
540}
541
542static void hynix_nand_decode_id(struct nand_chip *chip)
543{
544 struct mtd_info *mtd = nand_to_mtd(chip);
545 bool valid_jedecid;
546 u8 tmp;
547
548 /*
549 * Exclude all SLC NANDs from this advanced detection scheme.
550 * According to the ranges defined in several datasheets, it might
551 * appear that even SLC NANDs could fall in this extended ID scheme.
552 * If that the case rework the test to let SLC NANDs go through the
553 * detection process.
554 */
555 if (chip->id.len < 6 || nand_is_slc(chip)) {
556 nand_decode_ext_id(chip);
557 return;
558 }
559
560 /* Extract pagesize */
561 mtd->writesize = 2048 << (chip->id.data[3] & 0x03);
562
563 tmp = (chip->id.data[3] >> 4) & 0x3;
564 /*
565 * When bit7 is set that means we start counting at 1MiB, otherwise
566 * we start counting at 128KiB and shift this value the content of
567 * ID[3][4:5].
568 * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in
569 * this case the erasesize is set to 768KiB.
570 */
571 if (chip->id.data[3] & 0x80)
572 mtd->erasesize = SZ_1M << tmp;
573 else if (tmp == 3)
574 mtd->erasesize = SZ_512K + SZ_256K;
575 else
576 mtd->erasesize = SZ_128K << tmp;
577
578 /*
579 * Modern Toggle DDR NANDs have a valid JEDECID even though they are
580 * not exposing a valid JEDEC parameter table.
581 * These NANDs use a different NAND ID scheme.
582 */
583 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
584
585 hynix_nand_extract_oobsize(chip, valid_jedecid);
586 hynix_nand_extract_ecc_requirements(chip, valid_jedecid);
587 hynix_nand_extract_scrambling_requirements(chip, valid_jedecid);
588}
589
Boris Brezillon626994e2016-05-27 10:15:03 +0200590static void hynix_nand_cleanup(struct nand_chip *chip)
591{
592 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
593
594 if (!hynix)
595 return;
596
597 kfree(hynix->read_retry);
598 kfree(hynix);
599 nand_set_manufacturer_data(chip, NULL);
600}
601
Boris Brezillon01389b62016-06-08 10:30:18 +0200602static int hynix_nand_init(struct nand_chip *chip)
603{
Boris Brezillon626994e2016-05-27 10:15:03 +0200604 struct hynix_nand *hynix;
605 int ret;
606
Boris Brezillon01389b62016-06-08 10:30:18 +0200607 if (!nand_is_slc(chip))
608 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
609 else
610 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
611
Boris Brezillon626994e2016-05-27 10:15:03 +0200612 hynix = kzalloc(sizeof(*hynix), GFP_KERNEL);
613 if (!hynix)
614 return -ENOMEM;
615
616 nand_set_manufacturer_data(chip, hynix);
617
618 ret = hynix_nand_rr_init(chip);
619 if (ret)
620 hynix_nand_cleanup(chip);
621
622 return ret;
Boris Brezillon01389b62016-06-08 10:30:18 +0200623}
624
625const struct nand_manufacturer_ops hynix_nand_manuf_ops = {
626 .detect = hynix_nand_decode_id,
627 .init = hynix_nand_init,
Boris Brezillon626994e2016-05-27 10:15:03 +0200628 .cleanup = hynix_nand_cleanup,
Boris Brezillon01389b62016-06-08 10:30:18 +0200629};