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) || chip->controller->supported_op.data_only_read)
52 for (i = 0; i < JEDEC_PARAM_PAGES; i++) {
54 ret = nand_read_param_page_op(chip, 0x40, p,
57 ret = nand_read_data_op(chip, p, sizeof(*p), true,
60 ret = nand_change_read_column_op(chip, sizeof(*p) * i,
64 goto free_jedec_param_page;
67 crc = onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 510);
68 if (crc == le16_to_cpu(p->crc))
72 if (i == JEDEC_PARAM_PAGES) {
73 pr_err("Could not find valid JEDEC parameter page; aborting\n");
74 goto free_jedec_param_page;
78 val = le16_to_cpu(p->revision);
81 else if (val & (1 << 1))
82 jedec_version = 1; /* vendor specific version */
85 pr_info("unsupported JEDEC version: %d\n", val);
86 goto free_jedec_param_page;
89 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
90 sanitize_string(p->model, sizeof(p->model));
91 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
92 if (!chip->parameters.model) {
94 goto free_jedec_param_page;
97 if (p->opt_cmd[0] & JEDEC_OPT_CMD_READ_CACHE)
98 chip->parameters.supports_read_cache = true;
100 memorg->pagesize = le32_to_cpu(p->byte_per_page);
101 mtd->writesize = memorg->pagesize;
103 /* Please reference to the comment for nand_flash_detect_onfi. */
104 memorg->pages_per_eraseblock =
105 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
106 mtd->erasesize = memorg->pages_per_eraseblock * memorg->pagesize;
108 memorg->oobsize = le16_to_cpu(p->spare_bytes_per_page);
109 mtd->oobsize = memorg->oobsize;
111 memorg->luns_per_target = p->lun_count;
112 memorg->planes_per_lun = 1 << p->multi_plane_addr;
114 /* Please reference to the comment for nand_flash_detect_onfi. */
115 memorg->eraseblocks_per_lun =
116 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
117 memorg->bits_per_cell = p->bits_per_cell;
119 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
120 chip->options |= NAND_BUSWIDTH_16;
123 ecc = &p->ecc_info[0];
125 if (ecc->codeword_size >= 9) {
126 struct nand_ecc_props requirements = {
127 .strength = ecc->ecc_bits,
128 .step_size = 1 << ecc->codeword_size,
131 nanddev_set_ecc_requirements(base, &requirements);
133 pr_warn("Invalid codeword size\n");
138 free_jedec_param_page: