blob: 7eec0d96909af2097fdb736cd0f8bd9e793d6362 [file] [log] [blame]
Boris Brezillon01389b62016-06-08 10:30:18 +02001/*
2 * Copyright (C) 2017 Free Electrons
3 * Copyright (C) 2017 NextThing Co
4 *
5 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Boris Brezillond4092d72017-08-04 17:29:10 +020018#include <linux/mtd/rawnand.h>
Boris Brezillon78f34822016-05-27 14:36:36 +020019#include <linux/sizes.h>
Boris Brezillon626994e2016-05-27 10:15:03 +020020#include <linux/slab.h>
21
22#define NAND_HYNIX_CMD_SET_PARAMS 0x36
23#define NAND_HYNIX_CMD_APPLY_PARAMS 0x16
24
25#define NAND_HYNIX_1XNM_RR_REPEAT 8
26
27/**
28 * struct hynix_read_retry - read-retry data
29 * @nregs: number of register to set when applying a new read-retry mode
30 * @regs: register offsets (NAND chip dependent)
31 * @values: array of values to set in registers. The array size is equal to
32 * (nregs * nmodes)
33 */
34struct hynix_read_retry {
35 int nregs;
36 const u8 *regs;
37 u8 values[0];
38};
39
40/**
41 * struct hynix_nand - private Hynix NAND struct
42 * @nand_technology: manufacturing process expressed in picometer
43 * @read_retry: read-retry information
44 */
45struct hynix_nand {
46 const struct hynix_read_retry *read_retry;
47};
48
49/**
50 * struct hynix_read_retry_otp - structure describing how the read-retry OTP
51 * area
52 * @nregs: number of hynix private registers to set before reading the reading
53 * the OTP area
54 * @regs: registers that should be configured
55 * @values: values that should be set in regs
56 * @page: the address to pass to the READ_PAGE command. Depends on the NAND
57 * chip
58 * @size: size of the read-retry OTP section
59 */
60struct hynix_read_retry_otp {
61 int nregs;
62 const u8 *regs;
63 const u8 *values;
64 int page;
65 int size;
66};
Boris Brezillon01389b62016-06-08 10:30:18 +020067
Boris Brezillon78f34822016-05-27 14:36:36 +020068static bool hynix_nand_has_valid_jedecid(struct nand_chip *chip)
Boris Brezillon01389b62016-06-08 10:30:18 +020069{
Boris Brezillon97d90da2017-11-30 18:01:29 +010070 u8 jedecid[5] = { };
71 int ret;
72
73 ret = nand_readid_op(chip, 0x40, jedecid, sizeof(jedecid));
74 if (ret)
75 return false;
76
77 return !strncmp("JEDEC", jedecid, sizeof(jedecid));
78}
79
80static int hynix_nand_cmd_op(struct nand_chip *chip, u8 cmd)
81{
Miquel Raynal8878b122017-11-09 14:16:45 +010082 if (chip->exec_op) {
83 struct nand_op_instr instrs[] = {
84 NAND_OP_CMD(cmd, 0),
85 };
86 struct nand_operation op = NAND_OPERATION(instrs);
87
88 return nand_exec_op(chip, &op);
89 }
90
Boris Brezillonbf6065c2018-09-07 00:38:36 +020091 chip->legacy.cmdfunc(chip, cmd, -1, -1);
Boris Brezillon01389b62016-06-08 10:30:18 +020092
Boris Brezillon97d90da2017-11-30 18:01:29 +010093 return 0;
94}
95
96static int hynix_nand_reg_write_op(struct nand_chip *chip, u8 addr, u8 val)
97{
Boris Brezillon97d90da2017-11-30 18:01:29 +010098 u16 column = ((u16)addr << 8) | addr;
99
Boris Brezillon20366e12018-07-04 16:08:58 +0200100 if (chip->exec_op) {
101 struct nand_op_instr instrs[] = {
102 NAND_OP_ADDR(1, &addr, 0),
103 NAND_OP_8BIT_DATA_OUT(1, &val, 0),
104 };
105 struct nand_operation op = NAND_OPERATION(instrs);
106
107 return nand_exec_op(chip, &op);
108 }
109
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200110 chip->legacy.cmdfunc(chip, NAND_CMD_NONE, column, -1);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200111 chip->legacy.write_byte(chip, val);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100112
113 return 0;
Boris Brezillon78f34822016-05-27 14:36:36 +0200114}
Boris Brezillon01389b62016-06-08 10:30:18 +0200115
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +0200116static int hynix_nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Boris Brezillon626994e2016-05-27 10:15:03 +0200117{
Boris Brezillon626994e2016-05-27 10:15:03 +0200118 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
119 const u8 *values;
Boris Brezillon97d90da2017-11-30 18:01:29 +0100120 int i, ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200121
122 values = hynix->read_retry->values +
123 (retry_mode * hynix->read_retry->nregs);
124
125 /* Enter 'Set Hynix Parameters' mode */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100126 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
127 if (ret)
128 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200129
130 /*
131 * Configure the NAND in the requested read-retry mode.
132 * This is done by setting pre-defined values in internal NAND
133 * registers.
134 *
135 * The set of registers is NAND specific, and the values are either
136 * predefined or extracted from an OTP area on the NAND (values are
137 * probably tweaked at production in this case).
138 */
139 for (i = 0; i < hynix->read_retry->nregs; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100140 ret = hynix_nand_reg_write_op(chip, hynix->read_retry->regs[i],
141 values[i]);
142 if (ret)
143 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200144 }
145
146 /* Apply the new settings. */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100147 return hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
Boris Brezillon626994e2016-05-27 10:15:03 +0200148}
149
150/**
151 * hynix_get_majority - get the value that is occurring the most in a given
152 * set of values
153 * @in: the array of values to test
154 * @repeat: the size of the in array
155 * @out: pointer used to store the output value
156 *
157 * This function implements the 'majority check' logic that is supposed to
158 * overcome the unreliability of MLC NANDs when reading the OTP area storing
159 * the read-retry parameters.
160 *
161 * It's based on a pretty simple assumption: if we repeat the same value
162 * several times and then take the one that is occurring the most, we should
163 * find the correct value.
164 * Let's hope this dummy algorithm prevents us from losing the read-retry
165 * parameters.
166 */
167static int hynix_get_majority(const u8 *in, int repeat, u8 *out)
168{
169 int i, j, half = repeat / 2;
170
171 /*
172 * We only test the first half of the in array because we must ensure
173 * that the value is at least occurring repeat / 2 times.
174 *
175 * This loop is suboptimal since we may count the occurrences of the
176 * same value several time, but we are doing that on small sets, which
177 * makes it acceptable.
178 */
179 for (i = 0; i < half; i++) {
180 int cnt = 0;
181 u8 val = in[i];
182
183 /* Count all values that are matching the one at index i. */
184 for (j = i + 1; j < repeat; j++) {
185 if (in[j] == val)
186 cnt++;
187 }
188
189 /* We found a value occurring more than repeat / 2. */
190 if (cnt > half) {
191 *out = val;
192 return 0;
193 }
194 }
195
196 return -EIO;
197}
198
199static int hynix_read_rr_otp(struct nand_chip *chip,
200 const struct hynix_read_retry_otp *info,
201 void *buf)
202{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100203 int i, ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200204
Boris Brezillon97d90da2017-11-30 18:01:29 +0100205 ret = nand_reset_op(chip);
206 if (ret)
207 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200208
Boris Brezillon97d90da2017-11-30 18:01:29 +0100209 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
210 if (ret)
211 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200212
213 for (i = 0; i < info->nregs; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100214 ret = hynix_nand_reg_write_op(chip, info->regs[i],
215 info->values[i]);
216 if (ret)
217 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200218 }
219
Boris Brezillon97d90da2017-11-30 18:01:29 +0100220 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
221 if (ret)
222 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200223
224 /* Sequence to enter OTP mode? */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100225 ret = hynix_nand_cmd_op(chip, 0x17);
226 if (ret)
227 return ret;
228
229 ret = hynix_nand_cmd_op(chip, 0x4);
230 if (ret)
231 return ret;
232
233 ret = hynix_nand_cmd_op(chip, 0x19);
234 if (ret)
235 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200236
237 /* Now read the page */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100238 ret = nand_read_page_op(chip, info->page, 0, buf, info->size);
239 if (ret)
240 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200241
242 /* Put everything back to normal */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100243 ret = nand_reset_op(chip);
244 if (ret)
245 return ret;
Boris Brezillon626994e2016-05-27 10:15:03 +0200246
Boris Brezillon97d90da2017-11-30 18:01:29 +0100247 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
248 if (ret)
249 return ret;
250
251 ret = hynix_nand_reg_write_op(chip, 0x38, 0);
252 if (ret)
253 return ret;
254
255 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
256 if (ret)
257 return ret;
258
259 return nand_read_page_op(chip, 0, 0, NULL, 0);
Boris Brezillon626994e2016-05-27 10:15:03 +0200260}
261
262#define NAND_HYNIX_1XNM_RR_COUNT_OFFS 0
263#define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS 8
264#define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv) \
265 (16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize)))
266
267static int hynix_mlc_1xnm_rr_value(const u8 *buf, int nmodes, int nregs,
268 int mode, int reg, bool inv, u8 *val)
269{
270 u8 tmp[NAND_HYNIX_1XNM_RR_REPEAT];
271 int val_offs = (mode * nregs) + reg;
272 int set_size = nmodes * nregs;
273 int i, ret;
274
275 for (i = 0; i < NAND_HYNIX_1XNM_RR_REPEAT; i++) {
276 int set_offs = NAND_HYNIX_1XNM_RR_SET_OFFS(i, set_size, inv);
277
278 tmp[i] = buf[val_offs + set_offs];
279 }
280
281 ret = hynix_get_majority(tmp, NAND_HYNIX_1XNM_RR_REPEAT, val);
282 if (ret)
283 return ret;
284
285 if (inv)
286 *val = ~*val;
287
288 return 0;
289}
290
291static u8 hynix_1xnm_mlc_read_retry_regs[] = {
292 0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf
293};
294
295static int hynix_mlc_1xnm_rr_init(struct nand_chip *chip,
296 const struct hynix_read_retry_otp *info)
297{
298 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
299 struct hynix_read_retry *rr = NULL;
300 int ret, i, j;
301 u8 nregs, nmodes;
302 u8 *buf;
303
304 buf = kmalloc(info->size, GFP_KERNEL);
305 if (!buf)
306 return -ENOMEM;
307
308 ret = hynix_read_rr_otp(chip, info, buf);
309 if (ret)
310 goto out;
311
312 ret = hynix_get_majority(buf, NAND_HYNIX_1XNM_RR_REPEAT,
313 &nmodes);
314 if (ret)
315 goto out;
316
317 ret = hynix_get_majority(buf + NAND_HYNIX_1XNM_RR_REPEAT,
318 NAND_HYNIX_1XNM_RR_REPEAT,
319 &nregs);
320 if (ret)
321 goto out;
322
323 rr = kzalloc(sizeof(*rr) + (nregs * nmodes), GFP_KERNEL);
Dan Carpenter4ca8c1d2017-03-22 12:01:45 +0300324 if (!rr) {
325 ret = -ENOMEM;
Boris Brezillon626994e2016-05-27 10:15:03 +0200326 goto out;
Dan Carpenter4ca8c1d2017-03-22 12:01:45 +0300327 }
Boris Brezillon626994e2016-05-27 10:15:03 +0200328
329 for (i = 0; i < nmodes; i++) {
330 for (j = 0; j < nregs; j++) {
331 u8 *val = rr->values + (i * nregs);
332
333 ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
334 false, val);
335 if (!ret)
336 continue;
337
338 ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
339 true, val);
340 if (ret)
341 goto out;
342 }
343 }
344
345 rr->nregs = nregs;
346 rr->regs = hynix_1xnm_mlc_read_retry_regs;
347 hynix->read_retry = rr;
348 chip->setup_read_retry = hynix_nand_setup_read_retry;
349 chip->read_retries = nmodes;
350
351out:
352 kfree(buf);
353
354 if (ret)
355 kfree(rr);
356
357 return ret;
358}
359
360static const u8 hynix_mlc_1xnm_rr_otp_regs[] = { 0x38 };
361static const u8 hynix_mlc_1xnm_rr_otp_values[] = { 0x52 };
362
363static const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps[] = {
364 {
365 .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
366 .regs = hynix_mlc_1xnm_rr_otp_regs,
367 .values = hynix_mlc_1xnm_rr_otp_values,
368 .page = 0x21f,
369 .size = 784
370 },
371 {
372 .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
373 .regs = hynix_mlc_1xnm_rr_otp_regs,
374 .values = hynix_mlc_1xnm_rr_otp_values,
375 .page = 0x200,
376 .size = 528,
377 },
378};
379
380static int hynix_nand_rr_init(struct nand_chip *chip)
381{
382 int i, ret = 0;
383 bool valid_jedecid;
384
385 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
386
387 /*
388 * We only support read-retry for 1xnm NANDs, and those NANDs all
389 * expose a valid JEDEC ID.
390 */
391 if (valid_jedecid) {
392 u8 nand_tech = chip->id.data[5] >> 4;
393
394 /* 1xnm technology */
395 if (nand_tech == 4) {
396 for (i = 0; i < ARRAY_SIZE(hynix_mlc_1xnm_rr_otps);
397 i++) {
398 /*
399 * FIXME: Hynix recommend to copy the
400 * read-retry OTP area into a normal page.
401 */
402 ret = hynix_mlc_1xnm_rr_init(chip,
403 hynix_mlc_1xnm_rr_otps);
404 if (!ret)
405 break;
406 }
407 }
408 }
409
410 if (ret)
411 pr_warn("failed to initialize read-retry infrastructure");
412
413 return 0;
414}
415
Boris Brezillon78f34822016-05-27 14:36:36 +0200416static void hynix_nand_extract_oobsize(struct nand_chip *chip,
417 bool valid_jedecid)
418{
419 struct mtd_info *mtd = nand_to_mtd(chip);
420 u8 oobsize;
421
422 oobsize = ((chip->id.data[3] >> 2) & 0x3) |
423 ((chip->id.data[3] >> 4) & 0x4);
424
425 if (valid_jedecid) {
426 switch (oobsize) {
427 case 0:
428 mtd->oobsize = 2048;
429 break;
430 case 1:
431 mtd->oobsize = 1664;
432 break;
433 case 2:
434 mtd->oobsize = 1024;
435 break;
436 case 3:
437 mtd->oobsize = 640;
438 break;
439 default:
440 /*
441 * We should never reach this case, but if that
442 * happens, this probably means Hynix decided to use
443 * a different extended ID format, and we should find
444 * a way to support it.
445 */
446 WARN(1, "Invalid OOB size");
447 break;
448 }
449 } else {
450 switch (oobsize) {
Boris Brezillon01389b62016-06-08 10:30:18 +0200451 case 0:
452 mtd->oobsize = 128;
453 break;
454 case 1:
455 mtd->oobsize = 224;
456 break;
457 case 2:
458 mtd->oobsize = 448;
459 break;
460 case 3:
461 mtd->oobsize = 64;
462 break;
463 case 4:
464 mtd->oobsize = 32;
465 break;
466 case 5:
467 mtd->oobsize = 16;
468 break;
Boris Brezillon78f34822016-05-27 14:36:36 +0200469 case 6:
Boris Brezillon01389b62016-06-08 10:30:18 +0200470 mtd->oobsize = 640;
471 break;
Boris Brezillon78f34822016-05-27 14:36:36 +0200472 default:
473 /*
474 * We should never reach this case, but if that
475 * happens, this probably means Hynix decided to use
476 * a different extended ID format, and we should find
477 * a way to support it.
478 */
479 WARN(1, "Invalid OOB size");
480 break;
Boris Brezillon01389b62016-06-08 10:30:18 +0200481 }
Martin Blumenstingl16c4fba2018-06-24 22:53:55 +0200482
483 /*
484 * The datasheet of H27UCG8T2BTR mentions that the "Redundant
485 * Area Size" is encoded "per 8KB" (page size). This chip uses
486 * a page size of 16KiB. The datasheet mentions an OOB size of
487 * 1.280 bytes, but the OOB size encoded in the ID bytes (using
488 * the existing logic above) is 640 bytes.
489 * Update the OOB size for this chip by taking the value
490 * determined above and scaling it to the actual page size (so
491 * the actual OOB size for this chip is: 640 * 16k / 8k).
492 */
493 if (chip->id.data[1] == 0xde)
494 mtd->oobsize *= mtd->writesize / SZ_8K;
Boris Brezillon01389b62016-06-08 10:30:18 +0200495 }
496}
497
Boris Brezillon78f34822016-05-27 14:36:36 +0200498static void hynix_nand_extract_ecc_requirements(struct nand_chip *chip,
499 bool valid_jedecid)
500{
501 u8 ecc_level = (chip->id.data[4] >> 4) & 0x7;
502
503 if (valid_jedecid) {
504 /* Reference: H27UCG8T2E datasheet */
505 chip->ecc_step_ds = 1024;
506
507 switch (ecc_level) {
508 case 0:
509 chip->ecc_step_ds = 0;
510 chip->ecc_strength_ds = 0;
511 break;
512 case 1:
513 chip->ecc_strength_ds = 4;
514 break;
515 case 2:
516 chip->ecc_strength_ds = 24;
517 break;
518 case 3:
519 chip->ecc_strength_ds = 32;
520 break;
521 case 4:
522 chip->ecc_strength_ds = 40;
523 break;
524 case 5:
525 chip->ecc_strength_ds = 50;
526 break;
527 case 6:
528 chip->ecc_strength_ds = 60;
529 break;
530 default:
531 /*
532 * We should never reach this case, but if that
533 * happens, this probably means Hynix decided to use
534 * a different extended ID format, and we should find
535 * a way to support it.
536 */
537 WARN(1, "Invalid ECC requirements");
538 }
539 } else {
540 /*
541 * The ECC requirements field meaning depends on the
542 * NAND technology.
543 */
Martin Blumenstinglfd213b52017-08-05 14:16:24 +0200544 u8 nand_tech = chip->id.data[5] & 0x7;
Boris Brezillon78f34822016-05-27 14:36:36 +0200545
546 if (nand_tech < 3) {
547 /* > 26nm, reference: H27UBG8T2A datasheet */
548 if (ecc_level < 5) {
549 chip->ecc_step_ds = 512;
550 chip->ecc_strength_ds = 1 << ecc_level;
551 } else if (ecc_level < 7) {
552 if (ecc_level == 5)
553 chip->ecc_step_ds = 2048;
554 else
555 chip->ecc_step_ds = 1024;
556 chip->ecc_strength_ds = 24;
557 } else {
558 /*
559 * We should never reach this case, but if that
560 * happens, this probably means Hynix decided
561 * to use a different extended ID format, and
562 * we should find a way to support it.
563 */
564 WARN(1, "Invalid ECC requirements");
565 }
566 } else {
567 /* <= 26nm, reference: H27UBG8T2B datasheet */
568 if (!ecc_level) {
569 chip->ecc_step_ds = 0;
570 chip->ecc_strength_ds = 0;
571 } else if (ecc_level < 5) {
572 chip->ecc_step_ds = 512;
573 chip->ecc_strength_ds = 1 << (ecc_level - 1);
574 } else {
575 chip->ecc_step_ds = 1024;
576 chip->ecc_strength_ds = 24 +
577 (8 * (ecc_level - 5));
578 }
579 }
580 }
581}
582
583static void hynix_nand_extract_scrambling_requirements(struct nand_chip *chip,
584 bool valid_jedecid)
585{
586 u8 nand_tech;
587
588 /* We need scrambling on all TLC NANDs*/
589 if (chip->bits_per_cell > 2)
590 chip->options |= NAND_NEED_SCRAMBLING;
591
592 /* And on MLC NANDs with sub-3xnm process */
593 if (valid_jedecid) {
594 nand_tech = chip->id.data[5] >> 4;
595
596 /* < 3xnm */
597 if (nand_tech > 0)
598 chip->options |= NAND_NEED_SCRAMBLING;
599 } else {
Martin Blumenstinglfd213b52017-08-05 14:16:24 +0200600 nand_tech = chip->id.data[5] & 0x7;
Boris Brezillon78f34822016-05-27 14:36:36 +0200601
602 /* < 32nm */
603 if (nand_tech > 2)
604 chip->options |= NAND_NEED_SCRAMBLING;
605 }
606}
607
608static void hynix_nand_decode_id(struct nand_chip *chip)
609{
610 struct mtd_info *mtd = nand_to_mtd(chip);
611 bool valid_jedecid;
612 u8 tmp;
613
614 /*
615 * Exclude all SLC NANDs from this advanced detection scheme.
616 * According to the ranges defined in several datasheets, it might
617 * appear that even SLC NANDs could fall in this extended ID scheme.
618 * If that the case rework the test to let SLC NANDs go through the
619 * detection process.
620 */
621 if (chip->id.len < 6 || nand_is_slc(chip)) {
622 nand_decode_ext_id(chip);
623 return;
624 }
625
626 /* Extract pagesize */
627 mtd->writesize = 2048 << (chip->id.data[3] & 0x03);
628
629 tmp = (chip->id.data[3] >> 4) & 0x3;
630 /*
631 * When bit7 is set that means we start counting at 1MiB, otherwise
632 * we start counting at 128KiB and shift this value the content of
633 * ID[3][4:5].
634 * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in
635 * this case the erasesize is set to 768KiB.
636 */
637 if (chip->id.data[3] & 0x80)
638 mtd->erasesize = SZ_1M << tmp;
639 else if (tmp == 3)
640 mtd->erasesize = SZ_512K + SZ_256K;
641 else
642 mtd->erasesize = SZ_128K << tmp;
643
644 /*
645 * Modern Toggle DDR NANDs have a valid JEDECID even though they are
646 * not exposing a valid JEDEC parameter table.
647 * These NANDs use a different NAND ID scheme.
648 */
649 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
650
651 hynix_nand_extract_oobsize(chip, valid_jedecid);
652 hynix_nand_extract_ecc_requirements(chip, valid_jedecid);
653 hynix_nand_extract_scrambling_requirements(chip, valid_jedecid);
654}
655
Boris Brezillon626994e2016-05-27 10:15:03 +0200656static void hynix_nand_cleanup(struct nand_chip *chip)
657{
658 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
659
660 if (!hynix)
661 return;
662
663 kfree(hynix->read_retry);
664 kfree(hynix);
665 nand_set_manufacturer_data(chip, NULL);
666}
667
Boris Brezillon01389b62016-06-08 10:30:18 +0200668static int hynix_nand_init(struct nand_chip *chip)
669{
Boris Brezillon626994e2016-05-27 10:15:03 +0200670 struct hynix_nand *hynix;
671 int ret;
672
Boris Brezillon01389b62016-06-08 10:30:18 +0200673 if (!nand_is_slc(chip))
674 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
675 else
676 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
677
Boris Brezillon626994e2016-05-27 10:15:03 +0200678 hynix = kzalloc(sizeof(*hynix), GFP_KERNEL);
679 if (!hynix)
680 return -ENOMEM;
681
682 nand_set_manufacturer_data(chip, hynix);
683
684 ret = hynix_nand_rr_init(chip);
685 if (ret)
686 hynix_nand_cleanup(chip);
687
688 return ret;
Boris Brezillon01389b62016-06-08 10:30:18 +0200689}
690
691const struct nand_manufacturer_ops hynix_nand_manuf_ops = {
692 .detect = hynix_nand_decode_id,
693 .init = hynix_nand_init,
Boris Brezillon626994e2016-05-27 10:15:03 +0200694 .cleanup = hynix_nand_cleanup,
Boris Brezillon01389b62016-06-08 10:30:18 +0200695};