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 ONFI_PARAM_PAGES 3
21 u16
onfi_crc16(u16 crc
, u8
const *p
, size_t len
)
26 for (i
= 0; i
< 8; i
++)
27 crc
= (crc
<< 1) ^ ((crc
& 0x8000) ? 0x8005 : 0);
33 /* Parse the Extended Parameter Page. */
34 static int nand_flash_detect_ext_param_page(struct nand_chip
*chip
,
35 struct nand_onfi_params
*p
)
37 struct nand_device
*base
= &chip
->base
;
38 struct nand_ecc_props requirements
;
39 struct onfi_ext_param_page
*ep
;
40 struct onfi_ext_section
*s
;
41 struct onfi_ext_ecc_info
*ecc
;
47 len
= le16_to_cpu(p
->ext_param_page_length
) * 16;
48 ep
= kmalloc(len
, GFP_KERNEL
);
53 * Use the Change Read Column command to skip the ONFI param pages and
54 * ensure we read at the right location.
56 ret
= nand_change_read_column_op(chip
,
57 sizeof(*p
) * p
->num_of_param_pages
,
63 if ((onfi_crc16(ONFI_CRC_BASE
, ((uint8_t *)ep
) + 2, len
- 2)
64 != le16_to_cpu(ep
->crc
))) {
65 pr_debug("fail in the CRC.\n");
70 * Check the signature.
71 * Do not strictly follow the ONFI spec, maybe changed in future.
73 if (strncmp(ep
->sig
, "EPPS", 4)) {
74 pr_debug("The signature is invalid.\n");
78 /* find the ECC section. */
79 cursor
= (uint8_t *)(ep
+ 1);
80 for (i
= 0; i
< ONFI_EXT_SECTION_MAX
; i
++) {
82 if (s
->type
== ONFI_SECTION_TYPE_2
)
84 cursor
+= s
->length
* 16;
86 if (i
== ONFI_EXT_SECTION_MAX
) {
87 pr_debug("We can not find the ECC section.\n");
91 /* get the info we want. */
92 ecc
= (struct onfi_ext_ecc_info
*)cursor
;
94 if (!ecc
->codeword_size
) {
95 pr_debug("Invalid codeword size\n");
99 requirements
.strength
= ecc
->ecc_bits
;
100 requirements
.step_size
= 1 << ecc
->codeword_size
;
101 nanddev_set_ecc_requirements(base
, &requirements
);
111 * Recover data with bit-wise majority
113 static void nand_bit_wise_majority(const void **srcbufs
,
114 unsigned int nsrcbufs
,
116 unsigned int bufsize
)
120 for (i
= 0; i
< bufsize
; i
++) {
123 for (j
= 0; j
< 8; j
++) {
124 unsigned int cnt
= 0;
126 for (k
= 0; k
< nsrcbufs
; k
++) {
127 const u8
*srcbuf
= srcbufs
[k
];
129 if (srcbuf
[i
] & BIT(j
))
133 if (cnt
> nsrcbufs
/ 2)
137 ((u8
*)dstbuf
)[i
] = val
;
142 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
144 int nand_onfi_detect(struct nand_chip
*chip
)
146 struct nand_device
*base
= &chip
->base
;
147 struct mtd_info
*mtd
= nand_to_mtd(chip
);
148 struct nand_memory_organization
*memorg
;
149 struct nand_onfi_params
*p
= NULL
, *pbuf
;
150 struct onfi_params
*onfi
;
151 bool use_datain
= false;
152 int onfi_version
= 0;
157 memorg
= nanddev_get_memorg(&chip
->base
);
159 /* Try ONFI for unknown chip or LP */
160 ret
= nand_readid_op(chip
, 0x20, id
, sizeof(id
));
161 if (ret
|| strncmp(id
, "ONFI", 4))
164 /* ONFI chip: allocate a buffer to hold its parameter page */
165 pbuf
= kzalloc((sizeof(*pbuf
) * ONFI_PARAM_PAGES
), GFP_KERNEL
);
169 if (!nand_has_exec_op(chip
) ||
170 !nand_read_data_op(chip
, &pbuf
[0], sizeof(*pbuf
), true, true))
173 for (i
= 0; i
< ONFI_PARAM_PAGES
; i
++) {
175 ret
= nand_read_param_page_op(chip
, 0, &pbuf
[i
],
178 ret
= nand_read_data_op(chip
, &pbuf
[i
], sizeof(*pbuf
),
181 ret
= nand_change_read_column_op(chip
, sizeof(*pbuf
) * i
,
182 &pbuf
[i
], sizeof(*pbuf
),
186 goto free_onfi_param_page
;
189 crc
= onfi_crc16(ONFI_CRC_BASE
, (u8
*)&pbuf
[i
], 254);
190 if (crc
== le16_to_cpu(pbuf
[i
].crc
)) {
196 if (i
== ONFI_PARAM_PAGES
) {
197 const void *srcbufs
[ONFI_PARAM_PAGES
];
200 for (j
= 0; j
< ONFI_PARAM_PAGES
; j
++)
201 srcbufs
[j
] = pbuf
+ j
;
203 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
204 nand_bit_wise_majority(srcbufs
, ONFI_PARAM_PAGES
, pbuf
,
207 crc
= onfi_crc16(ONFI_CRC_BASE
, (u8
*)pbuf
, 254);
208 if (crc
!= le16_to_cpu(pbuf
->crc
)) {
209 pr_err("ONFI parameter recovery failed, aborting\n");
210 goto free_onfi_param_page
;
215 if (chip
->manufacturer
.desc
&& chip
->manufacturer
.desc
->ops
&&
216 chip
->manufacturer
.desc
->ops
->fixup_onfi_param_page
)
217 chip
->manufacturer
.desc
->ops
->fixup_onfi_param_page(chip
, p
);
220 val
= le16_to_cpu(p
->revision
);
221 if (val
& ONFI_VERSION_2_3
)
223 else if (val
& ONFI_VERSION_2_2
)
225 else if (val
& ONFI_VERSION_2_1
)
227 else if (val
& ONFI_VERSION_2_0
)
229 else if (val
& ONFI_VERSION_1_0
)
233 pr_info("unsupported ONFI version: %d\n", val
);
234 goto free_onfi_param_page
;
237 sanitize_string(p
->manufacturer
, sizeof(p
->manufacturer
));
238 sanitize_string(p
->model
, sizeof(p
->model
));
239 chip
->parameters
.model
= kstrdup(p
->model
, GFP_KERNEL
);
240 if (!chip
->parameters
.model
) {
242 goto free_onfi_param_page
;
245 memorg
->pagesize
= le32_to_cpu(p
->byte_per_page
);
246 mtd
->writesize
= memorg
->pagesize
;
249 * pages_per_block and blocks_per_lun may not be a power-of-2 size
250 * (don't ask me who thought of this...). MTD assumes that these
251 * dimensions will be power-of-2, so just truncate the remaining area.
253 memorg
->pages_per_eraseblock
=
254 1 << (fls(le32_to_cpu(p
->pages_per_block
)) - 1);
255 mtd
->erasesize
= memorg
->pages_per_eraseblock
* memorg
->pagesize
;
257 memorg
->oobsize
= le16_to_cpu(p
->spare_bytes_per_page
);
258 mtd
->oobsize
= memorg
->oobsize
;
260 memorg
->luns_per_target
= p
->lun_count
;
261 memorg
->planes_per_lun
= 1 << p
->interleaved_bits
;
263 /* See erasesize comment */
264 memorg
->eraseblocks_per_lun
=
265 1 << (fls(le32_to_cpu(p
->blocks_per_lun
)) - 1);
266 memorg
->max_bad_eraseblocks_per_lun
= le32_to_cpu(p
->blocks_per_lun
);
267 memorg
->bits_per_cell
= p
->bits_per_cell
;
269 if (le16_to_cpu(p
->features
) & ONFI_FEATURE_16_BIT_BUS
)
270 chip
->options
|= NAND_BUSWIDTH_16
;
272 if (p
->ecc_bits
!= 0xff) {
273 struct nand_ecc_props requirements
= {
274 .strength
= p
->ecc_bits
,
278 nanddev_set_ecc_requirements(base
, &requirements
);
279 } else if (onfi_version
>= 21 &&
280 (le16_to_cpu(p
->features
) & ONFI_FEATURE_EXT_PARAM_PAGE
)) {
283 * The nand_flash_detect_ext_param_page() uses the
284 * Change Read Column command which maybe not supported
285 * by the chip->legacy.cmdfunc. So try to update the
286 * chip->legacy.cmdfunc now. We do not replace user supplied
289 nand_legacy_adjust_cmdfunc(chip
);
291 /* The Extended Parameter Page is supported since ONFI 2.1. */
292 if (nand_flash_detect_ext_param_page(chip
, p
))
293 pr_warn("Failed to detect ONFI extended param page\n");
295 pr_warn("Could not retrieve ONFI ECC requirements\n");
298 /* Save some parameters from the parameter page for future use */
299 if (le16_to_cpu(p
->opt_cmd
) & ONFI_OPT_CMD_SET_GET_FEATURES
) {
300 chip
->parameters
.supports_set_get_features
= true;
301 bitmap_set(chip
->parameters
.get_feature_list
,
302 ONFI_FEATURE_ADDR_TIMING_MODE
, 1);
303 bitmap_set(chip
->parameters
.set_feature_list
,
304 ONFI_FEATURE_ADDR_TIMING_MODE
, 1);
307 onfi
= kzalloc(sizeof(*onfi
), GFP_KERNEL
);
313 onfi
->version
= onfi_version
;
314 onfi
->tPROG
= le16_to_cpu(p
->t_prog
);
315 onfi
->tBERS
= le16_to_cpu(p
->t_bers
);
316 onfi
->tR
= le16_to_cpu(p
->t_r
);
317 onfi
->tCCS
= le16_to_cpu(p
->t_ccs
);
318 onfi
->async_timing_mode
= le16_to_cpu(p
->async_timing_mode
);
319 onfi
->vendor_revision
= le16_to_cpu(p
->vendor_revision
);
320 memcpy(onfi
->vendor
, p
->vendor
, sizeof(p
->vendor
));
321 chip
->parameters
.onfi
= onfi
;
323 /* Identification done, free the full ONFI parameter page and exit */
329 kfree(chip
->parameters
.model
);
330 free_onfi_param_page
: