]> Git Repo - linux.git/blob - drivers/mtd/nand/raw/nand_onfi.c
Merge tag 'microblaze-v5.0-rc1' of git://git.monstr.eu/linux-2.6-microblaze
[linux.git] / drivers / mtd / nand / raw / nand_onfi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2000 Steven J. Hill ([email protected])
4  *                2002-2006 Thomas Gleixner ([email protected])
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 u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
20 {
21         int i;
22         while (len--) {
23                 crc ^= *p++ << 8;
24                 for (i = 0; i < 8; i++)
25                         crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
26         }
27
28         return crc;
29 }
30
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)
34 {
35         struct onfi_ext_param_page *ep;
36         struct onfi_ext_section *s;
37         struct onfi_ext_ecc_info *ecc;
38         uint8_t *cursor;
39         int ret;
40         int len;
41         int i;
42
43         len = le16_to_cpu(p->ext_param_page_length) * 16;
44         ep = kmalloc(len, GFP_KERNEL);
45         if (!ep)
46                 return -ENOMEM;
47
48         /* Send our own NAND_CMD_PARAM. */
49         ret = nand_read_param_page_op(chip, 0, NULL, 0);
50         if (ret)
51                 goto ext_out;
52
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,
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->ecc_strength_ds = ecc->ecc_bits;
98         chip->ecc_step_ds = 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_onfi_params *p;
144         struct onfi_params *onfi;
145         int onfi_version = 0;
146         char id[4];
147         int i, ret, val;
148
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))
152                 return 0;
153
154         /* ONFI chip: allocate a buffer to hold its parameter page */
155         p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
156         if (!p)
157                 return -ENOMEM;
158
159         ret = nand_read_param_page_op(chip, 0, NULL, 0);
160         if (ret) {
161                 ret = 0;
162                 goto free_onfi_param_page;
163         }
164
165         for (i = 0; i < 3; i++) {
166                 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
167                 if (ret) {
168                         ret = 0;
169                         goto free_onfi_param_page;
170                 }
171
172                 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
173                                 le16_to_cpu(p->crc)) {
174                         if (i)
175                                 memcpy(p, &p[i], sizeof(*p));
176                         break;
177                 }
178         }
179
180         if (i == 3) {
181                 const void *srcbufs[3] = {p, p + 1, p + 2};
182
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,
185                                        sizeof(*p));
186
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;
191                 }
192         }
193
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);
197
198         /* Check version */
199         val = le16_to_cpu(p->revision);
200         if (val & ONFI_VERSION_2_3)
201                 onfi_version = 23;
202         else if (val & ONFI_VERSION_2_2)
203                 onfi_version = 22;
204         else if (val & ONFI_VERSION_2_1)
205                 onfi_version = 21;
206         else if (val & ONFI_VERSION_2_0)
207                 onfi_version = 20;
208         else if (val & ONFI_VERSION_1_0)
209                 onfi_version = 10;
210
211         if (!onfi_version) {
212                 pr_info("unsupported ONFI version: %d\n", val);
213                 goto free_onfi_param_page;
214         }
215
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) {
220                 ret = -ENOMEM;
221                 goto free_onfi_param_page;
222         }
223
224         mtd->writesize = le32_to_cpu(p->byte_per_page);
225
226         /*
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.
230          */
231         mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
232         mtd->erasesize *= mtd->writesize;
233
234         mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
235
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;
240
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);
243
244         if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
245                 chip->options |= NAND_BUSWIDTH_16;
246
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)) {
252
253                 /*
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
258                  * command function.
259                  */
260                 nand_legacy_adjust_cmdfunc(chip);
261
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");
265         } else {
266                 pr_warn("Could not retrieve ONFI ECC requirements\n");
267         }
268
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);
276         }
277
278         onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
279         if (!onfi) {
280                 ret = -ENOMEM;
281                 goto free_model;
282         }
283
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;
293
294         /* Identification done, free the full ONFI parameter page and exit */
295         kfree(p);
296
297         return 1;
298
299 free_model:
300         kfree(chip->parameters.model);
301 free_onfi_param_page:
302         kfree(p);
303
304         return ret;
305 }
This page took 0.051597 seconds and 4 git commands to generate.