This source file includes following definitions.
- docg3_ooblayout_ecc
- docg3_ooblayout_free
- doc_readb
- doc_readw
- doc_writeb
- doc_writew
- doc_flash_command
- doc_flash_sequence
- doc_flash_address
- doc_register_readb
- doc_register_readw
- doc_delay
- is_prot_seq_error
- doc_is_ready
- doc_wait_ready
- doc_reset_seq
- doc_read_data_area
- doc_write_data_area
- doc_set_reliable_mode
- doc_set_asic_mode
- doc_set_device_id
- doc_set_extra_page_mode
- doc_setup_addr_sector
- doc_setup_writeaddr_sector
- doc_read_seek
- doc_write_seek
- doc_read_page_ecc_init
- doc_write_page_ecc_init
- doc_ecc_disable
- doc_hamming_ecc_init
- doc_ecc_bch_fix_data
- doc_read_page_prepare
- doc_read_page_getbytes
- doc_write_page_putbytes
- doc_get_bch_hw_ecc
- doc_page_finish
- doc_read_page_finish
- calc_block_sector
- doc_read_oob
- doc_reload_bbt
- doc_block_isbad
- doc_get_erase_count
- doc_get_op_status
- doc_write_erase_wait_status
- doc_erase_block
- doc_erase
- doc_write_page
- doc_guess_autoecc
- doc_fill_autooob
- doc_backup_oob
- doc_write_oob
- sysfs_dev2docg3
- dps0_is_key_locked
- dps1_is_key_locked
- dps0_insert_key
- dps1_insert_key
- doc_register_sysfs
- doc_unregister_sysfs
- flashcontrol_show
- asic_mode_show
- device_id_show
- protection_show
- doc_dbg_register
- doc_set_driver_info
- doc_probe_device
- doc_release_device
- docg3_resume
- docg3_suspend
- docg3_probe
- docg3_release
1
2
3
4
5
6
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/delay.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/bitmap.h>
20 #include <linux/bitrev.h>
21 #include <linux/bch.h>
22
23 #include <linux/debugfs.h>
24 #include <linux/seq_file.h>
25
26 #define CREATE_TRACE_POINTS
27 #include "docg3.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 static unsigned int reliable_mode;
52 module_param(reliable_mode, uint, 0);
53 MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
54 "2=reliable) : MLC normal operations are in normal mode");
55
56 static int docg3_ooblayout_ecc(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58 {
59 if (section)
60 return -ERANGE;
61
62
63 oobregion->offset = 7;
64 oobregion->length = 8;
65
66 return 0;
67 }
68
69 static int docg3_ooblayout_free(struct mtd_info *mtd, int section,
70 struct mtd_oob_region *oobregion)
71 {
72 if (section > 1)
73 return -ERANGE;
74
75
76 if (!section) {
77 oobregion->offset = 0;
78 oobregion->length = 7;
79 } else {
80 oobregion->offset = 15;
81 oobregion->length = 1;
82 }
83
84 return 0;
85 }
86
87 static const struct mtd_ooblayout_ops nand_ooblayout_docg3_ops = {
88 .ecc = docg3_ooblayout_ecc,
89 .free = docg3_ooblayout_free,
90 };
91
92 static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
93 {
94 u8 val = readb(docg3->cascade->base + reg);
95
96 trace_docg3_io(0, 8, reg, (int)val);
97 return val;
98 }
99
100 static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
101 {
102 u16 val = readw(docg3->cascade->base + reg);
103
104 trace_docg3_io(0, 16, reg, (int)val);
105 return val;
106 }
107
108 static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
109 {
110 writeb(val, docg3->cascade->base + reg);
111 trace_docg3_io(1, 8, reg, val);
112 }
113
114 static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
115 {
116 writew(val, docg3->cascade->base + reg);
117 trace_docg3_io(1, 16, reg, val);
118 }
119
120 static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
121 {
122 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
123 }
124
125 static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
126 {
127 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
128 }
129
130 static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
131 {
132 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
133 }
134
135 static char const * const part_probes[] = { "cmdlinepart", "saftlpart", NULL };
136
137 static int doc_register_readb(struct docg3 *docg3, int reg)
138 {
139 u8 val;
140
141 doc_writew(docg3, reg, DOC_READADDRESS);
142 val = doc_readb(docg3, reg);
143 doc_vdbg("Read register %04x : %02x\n", reg, val);
144 return val;
145 }
146
147 static int doc_register_readw(struct docg3 *docg3, int reg)
148 {
149 u16 val;
150
151 doc_writew(docg3, reg, DOC_READADDRESS);
152 val = doc_readw(docg3, reg);
153 doc_vdbg("Read register %04x : %04x\n", reg, val);
154 return val;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 static void doc_delay(struct docg3 *docg3, int nbNOPs)
169 {
170 int i;
171
172 doc_vdbg("NOP x %d\n", nbNOPs);
173 for (i = 0; i < nbNOPs; i++)
174 doc_writeb(docg3, 0, DOC_NOP);
175 }
176
177 static int is_prot_seq_error(struct docg3 *docg3)
178 {
179 int ctrl;
180
181 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
182 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
183 }
184
185 static int doc_is_ready(struct docg3 *docg3)
186 {
187 int ctrl;
188
189 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
190 return ctrl & DOC_CTRL_FLASHREADY;
191 }
192
193 static int doc_wait_ready(struct docg3 *docg3)
194 {
195 int maxWaitCycles = 100;
196
197 do {
198 doc_delay(docg3, 4);
199 cpu_relax();
200 } while (!doc_is_ready(docg3) && maxWaitCycles--);
201 doc_delay(docg3, 2);
202 if (maxWaitCycles > 0)
203 return 0;
204 else
205 return -EIO;
206 }
207
208 static int doc_reset_seq(struct docg3 *docg3)
209 {
210 int ret;
211
212 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
213 doc_flash_sequence(docg3, DOC_SEQ_RESET);
214 doc_flash_command(docg3, DOC_CMD_RESET);
215 doc_delay(docg3, 2);
216 ret = doc_wait_ready(docg3);
217
218 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
219 return ret;
220 }
221
222
223
224
225
226
227
228
229
230
231 static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
232 int first)
233 {
234 int i, cdr, len4;
235 u16 data16, *dst16;
236 u8 data8, *dst8;
237
238 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
239 cdr = len & 0x1;
240 len4 = len - cdr;
241
242 if (first)
243 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
244 dst16 = buf;
245 for (i = 0; i < len4; i += 2) {
246 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
247 if (dst16) {
248 *dst16 = data16;
249 dst16++;
250 }
251 }
252
253 if (cdr) {
254 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
255 DOC_READADDRESS);
256 doc_delay(docg3, 1);
257 dst8 = (u8 *)dst16;
258 for (i = 0; i < cdr; i++) {
259 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
260 if (dst8) {
261 *dst8 = data8;
262 dst8++;
263 }
264 }
265 }
266 }
267
268
269
270
271
272
273
274
275
276 static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
277 {
278 int i, cdr, len4;
279 u16 *src16;
280 u8 *src8;
281
282 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
283 cdr = len & 0x3;
284 len4 = len - cdr;
285
286 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
287 src16 = (u16 *)buf;
288 for (i = 0; i < len4; i += 2) {
289 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
290 src16++;
291 }
292
293 src8 = (u8 *)src16;
294 for (i = 0; i < cdr; i++) {
295 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
296 DOC_READADDRESS);
297 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
298 src8++;
299 }
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 static void doc_set_reliable_mode(struct docg3 *docg3)
318 {
319 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
320
321 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
322 switch (docg3->reliable) {
323 case 0:
324 break;
325 case 1:
326 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
327 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
328 break;
329 case 2:
330 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
331 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
332 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
333 break;
334 default:
335 doc_err("doc_set_reliable_mode(): invalid mode\n");
336 break;
337 }
338 doc_delay(docg3, 2);
339 }
340
341
342
343
344
345
346
347
348
349
350
351 static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
352 {
353 int i;
354
355 for (i = 0; i < 12; i++)
356 doc_readb(docg3, DOC_IOSPACE_IPL);
357
358 mode |= DOC_ASICMODE_MDWREN;
359 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
360 doc_writeb(docg3, mode, DOC_ASICMODE);
361 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
362 doc_delay(docg3, 1);
363 }
364
365
366
367
368
369
370
371
372
373 static void doc_set_device_id(struct docg3 *docg3, int id)
374 {
375 u8 ctrl;
376
377 doc_dbg("doc_set_device_id(%d)\n", id);
378 doc_writeb(docg3, id, DOC_DEVICESELECT);
379 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
380
381 ctrl &= ~DOC_CTRL_VIOLATION;
382 ctrl |= DOC_CTRL_CE;
383 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397 static int doc_set_extra_page_mode(struct docg3 *docg3)
398 {
399 int fctrl;
400
401 doc_dbg("doc_set_extra_page_mode()\n");
402 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
403 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
404 doc_delay(docg3, 2);
405
406 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
407 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
408 return -EIO;
409 else
410 return 0;
411 }
412
413
414
415
416
417
418 static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
419 {
420 doc_delay(docg3, 1);
421 doc_flash_address(docg3, sector & 0xff);
422 doc_flash_address(docg3, (sector >> 8) & 0xff);
423 doc_flash_address(docg3, (sector >> 16) & 0xff);
424 doc_delay(docg3, 1);
425 }
426
427
428
429
430
431
432
433 static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
434 {
435 ofs = ofs >> 2;
436 doc_delay(docg3, 1);
437 doc_flash_address(docg3, ofs & 0xff);
438 doc_flash_address(docg3, sector & 0xff);
439 doc_flash_address(docg3, (sector >> 8) & 0xff);
440 doc_flash_address(docg3, (sector >> 16) & 0xff);
441 doc_delay(docg3, 1);
442 }
443
444
445
446
447
448
449
450
451
452
453
454
455
456 static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
457 int wear, int ofs)
458 {
459 int sector, ret = 0;
460
461 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
462 block0, block1, page, ofs, wear);
463
464 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
465 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
466 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
467 doc_delay(docg3, 2);
468 } else {
469 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
470 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
471 doc_delay(docg3, 2);
472 }
473
474 doc_set_reliable_mode(docg3);
475 if (wear)
476 ret = doc_set_extra_page_mode(docg3);
477 if (ret)
478 goto out;
479
480 doc_flash_sequence(docg3, DOC_SEQ_READ);
481 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
482 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
483 doc_setup_addr_sector(docg3, sector);
484
485 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
486 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
487 doc_setup_addr_sector(docg3, sector);
488 doc_delay(docg3, 1);
489
490 out:
491 return ret;
492 }
493
494
495
496
497
498
499
500
501
502
503
504
505 static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
506 int ofs)
507 {
508 int ret = 0, sector;
509
510 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
511 block0, block1, page, ofs);
512
513 doc_set_reliable_mode(docg3);
514
515 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
516 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
517 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
518 doc_delay(docg3, 2);
519 } else {
520 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
521 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
522 doc_delay(docg3, 2);
523 }
524
525 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
526 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
527
528 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
529 doc_setup_writeaddr_sector(docg3, sector, ofs);
530
531 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
532 doc_delay(docg3, 2);
533 ret = doc_wait_ready(docg3);
534 if (ret)
535 goto out;
536
537 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
538 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
539 doc_setup_writeaddr_sector(docg3, sector, ofs);
540 doc_delay(docg3, 1);
541
542 out:
543 return ret;
544 }
545
546
547
548
549
550
551
552
553
554
555
556
557 static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
558 {
559 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
560 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
561 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
562 DOC_ECCCONF0);
563 doc_delay(docg3, 4);
564 doc_register_readb(docg3, DOC_FLASHCONTROL);
565 return doc_wait_ready(docg3);
566 }
567
568
569
570
571
572
573
574
575
576
577
578 static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
579 {
580 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
581 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
582 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
583 DOC_ECCCONF0);
584 doc_delay(docg3, 4);
585 doc_register_readb(docg3, DOC_FLASHCONTROL);
586 return doc_wait_ready(docg3);
587 }
588
589
590
591
592
593
594
595
596 static void doc_ecc_disable(struct docg3 *docg3)
597 {
598 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
599 doc_delay(docg3, 4);
600 }
601
602
603
604
605
606
607
608
609
610 static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
611 {
612 u8 ecc_conf1;
613
614 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
615 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
616 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
617 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
618 }
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
644 {
645 u8 ecc[DOC_ECC_BCH_SIZE];
646 int errorpos[DOC_ECC_BCH_T], i, numerrs;
647
648 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
649 ecc[i] = bitrev8(hwecc[i]);
650 numerrs = decode_bch(docg3->cascade->bch, NULL,
651 DOC_ECC_BCH_COVERED_BYTES,
652 NULL, ecc, NULL, errorpos);
653 BUG_ON(numerrs == -EINVAL);
654 if (numerrs < 0)
655 goto out;
656
657 for (i = 0; i < numerrs; i++)
658 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
659 for (i = 0; i < numerrs; i++)
660 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
661
662 change_bit(errorpos[i], buf);
663 out:
664 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
665 return numerrs;
666 }
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
696 int page, int offset)
697 {
698 int wear_area = 0, ret = 0;
699
700 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
701 block0, block1, page, offset);
702 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
703 wear_area = 1;
704 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
705 return -EINVAL;
706
707 doc_set_device_id(docg3, docg3->device_id);
708 ret = doc_reset_seq(docg3);
709 if (ret)
710 goto err;
711
712
713 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
714 if (ret)
715 goto err;
716
717 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
718 doc_delay(docg3, 2);
719 doc_wait_ready(docg3);
720
721 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
722 doc_delay(docg3, 1);
723 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
724 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
725 doc_flash_address(docg3, offset >> 2);
726 doc_delay(docg3, 1);
727 doc_wait_ready(docg3);
728
729 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
730
731 return 0;
732 err:
733 doc_writeb(docg3, 0, DOC_DATAEND);
734 doc_delay(docg3, 2);
735 return -EIO;
736 }
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753 static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
754 int first, int last_odd)
755 {
756 if (last_odd && len > 0) {
757 doc_read_data_area(docg3, buf, 1, first);
758 doc_read_data_area(docg3, buf ? buf + 1 : buf, len - 1, 0);
759 } else {
760 doc_read_data_area(docg3, buf, len, first);
761 }
762 doc_delay(docg3, 2);
763 return len;
764 }
765
766
767
768
769
770
771
772
773 static void doc_write_page_putbytes(struct docg3 *docg3, int len,
774 const u_char *buf)
775 {
776 doc_write_data_area(docg3, buf, len);
777 doc_delay(docg3, 2);
778 }
779
780
781
782
783
784
785 static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
786 {
787 int i;
788
789 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
790 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
791 }
792
793
794
795
796
797 static void doc_page_finish(struct docg3 *docg3)
798 {
799 doc_writeb(docg3, 0, DOC_DATAEND);
800 doc_delay(docg3, 2);
801 }
802
803
804
805
806
807
808
809
810
811 static void doc_read_page_finish(struct docg3 *docg3)
812 {
813 doc_page_finish(docg3);
814 doc_set_device_id(docg3, 0);
815 }
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832 static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
833 int *ofs, int reliable)
834 {
835 uint sector, pages_biblock;
836
837 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
838 if (reliable == 1 || reliable == 2)
839 pages_biblock /= 2;
840
841 sector = from / DOC_LAYOUT_PAGE_SIZE;
842 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
843 *block1 = *block0 + 1;
844 *page = sector % pages_biblock;
845 *page /= DOC_LAYOUT_NBPLANES;
846 if (reliable == 1 || reliable == 2)
847 *page *= 2;
848 if (sector % 2)
849 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
850 else
851 *ofs = 0;
852 }
853
854
855
856
857
858
859
860
861
862
863
864
865 static int doc_read_oob(struct mtd_info *mtd, loff_t from,
866 struct mtd_oob_ops *ops)
867 {
868 struct docg3 *docg3 = mtd->priv;
869 int block0, block1, page, ret, skip, ofs = 0;
870 u8 *oobbuf = ops->oobbuf;
871 u8 *buf = ops->datbuf;
872 size_t len, ooblen, nbdata, nboob;
873 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
874 int max_bitflips = 0;
875
876 if (buf)
877 len = ops->len;
878 else
879 len = 0;
880 if (oobbuf)
881 ooblen = ops->ooblen;
882 else
883 ooblen = 0;
884
885 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
886 oobbuf += ops->ooboffs;
887
888 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
889 from, ops->mode, buf, len, oobbuf, ooblen);
890 if (ooblen % DOC_LAYOUT_OOB_SIZE)
891 return -EINVAL;
892
893 ops->oobretlen = 0;
894 ops->retlen = 0;
895 ret = 0;
896 skip = from % DOC_LAYOUT_PAGE_SIZE;
897 mutex_lock(&docg3->cascade->lock);
898 while (ret >= 0 && (len > 0 || ooblen > 0)) {
899 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
900 docg3->reliable);
901 nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
902 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
903 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
904 if (ret < 0)
905 goto out;
906 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
907 if (ret < 0)
908 goto err_in_read;
909 ret = doc_read_page_getbytes(docg3, skip, NULL, 1, 0);
910 if (ret < skip)
911 goto err_in_read;
912 ret = doc_read_page_getbytes(docg3, nbdata, buf, 0, skip % 2);
913 if (ret < nbdata)
914 goto err_in_read;
915 doc_read_page_getbytes(docg3,
916 DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
917 NULL, 0, (skip + nbdata) % 2);
918 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0);
919 if (ret < nboob)
920 goto err_in_read;
921 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
922 NULL, 0, nboob % 2);
923
924 doc_get_bch_hw_ecc(docg3, hwecc);
925 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
926
927 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
928 doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf);
929 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
930 doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8);
931 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
932 }
933 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
934 doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc);
935
936 ret = -EIO;
937 if (is_prot_seq_error(docg3))
938 goto err_in_read;
939 ret = 0;
940 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
941 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
942 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
943 (ops->mode != MTD_OPS_RAW) &&
944 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
945 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
946 if (ret < 0) {
947 mtd->ecc_stats.failed++;
948 ret = -EBADMSG;
949 }
950 if (ret > 0) {
951 mtd->ecc_stats.corrected += ret;
952 max_bitflips = max(max_bitflips, ret);
953 ret = max_bitflips;
954 }
955 }
956
957 doc_read_page_finish(docg3);
958 ops->retlen += nbdata;
959 ops->oobretlen += nboob;
960 buf += nbdata;
961 oobbuf += nboob;
962 len -= nbdata;
963 ooblen -= nboob;
964 from += DOC_LAYOUT_PAGE_SIZE;
965 skip = 0;
966 }
967
968 out:
969 mutex_unlock(&docg3->cascade->lock);
970 return ret;
971 err_in_read:
972 doc_read_page_finish(docg3);
973 goto out;
974 }
975
976 static int doc_reload_bbt(struct docg3 *docg3)
977 {
978 int block = DOC_LAYOUT_BLOCK_BBT;
979 int ret = 0, nbpages, page;
980 u_char *buf = docg3->bbt;
981
982 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
983 for (page = 0; !ret && (page < nbpages); page++) {
984 ret = doc_read_page_prepare(docg3, block, block + 1,
985 page + DOC_LAYOUT_PAGE_BBT, 0);
986 if (!ret)
987 ret = doc_read_page_ecc_init(docg3,
988 DOC_LAYOUT_PAGE_SIZE);
989 if (!ret)
990 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
991 buf, 1, 0);
992 buf += DOC_LAYOUT_PAGE_SIZE;
993 }
994 doc_read_page_finish(docg3);
995 return ret;
996 }
997
998
999
1000
1001
1002
1003
1004
1005 static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1006 {
1007 struct docg3 *docg3 = mtd->priv;
1008 int block0, block1, page, ofs, is_good;
1009
1010 calc_block_sector(from, &block0, &block1, &page, &ofs,
1011 docg3->reliable);
1012 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1013 from, block0, block1, page, ofs);
1014
1015 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1016 return 0;
1017 if (block1 > docg3->max_block)
1018 return -EINVAL;
1019
1020 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1021 return !is_good;
1022 }
1023
1024 #if 0
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1036 {
1037 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1038 int ret, plane1_erase_count, plane2_erase_count;
1039 int block0, block1, page, ofs;
1040
1041 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1042 if (from % DOC_LAYOUT_PAGE_SIZE)
1043 return -EINVAL;
1044 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
1045 if (block1 > docg3->max_block)
1046 return -EINVAL;
1047
1048 ret = doc_reset_seq(docg3);
1049 if (!ret)
1050 ret = doc_read_page_prepare(docg3, block0, block1, page,
1051 ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
1052 if (!ret)
1053 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1054 buf, 1, 0);
1055 doc_read_page_finish(docg3);
1056
1057 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1058 return -EIO;
1059 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1060 | ((u8)(~buf[5]) << 16);
1061 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1062 | ((u8)(~buf[7]) << 16);
1063
1064 return max(plane1_erase_count, plane2_erase_count);
1065 }
1066 #endif
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 static int doc_get_op_status(struct docg3 *docg3)
1077 {
1078 u8 status;
1079
1080 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1081 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1082 doc_delay(docg3, 5);
1083
1084 doc_ecc_disable(docg3);
1085 doc_read_data_area(docg3, &status, 1, 1);
1086 return status;
1087 }
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099 static int doc_write_erase_wait_status(struct docg3 *docg3)
1100 {
1101 int i, status, ret = 0;
1102
1103 for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
1104 msleep(20);
1105 if (!doc_is_ready(docg3)) {
1106 doc_dbg("Timeout reached and the chip is still not ready\n");
1107 ret = -EAGAIN;
1108 goto out;
1109 }
1110
1111 status = doc_get_op_status(docg3);
1112 if (status & DOC_PLANES_STATUS_FAIL) {
1113 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1114 status);
1115 ret = -EIO;
1116 }
1117
1118 out:
1119 doc_page_finish(docg3);
1120 return ret;
1121 }
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1135 {
1136 int ret, sector;
1137
1138 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1139 ret = doc_reset_seq(docg3);
1140 if (ret)
1141 return -EIO;
1142
1143 doc_set_reliable_mode(docg3);
1144 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1145
1146 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1147 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1148 doc_setup_addr_sector(docg3, sector);
1149 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1150 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1151 doc_setup_addr_sector(docg3, sector);
1152 doc_delay(docg3, 1);
1153
1154 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1155 doc_delay(docg3, 2);
1156
1157 if (is_prot_seq_error(docg3)) {
1158 doc_err("Erase blocks %d,%d error\n", block0, block1);
1159 return -EIO;
1160 }
1161
1162 return doc_write_erase_wait_status(docg3);
1163 }
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1177 {
1178 struct docg3 *docg3 = mtd->priv;
1179 uint64_t len;
1180 int block0, block1, page, ret = 0, ofs = 0;
1181
1182 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1183
1184 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1185 &ofs, docg3->reliable);
1186 if (info->addr + info->len > mtd->size || page || ofs)
1187 return -EINVAL;
1188
1189 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1190 docg3->reliable);
1191 mutex_lock(&docg3->cascade->lock);
1192 doc_set_device_id(docg3, docg3->device_id);
1193 doc_set_reliable_mode(docg3);
1194 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1195 ret = doc_erase_block(docg3, block0, block1);
1196 block0 += 2;
1197 block1 += 2;
1198 }
1199 mutex_unlock(&docg3->cascade->lock);
1200
1201 return ret;
1202 }
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223 static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1224 const u_char *oob, int autoecc)
1225 {
1226 int block0, block1, page, ret, ofs = 0;
1227 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
1228
1229 doc_dbg("doc_write_page(to=%lld)\n", to);
1230 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
1231
1232 doc_set_device_id(docg3, docg3->device_id);
1233 ret = doc_reset_seq(docg3);
1234 if (ret)
1235 goto err;
1236
1237
1238 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1239 if (ret)
1240 goto err;
1241
1242 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
1243 doc_delay(docg3, 2);
1244 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1245
1246 if (oob && autoecc) {
1247 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1248 doc_delay(docg3, 2);
1249 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1250
1251 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1252 doc_delay(docg3, 2);
1253 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1254 &hamming);
1255 doc_delay(docg3, 2);
1256
1257 doc_get_bch_hw_ecc(docg3, hwecc);
1258 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
1259 doc_delay(docg3, 2);
1260
1261 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1262 }
1263 if (oob && !autoecc)
1264 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1265
1266 doc_delay(docg3, 2);
1267 doc_page_finish(docg3);
1268 doc_delay(docg3, 2);
1269 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1270 doc_delay(docg3, 2);
1271
1272
1273
1274
1275
1276 ret = doc_write_erase_wait_status(docg3);
1277 return ret;
1278 err:
1279 doc_read_page_finish(docg3);
1280 return ret;
1281 }
1282
1283
1284
1285
1286
1287
1288
1289 static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1290 {
1291 int autoecc;
1292
1293 switch (ops->mode) {
1294 case MTD_OPS_PLACE_OOB:
1295 case MTD_OPS_AUTO_OOB:
1296 autoecc = 1;
1297 break;
1298 case MTD_OPS_RAW:
1299 autoecc = 0;
1300 break;
1301 default:
1302 autoecc = -EINVAL;
1303 }
1304 return autoecc;
1305 }
1306
1307
1308
1309
1310
1311
1312
1313 static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1314 {
1315 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1316 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1317 }
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336 static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1337 struct mtd_oob_ops *ops)
1338 {
1339 int ooblen = ops->ooblen, autoecc;
1340
1341 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1342 return -EINVAL;
1343 autoecc = doc_guess_autoecc(ops);
1344 if (autoecc < 0)
1345 return autoecc;
1346
1347 docg3->oob_write_ofs = to;
1348 docg3->oob_autoecc = autoecc;
1349 if (ops->mode == MTD_OPS_AUTO_OOB) {
1350 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1351 ops->oobretlen = 8;
1352 } else {
1353 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1354 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1355 }
1356 return 0;
1357 }
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1375 struct mtd_oob_ops *ops)
1376 {
1377 struct docg3 *docg3 = mtd->priv;
1378 int ret, autoecc, oobdelta;
1379 u8 *oobbuf = ops->oobbuf;
1380 u8 *buf = ops->datbuf;
1381 size_t len, ooblen;
1382 u8 oob[DOC_LAYOUT_OOB_SIZE];
1383
1384 if (buf)
1385 len = ops->len;
1386 else
1387 len = 0;
1388 if (oobbuf)
1389 ooblen = ops->ooblen;
1390 else
1391 ooblen = 0;
1392
1393 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1394 oobbuf += ops->ooboffs;
1395
1396 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1397 ofs, ops->mode, buf, len, oobbuf, ooblen);
1398 switch (ops->mode) {
1399 case MTD_OPS_PLACE_OOB:
1400 case MTD_OPS_RAW:
1401 oobdelta = mtd->oobsize;
1402 break;
1403 case MTD_OPS_AUTO_OOB:
1404 oobdelta = mtd->oobavail;
1405 break;
1406 default:
1407 return -EINVAL;
1408 }
1409 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1410 (ofs % DOC_LAYOUT_PAGE_SIZE))
1411 return -EINVAL;
1412 if (len && ooblen &&
1413 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1414 return -EINVAL;
1415
1416 ops->oobretlen = 0;
1417 ops->retlen = 0;
1418 ret = 0;
1419 if (len == 0 && ooblen == 0)
1420 return -EINVAL;
1421 if (len == 0 && ooblen > 0)
1422 return doc_backup_oob(docg3, ofs, ops);
1423
1424 autoecc = doc_guess_autoecc(ops);
1425 if (autoecc < 0)
1426 return autoecc;
1427
1428 mutex_lock(&docg3->cascade->lock);
1429 while (!ret && len > 0) {
1430 memset(oob, 0, sizeof(oob));
1431 if (ofs == docg3->oob_write_ofs)
1432 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1433 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1434 doc_fill_autooob(oob, oobbuf);
1435 else if (ooblen > 0)
1436 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1437 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1438
1439 ofs += DOC_LAYOUT_PAGE_SIZE;
1440 len -= DOC_LAYOUT_PAGE_SIZE;
1441 buf += DOC_LAYOUT_PAGE_SIZE;
1442 if (ooblen) {
1443 oobbuf += oobdelta;
1444 ooblen -= oobdelta;
1445 ops->oobretlen += oobdelta;
1446 }
1447 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1448 }
1449
1450 doc_set_device_id(docg3, 0);
1451 mutex_unlock(&docg3->cascade->lock);
1452 return ret;
1453 }
1454
1455 static struct docg3 *sysfs_dev2docg3(struct device *dev,
1456 struct device_attribute *attr)
1457 {
1458 int floor;
1459 struct mtd_info **docg3_floors = dev_get_drvdata(dev);
1460
1461 floor = attr->attr.name[1] - '0';
1462 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1463 return NULL;
1464 else
1465 return docg3_floors[floor]->priv;
1466 }
1467
1468 static ssize_t dps0_is_key_locked(struct device *dev,
1469 struct device_attribute *attr, char *buf)
1470 {
1471 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1472 int dps0;
1473
1474 mutex_lock(&docg3->cascade->lock);
1475 doc_set_device_id(docg3, docg3->device_id);
1476 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1477 doc_set_device_id(docg3, 0);
1478 mutex_unlock(&docg3->cascade->lock);
1479
1480 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1481 }
1482
1483 static ssize_t dps1_is_key_locked(struct device *dev,
1484 struct device_attribute *attr, char *buf)
1485 {
1486 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1487 int dps1;
1488
1489 mutex_lock(&docg3->cascade->lock);
1490 doc_set_device_id(docg3, docg3->device_id);
1491 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1492 doc_set_device_id(docg3, 0);
1493 mutex_unlock(&docg3->cascade->lock);
1494
1495 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1496 }
1497
1498 static ssize_t dps0_insert_key(struct device *dev,
1499 struct device_attribute *attr,
1500 const char *buf, size_t count)
1501 {
1502 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1503 int i;
1504
1505 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1506 return -EINVAL;
1507
1508 mutex_lock(&docg3->cascade->lock);
1509 doc_set_device_id(docg3, docg3->device_id);
1510 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1511 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1512 doc_set_device_id(docg3, 0);
1513 mutex_unlock(&docg3->cascade->lock);
1514 return count;
1515 }
1516
1517 static ssize_t dps1_insert_key(struct device *dev,
1518 struct device_attribute *attr,
1519 const char *buf, size_t count)
1520 {
1521 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1522 int i;
1523
1524 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1525 return -EINVAL;
1526
1527 mutex_lock(&docg3->cascade->lock);
1528 doc_set_device_id(docg3, docg3->device_id);
1529 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1530 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1531 doc_set_device_id(docg3, 0);
1532 mutex_unlock(&docg3->cascade->lock);
1533 return count;
1534 }
1535
1536 #define FLOOR_SYSFS(id) { \
1537 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1538 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1539 __ATTR(f##id##_dps0_protection_key, S_IWUSR|S_IWGRP, NULL, dps0_insert_key), \
1540 __ATTR(f##id##_dps1_protection_key, S_IWUSR|S_IWGRP, NULL, dps1_insert_key), \
1541 }
1542
1543 static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1544 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1545 };
1546
1547 static int doc_register_sysfs(struct platform_device *pdev,
1548 struct docg3_cascade *cascade)
1549 {
1550 struct device *dev = &pdev->dev;
1551 int floor;
1552 int ret;
1553 int i;
1554
1555 for (floor = 0;
1556 floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1557 floor++) {
1558 for (i = 0; i < 4; i++) {
1559 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1560 if (ret)
1561 goto remove_files;
1562 }
1563 }
1564
1565 return 0;
1566
1567 remove_files:
1568 do {
1569 while (--i >= 0)
1570 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1571 i = 4;
1572 } while (--floor >= 0);
1573
1574 return ret;
1575 }
1576
1577 static void doc_unregister_sysfs(struct platform_device *pdev,
1578 struct docg3_cascade *cascade)
1579 {
1580 struct device *dev = &pdev->dev;
1581 int floor, i;
1582
1583 for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
1584 floor++)
1585 for (i = 0; i < 4; i++)
1586 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1587 }
1588
1589
1590
1591
1592 static int flashcontrol_show(struct seq_file *s, void *p)
1593 {
1594 struct docg3 *docg3 = (struct docg3 *)s->private;
1595
1596 u8 fctrl;
1597
1598 mutex_lock(&docg3->cascade->lock);
1599 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1600 mutex_unlock(&docg3->cascade->lock);
1601
1602 seq_printf(s, "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1603 fctrl,
1604 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1605 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1606 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1607 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1608 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1609
1610 return 0;
1611 }
1612 DEFINE_SHOW_ATTRIBUTE(flashcontrol);
1613
1614 static int asic_mode_show(struct seq_file *s, void *p)
1615 {
1616 struct docg3 *docg3 = (struct docg3 *)s->private;
1617
1618 int pctrl, mode;
1619
1620 mutex_lock(&docg3->cascade->lock);
1621 pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1622 mode = pctrl & 0x03;
1623 mutex_unlock(&docg3->cascade->lock);
1624
1625 seq_printf(s,
1626 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1627 pctrl,
1628 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1629 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1630 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1631 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1632 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1633 mode >> 1, mode & 0x1);
1634
1635 switch (mode) {
1636 case DOC_ASICMODE_RESET:
1637 seq_puts(s, "reset");
1638 break;
1639 case DOC_ASICMODE_NORMAL:
1640 seq_puts(s, "normal");
1641 break;
1642 case DOC_ASICMODE_POWERDOWN:
1643 seq_puts(s, "powerdown");
1644 break;
1645 }
1646 seq_puts(s, ")\n");
1647 return 0;
1648 }
1649 DEFINE_SHOW_ATTRIBUTE(asic_mode);
1650
1651 static int device_id_show(struct seq_file *s, void *p)
1652 {
1653 struct docg3 *docg3 = (struct docg3 *)s->private;
1654 int id;
1655
1656 mutex_lock(&docg3->cascade->lock);
1657 id = doc_register_readb(docg3, DOC_DEVICESELECT);
1658 mutex_unlock(&docg3->cascade->lock);
1659
1660 seq_printf(s, "DeviceId = %d\n", id);
1661 return 0;
1662 }
1663 DEFINE_SHOW_ATTRIBUTE(device_id);
1664
1665 static int protection_show(struct seq_file *s, void *p)
1666 {
1667 struct docg3 *docg3 = (struct docg3 *)s->private;
1668 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1669
1670 mutex_lock(&docg3->cascade->lock);
1671 protect = doc_register_readb(docg3, DOC_PROTECTION);
1672 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1673 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1674 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1675 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1676 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1677 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
1678 mutex_unlock(&docg3->cascade->lock);
1679
1680 seq_printf(s, "Protection = 0x%02x (", protect);
1681 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1682 seq_puts(s, "FOUNDRY_OTP_LOCK,");
1683 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1684 seq_puts(s, "CUSTOMER_OTP_LOCK,");
1685 if (protect & DOC_PROTECT_LOCK_INPUT)
1686 seq_puts(s, "LOCK_INPUT,");
1687 if (protect & DOC_PROTECT_STICKY_LOCK)
1688 seq_puts(s, "STICKY_LOCK,");
1689 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1690 seq_puts(s, "PROTECTION ON,");
1691 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1692 seq_puts(s, "IPL_DOWNLOAD_LOCK,");
1693 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1694 seq_puts(s, "PROTECT_ERR,");
1695 else
1696 seq_puts(s, "NO_PROTECT_ERR");
1697 seq_puts(s, ")\n");
1698
1699 seq_printf(s, "DPS0 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1700 dps0, dps0_low, dps0_high,
1701 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1702 !!(dps0 & DOC_DPS_READ_PROTECTED),
1703 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1704 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1705 !!(dps0 & DOC_DPS_KEY_OK));
1706 seq_printf(s, "DPS1 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1707 dps1, dps1_low, dps1_high,
1708 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1709 !!(dps1 & DOC_DPS_READ_PROTECTED),
1710 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1711 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1712 !!(dps1 & DOC_DPS_KEY_OK));
1713 return 0;
1714 }
1715 DEFINE_SHOW_ATTRIBUTE(protection);
1716
1717 static void __init doc_dbg_register(struct mtd_info *floor)
1718 {
1719 struct dentry *root = floor->dbg.dfs_dir;
1720 struct docg3 *docg3 = floor->priv;
1721
1722 if (IS_ERR_OR_NULL(root)) {
1723 if (IS_ENABLED(CONFIG_DEBUG_FS) &&
1724 !IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
1725 dev_warn(floor->dev.parent,
1726 "CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n");
1727 return;
1728 }
1729
1730 debugfs_create_file("docg3_flashcontrol", S_IRUSR, root, docg3,
1731 &flashcontrol_fops);
1732 debugfs_create_file("docg3_asic_mode", S_IRUSR, root, docg3,
1733 &asic_mode_fops);
1734 debugfs_create_file("docg3_device_id", S_IRUSR, root, docg3,
1735 &device_id_fops);
1736 debugfs_create_file("docg3_protection", S_IRUSR, root, docg3,
1737 &protection_fops);
1738 }
1739
1740
1741
1742
1743
1744
1745 static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1746 {
1747 struct docg3 *docg3 = mtd->priv;
1748 int cfg;
1749
1750 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1751 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1752 docg3->reliable = reliable_mode;
1753
1754 switch (chip_id) {
1755 case DOC_CHIPID_G3:
1756 mtd->name = devm_kasprintf(docg3->dev, GFP_KERNEL, "docg3.%d",
1757 docg3->device_id);
1758 if (!mtd->name)
1759 return -ENOMEM;
1760 docg3->max_block = 2047;
1761 break;
1762 }
1763 mtd->type = MTD_NANDFLASH;
1764 mtd->flags = MTD_CAP_NANDFLASH;
1765 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1766 if (docg3->reliable == 2)
1767 mtd->size /= 2;
1768 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1769 if (docg3->reliable == 2)
1770 mtd->erasesize /= 2;
1771 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1772 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1773 mtd->_erase = doc_erase;
1774 mtd->_read_oob = doc_read_oob;
1775 mtd->_write_oob = doc_write_oob;
1776 mtd->_block_isbad = doc_block_isbad;
1777 mtd_set_ooblayout(mtd, &nand_ooblayout_docg3_ops);
1778 mtd->oobavail = 8;
1779 mtd->ecc_strength = DOC_ECC_BCH_T;
1780
1781 return 0;
1782 }
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797 static struct mtd_info * __init
1798 doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
1799 {
1800 int ret, bbt_nbpages;
1801 u16 chip_id, chip_id_inv;
1802 struct docg3 *docg3;
1803 struct mtd_info *mtd;
1804
1805 ret = -ENOMEM;
1806 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1807 if (!docg3)
1808 goto nomem1;
1809 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1810 if (!mtd)
1811 goto nomem2;
1812 mtd->priv = docg3;
1813 mtd->dev.parent = dev;
1814 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1815 8 * DOC_LAYOUT_PAGE_SIZE);
1816 docg3->bbt = kcalloc(DOC_LAYOUT_PAGE_SIZE, bbt_nbpages, GFP_KERNEL);
1817 if (!docg3->bbt)
1818 goto nomem3;
1819
1820 docg3->dev = dev;
1821 docg3->device_id = floor;
1822 docg3->cascade = cascade;
1823 doc_set_device_id(docg3, docg3->device_id);
1824 if (!floor)
1825 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1826 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1827
1828 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1829 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1830
1831 ret = 0;
1832 if (chip_id != (u16)(~chip_id_inv)) {
1833 goto nomem4;
1834 }
1835
1836 switch (chip_id) {
1837 case DOC_CHIPID_G3:
1838 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1839 docg3->cascade->base, floor);
1840 break;
1841 default:
1842 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1843 goto nomem4;
1844 }
1845
1846 ret = doc_set_driver_info(chip_id, mtd);
1847 if (ret)
1848 goto nomem4;
1849
1850 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1851 doc_reload_bbt(docg3);
1852 return mtd;
1853
1854 nomem4:
1855 kfree(docg3->bbt);
1856 nomem3:
1857 kfree(mtd);
1858 nomem2:
1859 kfree(docg3);
1860 nomem1:
1861 return ret ? ERR_PTR(ret) : NULL;
1862 }
1863
1864
1865
1866
1867
1868 static void doc_release_device(struct mtd_info *mtd)
1869 {
1870 struct docg3 *docg3 = mtd->priv;
1871
1872 mtd_device_unregister(mtd);
1873 kfree(docg3->bbt);
1874 kfree(docg3);
1875 kfree(mtd);
1876 }
1877
1878
1879
1880
1881
1882
1883
1884 static int docg3_resume(struct platform_device *pdev)
1885 {
1886 int i;
1887 struct docg3_cascade *cascade;
1888 struct mtd_info **docg3_floors, *mtd;
1889 struct docg3 *docg3;
1890
1891 cascade = platform_get_drvdata(pdev);
1892 docg3_floors = cascade->floors;
1893 mtd = docg3_floors[0];
1894 docg3 = mtd->priv;
1895
1896 doc_dbg("docg3_resume()\n");
1897 for (i = 0; i < 12; i++)
1898 doc_readb(docg3, DOC_IOSPACE_IPL);
1899 return 0;
1900 }
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911 static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1912 {
1913 int floor, i;
1914 struct docg3_cascade *cascade;
1915 struct mtd_info **docg3_floors, *mtd;
1916 struct docg3 *docg3;
1917 u8 ctrl, pwr_down;
1918
1919 cascade = platform_get_drvdata(pdev);
1920 docg3_floors = cascade->floors;
1921 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1922 mtd = docg3_floors[floor];
1923 if (!mtd)
1924 continue;
1925 docg3 = mtd->priv;
1926
1927 doc_writeb(docg3, floor, DOC_DEVICESELECT);
1928 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1929 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1930 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1931
1932 for (i = 0; i < 10; i++) {
1933 usleep_range(3000, 4000);
1934 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
1935 if (pwr_down & DOC_POWERDOWN_READY)
1936 break;
1937 }
1938 if (pwr_down & DOC_POWERDOWN_READY) {
1939 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
1940 floor);
1941 } else {
1942 doc_err("docg3_suspend(): floor %d powerdown failed\n",
1943 floor);
1944 return -EIO;
1945 }
1946 }
1947
1948 mtd = docg3_floors[0];
1949 docg3 = mtd->priv;
1950 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
1951 return 0;
1952 }
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963 static int __init docg3_probe(struct platform_device *pdev)
1964 {
1965 struct device *dev = &pdev->dev;
1966 struct mtd_info *mtd;
1967 struct resource *ress;
1968 void __iomem *base;
1969 int ret, floor;
1970 struct docg3_cascade *cascade;
1971
1972 ret = -ENXIO;
1973 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1974 if (!ress) {
1975 dev_err(dev, "No I/O memory resource defined\n");
1976 return ret;
1977 }
1978 base = devm_ioremap(dev, ress->start, DOC_IOSPACE_SIZE);
1979
1980 ret = -ENOMEM;
1981 cascade = devm_kcalloc(dev, DOC_MAX_NBFLOORS, sizeof(*cascade),
1982 GFP_KERNEL);
1983 if (!cascade)
1984 return ret;
1985 cascade->base = base;
1986 mutex_init(&cascade->lock);
1987 cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
1988 DOC_ECC_BCH_PRIMPOLY);
1989 if (!cascade->bch)
1990 return ret;
1991
1992 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1993 mtd = doc_probe_device(cascade, floor, dev);
1994 if (IS_ERR(mtd)) {
1995 ret = PTR_ERR(mtd);
1996 goto err_probe;
1997 }
1998 if (!mtd) {
1999 if (floor == 0)
2000 goto notfound;
2001 else
2002 continue;
2003 }
2004 cascade->floors[floor] = mtd;
2005 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2006 0);
2007 if (ret)
2008 goto err_probe;
2009
2010 doc_dbg_register(cascade->floors[floor]);
2011 }
2012
2013 ret = doc_register_sysfs(pdev, cascade);
2014 if (ret)
2015 goto err_probe;
2016
2017 platform_set_drvdata(pdev, cascade);
2018 return 0;
2019
2020 notfound:
2021 ret = -ENODEV;
2022 dev_info(dev, "No supported DiskOnChip found\n");
2023 err_probe:
2024 free_bch(cascade->bch);
2025 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2026 if (cascade->floors[floor])
2027 doc_release_device(cascade->floors[floor]);
2028 return ret;
2029 }
2030
2031
2032
2033
2034
2035
2036
2037 static int docg3_release(struct platform_device *pdev)
2038 {
2039 struct docg3_cascade *cascade = platform_get_drvdata(pdev);
2040 struct docg3 *docg3 = cascade->floors[0]->priv;
2041 int floor;
2042
2043 doc_unregister_sysfs(pdev, cascade);
2044 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2045 if (cascade->floors[floor])
2046 doc_release_device(cascade->floors[floor]);
2047
2048 free_bch(docg3->cascade->bch);
2049 return 0;
2050 }
2051
2052 #ifdef CONFIG_OF
2053 static const struct of_device_id docg3_dt_ids[] = {
2054 { .compatible = "m-systems,diskonchip-g3" },
2055 {}
2056 };
2057 MODULE_DEVICE_TABLE(of, docg3_dt_ids);
2058 #endif
2059
2060 static struct platform_driver g3_driver = {
2061 .driver = {
2062 .name = "docg3",
2063 .of_match_table = of_match_ptr(docg3_dt_ids),
2064 },
2065 .suspend = docg3_suspend,
2066 .resume = docg3_resume,
2067 .remove = docg3_release,
2068 };
2069
2070 module_platform_driver_probe(g3_driver, docg3_probe);
2071
2072 MODULE_LICENSE("GPL");
2073 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2074 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");