1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
4 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
7 * David Woodhouse for adding multichip support
9 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
10 * rework for 2K page size chips
12 * This file contains all ONFI helpers.
15 #include <linux/slab.h>
17 #include "internals.h"
19 #define JEDEC_PARAM_PAGES 3
22 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
24 int nand_jedec_detect(struct nand_chip
*chip
)
26 struct nand_device
*base
= &chip
->base
;
27 struct mtd_info
*mtd
= nand_to_mtd(chip
);
28 struct nand_memory_organization
*memorg
;
29 struct nand_jedec_params
*p
;
30 struct jedec_ecc_info
*ecc
;
31 bool use_datain
= false;
32 int jedec_version
= 0;
37 memorg
= nanddev_get_memorg(&chip
->base
);
39 /* Try JEDEC for unknown chip or LP */
40 ret
= nand_readid_op(chip
, 0x40, id
, sizeof(id
));
41 if (ret
|| strncmp(id
, "JEDEC", sizeof(id
)))
44 /* JEDEC chip: allocate a buffer to hold its parameter page */
45 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
49 if (!nand_has_exec_op(chip
) ||
50 !nand_read_data_op(chip
, p
, sizeof(*p
), true, true))
53 for (i
= 0; i
< JEDEC_PARAM_PAGES
; i
++) {
55 ret
= nand_read_param_page_op(chip
, 0x40, p
,
58 ret
= nand_read_data_op(chip
, p
, sizeof(*p
), true,
61 ret
= nand_change_read_column_op(chip
, sizeof(*p
) * i
,
65 goto free_jedec_param_page
;
68 crc
= onfi_crc16(ONFI_CRC_BASE
, (u8
*)p
, 510);
69 if (crc
== le16_to_cpu(p
->crc
))
73 if (i
== JEDEC_PARAM_PAGES
) {
74 pr_err("Could not find valid JEDEC parameter page; aborting\n");
75 goto free_jedec_param_page
;
79 val
= le16_to_cpu(p
->revision
);
82 else if (val
& (1 << 1))
83 jedec_version
= 1; /* vendor specific version */
86 pr_info("unsupported JEDEC version: %d\n", val
);
87 goto free_jedec_param_page
;
90 sanitize_string(p
->manufacturer
, sizeof(p
->manufacturer
));
91 sanitize_string(p
->model
, sizeof(p
->model
));
92 chip
->parameters
.model
= kstrdup(p
->model
, GFP_KERNEL
);
93 if (!chip
->parameters
.model
) {
95 goto free_jedec_param_page
;
98 memorg
->pagesize
= le32_to_cpu(p
->byte_per_page
);
99 mtd
->writesize
= memorg
->pagesize
;
101 /* Please reference to the comment for nand_flash_detect_onfi. */
102 memorg
->pages_per_eraseblock
=
103 1 << (fls(le32_to_cpu(p
->pages_per_block
)) - 1);
104 mtd
->erasesize
= memorg
->pages_per_eraseblock
* memorg
->pagesize
;
106 memorg
->oobsize
= le16_to_cpu(p
->spare_bytes_per_page
);
107 mtd
->oobsize
= memorg
->oobsize
;
109 memorg
->luns_per_target
= p
->lun_count
;
110 memorg
->planes_per_lun
= 1 << p
->multi_plane_addr
;
112 /* Please reference to the comment for nand_flash_detect_onfi. */
113 memorg
->eraseblocks_per_lun
=
114 1 << (fls(le32_to_cpu(p
->blocks_per_lun
)) - 1);
115 memorg
->bits_per_cell
= p
->bits_per_cell
;
117 if (le16_to_cpu(p
->features
) & JEDEC_FEATURE_16_BIT_BUS
)
118 chip
->options
|= NAND_BUSWIDTH_16
;
121 ecc
= &p
->ecc_info
[0];
123 if (ecc
->codeword_size
>= 9) {
124 struct nand_ecc_props requirements
= {
125 .strength
= ecc
->ecc_bits
,
126 .step_size
= 1 << ecc
->codeword_size
,
129 nanddev_set_ecc_requirements(base
, &requirements
);
131 pr_warn("Invalid codeword size\n");
136 free_jedec_param_page
: