Skip to content

Commit 8ae3fbf

Browse files
Boris Brezillonmiquelraynal
authored andcommitted
mtd: rawnand: Move JEDEC code to nand_jedec.c
This moves JEDEC related code to nand_jedec.c and JEDEC related struct/macros to include/linux/mtd/jedec.h. Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
1 parent 1c325cc commit 8ae3fbf

6 files changed

Lines changed: 210 additions & 175 deletions

File tree

‎drivers/mtd/nand/raw/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ obj-$(CONFIG_MTD_NAND_TEGRA) += tegra_nand.o
5959

6060
nand-objs := nand_base.o nand_legacy.o nand_bbt.o nand_timings.o nand_ids.o
6161
nand-objs += nand_onfi.o
62+
nand-objs += nand_jedec.o
6263
nand-objs += nand_amd.o
6364
nand-objs += nand_hynix.o
6465
nand-objs += nand_macronix.o

‎drivers/mtd/nand/raw/internals.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,7 @@ int nand_legacy_check_hooks(struct nand_chip *chip);
108108
u16 onfi_crc16(u16 crc, u8 const *p, size_t len);
109109
int nand_onfi_detect(struct nand_chip *chip);
110110

111+
/* JEDEC functions */
112+
int nand_jedec_detect(struct nand_chip *chip);
113+
111114
#endif /* __LINUX_RAWNAND_INTERNALS */

‎drivers/mtd/nand/raw/nand_base.c‎

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,102 +4387,6 @@ void sanitize_string(uint8_t *s, size_t len)
43874387
strim(s);
43884388
}
43894389

4390-
/*
4391-
* Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
4392-
*/
4393-
static int nand_flash_detect_jedec(struct nand_chip *chip)
4394-
{
4395-
struct mtd_info *mtd = nand_to_mtd(chip);
4396-
struct nand_jedec_params *p;
4397-
struct jedec_ecc_info *ecc;
4398-
int jedec_version = 0;
4399-
char id[5];
4400-
int i, val, ret;
4401-
4402-
/* Try JEDEC for unknown chip or LP */
4403-
ret = nand_readid_op(chip, 0x40, id, sizeof(id));
4404-
if (ret || strncmp(id, "JEDEC", sizeof(id)))
4405-
return 0;
4406-
4407-
/* JEDEC chip: allocate a buffer to hold its parameter page */
4408-
p = kzalloc(sizeof(*p), GFP_KERNEL);
4409-
if (!p)
4410-
return -ENOMEM;
4411-
4412-
ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
4413-
if (ret) {
4414-
ret = 0;
4415-
goto free_jedec_param_page;
4416-
}
4417-
4418-
for (i = 0; i < 3; i++) {
4419-
ret = nand_read_data_op(chip, p, sizeof(*p), true);
4420-
if (ret) {
4421-
ret = 0;
4422-
goto free_jedec_param_page;
4423-
}
4424-
4425-
if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
4426-
le16_to_cpu(p->crc))
4427-
break;
4428-
}
4429-
4430-
if (i == 3) {
4431-
pr_err("Could not find valid JEDEC parameter page; aborting\n");
4432-
goto free_jedec_param_page;
4433-
}
4434-
4435-
/* Check version */
4436-
val = le16_to_cpu(p->revision);
4437-
if (val & (1 << 2))
4438-
jedec_version = 10;
4439-
else if (val & (1 << 1))
4440-
jedec_version = 1; /* vendor specific version */
4441-
4442-
if (!jedec_version) {
4443-
pr_info("unsupported JEDEC version: %d\n", val);
4444-
goto free_jedec_param_page;
4445-
}
4446-
4447-
sanitize_string(p->manufacturer, sizeof(p->manufacturer));
4448-
sanitize_string(p->model, sizeof(p->model));
4449-
chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
4450-
if (!chip->parameters.model) {
4451-
ret = -ENOMEM;
4452-
goto free_jedec_param_page;
4453-
}
4454-
4455-
mtd->writesize = le32_to_cpu(p->byte_per_page);
4456-
4457-
/* Please reference to the comment for nand_flash_detect_onfi. */
4458-
mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
4459-
mtd->erasesize *= mtd->writesize;
4460-
4461-
mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
4462-
4463-
/* Please reference to the comment for nand_flash_detect_onfi. */
4464-
chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
4465-
chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
4466-
chip->bits_per_cell = p->bits_per_cell;
4467-
4468-
if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
4469-
chip->options |= NAND_BUSWIDTH_16;
4470-
4471-
/* ECC info */
4472-
ecc = &p->ecc_info[0];
4473-
4474-
if (ecc->codeword_size >= 9) {
4475-
chip->ecc_strength_ds = ecc->ecc_bits;
4476-
chip->ecc_step_ds = 1 << ecc->codeword_size;
4477-
} else {
4478-
pr_warn("Invalid codeword size\n");
4479-
}
4480-
4481-
free_jedec_param_page:
4482-
kfree(p);
4483-
return ret;
4484-
}
4485-
44864390
/*
44874391
* nand_id_has_period - Check if an ID string has a given wraparound period
44884392
* @id_data: the ID string
@@ -4795,7 +4699,7 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
47954699
goto ident_done;
47964700

47974701
/* Check if the chip is JEDEC compliant */
4798-
ret = nand_flash_detect_jedec(chip);
4702+
ret = nand_jedec_detect(chip);
47994703
if (ret < 0)
48004704
return ret;
48014705
else if (ret)

‎drivers/mtd/nand/raw/nand_jedec.c‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
4+
* 2002-2006 Thomas Gleixner (tglx@linutronix.de)
5+
*
6+
* Credits:
7+
* David Woodhouse for adding multichip support
8+
*
9+
* Aleph One Ltd. and Toby Churchill Ltd. for supporting the
10+
* rework for 2K page size chips
11+
*
12+
* This file contains all ONFI helpers.
13+
*/
14+
15+
#include <linux/slab.h>
16+
17+
#include "internals.h"
18+
19+
/*
20+
* Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
21+
*/
22+
int nand_jedec_detect(struct nand_chip *chip)
23+
{
24+
struct mtd_info *mtd = nand_to_mtd(chip);
25+
struct nand_jedec_params *p;
26+
struct jedec_ecc_info *ecc;
27+
int jedec_version = 0;
28+
char id[5];
29+
int i, val, ret;
30+
31+
/* Try JEDEC for unknown chip or LP */
32+
ret = nand_readid_op(chip, 0x40, id, sizeof(id));
33+
if (ret || strncmp(id, "JEDEC", sizeof(id)))
34+
return 0;
35+
36+
/* JEDEC chip: allocate a buffer to hold its parameter page */
37+
p = kzalloc(sizeof(*p), GFP_KERNEL);
38+
if (!p)
39+
return -ENOMEM;
40+
41+
ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
42+
if (ret) {
43+
ret = 0;
44+
goto free_jedec_param_page;
45+
}
46+
47+
for (i = 0; i < 3; i++) {
48+
ret = nand_read_data_op(chip, p, sizeof(*p), true);
49+
if (ret) {
50+
ret = 0;
51+
goto free_jedec_param_page;
52+
}
53+
54+
if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
55+
le16_to_cpu(p->crc))
56+
break;
57+
}
58+
59+
if (i == 3) {
60+
pr_err("Could not find valid JEDEC parameter page; aborting\n");
61+
goto free_jedec_param_page;
62+
}
63+
64+
/* Check version */
65+
val = le16_to_cpu(p->revision);
66+
if (val & (1 << 2))
67+
jedec_version = 10;
68+
else if (val & (1 << 1))
69+
jedec_version = 1; /* vendor specific version */
70+
71+
if (!jedec_version) {
72+
pr_info("unsupported JEDEC version: %d\n", val);
73+
goto free_jedec_param_page;
74+
}
75+
76+
sanitize_string(p->manufacturer, sizeof(p->manufacturer));
77+
sanitize_string(p->model, sizeof(p->model));
78+
chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
79+
if (!chip->parameters.model) {
80+
ret = -ENOMEM;
81+
goto free_jedec_param_page;
82+
}
83+
84+
mtd->writesize = le32_to_cpu(p->byte_per_page);
85+
86+
/* Please reference to the comment for nand_flash_detect_onfi. */
87+
mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
88+
mtd->erasesize *= mtd->writesize;
89+
90+
mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
91+
92+
/* Please reference to the comment for nand_flash_detect_onfi. */
93+
chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
94+
chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
95+
chip->bits_per_cell = p->bits_per_cell;
96+
97+
if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
98+
chip->options |= NAND_BUSWIDTH_16;
99+
100+
/* ECC info */
101+
ecc = &p->ecc_info[0];
102+
103+
if (ecc->codeword_size >= 9) {
104+
chip->ecc_strength_ds = ecc->ecc_bits;
105+
chip->ecc_step_ds = 1 << ecc->codeword_size;
106+
} else {
107+
pr_warn("Invalid codeword size\n");
108+
}
109+
110+
free_jedec_param_page:
111+
kfree(p);
112+
return ret;
113+
}

‎include/linux/mtd/jedec.h‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
4+
* Steven J. Hill <sjhill@realitydiluted.com>
5+
* Thomas Gleixner <tglx@linutronix.de>
6+
*
7+
* Contains all JEDEC related definitions
8+
*/
9+
10+
#ifndef __LINUX_MTD_JEDEC_H
11+
#define __LINUX_MTD_JEDEC_H
12+
13+
struct jedec_ecc_info {
14+
u8 ecc_bits;
15+
u8 codeword_size;
16+
__le16 bb_per_lun;
17+
__le16 block_endurance;
18+
u8 reserved[2];
19+
} __packed;
20+
21+
/* JEDEC features */
22+
#define JEDEC_FEATURE_16_BIT_BUS (1 << 0)
23+
24+
struct nand_jedec_params {
25+
/* rev info and features block */
26+
/* 'J' 'E' 'S' 'D' */
27+
u8 sig[4];
28+
__le16 revision;
29+
__le16 features;
30+
u8 opt_cmd[3];
31+
__le16 sec_cmd;
32+
u8 num_of_param_pages;
33+
u8 reserved0[18];
34+
35+
/* manufacturer information block */
36+
char manufacturer[12];
37+
char model[20];
38+
u8 jedec_id[6];
39+
u8 reserved1[10];
40+
41+
/* memory organization block */
42+
__le32 byte_per_page;
43+
__le16 spare_bytes_per_page;
44+
u8 reserved2[6];
45+
__le32 pages_per_block;
46+
__le32 blocks_per_lun;
47+
u8 lun_count;
48+
u8 addr_cycles;
49+
u8 bits_per_cell;
50+
u8 programs_per_page;
51+
u8 multi_plane_addr;
52+
u8 multi_plane_op_attr;
53+
u8 reserved3[38];
54+
55+
/* electrical parameter block */
56+
__le16 async_sdr_speed_grade;
57+
__le16 toggle_ddr_speed_grade;
58+
__le16 sync_ddr_speed_grade;
59+
u8 async_sdr_features;
60+
u8 toggle_ddr_features;
61+
u8 sync_ddr_features;
62+
__le16 t_prog;
63+
__le16 t_bers;
64+
__le16 t_r;
65+
__le16 t_r_multi_plane;
66+
__le16 t_ccs;
67+
__le16 io_pin_capacitance_typ;
68+
__le16 input_pin_capacitance_typ;
69+
__le16 clk_pin_capacitance_typ;
70+
u8 driver_strength_support;
71+
__le16 t_adl;
72+
u8 reserved4[36];
73+
74+
/* ECC and endurance block */
75+
u8 guaranteed_good_blocks;
76+
__le16 guaranteed_block_endurance;
77+
struct jedec_ecc_info ecc_info[4];
78+
u8 reserved5[29];
79+
80+
/* reserved */
81+
u8 reserved6[148];
82+
83+
/* vendor */
84+
__le16 vendor_rev_num;
85+
u8 reserved7[88];
86+
87+
/* CRC for Parameter Page */
88+
__le16 crc;
89+
} __packed;
90+
91+
#endif /* __LINUX_MTD_JEDEC_H */

0 commit comments

Comments
 (0)