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