20
20
#include <linux/irq.h>
21
21
#include <linux/completion.h>
22
22
#include <linux/of.h>
23
+ #include <linux/bitfield.h>
23
24
24
25
#define DRIVER_NAME "mxc_nand"
25
26
47
48
#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
48
49
#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
49
50
51
+ #define NFC_V1_V2_ECC_STATUS_RESULT_ERM GENMASK(3, 2)
52
+
50
53
#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
51
54
#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
52
55
#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
@@ -132,7 +135,7 @@ struct mxc_nand_devtype_data {
132
135
uint16_t (* get_dev_status )(struct mxc_nand_host * );
133
136
int (* check_int )(struct mxc_nand_host * );
134
137
void (* irq_control )(struct mxc_nand_host * , int );
135
- u32 (* get_ecc_status )(struct mxc_nand_host * );
138
+ u32 (* get_ecc_status )(struct nand_chip * );
136
139
const struct mtd_ooblayout_ops * ooblayout ;
137
140
void (* select_chip )(struct nand_chip * chip , int cs );
138
141
int (* setup_interface )(struct nand_chip * chip , int csline ,
@@ -175,6 +178,7 @@ struct mxc_nand_host {
175
178
int eccsize ;
176
179
int used_oobsize ;
177
180
int active_cs ;
181
+ unsigned int ecc_stats_v1 ;
178
182
179
183
struct completion op_completion ;
180
184
@@ -406,19 +410,81 @@ static void irq_control(struct mxc_nand_host *host, int activate)
406
410
}
407
411
}
408
412
409
- static u32 get_ecc_status_v1 (struct mxc_nand_host * host )
413
+ static u32 get_ecc_status_v1 (struct nand_chip * chip )
410
414
{
411
- return readw (NFC_V1_V2_ECC_STATUS_RESULT );
415
+ struct mtd_info * mtd = nand_to_mtd (chip );
416
+ struct mxc_nand_host * host = nand_get_controller_data (chip );
417
+ unsigned int ecc_stats , max_bitflips = 0 ;
418
+ int no_subpages , i ;
419
+
420
+ no_subpages = mtd -> writesize >> 9 ;
421
+
422
+ ecc_stats = host -> ecc_stats_v1 ;
423
+
424
+ for (i = 0 ; i < no_subpages ; i ++ ) {
425
+ switch (ecc_stats & 0x3 ) {
426
+ case 0 :
427
+ default :
428
+ break ;
429
+ case 1 :
430
+ mtd -> ecc_stats .corrected ++ ;
431
+ max_bitflips = 1 ;
432
+ break ;
433
+ case 2 :
434
+ mtd -> ecc_stats .failed ++ ;
435
+ break ;
436
+ }
437
+
438
+ ecc_stats >>= 2 ;
439
+ }
440
+
441
+ return max_bitflips ;
412
442
}
413
443
414
- static u32 get_ecc_status_v2 (struct mxc_nand_host * host )
444
+ static u32 get_ecc_status_v2_v3 (struct nand_chip * chip , unsigned int ecc_stat )
415
445
{
416
- return readl (NFC_V1_V2_ECC_STATUS_RESULT );
446
+ struct mtd_info * mtd = nand_to_mtd (chip );
447
+ struct mxc_nand_host * host = nand_get_controller_data (chip );
448
+ u8 ecc_bit_mask , err_limit ;
449
+ unsigned int max_bitflips = 0 ;
450
+ int no_subpages , err ;
451
+
452
+ ecc_bit_mask = (host -> eccsize == 4 ) ? 0x7 : 0xf ;
453
+ err_limit = (host -> eccsize == 4 ) ? 0x4 : 0x8 ;
454
+
455
+ no_subpages = mtd -> writesize >> 9 ;
456
+
457
+ do {
458
+ err = ecc_stat & ecc_bit_mask ;
459
+ if (err > err_limit ) {
460
+ mtd -> ecc_stats .failed ++ ;
461
+ } else {
462
+ mtd -> ecc_stats .corrected += err ;
463
+ max_bitflips = max_t (unsigned int , max_bitflips , err );
464
+ }
465
+
466
+ ecc_stat >>= 4 ;
467
+ } while (-- no_subpages );
468
+
469
+ return max_bitflips ;
417
470
}
418
471
419
- static u32 get_ecc_status_v3 (struct mxc_nand_host * host )
472
+ static u32 get_ecc_status_v2 (struct nand_chip * chip )
420
473
{
421
- return readl (NFC_V3_ECC_STATUS_RESULT );
474
+ struct mxc_nand_host * host = nand_get_controller_data (chip );
475
+
476
+ u32 ecc_stat = readl (NFC_V1_V2_ECC_STATUS_RESULT );
477
+
478
+ return get_ecc_status_v2_v3 (chip , ecc_stat );
479
+ }
480
+
481
+ static u32 get_ecc_status_v3 (struct nand_chip * chip )
482
+ {
483
+ struct mxc_nand_host * host = nand_get_controller_data (chip );
484
+
485
+ u32 ecc_stat = readl (NFC_V3_ECC_STATUS_RESULT );
486
+
487
+ return get_ecc_status_v2_v3 (chip , ecc_stat );
422
488
}
423
489
424
490
static irqreturn_t mxc_nfc_irq (int irq , void * dev_id )
@@ -712,9 +778,9 @@ static int mxc_nand_read_page_v1(struct nand_chip *chip, void *buf, void *oob,
712
778
{
713
779
struct mtd_info * mtd = nand_to_mtd (chip );
714
780
struct mxc_nand_host * host = nand_get_controller_data (chip );
715
- unsigned int bitflips_corrected = 0 ;
716
781
int no_subpages ;
717
782
int i ;
783
+ unsigned int ecc_stats = 0 ;
718
784
719
785
host -> devtype_data -> enable_hwecc (chip , ecc );
720
786
@@ -727,8 +793,6 @@ static int mxc_nand_read_page_v1(struct nand_chip *chip, void *buf, void *oob,
727
793
no_subpages = mtd -> writesize >> 9 ;
728
794
729
795
for (i = 0 ; i < no_subpages ; i ++ ) {
730
- uint16_t ecc_stats ;
731
-
732
796
/* NANDFC buffer 0 is used for page read/write */
733
797
writew ((host -> active_cs << 4 ) | i , NFC_V1_V2_BUF_ADDR );
734
798
@@ -737,43 +801,25 @@ static int mxc_nand_read_page_v1(struct nand_chip *chip, void *buf, void *oob,
737
801
/* Wait for operation to complete */
738
802
wait_op_done (host , true);
739
803
740
- ecc_stats = get_ecc_status_v1 (host );
741
-
742
- ecc_stats >>= 2 ;
743
-
744
- if (buf && ecc ) {
745
- switch (ecc_stats & 0x3 ) {
746
- case 0 :
747
- default :
748
- break ;
749
- case 1 :
750
- mtd -> ecc_stats .corrected ++ ;
751
- bitflips_corrected = 1 ;
752
- break ;
753
- case 2 :
754
- mtd -> ecc_stats .failed ++ ;
755
- break ;
756
- }
757
- }
804
+ ecc_stats |= FIELD_GET (NFC_V1_V2_ECC_STATUS_RESULT_ERM ,
805
+ readw (NFC_V1_V2_ECC_STATUS_RESULT )) << i * 2 ;
758
806
}
759
807
808
+ host -> ecc_stats_v1 = ecc_stats ;
809
+
760
810
if (buf )
761
811
memcpy32_fromio (buf , host -> main_area0 , mtd -> writesize );
762
812
if (oob )
763
813
copy_spare (mtd , true, oob );
764
814
765
- return bitflips_corrected ;
815
+ return 0 ;
766
816
}
767
817
768
818
static int mxc_nand_read_page_v2_v3 (struct nand_chip * chip , void * buf ,
769
819
void * oob , bool ecc , int page )
770
820
{
771
821
struct mtd_info * mtd = nand_to_mtd (chip );
772
822
struct mxc_nand_host * host = nand_get_controller_data (chip );
773
- unsigned int max_bitflips = 0 ;
774
- u32 ecc_stat , err ;
775
- int no_subpages ;
776
- u8 ecc_bit_mask , err_limit ;
777
823
778
824
host -> devtype_data -> enable_hwecc (chip , ecc );
779
825
@@ -791,40 +837,26 @@ static int mxc_nand_read_page_v2_v3(struct nand_chip *chip, void *buf,
791
837
if (oob )
792
838
copy_spare (mtd , true, oob );
793
839
794
- ecc_bit_mask = (host -> eccsize == 4 ) ? 0x7 : 0xf ;
795
- err_limit = (host -> eccsize == 4 ) ? 0x4 : 0x8 ;
796
-
797
- no_subpages = mtd -> writesize >> 9 ;
798
-
799
- ecc_stat = host -> devtype_data -> get_ecc_status (host );
800
-
801
- do {
802
- err = ecc_stat & ecc_bit_mask ;
803
- if (err > err_limit ) {
804
- mtd -> ecc_stats .failed ++ ;
805
- } else {
806
- mtd -> ecc_stats .corrected += err ;
807
- max_bitflips = max_t (unsigned int , max_bitflips , err );
808
- }
809
-
810
- ecc_stat >>= 4 ;
811
- } while (-- no_subpages );
812
-
813
- return max_bitflips ;
840
+ return 0 ;
814
841
}
815
842
816
843
static int mxc_nand_read_page (struct nand_chip * chip , uint8_t * buf ,
817
844
int oob_required , int page )
818
845
{
819
846
struct mxc_nand_host * host = nand_get_controller_data (chip );
820
847
void * oob_buf ;
848
+ int ret ;
821
849
822
850
if (oob_required )
823
851
oob_buf = chip -> oob_poi ;
824
852
else
825
853
oob_buf = NULL ;
826
854
827
- return host -> devtype_data -> read_page (chip , buf , oob_buf , 1 , page );
855
+ ret = host -> devtype_data -> read_page (chip , buf , oob_buf , 1 , page );
856
+ if (ret )
857
+ return ret ;
858
+
859
+ return host -> devtype_data -> get_ecc_status (chip );
828
860
}
829
861
830
862
static int mxc_nand_read_page_raw (struct nand_chip * chip , uint8_t * buf ,
0 commit comments