xref: /openbmc/linux/drivers/mtd/nand/raw/nand_onfi.c (revision 82df5b73)
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 ONFI_PARAM_PAGES 3
20 
21 u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
22 {
23 	int i;
24 	while (len--) {
25 		crc ^= *p++ << 8;
26 		for (i = 0; i < 8; i++)
27 			crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
28 	}
29 
30 	return crc;
31 }
32 
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)
36 {
37 	struct onfi_ext_param_page *ep;
38 	struct onfi_ext_section *s;
39 	struct onfi_ext_ecc_info *ecc;
40 	uint8_t *cursor;
41 	int ret;
42 	int len;
43 	int i;
44 
45 	len = le16_to_cpu(p->ext_param_page_length) * 16;
46 	ep = kmalloc(len, GFP_KERNEL);
47 	if (!ep)
48 		return -ENOMEM;
49 
50 	/*
51 	 * Use the Change Read Column command to skip the ONFI param pages and
52 	 * ensure we read at the right location.
53 	 */
54 	ret = nand_change_read_column_op(chip,
55 					 sizeof(*p) * p->num_of_param_pages,
56 					 ep, len, true);
57 	if (ret)
58 		goto ext_out;
59 
60 	ret = -EINVAL;
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");
64 		goto ext_out;
65 	}
66 
67 	/*
68 	 * Check the signature.
69 	 * Do not strictly follow the ONFI spec, maybe changed in future.
70 	 */
71 	if (strncmp(ep->sig, "EPPS", 4)) {
72 		pr_debug("The signature is invalid.\n");
73 		goto ext_out;
74 	}
75 
76 	/* find the ECC section. */
77 	cursor = (uint8_t *)(ep + 1);
78 	for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
79 		s = ep->sections + i;
80 		if (s->type == ONFI_SECTION_TYPE_2)
81 			break;
82 		cursor += s->length * 16;
83 	}
84 	if (i == ONFI_EXT_SECTION_MAX) {
85 		pr_debug("We can not find the ECC section.\n");
86 		goto ext_out;
87 	}
88 
89 	/* get the info we want. */
90 	ecc = (struct onfi_ext_ecc_info *)cursor;
91 
92 	if (!ecc->codeword_size) {
93 		pr_debug("Invalid codeword size\n");
94 		goto ext_out;
95 	}
96 
97 	chip->base.eccreq.strength = ecc->ecc_bits;
98 	chip->base.eccreq.step_size = 1 << ecc->codeword_size;
99 	ret = 0;
100 
101 ext_out:
102 	kfree(ep);
103 	return ret;
104 }
105 
106 /*
107  * Recover data with bit-wise majority
108  */
109 static void nand_bit_wise_majority(const void **srcbufs,
110 				   unsigned int nsrcbufs,
111 				   void *dstbuf,
112 				   unsigned int bufsize)
113 {
114 	int i, j, k;
115 
116 	for (i = 0; i < bufsize; i++) {
117 		u8 val = 0;
118 
119 		for (j = 0; j < 8; j++) {
120 			unsigned int cnt = 0;
121 
122 			for (k = 0; k < nsrcbufs; k++) {
123 				const u8 *srcbuf = srcbufs[k];
124 
125 				if (srcbuf[i] & BIT(j))
126 					cnt++;
127 			}
128 
129 			if (cnt > nsrcbufs / 2)
130 				val |= BIT(j);
131 		}
132 
133 		((u8 *)dstbuf)[i] = val;
134 	}
135 }
136 
137 /*
138  * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
139  */
140 int nand_onfi_detect(struct nand_chip *chip)
141 {
142 	struct mtd_info *mtd = nand_to_mtd(chip);
143 	struct nand_memory_organization *memorg;
144 	struct nand_onfi_params *p = NULL, *pbuf;
145 	struct onfi_params *onfi;
146 	bool use_datain = false;
147 	int onfi_version = 0;
148 	char id[4];
149 	int i, ret, val;
150 	u16 crc;
151 
152 	memorg = nanddev_get_memorg(&chip->base);
153 
154 	/* Try ONFI for unknown chip or LP */
155 	ret = nand_readid_op(chip, 0x20, id, sizeof(id));
156 	if (ret || strncmp(id, "ONFI", 4))
157 		return 0;
158 
159 	/* ONFI chip: allocate a buffer to hold its parameter page */
160 	pbuf = kzalloc((sizeof(*pbuf) * ONFI_PARAM_PAGES), GFP_KERNEL);
161 	if (!pbuf)
162 		return -ENOMEM;
163 
164 	if (!nand_has_exec_op(chip) ||
165 	    !nand_read_data_op(chip, &pbuf[0], sizeof(*pbuf), true, true))
166 		use_datain = true;
167 
168 	for (i = 0; i < ONFI_PARAM_PAGES; i++) {
169 		if (!i)
170 			ret = nand_read_param_page_op(chip, 0, &pbuf[i],
171 						      sizeof(*pbuf));
172 		else if (use_datain)
173 			ret = nand_read_data_op(chip, &pbuf[i], sizeof(*pbuf),
174 						true, false);
175 		else
176 			ret = nand_change_read_column_op(chip, sizeof(*pbuf) * i,
177 							 &pbuf[i], sizeof(*pbuf),
178 							 true);
179 		if (ret) {
180 			ret = 0;
181 			goto free_onfi_param_page;
182 		}
183 
184 		crc = onfi_crc16(ONFI_CRC_BASE, (u8 *)&pbuf[i], 254);
185 		if (crc == le16_to_cpu(pbuf[i].crc)) {
186 			p = &pbuf[i];
187 			break;
188 		}
189 	}
190 
191 	if (i == ONFI_PARAM_PAGES) {
192 		const void *srcbufs[ONFI_PARAM_PAGES];
193 		unsigned int j;
194 
195 		for (j = 0; j < ONFI_PARAM_PAGES; j++)
196 			srcbufs[j] = pbuf + j;
197 
198 		pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
199 		nand_bit_wise_majority(srcbufs, ONFI_PARAM_PAGES, pbuf,
200 				       sizeof(*pbuf));
201 
202 		crc = onfi_crc16(ONFI_CRC_BASE, (u8 *)pbuf, 254);
203 		if (crc != le16_to_cpu(pbuf->crc)) {
204 			pr_err("ONFI parameter recovery failed, aborting\n");
205 			goto free_onfi_param_page;
206 		}
207 		p = pbuf;
208 	}
209 
210 	if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
211 	    chip->manufacturer.desc->ops->fixup_onfi_param_page)
212 		chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
213 
214 	/* Check version */
215 	val = le16_to_cpu(p->revision);
216 	if (val & ONFI_VERSION_2_3)
217 		onfi_version = 23;
218 	else if (val & ONFI_VERSION_2_2)
219 		onfi_version = 22;
220 	else if (val & ONFI_VERSION_2_1)
221 		onfi_version = 21;
222 	else if (val & ONFI_VERSION_2_0)
223 		onfi_version = 20;
224 	else if (val & ONFI_VERSION_1_0)
225 		onfi_version = 10;
226 
227 	if (!onfi_version) {
228 		pr_info("unsupported ONFI version: %d\n", val);
229 		goto free_onfi_param_page;
230 	}
231 
232 	sanitize_string(p->manufacturer, sizeof(p->manufacturer));
233 	sanitize_string(p->model, sizeof(p->model));
234 	chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
235 	if (!chip->parameters.model) {
236 		ret = -ENOMEM;
237 		goto free_onfi_param_page;
238 	}
239 
240 	memorg->pagesize = le32_to_cpu(p->byte_per_page);
241 	mtd->writesize = memorg->pagesize;
242 
243 	/*
244 	 * pages_per_block and blocks_per_lun may not be a power-of-2 size
245 	 * (don't ask me who thought of this...). MTD assumes that these
246 	 * dimensions will be power-of-2, so just truncate the remaining area.
247 	 */
248 	memorg->pages_per_eraseblock =
249 			1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
250 	mtd->erasesize = memorg->pages_per_eraseblock * memorg->pagesize;
251 
252 	memorg->oobsize = le16_to_cpu(p->spare_bytes_per_page);
253 	mtd->oobsize = memorg->oobsize;
254 
255 	memorg->luns_per_target = p->lun_count;
256 	memorg->planes_per_lun = 1 << p->interleaved_bits;
257 
258 	/* See erasesize comment */
259 	memorg->eraseblocks_per_lun =
260 		1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
261 	memorg->max_bad_eraseblocks_per_lun = le32_to_cpu(p->blocks_per_lun);
262 	memorg->bits_per_cell = p->bits_per_cell;
263 
264 	if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
265 		chip->options |= NAND_BUSWIDTH_16;
266 
267 	if (p->ecc_bits != 0xff) {
268 		chip->base.eccreq.strength = p->ecc_bits;
269 		chip->base.eccreq.step_size = 512;
270 	} else if (onfi_version >= 21 &&
271 		(le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
272 
273 		/*
274 		 * The nand_flash_detect_ext_param_page() uses the
275 		 * Change Read Column command which maybe not supported
276 		 * by the chip->legacy.cmdfunc. So try to update the
277 		 * chip->legacy.cmdfunc now. We do not replace user supplied
278 		 * command function.
279 		 */
280 		nand_legacy_adjust_cmdfunc(chip);
281 
282 		/* The Extended Parameter Page is supported since ONFI 2.1. */
283 		if (nand_flash_detect_ext_param_page(chip, p))
284 			pr_warn("Failed to detect ONFI extended param page\n");
285 	} else {
286 		pr_warn("Could not retrieve ONFI ECC requirements\n");
287 	}
288 
289 	/* Save some parameters from the parameter page for future use */
290 	if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
291 		chip->parameters.supports_set_get_features = true;
292 		bitmap_set(chip->parameters.get_feature_list,
293 			   ONFI_FEATURE_ADDR_TIMING_MODE, 1);
294 		bitmap_set(chip->parameters.set_feature_list,
295 			   ONFI_FEATURE_ADDR_TIMING_MODE, 1);
296 	}
297 
298 	onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
299 	if (!onfi) {
300 		ret = -ENOMEM;
301 		goto free_model;
302 	}
303 
304 	onfi->version = onfi_version;
305 	onfi->tPROG = le16_to_cpu(p->t_prog);
306 	onfi->tBERS = le16_to_cpu(p->t_bers);
307 	onfi->tR = le16_to_cpu(p->t_r);
308 	onfi->tCCS = le16_to_cpu(p->t_ccs);
309 	onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
310 	onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
311 	memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
312 	chip->parameters.onfi = onfi;
313 
314 	/* Identification done, free the full ONFI parameter page and exit */
315 	kfree(pbuf);
316 
317 	return 1;
318 
319 free_model:
320 	kfree(chip->parameters.model);
321 free_onfi_param_page:
322 	kfree(pbuf);
323 
324 	return ret;
325 }
326