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 #define JEDEC_PARAM_PAGES 3 20 21 /* 22 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. 23 */ 24 int nand_jedec_detect(struct nand_chip *chip) 25 { 26 struct mtd_info *mtd = nand_to_mtd(chip); 27 struct nand_memory_organization *memorg; 28 struct nand_jedec_params *p; 29 struct jedec_ecc_info *ecc; 30 bool use_datain = false; 31 int jedec_version = 0; 32 char id[5]; 33 int i, val, ret; 34 u16 crc; 35 36 memorg = nanddev_get_memorg(&chip->base); 37 38 /* Try JEDEC for unknown chip or LP */ 39 ret = nand_readid_op(chip, 0x40, id, sizeof(id)); 40 if (ret || strncmp(id, "JEDEC", sizeof(id))) 41 return 0; 42 43 /* JEDEC chip: allocate a buffer to hold its parameter page */ 44 p = kzalloc(sizeof(*p), GFP_KERNEL); 45 if (!p) 46 return -ENOMEM; 47 48 if (!nand_has_exec_op(chip) || 49 !nand_read_data_op(chip, p, sizeof(*p), true, true)) 50 use_datain = true; 51 52 for (i = 0; i < JEDEC_PARAM_PAGES; i++) { 53 if (!i) 54 ret = nand_read_param_page_op(chip, 0x40, p, 55 sizeof(*p)); 56 else if (use_datain) 57 ret = nand_read_data_op(chip, p, sizeof(*p), true, 58 false); 59 else 60 ret = nand_change_read_column_op(chip, sizeof(*p) * i, 61 p, sizeof(*p), true); 62 if (ret) { 63 ret = 0; 64 goto free_jedec_param_page; 65 } 66 67 crc = onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 510); 68 if (crc == le16_to_cpu(p->crc)) 69 break; 70 } 71 72 if (i == JEDEC_PARAM_PAGES) { 73 pr_err("Could not find valid JEDEC parameter page; aborting\n"); 74 goto free_jedec_param_page; 75 } 76 77 /* Check version */ 78 val = le16_to_cpu(p->revision); 79 if (val & (1 << 2)) 80 jedec_version = 10; 81 else if (val & (1 << 1)) 82 jedec_version = 1; /* vendor specific version */ 83 84 if (!jedec_version) { 85 pr_info("unsupported JEDEC version: %d\n", val); 86 goto free_jedec_param_page; 87 } 88 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) { 93 ret = -ENOMEM; 94 goto free_jedec_param_page; 95 } 96 97 memorg->pagesize = le32_to_cpu(p->byte_per_page); 98 mtd->writesize = memorg->pagesize; 99 100 /* Please reference to the comment for nand_flash_detect_onfi. */ 101 memorg->pages_per_eraseblock = 102 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); 103 mtd->erasesize = memorg->pages_per_eraseblock * memorg->pagesize; 104 105 memorg->oobsize = le16_to_cpu(p->spare_bytes_per_page); 106 mtd->oobsize = memorg->oobsize; 107 108 memorg->luns_per_target = p->lun_count; 109 memorg->planes_per_lun = 1 << p->multi_plane_addr; 110 111 /* Please reference to the comment for nand_flash_detect_onfi. */ 112 memorg->eraseblocks_per_lun = 113 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); 114 memorg->bits_per_cell = p->bits_per_cell; 115 116 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS) 117 chip->options |= NAND_BUSWIDTH_16; 118 119 /* ECC info */ 120 ecc = &p->ecc_info[0]; 121 122 if (ecc->codeword_size >= 9) { 123 chip->base.eccreq.strength = ecc->ecc_bits; 124 chip->base.eccreq.step_size = 1 << ecc->codeword_size; 125 } else { 126 pr_warn("Invalid codeword size\n"); 127 } 128 129 ret = 1; 130 131 free_jedec_param_page: 132 kfree(p); 133 return ret; 134 } 135