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