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>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
22 #define NAND_HYNIX_CMD_SET_PARAMS 0x36
23 #define NAND_HYNIX_CMD_APPLY_PARAMS 0x16
25 #define NAND_HYNIX_1XNM_RR_REPEAT 8
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
34 struct hynix_read_retry
{
41 * struct hynix_nand - private Hynix NAND struct
42 * @nand_technology: manufacturing process expressed in picometer
43 * @read_retry: read-retry information
46 const struct hynix_read_retry
*read_retry
;
50 * struct hynix_read_retry_otp - structure describing how the read-retry OTP
52 * @nregs: number of hynix private registers to set before reading the reading
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
58 * @size: size of the read-retry OTP section
60 struct hynix_read_retry_otp
{
68 static bool hynix_nand_has_valid_jedecid(struct nand_chip
*chip
)
73 ret
= nand_readid_op(chip
, 0x40, jedecid
, sizeof(jedecid
));
77 return !strncmp("JEDEC", jedecid
, sizeof(jedecid
));
80 static int hynix_nand_cmd_op(struct nand_chip
*chip
, u8 cmd
)
82 struct mtd_info
*mtd
= nand_to_mtd(chip
);
85 struct nand_op_instr instrs
[] = {
88 struct nand_operation op
= NAND_OPERATION(instrs
);
90 return nand_exec_op(chip
, &op
);
93 chip
->cmdfunc(mtd
, cmd
, -1, -1);
98 static int hynix_nand_reg_write_op(struct nand_chip
*chip
, u8 addr
, u8 val
)
100 struct mtd_info
*mtd
= nand_to_mtd(chip
);
101 u16 column
= ((u16
)addr
<< 8) | addr
;
104 struct nand_op_instr instrs
[] = {
105 NAND_OP_ADDR(1, &addr
, 0),
106 NAND_OP_8BIT_DATA_OUT(1, &val
, 0),
108 struct nand_operation op
= NAND_OPERATION(instrs
);
110 return nand_exec_op(chip
, &op
);
113 chip
->cmdfunc(mtd
, NAND_CMD_NONE
, column
, -1);
114 chip
->write_byte(mtd
, val
);
119 static int hynix_nand_setup_read_retry(struct mtd_info
*mtd
, int retry_mode
)
121 struct nand_chip
*chip
= mtd_to_nand(mtd
);
122 struct hynix_nand
*hynix
= nand_get_manufacturer_data(chip
);
126 values
= hynix
->read_retry
->values
+
127 (retry_mode
* hynix
->read_retry
->nregs
);
129 /* Enter 'Set Hynix Parameters' mode */
130 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_SET_PARAMS
);
135 * Configure the NAND in the requested read-retry mode.
136 * This is done by setting pre-defined values in internal NAND
139 * The set of registers is NAND specific, and the values are either
140 * predefined or extracted from an OTP area on the NAND (values are
141 * probably tweaked at production in this case).
143 for (i
= 0; i
< hynix
->read_retry
->nregs
; i
++) {
144 ret
= hynix_nand_reg_write_op(chip
, hynix
->read_retry
->regs
[i
],
150 /* Apply the new settings. */
151 return hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_APPLY_PARAMS
);
155 * hynix_get_majority - get the value that is occurring the most in a given
157 * @in: the array of values to test
158 * @repeat: the size of the in array
159 * @out: pointer used to store the output value
161 * This function implements the 'majority check' logic that is supposed to
162 * overcome the unreliability of MLC NANDs when reading the OTP area storing
163 * the read-retry parameters.
165 * It's based on a pretty simple assumption: if we repeat the same value
166 * several times and then take the one that is occurring the most, we should
167 * find the correct value.
168 * Let's hope this dummy algorithm prevents us from losing the read-retry
171 static int hynix_get_majority(const u8
*in
, int repeat
, u8
*out
)
173 int i
, j
, half
= repeat
/ 2;
176 * We only test the first half of the in array because we must ensure
177 * that the value is at least occurring repeat / 2 times.
179 * This loop is suboptimal since we may count the occurrences of the
180 * same value several time, but we are doing that on small sets, which
181 * makes it acceptable.
183 for (i
= 0; i
< half
; i
++) {
187 /* Count all values that are matching the one at index i. */
188 for (j
= i
+ 1; j
< repeat
; j
++) {
193 /* We found a value occurring more than repeat / 2. */
203 static int hynix_read_rr_otp(struct nand_chip
*chip
,
204 const struct hynix_read_retry_otp
*info
,
209 ret
= nand_reset_op(chip
);
213 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_SET_PARAMS
);
217 for (i
= 0; i
< info
->nregs
; i
++) {
218 ret
= hynix_nand_reg_write_op(chip
, info
->regs
[i
],
224 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_APPLY_PARAMS
);
228 /* Sequence to enter OTP mode? */
229 ret
= hynix_nand_cmd_op(chip
, 0x17);
233 ret
= hynix_nand_cmd_op(chip
, 0x4);
237 ret
= hynix_nand_cmd_op(chip
, 0x19);
241 /* Now read the page */
242 ret
= nand_read_page_op(chip
, info
->page
, 0, buf
, info
->size
);
246 /* Put everything back to normal */
247 ret
= nand_reset_op(chip
);
251 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_SET_PARAMS
);
255 ret
= hynix_nand_reg_write_op(chip
, 0x38, 0);
259 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_APPLY_PARAMS
);
263 return nand_read_page_op(chip
, 0, 0, NULL
, 0);
266 #define NAND_HYNIX_1XNM_RR_COUNT_OFFS 0
267 #define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS 8
268 #define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv) \
269 (16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize)))
271 static int hynix_mlc_1xnm_rr_value(const u8
*buf
, int nmodes
, int nregs
,
272 int mode
, int reg
, bool inv
, u8
*val
)
274 u8 tmp
[NAND_HYNIX_1XNM_RR_REPEAT
];
275 int val_offs
= (mode
* nregs
) + reg
;
276 int set_size
= nmodes
* nregs
;
279 for (i
= 0; i
< NAND_HYNIX_1XNM_RR_REPEAT
; i
++) {
280 int set_offs
= NAND_HYNIX_1XNM_RR_SET_OFFS(i
, set_size
, inv
);
282 tmp
[i
] = buf
[val_offs
+ set_offs
];
285 ret
= hynix_get_majority(tmp
, NAND_HYNIX_1XNM_RR_REPEAT
, val
);
295 static u8 hynix_1xnm_mlc_read_retry_regs
[] = {
296 0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf
299 static int hynix_mlc_1xnm_rr_init(struct nand_chip
*chip
,
300 const struct hynix_read_retry_otp
*info
)
302 struct hynix_nand
*hynix
= nand_get_manufacturer_data(chip
);
303 struct hynix_read_retry
*rr
= NULL
;
308 buf
= kmalloc(info
->size
, GFP_KERNEL
);
312 ret
= hynix_read_rr_otp(chip
, info
, buf
);
316 ret
= hynix_get_majority(buf
, NAND_HYNIX_1XNM_RR_REPEAT
,
321 ret
= hynix_get_majority(buf
+ NAND_HYNIX_1XNM_RR_REPEAT
,
322 NAND_HYNIX_1XNM_RR_REPEAT
,
327 rr
= kzalloc(sizeof(*rr
) + (nregs
* nmodes
), GFP_KERNEL
);
333 for (i
= 0; i
< nmodes
; i
++) {
334 for (j
= 0; j
< nregs
; j
++) {
335 u8
*val
= rr
->values
+ (i
* nregs
);
337 ret
= hynix_mlc_1xnm_rr_value(buf
, nmodes
, nregs
, i
, j
,
342 ret
= hynix_mlc_1xnm_rr_value(buf
, nmodes
, nregs
, i
, j
,
350 rr
->regs
= hynix_1xnm_mlc_read_retry_regs
;
351 hynix
->read_retry
= rr
;
352 chip
->setup_read_retry
= hynix_nand_setup_read_retry
;
353 chip
->read_retries
= nmodes
;
364 static const u8 hynix_mlc_1xnm_rr_otp_regs
[] = { 0x38 };
365 static const u8 hynix_mlc_1xnm_rr_otp_values
[] = { 0x52 };
367 static const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps
[] = {
369 .nregs
= ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs
),
370 .regs
= hynix_mlc_1xnm_rr_otp_regs
,
371 .values
= hynix_mlc_1xnm_rr_otp_values
,
376 .nregs
= ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs
),
377 .regs
= hynix_mlc_1xnm_rr_otp_regs
,
378 .values
= hynix_mlc_1xnm_rr_otp_values
,
384 static int hynix_nand_rr_init(struct nand_chip
*chip
)
389 valid_jedecid
= hynix_nand_has_valid_jedecid(chip
);
392 * We only support read-retry for 1xnm NANDs, and those NANDs all
393 * expose a valid JEDEC ID.
396 u8 nand_tech
= chip
->id
.data
[5] >> 4;
398 /* 1xnm technology */
399 if (nand_tech
== 4) {
400 for (i
= 0; i
< ARRAY_SIZE(hynix_mlc_1xnm_rr_otps
);
403 * FIXME: Hynix recommend to copy the
404 * read-retry OTP area into a normal page.
406 ret
= hynix_mlc_1xnm_rr_init(chip
,
407 hynix_mlc_1xnm_rr_otps
);
415 pr_warn("failed to initialize read-retry infrastructure");
420 static void hynix_nand_extract_oobsize(struct nand_chip
*chip
,
423 struct mtd_info
*mtd
= nand_to_mtd(chip
);
426 oobsize
= ((chip
->id
.data
[3] >> 2) & 0x3) |
427 ((chip
->id
.data
[3] >> 4) & 0x4);
445 * We should never reach this case, but if that
446 * happens, this probably means Hynix decided to use
447 * a different extended ID format, and we should find
448 * a way to support it.
450 WARN(1, "Invalid OOB size");
478 * We should never reach this case, but if that
479 * happens, this probably means Hynix decided to use
480 * a different extended ID format, and we should find
481 * a way to support it.
483 WARN(1, "Invalid OOB size");
488 * The datasheet of H27UCG8T2BTR mentions that the "Redundant
489 * Area Size" is encoded "per 8KB" (page size). This chip uses
490 * a page size of 16KiB. The datasheet mentions an OOB size of
491 * 1.280 bytes, but the OOB size encoded in the ID bytes (using
492 * the existing logic above) is 640 bytes.
493 * Update the OOB size for this chip by taking the value
494 * determined above and scaling it to the actual page size (so
495 * the actual OOB size for this chip is: 640 * 16k / 8k).
497 if (chip
->id
.data
[1] == 0xde)
498 mtd
->oobsize
*= mtd
->writesize
/ SZ_8K
;
502 static void hynix_nand_extract_ecc_requirements(struct nand_chip
*chip
,
505 u8 ecc_level
= (chip
->id
.data
[4] >> 4) & 0x7;
508 /* Reference: H27UCG8T2E datasheet */
509 chip
->ecc_step_ds
= 1024;
513 chip
->ecc_step_ds
= 0;
514 chip
->ecc_strength_ds
= 0;
517 chip
->ecc_strength_ds
= 4;
520 chip
->ecc_strength_ds
= 24;
523 chip
->ecc_strength_ds
= 32;
526 chip
->ecc_strength_ds
= 40;
529 chip
->ecc_strength_ds
= 50;
532 chip
->ecc_strength_ds
= 60;
536 * We should never reach this case, but if that
537 * happens, this probably means Hynix decided to use
538 * a different extended ID format, and we should find
539 * a way to support it.
541 WARN(1, "Invalid ECC requirements");
545 * The ECC requirements field meaning depends on the
548 u8 nand_tech
= chip
->id
.data
[5] & 0x7;
551 /* > 26nm, reference: H27UBG8T2A datasheet */
553 chip
->ecc_step_ds
= 512;
554 chip
->ecc_strength_ds
= 1 << ecc_level
;
555 } else if (ecc_level
< 7) {
557 chip
->ecc_step_ds
= 2048;
559 chip
->ecc_step_ds
= 1024;
560 chip
->ecc_strength_ds
= 24;
563 * We should never reach this case, but if that
564 * happens, this probably means Hynix decided
565 * to use a different extended ID format, and
566 * we should find a way to support it.
568 WARN(1, "Invalid ECC requirements");
571 /* <= 26nm, reference: H27UBG8T2B datasheet */
573 chip
->ecc_step_ds
= 0;
574 chip
->ecc_strength_ds
= 0;
575 } else if (ecc_level
< 5) {
576 chip
->ecc_step_ds
= 512;
577 chip
->ecc_strength_ds
= 1 << (ecc_level
- 1);
579 chip
->ecc_step_ds
= 1024;
580 chip
->ecc_strength_ds
= 24 +
581 (8 * (ecc_level
- 5));
587 static void hynix_nand_extract_scrambling_requirements(struct nand_chip
*chip
,
592 /* We need scrambling on all TLC NANDs*/
593 if (chip
->bits_per_cell
> 2)
594 chip
->options
|= NAND_NEED_SCRAMBLING
;
596 /* And on MLC NANDs with sub-3xnm process */
598 nand_tech
= chip
->id
.data
[5] >> 4;
602 chip
->options
|= NAND_NEED_SCRAMBLING
;
604 nand_tech
= chip
->id
.data
[5] & 0x7;
608 chip
->options
|= NAND_NEED_SCRAMBLING
;
612 static void hynix_nand_decode_id(struct nand_chip
*chip
)
614 struct mtd_info
*mtd
= nand_to_mtd(chip
);
619 * Exclude all SLC NANDs from this advanced detection scheme.
620 * According to the ranges defined in several datasheets, it might
621 * appear that even SLC NANDs could fall in this extended ID scheme.
622 * If that the case rework the test to let SLC NANDs go through the
625 if (chip
->id
.len
< 6 || nand_is_slc(chip
)) {
626 nand_decode_ext_id(chip
);
630 /* Extract pagesize */
631 mtd
->writesize
= 2048 << (chip
->id
.data
[3] & 0x03);
633 tmp
= (chip
->id
.data
[3] >> 4) & 0x3;
635 * When bit7 is set that means we start counting at 1MiB, otherwise
636 * we start counting at 128KiB and shift this value the content of
638 * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in
639 * this case the erasesize is set to 768KiB.
641 if (chip
->id
.data
[3] & 0x80)
642 mtd
->erasesize
= SZ_1M
<< tmp
;
644 mtd
->erasesize
= SZ_512K
+ SZ_256K
;
646 mtd
->erasesize
= SZ_128K
<< tmp
;
649 * Modern Toggle DDR NANDs have a valid JEDECID even though they are
650 * not exposing a valid JEDEC parameter table.
651 * These NANDs use a different NAND ID scheme.
653 valid_jedecid
= hynix_nand_has_valid_jedecid(chip
);
655 hynix_nand_extract_oobsize(chip
, valid_jedecid
);
656 hynix_nand_extract_ecc_requirements(chip
, valid_jedecid
);
657 hynix_nand_extract_scrambling_requirements(chip
, valid_jedecid
);
660 static void hynix_nand_cleanup(struct nand_chip
*chip
)
662 struct hynix_nand
*hynix
= nand_get_manufacturer_data(chip
);
667 kfree(hynix
->read_retry
);
669 nand_set_manufacturer_data(chip
, NULL
);
672 static int hynix_nand_init(struct nand_chip
*chip
)
674 struct hynix_nand
*hynix
;
677 if (!nand_is_slc(chip
))
678 chip
->bbt_options
|= NAND_BBT_SCANLASTPAGE
;
680 chip
->bbt_options
|= NAND_BBT_SCAN2NDPAGE
;
682 hynix
= kzalloc(sizeof(*hynix
), GFP_KERNEL
);
686 nand_set_manufacturer_data(chip
, hynix
);
688 ret
= hynix_nand_rr_init(chip
);
690 hynix_nand_cleanup(chip
);
695 const struct nand_manufacturer_ops hynix_nand_manuf_ops
= {
696 .detect
= hynix_nand_decode_id
,
697 .init
= hynix_nand_init
,
698 .cleanup
= hynix_nand_cleanup
,