1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/mtd/rawnand.h>
19#include <linux/sizes.h>
20#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
29
30
31
32
33
34struct hynix_read_retry {
35 int nregs;
36 const u8 *regs;
37 u8 values[0];
38};
39
40
41
42
43
44
45struct hynix_nand {
46 const struct hynix_read_retry *read_retry;
47};
48
49
50
51
52
53
54
55
56
57
58
59
60struct hynix_read_retry_otp {
61 int nregs;
62 const u8 *regs;
63 const u8 *values;
64 int page;
65 int size;
66};
67
68static bool hynix_nand_has_valid_jedecid(struct nand_chip *chip)
69{
70 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{
82 struct mtd_info *mtd = nand_to_mtd(chip);
83
84 if (chip->exec_op) {
85 struct nand_op_instr instrs[] = {
86 NAND_OP_CMD(cmd, 0),
87 };
88 struct nand_operation op = NAND_OPERATION(instrs);
89
90 return nand_exec_op(chip, &op);
91 }
92
93 chip->cmdfunc(mtd, cmd, -1, -1);
94
95 return 0;
96}
97
98static int hynix_nand_reg_write_op(struct nand_chip *chip, u8 addr, u8 val)
99{
100 struct mtd_info *mtd = nand_to_mtd(chip);
101 u16 column = ((u16)addr << 8) | addr;
102
103 if (chip->exec_op) {
104 struct nand_op_instr instrs[] = {
105 NAND_OP_ADDR(1, &addr, 0),
106 NAND_OP_8BIT_DATA_OUT(1, &val, 0),
107 };
108 struct nand_operation op = NAND_OPERATION(instrs);
109
110 return nand_exec_op(chip, &op);
111 }
112
113 chip->cmdfunc(mtd, NAND_CMD_NONE, column, -1);
114 chip->write_byte(mtd, val);
115
116 return 0;
117}
118
119static int hynix_nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
120{
121 struct nand_chip *chip = mtd_to_nand(mtd);
122 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
123 const u8 *values;
124 int i, ret;
125
126 values = hynix->read_retry->values +
127 (retry_mode * hynix->read_retry->nregs);
128
129
130 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
131 if (ret)
132 return ret;
133
134
135
136
137
138
139
140
141
142
143 for (i = 0; i < hynix->read_retry->nregs; i++) {
144 ret = hynix_nand_reg_write_op(chip, hynix->read_retry->regs[i],
145 values[i]);
146 if (ret)
147 return ret;
148 }
149
150
151 return hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
152}
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171static int hynix_get_majority(const u8 *in, int repeat, u8 *out)
172{
173 int i, j, half = repeat / 2;
174
175
176
177
178
179
180
181
182
183 for (i = 0; i < half; i++) {
184 int cnt = 0;
185 u8 val = in[i];
186
187
188 for (j = i + 1; j < repeat; j++) {
189 if (in[j] == val)
190 cnt++;
191 }
192
193
194 if (cnt > half) {
195 *out = val;
196 return 0;
197 }
198 }
199
200 return -EIO;
201}
202
203static int hynix_read_rr_otp(struct nand_chip *chip,
204 const struct hynix_read_retry_otp *info,
205 void *buf)
206{
207 int i, ret;
208
209 ret = nand_reset_op(chip);
210 if (ret)
211 return ret;
212
213 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
214 if (ret)
215 return ret;
216
217 for (i = 0; i < info->nregs; i++) {
218 ret = hynix_nand_reg_write_op(chip, info->regs[i],
219 info->values[i]);
220 if (ret)
221 return ret;
222 }
223
224 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
225 if (ret)
226 return ret;
227
228
229 ret = hynix_nand_cmd_op(chip, 0x17);
230 if (ret)
231 return ret;
232
233 ret = hynix_nand_cmd_op(chip, 0x4);
234 if (ret)
235 return ret;
236
237 ret = hynix_nand_cmd_op(chip, 0x19);
238 if (ret)
239 return ret;
240
241
242 ret = nand_read_page_op(chip, info->page, 0, buf, info->size);
243 if (ret)
244 return ret;
245
246
247 ret = nand_reset_op(chip);
248 if (ret)
249 return ret;
250
251 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
252 if (ret)
253 return ret;
254
255 ret = hynix_nand_reg_write_op(chip, 0x38, 0);
256 if (ret)
257 return ret;
258
259 ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
260 if (ret)
261 return ret;
262
263 return nand_read_page_op(chip, 0, 0, NULL, 0);
264}
265
266#define NAND_HYNIX_1XNM_RR_COUNT_OFFS 0
267#define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS 8
268#define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv) \
269 (16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize)))
270
271static int hynix_mlc_1xnm_rr_value(const u8 *buf, int nmodes, int nregs,
272 int mode, int reg, bool inv, u8 *val)
273{
274 u8 tmp[NAND_HYNIX_1XNM_RR_REPEAT];
275 int val_offs = (mode * nregs) + reg;
276 int set_size = nmodes * nregs;
277 int i, ret;
278
279 for (i = 0; i < NAND_HYNIX_1XNM_RR_REPEAT; i++) {
280 int set_offs = NAND_HYNIX_1XNM_RR_SET_OFFS(i, set_size, inv);
281
282 tmp[i] = buf[val_offs + set_offs];
283 }
284
285 ret = hynix_get_majority(tmp, NAND_HYNIX_1XNM_RR_REPEAT, val);
286 if (ret)
287 return ret;
288
289 if (inv)
290 *val = ~*val;
291
292 return 0;
293}
294
295static u8 hynix_1xnm_mlc_read_retry_regs[] = {
296 0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf
297};
298
299static int hynix_mlc_1xnm_rr_init(struct nand_chip *chip,
300 const struct hynix_read_retry_otp *info)
301{
302 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
303 struct hynix_read_retry *rr = NULL;
304 int ret, i, j;
305 u8 nregs, nmodes;
306 u8 *buf;
307
308 buf = kmalloc(info->size, GFP_KERNEL);
309 if (!buf)
310 return -ENOMEM;
311
312 ret = hynix_read_rr_otp(chip, info, buf);
313 if (ret)
314 goto out;
315
316 ret = hynix_get_majority(buf, NAND_HYNIX_1XNM_RR_REPEAT,
317 &nmodes);
318 if (ret)
319 goto out;
320
321 ret = hynix_get_majority(buf + NAND_HYNIX_1XNM_RR_REPEAT,
322 NAND_HYNIX_1XNM_RR_REPEAT,
323 &nregs);
324 if (ret)
325 goto out;
326
327 rr = kzalloc(sizeof(*rr) + (nregs * nmodes), GFP_KERNEL);
328 if (!rr) {
329 ret = -ENOMEM;
330 goto out;
331 }
332
333 for (i = 0; i < nmodes; i++) {
334 for (j = 0; j < nregs; j++) {
335 u8 *val = rr->values + (i * nregs);
336
337 ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
338 false, val);
339 if (!ret)
340 continue;
341
342 ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
343 true, val);
344 if (ret)
345 goto out;
346 }
347 }
348
349 rr->nregs = nregs;
350 rr->regs = hynix_1xnm_mlc_read_retry_regs;
351 hynix->read_retry = rr;
352 chip->setup_read_retry = hynix_nand_setup_read_retry;
353 chip->read_retries = nmodes;
354
355out:
356 kfree(buf);
357
358 if (ret)
359 kfree(rr);
360
361 return ret;
362}
363
364static const u8 hynix_mlc_1xnm_rr_otp_regs[] = { 0x38 };
365static const u8 hynix_mlc_1xnm_rr_otp_values[] = { 0x52 };
366
367static const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps[] = {
368 {
369 .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
370 .regs = hynix_mlc_1xnm_rr_otp_regs,
371 .values = hynix_mlc_1xnm_rr_otp_values,
372 .page = 0x21f,
373 .size = 784
374 },
375 {
376 .nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
377 .regs = hynix_mlc_1xnm_rr_otp_regs,
378 .values = hynix_mlc_1xnm_rr_otp_values,
379 .page = 0x200,
380 .size = 528,
381 },
382};
383
384static int hynix_nand_rr_init(struct nand_chip *chip)
385{
386 int i, ret = 0;
387 bool valid_jedecid;
388
389 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
390
391
392
393
394
395 if (valid_jedecid) {
396 u8 nand_tech = chip->id.data[5] >> 4;
397
398
399 if (nand_tech == 4) {
400 for (i = 0; i < ARRAY_SIZE(hynix_mlc_1xnm_rr_otps);
401 i++) {
402
403
404
405
406 ret = hynix_mlc_1xnm_rr_init(chip,
407 hynix_mlc_1xnm_rr_otps);
408 if (!ret)
409 break;
410 }
411 }
412 }
413
414 if (ret)
415 pr_warn("failed to initialize read-retry infrastructure");
416
417 return 0;
418}
419
420static void hynix_nand_extract_oobsize(struct nand_chip *chip,
421 bool valid_jedecid)
422{
423 struct mtd_info *mtd = nand_to_mtd(chip);
424 u8 oobsize;
425
426 oobsize = ((chip->id.data[3] >> 2) & 0x3) |
427 ((chip->id.data[3] >> 4) & 0x4);
428
429 if (valid_jedecid) {
430 switch (oobsize) {
431 case 0:
432 mtd->oobsize = 2048;
433 break;
434 case 1:
435 mtd->oobsize = 1664;
436 break;
437 case 2:
438 mtd->oobsize = 1024;
439 break;
440 case 3:
441 mtd->oobsize = 640;
442 break;
443 default:
444
445
446
447
448
449
450 WARN(1, "Invalid OOB size");
451 break;
452 }
453 } else {
454 switch (oobsize) {
455 case 0:
456 mtd->oobsize = 128;
457 break;
458 case 1:
459 mtd->oobsize = 224;
460 break;
461 case 2:
462 mtd->oobsize = 448;
463 break;
464 case 3:
465 mtd->oobsize = 64;
466 break;
467 case 4:
468 mtd->oobsize = 32;
469 break;
470 case 5:
471 mtd->oobsize = 16;
472 break;
473 case 6:
474 mtd->oobsize = 640;
475 break;
476 default:
477
478
479
480
481
482
483 WARN(1, "Invalid OOB size");
484 break;
485 }
486
487
488
489
490
491
492
493
494
495
496
497 if (chip->id.data[1] == 0xde)
498 mtd->oobsize *= mtd->writesize / SZ_8K;
499 }
500}
501
502static void hynix_nand_extract_ecc_requirements(struct nand_chip *chip,
503 bool valid_jedecid)
504{
505 u8 ecc_level = (chip->id.data[4] >> 4) & 0x7;
506
507 if (valid_jedecid) {
508
509 chip->ecc_step_ds = 1024;
510
511 switch (ecc_level) {
512 case 0:
513 chip->ecc_step_ds = 0;
514 chip->ecc_strength_ds = 0;
515 break;
516 case 1:
517 chip->ecc_strength_ds = 4;
518 break;
519 case 2:
520 chip->ecc_strength_ds = 24;
521 break;
522 case 3:
523 chip->ecc_strength_ds = 32;
524 break;
525 case 4:
526 chip->ecc_strength_ds = 40;
527 break;
528 case 5:
529 chip->ecc_strength_ds = 50;
530 break;
531 case 6:
532 chip->ecc_strength_ds = 60;
533 break;
534 default:
535
536
537
538
539
540
541 WARN(1, "Invalid ECC requirements");
542 }
543 } else {
544
545
546
547
548 u8 nand_tech = chip->id.data[5] & 0x7;
549
550 if (nand_tech < 3) {
551
552 if (ecc_level < 5) {
553 chip->ecc_step_ds = 512;
554 chip->ecc_strength_ds = 1 << ecc_level;
555 } else if (ecc_level < 7) {
556 if (ecc_level == 5)
557 chip->ecc_step_ds = 2048;
558 else
559 chip->ecc_step_ds = 1024;
560 chip->ecc_strength_ds = 24;
561 } else {
562
563
564
565
566
567
568 WARN(1, "Invalid ECC requirements");
569 }
570 } else {
571
572 if (!ecc_level) {
573 chip->ecc_step_ds = 0;
574 chip->ecc_strength_ds = 0;
575 } else if (ecc_level < 5) {
576 chip->ecc_step_ds = 512;
577 chip->ecc_strength_ds = 1 << (ecc_level - 1);
578 } else {
579 chip->ecc_step_ds = 1024;
580 chip->ecc_strength_ds = 24 +
581 (8 * (ecc_level - 5));
582 }
583 }
584 }
585}
586
587static void hynix_nand_extract_scrambling_requirements(struct nand_chip *chip,
588 bool valid_jedecid)
589{
590 u8 nand_tech;
591
592
593 if (chip->bits_per_cell > 2)
594 chip->options |= NAND_NEED_SCRAMBLING;
595
596
597 if (valid_jedecid) {
598 nand_tech = chip->id.data[5] >> 4;
599
600
601 if (nand_tech > 0)
602 chip->options |= NAND_NEED_SCRAMBLING;
603 } else {
604 nand_tech = chip->id.data[5] & 0x7;
605
606
607 if (nand_tech > 2)
608 chip->options |= NAND_NEED_SCRAMBLING;
609 }
610}
611
612static void hynix_nand_decode_id(struct nand_chip *chip)
613{
614 struct mtd_info *mtd = nand_to_mtd(chip);
615 bool valid_jedecid;
616 u8 tmp;
617
618
619
620
621
622
623
624
625 if (chip->id.len < 6 || nand_is_slc(chip)) {
626 nand_decode_ext_id(chip);
627 return;
628 }
629
630
631 mtd->writesize = 2048 << (chip->id.data[3] & 0x03);
632
633 tmp = (chip->id.data[3] >> 4) & 0x3;
634
635
636
637
638
639
640
641 if (chip->id.data[3] & 0x80)
642 mtd->erasesize = SZ_1M << tmp;
643 else if (tmp == 3)
644 mtd->erasesize = SZ_512K + SZ_256K;
645 else
646 mtd->erasesize = SZ_128K << tmp;
647
648
649
650
651
652
653 valid_jedecid = hynix_nand_has_valid_jedecid(chip);
654
655 hynix_nand_extract_oobsize(chip, valid_jedecid);
656 hynix_nand_extract_ecc_requirements(chip, valid_jedecid);
657 hynix_nand_extract_scrambling_requirements(chip, valid_jedecid);
658}
659
660static void hynix_nand_cleanup(struct nand_chip *chip)
661{
662 struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
663
664 if (!hynix)
665 return;
666
667 kfree(hynix->read_retry);
668 kfree(hynix);
669 nand_set_manufacturer_data(chip, NULL);
670}
671
672static int hynix_nand_init(struct nand_chip *chip)
673{
674 struct hynix_nand *hynix;
675 int ret;
676
677 if (!nand_is_slc(chip))
678 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
679 else
680 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
681
682 hynix = kzalloc(sizeof(*hynix), GFP_KERNEL);
683 if (!hynix)
684 return -ENOMEM;
685
686 nand_set_manufacturer_data(chip, hynix);
687
688 ret = hynix_nand_rr_init(chip);
689 if (ret)
690 hynix_nand_cleanup(chip);
691
692 return ret;
693}
694
695const struct nand_manufacturer_ops hynix_nand_manuf_ops = {
696 .detect = hynix_nand_decode_id,
697 .init = hynix_nand_init,
698 .cleanup = hynix_nand_cleanup,
699};
700