1 // SPDX-License-Identifier: GPL-2.0
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 u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
24 for (i = 0; i < 8; i++)
25 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
31 /* Parse the Extended Parameter Page. */
32 static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
33 struct nand_onfi_params *p)
35 struct onfi_ext_param_page *ep;
36 struct onfi_ext_section *s;
37 struct onfi_ext_ecc_info *ecc;
43 len = le16_to_cpu(p->ext_param_page_length) * 16;
44 ep = kmalloc(len, GFP_KERNEL);
48 /* Send our own NAND_CMD_PARAM. */
49 ret = nand_read_param_page_op(chip, 0, NULL, 0);
53 /* Use the Change Read Column command to skip the ONFI param pages. */
54 ret = nand_change_read_column_op(chip,
55 sizeof(*p) * p->num_of_param_pages,
61 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
62 != le16_to_cpu(ep->crc))) {
63 pr_debug("fail in the CRC.\n");
68 * Check the signature.
69 * Do not strictly follow the ONFI spec, maybe changed in future.
71 if (strncmp(ep->sig, "EPPS", 4)) {
72 pr_debug("The signature is invalid.\n");
76 /* find the ECC section. */
77 cursor = (uint8_t *)(ep + 1);
78 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
80 if (s->type == ONFI_SECTION_TYPE_2)
82 cursor += s->length * 16;
84 if (i == ONFI_EXT_SECTION_MAX) {
85 pr_debug("We can not find the ECC section.\n");
89 /* get the info we want. */
90 ecc = (struct onfi_ext_ecc_info *)cursor;
92 if (!ecc->codeword_size) {
93 pr_debug("Invalid codeword size\n");
97 chip->ecc_strength_ds = ecc->ecc_bits;
98 chip->ecc_step_ds = 1 << ecc->codeword_size;
107 * Recover data with bit-wise majority
109 static void nand_bit_wise_majority(const void **srcbufs,
110 unsigned int nsrcbufs,
112 unsigned int bufsize)
116 for (i = 0; i < bufsize; i++) {
119 for (j = 0; j < 8; j++) {
120 unsigned int cnt = 0;
122 for (k = 0; k < nsrcbufs; k++) {
123 const u8 *srcbuf = srcbufs[k];
125 if (srcbuf[i] & BIT(j))
129 if (cnt > nsrcbufs / 2)
133 ((u8 *)dstbuf)[i] = val;
138 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
140 int nand_onfi_detect(struct nand_chip *chip)
142 struct mtd_info *mtd = nand_to_mtd(chip);
143 struct nand_onfi_params *p;
144 struct onfi_params *onfi;
145 int onfi_version = 0;
149 /* Try ONFI for unknown chip or LP */
150 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
151 if (ret || strncmp(id, "ONFI", 4))
154 /* ONFI chip: allocate a buffer to hold its parameter page */
155 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
159 ret = nand_read_param_page_op(chip, 0, NULL, 0);
162 goto free_onfi_param_page;
165 for (i = 0; i < 3; i++) {
166 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
169 goto free_onfi_param_page;
172 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
173 le16_to_cpu(p->crc)) {
175 memcpy(p, &p[i], sizeof(*p));
181 const void *srcbufs[3] = {p, p + 1, p + 2};
183 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
184 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
187 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
188 le16_to_cpu(p->crc)) {
189 pr_err("ONFI parameter recovery failed, aborting\n");
190 goto free_onfi_param_page;
194 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
195 chip->manufacturer.desc->ops->fixup_onfi_param_page)
196 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
199 val = le16_to_cpu(p->revision);
200 if (val & ONFI_VERSION_2_3)
202 else if (val & ONFI_VERSION_2_2)
204 else if (val & ONFI_VERSION_2_1)
206 else if (val & ONFI_VERSION_2_0)
208 else if (val & ONFI_VERSION_1_0)
212 pr_info("unsupported ONFI version: %d\n", val);
213 goto free_onfi_param_page;
216 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
217 sanitize_string(p->model, sizeof(p->model));
218 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
219 if (!chip->parameters.model) {
221 goto free_onfi_param_page;
224 mtd->writesize = le32_to_cpu(p->byte_per_page);
227 * pages_per_block and blocks_per_lun may not be a power-of-2 size
228 * (don't ask me who thought of this...). MTD assumes that these
229 * dimensions will be power-of-2, so just truncate the remaining area.
231 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
232 mtd->erasesize *= mtd->writesize;
234 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
236 /* See erasesize comment */
237 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
238 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
239 chip->bits_per_cell = p->bits_per_cell;
241 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
242 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
244 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
245 chip->options |= NAND_BUSWIDTH_16;
247 if (p->ecc_bits != 0xff) {
248 chip->ecc_strength_ds = p->ecc_bits;
249 chip->ecc_step_ds = 512;
250 } else if (onfi_version >= 21 &&
251 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
254 * The nand_flash_detect_ext_param_page() uses the
255 * Change Read Column command which maybe not supported
256 * by the chip->legacy.cmdfunc. So try to update the
257 * chip->legacy.cmdfunc now. We do not replace user supplied
260 nand_legacy_adjust_cmdfunc(chip);
262 /* The Extended Parameter Page is supported since ONFI 2.1. */
263 if (nand_flash_detect_ext_param_page(chip, p))
264 pr_warn("Failed to detect ONFI extended param page\n");
266 pr_warn("Could not retrieve ONFI ECC requirements\n");
269 /* Save some parameters from the parameter page for future use */
270 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
271 chip->parameters.supports_set_get_features = true;
272 bitmap_set(chip->parameters.get_feature_list,
273 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
274 bitmap_set(chip->parameters.set_feature_list,
275 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
278 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
284 onfi->version = onfi_version;
285 onfi->tPROG = le16_to_cpu(p->t_prog);
286 onfi->tBERS = le16_to_cpu(p->t_bers);
287 onfi->tR = le16_to_cpu(p->t_r);
288 onfi->tCCS = le16_to_cpu(p->t_ccs);
289 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
290 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
291 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
292 chip->parameters.onfi = onfi;
294 /* Identification done, free the full ONFI parameter page and exit */
300 kfree(chip->parameters.model);
301 free_onfi_param_page: