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
;
103 chip
->cmdfunc(mtd
, NAND_CMD_NONE
, column
, -1);
104 chip
->write_byte(mtd
, val
);
109 static int hynix_nand_setup_read_retry(struct mtd_info
*mtd
, int retry_mode
)
111 struct nand_chip
*chip
= mtd_to_nand(mtd
);
112 struct hynix_nand
*hynix
= nand_get_manufacturer_data(chip
);
116 values
= hynix
->read_retry
->values
+
117 (retry_mode
* hynix
->read_retry
->nregs
);
119 /* Enter 'Set Hynix Parameters' mode */
120 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_SET_PARAMS
);
125 * Configure the NAND in the requested read-retry mode.
126 * This is done by setting pre-defined values in internal NAND
129 * The set of registers is NAND specific, and the values are either
130 * predefined or extracted from an OTP area on the NAND (values are
131 * probably tweaked at production in this case).
133 for (i
= 0; i
< hynix
->read_retry
->nregs
; i
++) {
134 ret
= hynix_nand_reg_write_op(chip
, hynix
->read_retry
->regs
[i
],
140 /* Apply the new settings. */
141 return hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_APPLY_PARAMS
);
145 * hynix_get_majority - get the value that is occurring the most in a given
147 * @in: the array of values to test
148 * @repeat: the size of the in array
149 * @out: pointer used to store the output value
151 * This function implements the 'majority check' logic that is supposed to
152 * overcome the unreliability of MLC NANDs when reading the OTP area storing
153 * the read-retry parameters.
155 * It's based on a pretty simple assumption: if we repeat the same value
156 * several times and then take the one that is occurring the most, we should
157 * find the correct value.
158 * Let's hope this dummy algorithm prevents us from losing the read-retry
161 static int hynix_get_majority(const u8
*in
, int repeat
, u8
*out
)
163 int i
, j
, half
= repeat
/ 2;
166 * We only test the first half of the in array because we must ensure
167 * that the value is at least occurring repeat / 2 times.
169 * This loop is suboptimal since we may count the occurrences of the
170 * same value several time, but we are doing that on small sets, which
171 * makes it acceptable.
173 for (i
= 0; i
< half
; i
++) {
177 /* Count all values that are matching the one at index i. */
178 for (j
= i
+ 1; j
< repeat
; j
++) {
183 /* We found a value occurring more than repeat / 2. */
193 static int hynix_read_rr_otp(struct nand_chip
*chip
,
194 const struct hynix_read_retry_otp
*info
,
199 ret
= nand_reset_op(chip
);
203 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_SET_PARAMS
);
207 for (i
= 0; i
< info
->nregs
; i
++) {
208 ret
= hynix_nand_reg_write_op(chip
, info
->regs
[i
],
214 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_APPLY_PARAMS
);
218 /* Sequence to enter OTP mode? */
219 ret
= hynix_nand_cmd_op(chip
, 0x17);
223 ret
= hynix_nand_cmd_op(chip
, 0x4);
227 ret
= hynix_nand_cmd_op(chip
, 0x19);
231 /* Now read the page */
232 ret
= nand_read_page_op(chip
, info
->page
, 0, buf
, info
->size
);
236 /* Put everything back to normal */
237 ret
= nand_reset_op(chip
);
241 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_SET_PARAMS
);
245 ret
= hynix_nand_reg_write_op(chip
, 0x38, 0);
249 ret
= hynix_nand_cmd_op(chip
, NAND_HYNIX_CMD_APPLY_PARAMS
);
253 return nand_read_page_op(chip
, 0, 0, NULL
, 0);
256 #define NAND_HYNIX_1XNM_RR_COUNT_OFFS 0
257 #define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS 8
258 #define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv) \
259 (16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize)))
261 static int hynix_mlc_1xnm_rr_value(const u8
*buf
, int nmodes
, int nregs
,
262 int mode
, int reg
, bool inv
, u8
*val
)
264 u8 tmp
[NAND_HYNIX_1XNM_RR_REPEAT
];
265 int val_offs
= (mode
* nregs
) + reg
;
266 int set_size
= nmodes
* nregs
;
269 for (i
= 0; i
< NAND_HYNIX_1XNM_RR_REPEAT
; i
++) {
270 int set_offs
= NAND_HYNIX_1XNM_RR_SET_OFFS(i
, set_size
, inv
);
272 tmp
[i
] = buf
[val_offs
+ set_offs
];
275 ret
= hynix_get_majority(tmp
, NAND_HYNIX_1XNM_RR_REPEAT
, val
);
285 static u8 hynix_1xnm_mlc_read_retry_regs
[] = {
286 0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf
289 static int hynix_mlc_1xnm_rr_init(struct nand_chip
*chip
,
290 const struct hynix_read_retry_otp
*info
)
292 struct hynix_nand
*hynix
= nand_get_manufacturer_data(chip
);
293 struct hynix_read_retry
*rr
= NULL
;
298 buf
= kmalloc(info
->size
, GFP_KERNEL
);
302 ret
= hynix_read_rr_otp(chip
, info
, buf
);
306 ret
= hynix_get_majority(buf
, NAND_HYNIX_1XNM_RR_REPEAT
,
311 ret
= hynix_get_majority(buf
+ NAND_HYNIX_1XNM_RR_REPEAT
,
312 NAND_HYNIX_1XNM_RR_REPEAT
,
317 rr
= kzalloc(sizeof(*rr
) + (nregs
* nmodes
), GFP_KERNEL
);
323 for (i
= 0; i
< nmodes
; i
++) {
324 for (j
= 0; j
< nregs
; j
++) {
325 u8
*val
= rr
->values
+ (i
* nregs
);
327 ret
= hynix_mlc_1xnm_rr_value(buf
, nmodes
, nregs
, i
, j
,
332 ret
= hynix_mlc_1xnm_rr_value(buf
, nmodes
, nregs
, i
, j
,
340 rr
->regs
= hynix_1xnm_mlc_read_retry_regs
;
341 hynix
->read_retry
= rr
;
342 chip
->setup_read_retry
= hynix_nand_setup_read_retry
;
343 chip
->read_retries
= nmodes
;
354 static const u8 hynix_mlc_1xnm_rr_otp_regs
[] = { 0x38 };
355 static const u8 hynix_mlc_1xnm_rr_otp_values
[] = { 0x52 };
357 static const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps
[] = {
359 .nregs
= ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs
),
360 .regs
= hynix_mlc_1xnm_rr_otp_regs
,
361 .values
= hynix_mlc_1xnm_rr_otp_values
,
366 .nregs
= ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs
),
367 .regs
= hynix_mlc_1xnm_rr_otp_regs
,
368 .values
= hynix_mlc_1xnm_rr_otp_values
,
374 static int hynix_nand_rr_init(struct nand_chip
*chip
)
379 valid_jedecid
= hynix_nand_has_valid_jedecid(chip
);
382 * We only support read-retry for 1xnm NANDs, and those NANDs all
383 * expose a valid JEDEC ID.
386 u8 nand_tech
= chip
->id
.data
[5] >> 4;
388 /* 1xnm technology */
389 if (nand_tech
== 4) {
390 for (i
= 0; i
< ARRAY_SIZE(hynix_mlc_1xnm_rr_otps
);
393 * FIXME: Hynix recommend to copy the
394 * read-retry OTP area into a normal page.
396 ret
= hynix_mlc_1xnm_rr_init(chip
,
397 hynix_mlc_1xnm_rr_otps
);
405 pr_warn("failed to initialize read-retry infrastructure");
410 static void hynix_nand_extract_oobsize(struct nand_chip
*chip
,
413 struct mtd_info
*mtd
= nand_to_mtd(chip
);
416 oobsize
= ((chip
->id
.data
[3] >> 2) & 0x3) |
417 ((chip
->id
.data
[3] >> 4) & 0x4);
435 * We should never reach this case, but if that
436 * happens, this probably means Hynix decided to use
437 * a different extended ID format, and we should find
438 * a way to support it.
440 WARN(1, "Invalid OOB size");
468 * We should never reach this case, but if that
469 * happens, this probably means Hynix decided to use
470 * a different extended ID format, and we should find
471 * a way to support it.
473 WARN(1, "Invalid OOB size");
479 static void hynix_nand_extract_ecc_requirements(struct nand_chip
*chip
,
482 u8 ecc_level
= (chip
->id
.data
[4] >> 4) & 0x7;
485 /* Reference: H27UCG8T2E datasheet */
486 chip
->ecc_step_ds
= 1024;
490 chip
->ecc_step_ds
= 0;
491 chip
->ecc_strength_ds
= 0;
494 chip
->ecc_strength_ds
= 4;
497 chip
->ecc_strength_ds
= 24;
500 chip
->ecc_strength_ds
= 32;
503 chip
->ecc_strength_ds
= 40;
506 chip
->ecc_strength_ds
= 50;
509 chip
->ecc_strength_ds
= 60;
513 * We should never reach this case, but if that
514 * happens, this probably means Hynix decided to use
515 * a different extended ID format, and we should find
516 * a way to support it.
518 WARN(1, "Invalid ECC requirements");
522 * The ECC requirements field meaning depends on the
525 u8 nand_tech
= chip
->id
.data
[5] & 0x7;
528 /* > 26nm, reference: H27UBG8T2A datasheet */
530 chip
->ecc_step_ds
= 512;
531 chip
->ecc_strength_ds
= 1 << ecc_level
;
532 } else if (ecc_level
< 7) {
534 chip
->ecc_step_ds
= 2048;
536 chip
->ecc_step_ds
= 1024;
537 chip
->ecc_strength_ds
= 24;
540 * We should never reach this case, but if that
541 * happens, this probably means Hynix decided
542 * to use a different extended ID format, and
543 * we should find a way to support it.
545 WARN(1, "Invalid ECC requirements");
548 /* <= 26nm, reference: H27UBG8T2B datasheet */
550 chip
->ecc_step_ds
= 0;
551 chip
->ecc_strength_ds
= 0;
552 } else if (ecc_level
< 5) {
553 chip
->ecc_step_ds
= 512;
554 chip
->ecc_strength_ds
= 1 << (ecc_level
- 1);
556 chip
->ecc_step_ds
= 1024;
557 chip
->ecc_strength_ds
= 24 +
558 (8 * (ecc_level
- 5));
564 static void hynix_nand_extract_scrambling_requirements(struct nand_chip
*chip
,
569 /* We need scrambling on all TLC NANDs*/
570 if (chip
->bits_per_cell
> 2)
571 chip
->options
|= NAND_NEED_SCRAMBLING
;
573 /* And on MLC NANDs with sub-3xnm process */
575 nand_tech
= chip
->id
.data
[5] >> 4;
579 chip
->options
|= NAND_NEED_SCRAMBLING
;
581 nand_tech
= chip
->id
.data
[5] & 0x7;
585 chip
->options
|= NAND_NEED_SCRAMBLING
;
589 static void hynix_nand_decode_id(struct nand_chip
*chip
)
591 struct mtd_info
*mtd
= nand_to_mtd(chip
);
596 * Exclude all SLC NANDs from this advanced detection scheme.
597 * According to the ranges defined in several datasheets, it might
598 * appear that even SLC NANDs could fall in this extended ID scheme.
599 * If that the case rework the test to let SLC NANDs go through the
602 if (chip
->id
.len
< 6 || nand_is_slc(chip
)) {
603 nand_decode_ext_id(chip
);
607 /* Extract pagesize */
608 mtd
->writesize
= 2048 << (chip
->id
.data
[3] & 0x03);
610 tmp
= (chip
->id
.data
[3] >> 4) & 0x3;
612 * When bit7 is set that means we start counting at 1MiB, otherwise
613 * we start counting at 128KiB and shift this value the content of
615 * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in
616 * this case the erasesize is set to 768KiB.
618 if (chip
->id
.data
[3] & 0x80)
619 mtd
->erasesize
= SZ_1M
<< tmp
;
621 mtd
->erasesize
= SZ_512K
+ SZ_256K
;
623 mtd
->erasesize
= SZ_128K
<< tmp
;
626 * Modern Toggle DDR NANDs have a valid JEDECID even though they are
627 * not exposing a valid JEDEC parameter table.
628 * These NANDs use a different NAND ID scheme.
630 valid_jedecid
= hynix_nand_has_valid_jedecid(chip
);
632 hynix_nand_extract_oobsize(chip
, valid_jedecid
);
633 hynix_nand_extract_ecc_requirements(chip
, valid_jedecid
);
634 hynix_nand_extract_scrambling_requirements(chip
, valid_jedecid
);
637 static void hynix_nand_cleanup(struct nand_chip
*chip
)
639 struct hynix_nand
*hynix
= nand_get_manufacturer_data(chip
);
644 kfree(hynix
->read_retry
);
646 nand_set_manufacturer_data(chip
, NULL
);
649 static int hynix_nand_init(struct nand_chip
*chip
)
651 struct hynix_nand
*hynix
;
654 if (!nand_is_slc(chip
))
655 chip
->bbt_options
|= NAND_BBT_SCANLASTPAGE
;
657 chip
->bbt_options
|= NAND_BBT_SCAN2NDPAGE
;
659 hynix
= kzalloc(sizeof(*hynix
), GFP_KERNEL
);
663 nand_set_manufacturer_data(chip
, hynix
);
665 ret
= hynix_nand_rr_init(chip
);
667 hynix_nand_cleanup(chip
);
672 const struct nand_manufacturer_ops hynix_nand_manuf_ops
= {
673 .detect
= hynix_nand_decode_id
,
674 .init
= hynix_nand_init
,
675 .cleanup
= hynix_nand_cleanup
,