summaryrefslogtreecommitdiffstats
path: root/bsps/shared/dev/nand/xnandpsu.c
blob: 79025f3c04192a695481a37fa8d2cb581ea19fa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
/******************************************************************************
* Copyright (C) 2015 - 2022 Xilinx, Inc.  All rights reserved.
* SPDX-License-Identifier: MIT
******************************************************************************/

/*****************************************************************************/
/**
*
* @file xnandpsu.c
* @addtogroup Overview
* @{
*
* This file contains the implementation of the interface functions for
* XNandPsu driver. Refer to the header file xnandpsu.h for more detailed
* information.
*
* This module supports for NAND flash memory devices that conform to the
* "Open NAND Flash Interface" (ONFI) 3.0 Specification. This modules
* implements basic flash operations like read, write and erase.
*
* @note		Driver has been renamed to nandpsu after change in
*		naming convention.
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who    Date	   Changes
* ----- ----   ----------  -----------------------------------------------
* 1.0   nm     05/06/2014  First release
* 2.0   sb     01/12/2015  Removed Null checks for Buffer passed
*			   as parameter to Read API's
*			   - XNandPsu_Read()
*			   - XNandPsu_ReadPage
*			   Modified
*			   - XNandPsu_SetFeature()
*			   - XNandPsu_GetFeature()
*			   and made them public.
*			   Removed Failure Return for BCF Error check in
*			   XNandPsu_ReadPage() and added BCH_Error counter
*			   in the instance pointer structure.
* 			   Added XNandPsu_Prepare_Cmd API
*			   Replaced
*			   - XNandPsu_IntrStsEnable
*			   - XNandPsu_IntrStsClear
*			   - XNandPsu_IntrClear
*			   - XNandPsu_SetProgramReg
*			   with XNandPsu_WriteReg call
*			   Modified xnandpsu.c file API's with above changes.
*  			   Corrected the program command for Set Feature API.
*			   Modified
*			   - XNandPsu_OnfiReadStatus
*			   - XNandPsu_GetFeature
*			   - XNandPsu_SetFeature
*			   to add support for DDR mode.
*			   Changed Convention for SLC/MLC
*			   SLC --> HAMMING
*			   MLC --> BCH
*			   SlcMlc --> IsBCH
*			   Removed extra DMA mode initialization from
*			   the XNandPsu_CfgInitialize API.
*			   Modified
*			   - XNandPsu_SetEccAddrSize
*			   ECC address now is calculated based upon the
*			   size of spare area
*			   Modified Block Erase API, removed clearing of
*			   packet register before erase.
*			   Clearing Data Interface Register before
*			   XNandPsu_OnfiReset call.
*			   Modified XNandPsu_ChangeTimingMode API supporting
*			   SDR and NVDDR interface for timing modes 0 to 5.
*			   Modified Bbt Signature and Version Offset value for
*			   Oob and No-Oob region.
* 1.0   kpc    17/6/2015   Added timer based timeout intsead of sw counter.
* 1.1   mi     09/16/16 Removed compilation warnings with extra compiler flags.
* 1.1	nsk    11/07/16    Change memcpy to Xil_MemCpy to handle word aligned
*	                   data access.
* 1.2	nsk    01/19/17    Fix for the failure of reading nand first redundant
* 	                   parameter page. CR#966603
* 1.3	nsk    08/14/17    Added CCI support
* 1.4	nsk    04/10/18    Added ICCARM compiler support. CR#997552.
* 1.5   mus    11/05/18 Support 64 bit DMA addresses for Microblaze-X platform.
* 1.5   mus    11/05/18 Updated XNandPsu_ChangeClockFreq to fix compilation
*                       warnings.
* 1.6	sd     06/02/20    Added Clock support
* 1.6	sd     20/03/20    Added compilation flag
* 1.8   sg     03/18/21	   Added validation check for parameter page.
* 1.9   akm    07/15/21    Initialize NandInstPtr with Data Interface & Timing mode info.
* 1.10  akm    10/20/21    Fix gcc warnings.
* 1.10  akm    12/21/21    Validate input parameters before use.
* 1.10  akm    01/05/22    Remove assert checks form static and internal APIs.
* 1.11  akm    03/31/22    Fix unused parameter warning.
* 1.11  akm    03/31/22    Fix misleading-indentation warning.
*
* </pre>
*
******************************************************************************/

/***************************** Include Files *********************************/
#include "xnandpsu.h"
#include "xnandpsu_bbm.h"
#include "sleep.h"
#include "xil_mem.h"
/************************** Constant Definitions *****************************/

static const XNandPsu_EccMatrix EccMatrix[] = {
	/* 512 byte page */
	{XNANDPSU_PAGE_SIZE_512, 9U, 1U, XNANDPSU_HAMMING, 0x20DU, 0x3U},
	{XNANDPSU_PAGE_SIZE_512, 9U, 4U, XNANDPSU_BCH, 0x209U, 0x7U},
	{XNANDPSU_PAGE_SIZE_512, 9U, 8U, XNANDPSU_BCH, 0x203U, 0xDU},
	/* 2K byte page */
	{XNANDPSU_PAGE_SIZE_2K, 9U, 1U, XNANDPSU_HAMMING, 0x834U, 0xCU},
	{XNANDPSU_PAGE_SIZE_2K, 9U, 4U, XNANDPSU_BCH, 0x826U, 0x1AU},
	{XNANDPSU_PAGE_SIZE_2K, 9U, 8U, XNANDPSU_BCH, 0x80cU, 0x34U},
	{XNANDPSU_PAGE_SIZE_2K, 9U, 12U, XNANDPSU_BCH, 0x822U, 0x4EU},
	{XNANDPSU_PAGE_SIZE_2K, 10U, 24U, XNANDPSU_BCH, 0x81cU, 0x54U},
	/* 4K byte page */
	{XNANDPSU_PAGE_SIZE_4K, 9U, 1U, XNANDPSU_HAMMING, 0x1068U, 0x18U},
	{XNANDPSU_PAGE_SIZE_4K, 9U, 4U, XNANDPSU_BCH, 0x104cU, 0x34U},
	{XNANDPSU_PAGE_SIZE_4K, 9U, 8U, XNANDPSU_BCH, 0x1018U, 0x68U},
	{XNANDPSU_PAGE_SIZE_4K, 9U, 12U, XNANDPSU_BCH, 0x1044U, 0x9CU},
	{XNANDPSU_PAGE_SIZE_4K, 10U, 24U, XNANDPSU_BCH, 0x1038U, 0xA8U},
	/* 8K byte page */
	{XNANDPSU_PAGE_SIZE_8K, 9U, 1U, XNANDPSU_HAMMING, 0x20d0U, 0x30U},
	{XNANDPSU_PAGE_SIZE_8K, 9U, 4U, XNANDPSU_BCH, 0x2098U, 0x68U},
	{XNANDPSU_PAGE_SIZE_8K, 9U, 8U, XNANDPSU_BCH, 0x2030U, 0xD0U},
	{XNANDPSU_PAGE_SIZE_8K, 9U, 12U, XNANDPSU_BCH, 0x2088U, 0x138U},
	{XNANDPSU_PAGE_SIZE_8K, 10U, 24U, XNANDPSU_BCH, 0x2070U, 0x150U},
	/* 16K byte page */
	{XNANDPSU_PAGE_SIZE_16K, 9U, 1U, XNANDPSU_HAMMING, 0x4460U, 0x60U},
	{XNANDPSU_PAGE_SIZE_16K, 9U, 4U, XNANDPSU_BCH, 0x43f0U, 0xD0U},
	{XNANDPSU_PAGE_SIZE_16K, 9U, 8U, XNANDPSU_BCH, 0x4320U, 0x1A0U},
	{XNANDPSU_PAGE_SIZE_16K, 9U, 12U, XNANDPSU_BCH, 0x4250U, 0x270U},
	{XNANDPSU_PAGE_SIZE_16K, 10U, 24U, XNANDPSU_BCH, 0x4220U, 0x2A0U}
};

/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/

/************************** Function Prototypes ******************************/

static s32 XNandPsu_FlashInit(XNandPsu *InstancePtr);

static s32 XNandPsu_InitGeometry(XNandPsu *InstancePtr, OnfiParamPage *Param);

static void XNandPsu_InitFeatures(XNandPsu *InstancePtr, OnfiParamPage *Param);

static void XNandPsu_InitDataInterface(XNandPsu *InstancePtr, OnfiParamPage *Param);

static void XNandPsu_InitTimingMode(XNandPsu *InstancePtr, OnfiParamPage *Param);

static s32 XNandPsu_PollRegTimeout(XNandPsu *InstancePtr, u32 RegOffset,
					u32 Mask, u32 Timeout);

static void XNandPsu_SetPktSzCnt(XNandPsu *InstancePtr, u32 PktSize,
						u32 PktCount);

static void XNandPsu_SetPageColAddr(XNandPsu *InstancePtr, u32 Page, u16 Col);

static void XNandPsu_SetPageSize(XNandPsu *InstancePtr);

static void XNandPsu_SelectChip(XNandPsu *InstancePtr, u32 Target);

static s32 XNandPsu_OnfiReset(XNandPsu *InstancePtr, u32 Target);

static s32 XNandPsu_OnfiReadStatus(XNandPsu *InstancePtr, u32 Target,
							u16 *OnfiStatus);

static s32 XNandPsu_OnfiReadId(XNandPsu *InstancePtr, u32 Target, u8 IdAddr,
							u32 IdLen, u8 *Buf);

static s32 XNandPsu_OnfiReadParamPage(XNandPsu *InstancePtr, u32 Target,
						u8 *Buf);

static s32 XNandPsu_ProgramPage(XNandPsu *InstancePtr, u32 Target, u32 Page,
							u32 Col, u8 *Buf);

static s32 XNandPsu_ReadPage(XNandPsu *InstancePtr, u32 Target, u32 Page,
							u32 Col, u8 *Buf);

static s32 XNandPsu_CheckOnDie(XNandPsu *InstancePtr);

static void XNandPsu_SetEccAddrSize(XNandPsu *InstancePtr);

static s32 XNandPsu_ChangeReadColumn(XNandPsu *InstancePtr, u32 Target,
					u32 Col, u32 PktSize, u32 PktCount,
					u8 *Buf);

static s32 XNandPsu_ChangeWriteColumn(XNandPsu *InstancePtr, u32 Target,
					u32 Col, u32 PktSize, u32 PktCount,
					u8 *Buf);

static s32 XNandPsu_InitExtEcc(XNandPsu *InstancePtr, OnfiExtPrmPage *ExtPrm);

static s32 XNandPsu_Data_ReadWrite(XNandPsu *InstancePtr, u8* Buf, u32 PktCount,
				u32 PktSize, u32 Operation, u8 DmaMode);

static s32 XNandPsu_WaitFor_Transfer_Complete(XNandPsu *InstancePtr);

static s32 XNandPsu_Device_Ready(XNandPsu *InstancePtr, u32 Target);

static void XNandPsu_Fifo_Read(XNandPsu *InstancePtr, u8* Buf, u32 Size);

static void XNandPsu_Fifo_Write(XNandPsu *InstancePtr, u8* Buf, u32 Size);

static void XNandPsu_Update_DmaAddr(XNandPsu *InstancePtr, u8* Buf);
/*****************************************************************************/
/**
*
* This function initializes a specific XNandPsu instance. This function must
* be called prior to using the NAND flash device to read or write any data.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	ConfigPtr points to XNandPsu device configuration structure.
* @param	EffectiveAddr is the base address of NAND flash controller.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if fail.
*
* @note		The user needs to first call the XNandPsu_LookupConfig() API
*		which returns the Configuration structure pointer which is
*		passed as a parameter to the XNandPsu_CfgInitialize() API.
*
******************************************************************************/
s32 XNandPsu_CfgInitialize(XNandPsu *InstancePtr, XNandPsu_Config *ConfigPtr,
				u32 EffectiveAddr)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(ConfigPtr != NULL);

	s32 Status = XST_FAILURE;

	/* Initialize InstancePtr Config structure */
	InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
	InstancePtr->Config.BaseAddress = EffectiveAddr;
	InstancePtr->Config.IsCacheCoherent = ConfigPtr->IsCacheCoherent;
#if defined  (XCLOCKING)
	InstancePtr->Config.RefClk = ConfigPtr->RefClk;
#endif
	/* Operate in Polling Mode */
	InstancePtr->Mode = XNANDPSU_POLLING;
	/* Enable MDMA mode by default */
	InstancePtr->DmaMode = XNANDPSU_MDMA;
	InstancePtr->IsReady = XIL_COMPONENT_IS_READY;

#ifdef __rtems__
	/* Set page cache to unavailable */
	InstancePtr->PartialDataPageIndex = XNANDPSU_PAGE_CACHE_UNAVAILABLE;
#endif

	/* Initialize the NAND flash targets */
	Status = XNandPsu_FlashInit(InstancePtr);
	if (Status != XST_SUCCESS) {
#ifdef XNANDPSU_DEBUG
		xil_printf("%s: Flash init failed\r\n",__func__);
#endif
		goto Out;
	}
	/* Set ECC mode */
	if (InstancePtr->Features.EzNand != 0U) {
		InstancePtr->EccMode = XNANDPSU_EZNAND;
	} else if (InstancePtr->Features.OnDie != 0U) {
		InstancePtr->EccMode = XNANDPSU_ONDIE;
	} else {
		InstancePtr->EccMode = XNANDPSU_HWECC;
	}

	/* Initialize Ecc Error flip counters */
	 InstancePtr->Ecc_Stat_PerPage_flips = 0U;
	 InstancePtr->Ecc_Stats_total_flips = 0U;

	/*
	 * Scan for the bad block table(bbt) stored in the flash & load it in
	 * memory(RAM).  If bbt is not found, create bbt by scanning factory
	 * marked bad blocks and store it in last good blocks of flash.
	 */
	XNandPsu_InitBbtDesc(InstancePtr);
	Status = XNandPsu_ScanBbt(InstancePtr);
	if (Status != XST_SUCCESS) {
#ifdef XNANDPSU_DEBUG
		xil_printf("%s: BBT scan failed\r\n",__func__);
#endif
		goto Out;
	}

#ifdef __rtems__
	/* Set page cache to none */
	InstancePtr->PartialDataPageIndex = XNANDPSU_PAGE_CACHE_NONE;
#endif
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function initializes the NAND flash and gets the geometry information.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_FlashInit(XNandPsu *InstancePtr)
{
	u32 Target;
	u8 Id[ONFI_SIG_LEN] = {0U};
	OnfiParamPage Param[ONFI_MND_PRM_PGS] = {0U};
	s32 Status = XST_FAILURE;
	u32 Index;
	u32 Crc;
	u32 PrmPgOff;
	u32 PrmPgLen;
#ifdef __ICCARM__
#pragma pack(push, 1)
	OnfiExtPrmPage ExtParam = {0U};
#pragma pack(pop)
#else
	OnfiExtPrmPage ExtParam __attribute__ ((aligned(64))) = {0U};
#endif

	/* Clear Data Interface Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_DATA_INTF_OFFSET, 0U);

	/* Clear DMA Buffer Boundary Register */
	XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
			XNANDPSU_DMA_BUF_BND_OFFSET, 0U);

	for (Target = 0U; Target < XNANDPSU_MAX_TARGETS; Target++) {
		/* Reset the Target */
		Status = XNandPsu_OnfiReset(InstancePtr, Target);
		if (Status != XST_SUCCESS) {
			goto Out;
		}
		/* Read ONFI ID */
		Status = XNandPsu_OnfiReadId(InstancePtr, Target,
						ONFI_READ_ID_ADDR,
						ONFI_SIG_LEN,
						(u8 *)&Id[0]);
		if (Status != XST_SUCCESS) {
			goto Out;
		}

		if (!IS_ONFI(Id)) {
			if (Target == 0U) {
#ifdef XNANDPSU_DEBUG
				xil_printf("%s: ONFI ID doesn't match\r\n",
								__func__);
#endif
				Status = XST_FAILURE;
				goto Out;
			}
		}

		/* Read Parameter Page */
		Status = XNandPsu_OnfiReadParamPage(InstancePtr,
						Target, (u8 *)&Param[0]);
		if (Status != XST_SUCCESS) {
			goto Out;
		}
		for(Index = 0U; Index < ONFI_MND_PRM_PGS; Index++){
			/* Check CRC */
			Crc = XNandPsu_OnfiParamPageCrc((u8*)&Param[Index], 0U,
								ONFI_CRC_LEN);
			if (Crc != Param[Index].Crc) {
#ifdef XNANDPSU_DEBUG
				xil_printf("%s: ONFI parameter page (%d) crc check failed\r\n",
							__func__, Index);
#endif
				continue;
			} else {
				break;
			}
		}
		if (Index >= ONFI_MND_PRM_PGS) {
			Status = XST_FAILURE;
			goto Out;
		}
		/* Fill Geometry for the first target */
		if (Target == 0U) {
			Status = XNandPsu_InitGeometry(InstancePtr, &Param[Index]);
			if (Status != XST_SUCCESS) {
				goto Out;
			}
			XNandPsu_InitDataInterface(InstancePtr, &Param[Index]);
			XNandPsu_InitTimingMode(InstancePtr, &Param[Index]);
			XNandPsu_InitFeatures(InstancePtr, &Param[Index]);
			if ((!InstancePtr->Features.EzNand) != 0U) {
				Status =XNandPsu_CheckOnDie(InstancePtr);
				if (Status != XST_SUCCESS) {
					InstancePtr->Features.OnDie = 0U;
				}
			}
			if ((InstancePtr->Geometry.NumBitsECC == 0xFFU) &&
				(InstancePtr->Features.ExtPrmPage != 0U)) {
					/* ONFI 3.1 section 5.7.1.6 & 5.7.1.7 */
				PrmPgLen = (u32)Param[Index].ExtParamPageLen * 16U;
				PrmPgOff = (u32)((u32)Param[Index].NumOfParamPages *
						ONFI_PRM_PG_LEN) + (Index * (u32)PrmPgLen);

				Status = XNandPsu_ChangeReadColumn(
						InstancePtr, Target,
						PrmPgOff, PrmPgLen, 1U,
						(u8 *)(void *)&ExtParam);
				if (Status != XST_SUCCESS) {
					goto Out;
				}
				/* Check CRC */
				Crc = XNandPsu_OnfiParamPageCrc(
						(u8 *)&ExtParam,
						2U, PrmPgLen);
				if (Crc != ExtParam.Crc) {
#ifdef XNANDPSU_DEBUG
	xil_printf("%s: ONFI extended parameter page (%d) crc check failed\r\n",
							__func__, Index);
#endif
					Status = XST_FAILURE;
					goto Out;
				}
				/* Initialize Extended ECC info */
				Status = XNandPsu_InitExtEcc(
						InstancePtr,
						&ExtParam);
				if (Status != XST_SUCCESS) {
#ifdef XNANDPSU_DEBUG
	xil_printf("%s: Init extended ecc failed\r\n",__func__);
#endif
					goto Out;
				}
			}
			/* Configure ECC settings */
			XNandPsu_SetEccAddrSize(InstancePtr);
		}
		InstancePtr->Geometry.NumTargets++;
	}
	/* Calculate total number of blocks and total size of flash */
	InstancePtr->Geometry.NumPages = InstancePtr->Geometry.NumTargets *
					InstancePtr->Geometry.NumTargetPages;
	InstancePtr->Geometry.NumBlocks = InstancePtr->Geometry.NumTargets *
					InstancePtr->Geometry.NumTargetBlocks;
	InstancePtr->Geometry.DeviceSize =
					(u64)InstancePtr->Geometry.NumTargets *
					InstancePtr->Geometry.TargetSize;

	Status = XST_SUCCESS;
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function initializes the geometry information from ONFI parameter page.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Param is pointer to the ONFI parameter page.
*
* @return
*               - XST_SUCCESS if successful.
*               - XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_InitGeometry(XNandPsu *InstancePtr, OnfiParamPage *Param)
{
	s32 Status = XST_FAILURE;

	if (Param->BytesPerPage > XNANDPSU_MAX_PAGE_SIZE) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Invalid Bytes Per Page %d\r\n",
					__func__, Param->BytesPerPage);
#endif
			goto Out;
	}
	InstancePtr->Geometry.BytesPerPage = Param->BytesPerPage;


	if (Param->SpareBytesPerPage > XNANDPSU_MAX_SPARE_SIZE) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Invalid Spare Bytes Per Page %d\r\n",
					__func__, Param->SpareBytesPerPage);
#endif
			goto Out;
	}
	InstancePtr->Geometry.SpareBytesPerPage = Param->SpareBytesPerPage;

	if (Param->PagesPerBlock > XNANDPSU_MAX_PAGES_PER_BLOCK) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Invalid Page Count Per Block %d\r\n",
					__func__, Param->PagesPerBlock);
#endif
			goto Out;
	}
	InstancePtr->Geometry.PagesPerBlock = Param->PagesPerBlock;


	if (Param->BlocksPerLun > XNANDPSU_MAX_BLOCKS) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Invalid block count per LUN %d\r\n",
					__func__, Param->BlocksPerLun);
#endif
			goto Out;
	}
	InstancePtr->Geometry.BlocksPerLun = Param->BlocksPerLun;

	if (Param->NumLuns > XNANDPSU_MAX_LUNS) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Invalid LUN count %d\r\n",
					__func__, Param->NumLuns);
#endif
			goto Out;
	}
	InstancePtr->Geometry.NumLuns = Param->NumLuns;

	InstancePtr->Geometry.RowAddrCycles = Param->AddrCycles & 0xFU;
	InstancePtr->Geometry.ColAddrCycles = (Param->AddrCycles >> 4U) & 0xFU;
	InstancePtr->Geometry.NumBitsPerCell = Param->BitsPerCell;
	InstancePtr->Geometry.NumBitsECC = Param->EccBits;
	InstancePtr->Geometry.BlockSize = (Param->PagesPerBlock *
						Param->BytesPerPage);
	InstancePtr->Geometry.NumTargetBlocks = (Param->BlocksPerLun *
						(u32)Param->NumLuns);
	InstancePtr->Geometry.NumTargetPages = (Param->BlocksPerLun *
						(u32)Param->NumLuns *
						Param->PagesPerBlock);
	InstancePtr->Geometry.TargetSize = ((u64)Param->BlocksPerLun *
						(u64)Param->NumLuns *
						(u64)Param->PagesPerBlock *
						(u64)Param->BytesPerPage);
	InstancePtr->Geometry.EccCodeWordSize = 9U; /* 2 power of 9 = 512 */
	if (InstancePtr->Geometry.NumTargetBlocks > XNANDPSU_MAX_BLOCKS)
		xil_printf("!!! Device contains more blocks than the max defined blocks in driver\r\n");

#ifdef XNANDPSU_DEBUG
	xil_printf("Manufacturer: %s\r\n", Param->DeviceManufacturer);
	xil_printf("Device Model: %s\r\n", Param->DeviceModel);
	xil_printf("Jedec ID: 0x%x\r\n", Param->JedecManufacturerId);
	xil_printf("Bytes Per Page: 0x%x\r\n", Param->BytesPerPage);
	xil_printf("Spare Bytes Per Page: 0x%x\r\n", Param->SpareBytesPerPage);
	xil_printf("Pages Per Block: 0x%x\r\n", Param->PagesPerBlock);
	xil_printf("Blocks Per LUN: 0x%x\r\n", Param->BlocksPerLun);
	xil_printf("Number of LUNs: 0x%x\r\n", Param->NumLuns);
	xil_printf("Number of bits per cell: 0x%x\r\n", Param->BitsPerCell);
	xil_printf("Number of ECC bits: 0x%x\r\n", Param->EccBits);
	xil_printf("Block Size: 0x%x\r\n", InstancePtr->Geometry.BlockSize);

	xil_printf("Number of Target Blocks: 0x%x\r\n",
					InstancePtr->Geometry.NumTargetBlocks);
	xil_printf("Number of Target Pages: 0x%x\r\n",
					InstancePtr->Geometry.NumTargetPages);

#endif
	Status = XST_SUCCESS;
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function initializes the feature list from ONFI parameter page.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Param is pointer to ONFI parameter page buffer.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_InitFeatures(XNandPsu *InstancePtr, OnfiParamPage *Param)
{
	InstancePtr->Features.NvDdr = ((Param->Features & (1U << 5)) != 0U) ?
								1U : 0U;
	InstancePtr->Features.EzNand = ((Param->Features & (1U << 9)) != 0U) ?
								1U : 0U;
	InstancePtr->Features.ExtPrmPage = ((Param->Features & (1U << 7)) != 0U) ?
								1U : 0U;
}

/*****************************************************************************/
/**
*
* This function initializes the Data Interface from ONFI parameter page.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Param is pointer to ONFI parameter page buffer.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_InitDataInterface(XNandPsu *InstancePtr, OnfiParamPage *Param)
{
	if (Param->NVDDRTimingMode)
		InstancePtr->DataInterface = XNANDPSU_NVDDR;
	else if (Param->SDRTimingMode)
		InstancePtr->DataInterface = XNANDPSU_SDR;
}

/*****************************************************************************/
/**
*
* This function initializes the Timing mode from ONFI parameter page.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Param is pointer to ONFI parameter page buffer.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_InitTimingMode(XNandPsu *InstancePtr, OnfiParamPage *Param)
{
	s8 Mode;
	u8 TimingMode =  (u8)(Param->SDRTimingMode);

	if (InstancePtr->DataInterface == XNANDPSU_NVDDR)
		TimingMode = Param->NVDDRTimingMode;

	for(Mode = XNANDPSU_MAX_TIMING_MODE; Mode >= 0; Mode--) {
		if (TimingMode & (0x01 << Mode)) {
			InstancePtr->TimingMode = Mode;
			break;
		} else {
			continue;
		}
	}
}

/*****************************************************************************/
/**
*
* This function checks if the flash supports on-die ECC.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Param is pointer to ONFI parameter page.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_CheckOnDie(XNandPsu *InstancePtr)
{
	s32 Status = XST_FAILURE;
	u8 JedecId[2] = {0U};
	u8 EccSetFeature[4] = {0x08U, 0x00U, 0x00U, 0x00U};
	u8 EccGetFeature[4] ={0U};

	/*
	 * Check if this flash supports On-Die ECC.
	 * For more information, refer to Micron TN2945.
	 * Micron Flash: MT29F1G08ABADA, MT29F1G08ABBDA
	 *		 MT29F1G16ABBDA,
	 *		 MT29F2G08ABBEA, MT29F2G16ABBEA,
	 *		 MT29F2G08ABAEA, MT29F2G16ABAEA,
	 *		 MT29F4G08ABBDA, MT29F4G16ABBDA,
	 *		 MT29F4G08ABADA, MT29F4G16ABADA,
	 *		 MT29F8G08ADBDA, MT29F8G16ADBDA,
	 *		 MT29F8G08ADADA, MT29F8G16ADADA
	 */

	/* Read JEDEC ID */
	Status = XNandPsu_OnfiReadId(InstancePtr, 0U, 0x00U, 2U, &JedecId[0]);
	if (Status != XST_SUCCESS) {
		goto Out;
	}

	if ((JedecId[0] == 0x2CU) &&
	/* 1 Gb flash devices */
	((JedecId[1] == 0xF1U) ||
	(JedecId[1] == 0xA1U) ||
	(JedecId[1] == 0xB1U) ||
	/* 2 Gb flash devices */
	(JedecId[1] == 0xAAU) ||
	(JedecId[1] == 0xBAU) ||
	(JedecId[1] == 0xDAU) ||
	(JedecId[1] == 0xCAU) ||
	/* 4 Gb flash devices */
	(JedecId[1] == 0xACU) ||
	(JedecId[1] == 0xBCU) ||
	(JedecId[1] == 0xDCU) ||
	(JedecId[1] == 0xCCU) ||
	/* 8 Gb flash devices */
	(JedecId[1] == 0xA3U) ||
	(JedecId[1] == 0xB3U) ||
	(JedecId[1] == 0xD3U) ||
	(JedecId[1] == 0xC3U))) {
#ifdef XNANDPSU_DEBUG
		xil_printf("%s: Ondie flash detected, jedec id 0x%x 0x%x\r\n",
					__func__, JedecId[0], JedecId[1]);
#endif
		/* On-Die Set Feature */
		Status = XNandPsu_SetFeature(InstancePtr, 0U, 0x90U,
						&EccSetFeature[0]);
		if (Status != XST_SUCCESS) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Ondie set_feature failed\r\n",
								__func__);
#endif
			goto Out;
		}
		/* Check to see if ECC feature is set */
		Status = XNandPsu_GetFeature(InstancePtr, 0U, 0x90U,
						&EccGetFeature[0]);
		if (Status != XST_SUCCESS) {
#ifdef XNANDPSU_DEBUG
			xil_printf("%s: Ondie get_feature failed\r\n",
								__func__);
#endif
			goto Out;
		}
		if ((EccGetFeature[0] & 0x08U) != 0U) {
			InstancePtr->Features.OnDie = 1U;
			Status = XST_SUCCESS;
		}
	} else {
		/* On-Die flash not found */
		Status = XST_FAILURE;
	}
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function enables DMA mode of controller operation.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
void XNandPsu_EnableDmaMode(XNandPsu *InstancePtr)
{
	/* Assert the input arguments. */
	Xil_AssertVoid(InstancePtr != NULL);
	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	InstancePtr->DmaMode = XNANDPSU_MDMA;
}

/*****************************************************************************/
/**
*
* This function disables DMA mode of driver/controller operation.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
void XNandPsu_DisableDmaMode(XNandPsu *InstancePtr)
{
	/* Assert the input arguments. */
	Xil_AssertVoid(InstancePtr != NULL);
	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	InstancePtr->DmaMode = XNANDPSU_PIO;
}

/*****************************************************************************/
/**
*
* This function enables ECC mode of driver/controller operation.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
void XNandPsu_EnableEccMode(XNandPsu *InstancePtr)
{
	/* Assert the input arguments. */
	Xil_AssertVoid(InstancePtr != NULL);
	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	InstancePtr->EccMode = XNANDPSU_HWECC;
}

/*****************************************************************************/
/**
*
* This function disables ECC mode of driver/controller operation.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
void XNandPsu_DisableEccMode(XNandPsu *InstancePtr)
{
	/* Assert the input arguments. */
	Xil_AssertVoid(InstancePtr != NULL);
	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	InstancePtr->EccMode = XNANDPSU_NONE;
}

#ifdef __rtems__
#include <rtems/rtems/clock.h>
static void udelay( void )
{
	uint64_t time = rtems_clock_get_uptime_nanoseconds() + 1000;
	while (1) {
		uint64_t newtime = rtems_clock_get_uptime_nanoseconds();
		if (newtime > time) {
			break;
		}
	}
}
#define usleep(x) udelay()
#endif

/*****************************************************************************/
/**
*
* This function polls for a register bit set status till the timeout.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	RegOffset is the offset of register.
* @param	Mask is the bitmask.
* @param	Timeout is the timeout value.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_PollRegTimeout(XNandPsu *InstancePtr, u32 RegOffset,
					u32 Mask, u32 Timeout)
{
	s32 Status = XST_FAILURE;
	volatile u32 RegVal;
	u32 TimeoutVar = Timeout;

	while (TimeoutVar > 0U) {
		RegVal = XNandPsu_ReadReg(InstancePtr->Config.BaseAddress,
						RegOffset) & Mask;
		if (RegVal != 0U) {
			break;
		}
		TimeoutVar--;
		usleep(1);
	}

	if (TimeoutVar <= 0U) {
		Status = XST_FAILURE;
	} else {
		Status = XST_SUCCESS;
	}

	return Status;
}

/*****************************************************************************/
/**
*
* This function sets packet size and packet count values in packet register.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	PktSize is the packet size.
* @param	PktCount is the packet count.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_SetPktSzCnt(XNandPsu *InstancePtr, u32 PktSize,
						u32 PktCount)
{
	/* Update Packet Register with pkt size and count */
	XNandPsu_ReadModifyWrite(InstancePtr, XNANDPSU_PKT_OFFSET,
				((u32)XNANDPSU_PKT_PKT_SIZE_MASK |
				(u32)XNANDPSU_PKT_PKT_CNT_MASK),
				((PktSize & XNANDPSU_PKT_PKT_SIZE_MASK) |
				((PktCount << XNANDPSU_PKT_PKT_CNT_SHIFT) &
				XNANDPSU_PKT_PKT_CNT_MASK)));
}

/*****************************************************************************/
/**
*
* This function sets Page and Column values in the Memory address registers.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Page is the page value.
* @param	Col is the column value.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_SetPageColAddr(XNandPsu *InstancePtr, u32 Page, u16 Col)
{
	/* Program Memory Address Register 1 */
	XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_MEM_ADDR1_OFFSET,
				(((u32)Col & XNANDPSU_MEM_ADDR1_COL_ADDR_MASK) |
				((Page << (u32)XNANDPSU_MEM_ADDR1_PG_ADDR_SHIFT) &
				XNANDPSU_MEM_ADDR1_PG_ADDR_MASK)));
	/* Program Memory Address Register 2 */
	XNandPsu_ReadModifyWrite(InstancePtr, XNANDPSU_MEM_ADDR2_OFFSET,
				XNANDPSU_MEM_ADDR2_MEM_ADDR_MASK,
				((Page >> XNANDPSU_MEM_ADDR1_PG_ADDR_SHIFT) &
				XNANDPSU_MEM_ADDR2_MEM_ADDR_MASK));
}

/*****************************************************************************/
/**
*
* This function sets the size of page in Command Register.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_SetPageSize(XNandPsu *InstancePtr)
{
	u32 PageSizeMask = 0;
	u32 PageSize = InstancePtr->Geometry.BytesPerPage;

	/* Calculate page size mask */
	switch(PageSize) {
		case XNANDPSU_PAGE_SIZE_512:
			PageSizeMask = (0U << XNANDPSU_CMD_PG_SIZE_SHIFT);
			break;
		case XNANDPSU_PAGE_SIZE_2K:
			PageSizeMask = (1U << XNANDPSU_CMD_PG_SIZE_SHIFT);
			break;
		case XNANDPSU_PAGE_SIZE_4K:
			PageSizeMask = (2U << XNANDPSU_CMD_PG_SIZE_SHIFT);
			break;
		case XNANDPSU_PAGE_SIZE_8K:
			PageSizeMask = (3U << XNANDPSU_CMD_PG_SIZE_SHIFT);
			break;
		case XNANDPSU_PAGE_SIZE_16K:
			PageSizeMask = (4U << XNANDPSU_CMD_PG_SIZE_SHIFT);
			break;
		case XNANDPSU_PAGE_SIZE_1K_16BIT:
			PageSizeMask = (5U << XNANDPSU_CMD_PG_SIZE_SHIFT);
			break;
		default:
			/* Not supported */
			break;
	}
	/* Update Command Register */
	XNandPsu_ReadModifyWrite(InstancePtr, XNANDPSU_CMD_OFFSET,
				XNANDPSU_CMD_PG_SIZE_MASK, PageSizeMask);
}

/*****************************************************************************/
/**
*
* This function setup the Ecc Register.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_SetEccAddrSize(XNandPsu *InstancePtr)
{
	u32 PageSize = InstancePtr->Geometry.BytesPerPage;
	u32 CodeWordSize = InstancePtr->Geometry.EccCodeWordSize;
	u32 NumEccBits = InstancePtr->Geometry.NumBitsECC;
	u32 Index;
	u32 Found = 0U;
	u8 BchModeVal;

	for (Index = 0U; Index < (sizeof(EccMatrix)/sizeof(XNandPsu_EccMatrix));
						Index++) {
		if ((EccMatrix[Index].PageSize == PageSize) &&
			(EccMatrix[Index].CodeWordSize >= CodeWordSize)) {
			if (EccMatrix[Index].NumEccBits >= NumEccBits) {
				Found = Index;
				break;
			}
			else {
				Found = Index;
			}
		}
	}

	if (Found != 0U) {
		if(InstancePtr->Geometry.SpareBytesPerPage < 64U) {
			InstancePtr->EccCfg.EccAddr = (u16)PageSize;
		}
		else {
			InstancePtr->EccCfg.EccAddr = ((u16)PageSize +
				(InstancePtr->Geometry.SpareBytesPerPage
						- EccMatrix[Found].EccSize));
		}
		InstancePtr->EccCfg.EccSize = EccMatrix[Found].EccSize;
		InstancePtr->EccCfg.NumEccBits = EccMatrix[Found].NumEccBits;
		InstancePtr->EccCfg.CodeWordSize =
						EccMatrix[Found].CodeWordSize;
#ifdef XNANDPSU_DEBUG
		xil_printf("ECC: addr 0x%x size 0x%x numbits %d "
				   "codesz %d\r\n",
				   InstancePtr->EccCfg.EccAddr,
				   InstancePtr->EccCfg.EccSize,
				   InstancePtr->EccCfg.NumEccBits,
				   InstancePtr->EccCfg.CodeWordSize);
#endif
		if (EccMatrix[Found].IsBCH == XNANDPSU_HAMMING) {
			InstancePtr->EccCfg.IsBCH = 0U;
		} else {
			InstancePtr->EccCfg.IsBCH = 1U;
		}
		/* Write ECC register */
		XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				(u32)XNANDPSU_ECC_OFFSET,
				((u32)InstancePtr->EccCfg.EccAddr |
				((u32)InstancePtr->EccCfg.EccSize << (u32)16) |
				((u32)InstancePtr->EccCfg.IsBCH << (u32)27)));

		if (EccMatrix[Found].IsBCH == XNANDPSU_BCH) {
			/* Write memory address register 2 */
			switch(InstancePtr->EccCfg.NumEccBits) {
				case 16U:
					BchModeVal = 0x0U;
					break;
				case 12U:
					BchModeVal = 0x1U;
					break;
				case 8U:
					BchModeVal = 0x2U;
					break;
				case 4U:
					BchModeVal = 0x3U;
					break;
				case 24U:
					BchModeVal = 0x4U;
					break;
				default:
					BchModeVal = 0x0U;
					break;
			}
			XNandPsu_ReadModifyWrite(InstancePtr,
				XNANDPSU_MEM_ADDR2_OFFSET,
				XNANDPSU_MEM_ADDR2_NFC_BCH_MODE_MASK,
				((u32)BchModeVal <<
				(u32)XNANDPSU_MEM_ADDR2_NFC_BCH_MODE_SHIFT));
		}
	}
}

/*****************************************************************************/
/**
*
* This function setup the Ecc Spare Command Register.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_SetEccSpareCmd(XNandPsu *InstancePtr, u16 SpareCmd,
								u8 AddrCycles)
{
	XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				(u32)XNANDPSU_ECC_SPR_CMD_OFFSET,
				(u32)SpareCmd | ((u32)AddrCycles << 28U));
}

/*****************************************************************************/
/**
*
* This function sets the chip select value in memory address2 register.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_SelectChip(XNandPsu *InstancePtr, u32 Target)
{
#if defined  (XCLOCKING)
	Xil_ClockEnable(InstancePtr->Config.RefClk);
#endif
	/* Update Memory Address2 register with chip select */
	XNandPsu_ReadModifyWrite(InstancePtr, XNANDPSU_MEM_ADDR2_OFFSET,
			XNANDPSU_MEM_ADDR2_CHIP_SEL_MASK,
			((Target << XNANDPSU_MEM_ADDR2_CHIP_SEL_SHIFT) &
			XNANDPSU_MEM_ADDR2_CHIP_SEL_MASK));
}

/*****************************************************************************/
/**
*
* This function sends ONFI Reset command to the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_OnfiReset(XNandPsu *InstancePtr, u32 Target)
{
	s32 Status = XST_FAILURE;

	/* Enable Transfer Complete Interrupt in Interrupt Status Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_INTR_STS_EN_OFFSET,
		XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK);
	/* Program Command Register */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_RST, ONFI_CMD_INVALID, 0U,
			0U, 0U);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set Reset in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_PROG_OFFSET, XNANDPSU_PROG_RST_MASK);

	/* Poll for Transfer Complete event */
	Status = XNandPsu_WaitFor_Transfer_Complete(InstancePtr);

	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Read Status command to the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	OnfiStatus is the ONFI status value to return.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_OnfiReadStatus(XNandPsu *InstancePtr, u32 Target,
							u16 *OnfiStatus)
{
	s32 Status = XST_FAILURE;

	/* Enable Transfer Complete Interrupt in Interrupt Status Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_INTR_STS_EN_OFFSET,
		XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK);
	/* Program Command Register */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_RD_STS, ONFI_CMD_INVALID,
				0U, 0U, 0U);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Program Packet Size and Packet Count */
	if(InstancePtr->DataInterface == XNANDPSU_SDR)
		XNandPsu_SetPktSzCnt(InstancePtr, 1U, 1U);
	else
		XNandPsu_SetPktSzCnt(InstancePtr, 2U, 1U);

	/* Set Read Status in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_RD_STS_MASK);
	/* Poll for Transfer Complete event */
	Status = XNandPsu_WaitFor_Transfer_Complete(InstancePtr);
	/* Read Flash Status */
	*OnfiStatus = (u16) XNandPsu_ReadReg(InstancePtr->Config.BaseAddress,
						XNANDPSU_FLASH_STS_OFFSET);

	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Read ID command to the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Buf is the ONFI ID value to return.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_OnfiReadId(XNandPsu *InstancePtr, u32 Target, u8 IdAddr,
							u32 IdLen, u8 *Buf)
{
	s32 Status = XST_FAILURE;
	u32 Index;
	u32 Rem;
	u32 RegVal;
	u32 RemIdx;

	u32 *BufPtr = (u32 *)(void *)Buf;

	/*
	 * Enable Buffer Read Ready Interrupt in Interrupt Status Enable
	 * Register
	 */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_INTR_STS_EN_OFFSET,
		XNANDPSU_INTR_STS_EN_BUFF_RD_RDY_STS_EN_MASK);
	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_RD_ID, ONFI_CMD_INVALID, 0U,
					0U, ONFI_READ_ID_ADDR_CYCLES);

	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, 0U, IdAddr);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, IdLen, 1U);
	/* Set Read ID in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_RD_ID_MASK);

	/* Poll for Buffer Read Ready event */
	Status = XNandPsu_PollRegTimeout(
		InstancePtr,
		XNANDPSU_INTR_STS_OFFSET,
		XNANDPSU_INTR_STS_BUFF_RD_RDY_STS_EN_MASK,
		XNANDPSU_INTR_POLL_TIMEOUT);
	if (Status != XST_SUCCESS) {
#ifdef XNANDPSU_DEBUG
		xil_printf("%s: Poll for buf read ready timeout\r\n",
							__func__);
#endif
		goto Out;
	}
	/*
	 * Enable Transfer Complete Interrupt in Interrupt
	 * Status Enable Register
	 */

		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_INTR_STS_EN_OFFSET,
		XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK);

	/*
	 * Clear Buffer Read Ready Interrupt in Interrupt Status
	 * Register
	 */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_OFFSET,
			XNANDPSU_INTR_STS_BUFF_RD_RDY_STS_EN_MASK);
	/* Read Packet Data from Data Port Register */
	for (Index = 0U; Index < (IdLen/4); Index++) {
		*(BufPtr+Index) = XNandPsu_ReadReg(
					InstancePtr->Config.BaseAddress,
					XNANDPSU_BUF_DATA_PORT_OFFSET);
	}
	Rem = IdLen % 4;
	if (Rem != 0U) {
		RegVal = XNandPsu_ReadReg(
					InstancePtr->Config.BaseAddress,
					XNANDPSU_BUF_DATA_PORT_OFFSET);
		for (RemIdx = 0U; RemIdx < Rem; RemIdx++) {
			*(Buf + (Index * 4U) + RemIdx) = (u8) (RegVal >>
						(RemIdx * 8U)) & 0xFFU;
		}
	}

	Status = XNandPsu_WaitFor_Transfer_Complete(InstancePtr);

Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function sends the ONFI Read Parameter Page command to flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	PrmIndex is the index of parameter page.
* @param	Buf is the parameter page information to return.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_OnfiReadParamPage(XNandPsu *InstancePtr, u32 Target,
						u8 *Buf)
{
	s32 Status = XST_FAILURE;

	/*
	 * Enable Buffer Read Ready Interrupt in Interrupt Status Enable
	 * Register
	 */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_INTR_STS_EN_OFFSET,
		XNANDPSU_INTR_STS_EN_BUFF_RD_RDY_STS_EN_MASK);
	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_RD_PRM_PG, ONFI_CMD_INVALID,
					0U, 0U, ONFI_PRM_PG_ADDR_CYCLES);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, 0U, 0U);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, ONFI_MND_PRM_PGS*ONFI_PRM_PG_LEN, 1U);
	/* Set Read Parameter Page in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_RD_PRM_PG_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, 1U, ONFI_MND_PRM_PGS*ONFI_PRM_PG_LEN, 0, 0);

	return Status;
}

/*****************************************************************************/
/**
*
* This function returns the length including bad blocks from a given offset and
* length.
*
* @param	InstancePtr is the pointer to the XNandPsu instance.
* @param	Offset is the flash data address to read from.
* @param	Length is number of bytes to read.
*
* @return
*		- Return actual length including bad blocks.
*
* @note		None.
*
******************************************************************************/
static s32 XNandPsu_CalculateLength(XNandPsu *InstancePtr, u64 Offset,
							u64 Length)
{
	s32 Status;
	u32 BlockSize;
	u32 BlockLen;
	u32 Block;
	u64 TempLen = 0;
	u64 OffsetVar = Offset;

	BlockSize = InstancePtr->Geometry.BlockSize;

	while (TempLen < Length) {
		Block = (u32)(OffsetVar/BlockSize);
		BlockLen = BlockSize - (u32)(OffsetVar % BlockSize);
		if (OffsetVar >= InstancePtr->Geometry.DeviceSize) {
			Status = XST_FAILURE;
			goto Out;
		}
		/* Check if the block is bad */
		Status = XNandPsu_IsBlockBad(InstancePtr, Block);
		if (Status != XST_SUCCESS) {
			/* Good block */
			TempLen += BlockLen;
		}
		OffsetVar += BlockLen;
	}

	Status = XST_SUCCESS;
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function writes to the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Offset is the starting offset of flash to write.
* @param	Length is the number of bytes to write.
* @param	SrcBuf is the source data buffer to write.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_Write(XNandPsu *InstancePtr, u64 Offset, u64 Length, u8 *SrcBuf)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(SrcBuf != NULL);
	Xil_AssertNonvoid(Length != 0U);
	Xil_AssertNonvoid((Offset + Length) <=
				InstancePtr->Geometry.DeviceSize);

	s32 Status = XST_FAILURE;
	u32 Page;
	u32 Col;
	u32 Target;
	u32 Block;
	u32 PartialBytes = 0;
	u32 NumBytes;
	u32 RemLen;
	u8 *BufPtr;
	u8 *SrcBufPtr = (u8 *)SrcBuf;
	u64 OffsetVar = Offset;
	u64 LengthVar = Length;

	/*
	 * Check if write operation exceeds flash size when including
	 * bad blocks.
	 */
	Status = XNandPsu_CalculateLength(InstancePtr, OffsetVar, LengthVar);
	if (Status != XST_SUCCESS) {
		goto Out;
	}

#ifdef __rtems__
	if (InstancePtr->PartialDataPageIndex != XNANDPSU_PAGE_CACHE_UNAVAILABLE) {
		/* All writes invalidate the page cache */
		InstancePtr->PartialDataPageIndex = XNANDPSU_PAGE_CACHE_NONE;
	}
#endif
	while (LengthVar > 0U) {
		Block = (u32) (OffsetVar/InstancePtr->Geometry.BlockSize);
		/*
		 * Skip the bad blocks. Increment the offset by block size.
		 * For better results, always program the flash starting at
		 * a block boundary.
		 */
		if (XNandPsu_IsBlockBad(InstancePtr, Block) == XST_SUCCESS) {
			OffsetVar += (u64)InstancePtr->Geometry.BlockSize;
			continue;
		}
		/* Calculate Page and Column address values */
		Page = (u32) (OffsetVar/InstancePtr->Geometry.BytesPerPage);
		Col = (u32) (OffsetVar &
				(InstancePtr->Geometry.BytesPerPage - 1U));
		PartialBytes = 0U;
		/*
		 * Check if partial write.
		 * If column address is > 0 or Length is < page size
		 */
		if ((Col > 0U) ||
			(LengthVar < InstancePtr->Geometry.BytesPerPage)) {
			RemLen = InstancePtr->Geometry.BytesPerPage - Col;
			PartialBytes = (RemLen < (u32)LengthVar) ?
					RemLen : (u32)LengthVar;
		}

		Target = (u32) (OffsetVar/InstancePtr->Geometry.TargetSize);
#ifdef __rtems__
		{
#else
		if (Page > InstancePtr->Geometry.NumTargetPages) {
#endif
			Page %= InstancePtr->Geometry.NumTargetPages;
		}

		/* Check if partial write */
		if (PartialBytes > 0U) {
			BufPtr = &InstancePtr->PartialDataBuf[0];
			(void)memset(BufPtr, 0xFF,
					InstancePtr->Geometry.BytesPerPage);
			(void)Xil_MemCpy(BufPtr + Col, SrcBufPtr, PartialBytes);

			NumBytes = PartialBytes;
		} else {
			BufPtr = (u8 *)SrcBufPtr;
			NumBytes = (InstancePtr->Geometry.BytesPerPage <
					(u32)LengthVar) ?
					InstancePtr->Geometry.BytesPerPage :
					(u32)LengthVar;
		}
		/* Program page */
		Status = XNandPsu_ProgramPage(InstancePtr, Target, Page, 0U,
								BufPtr);
		if (Status != XST_SUCCESS)
			goto Out;

		Status = XNandPsu_Device_Ready(InstancePtr, Target);
		if (Status != XST_SUCCESS)
			goto Out;

		SrcBufPtr += NumBytes;
		OffsetVar += NumBytes;
		LengthVar -= NumBytes;
	}

Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function reads from the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Offset is the starting offset of flash to read.
* @param	Length is the number of bytes to read.
* @param	DestBuf is the destination data buffer to fill in.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_Read(XNandPsu *InstancePtr, u64 Offset, u64 Length, u8 *DestBuf)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(DestBuf != NULL);
	Xil_AssertNonvoid(Length != 0U);
	Xil_AssertNonvoid((Offset + Length) <=
				InstancePtr->Geometry.DeviceSize);

	s32 Status = XST_FAILURE;
	u32 Page;
	u32 Col;
	u32 Target;
	u32 Block;
	u32 PartialBytes = 0U;
	u32 RemLen;
	u32 NumBytes;
	u8 *BufPtr;
	u8 *DestBufPtr = (u8 *)DestBuf;
	u64 OffsetVar = Offset;
	u64 LengthVar = Length;

	/*
	 * Check if read operation exceeds flash size when including
	 * bad blocks.
	 */
	Status = XNandPsu_CalculateLength(InstancePtr, OffsetVar, LengthVar);
	if (Status != XST_SUCCESS) {
		goto Out;
	}

	while (LengthVar > 0U) {
		Block = (u32)(OffsetVar/InstancePtr->Geometry.BlockSize);
		/*
		 * Skip the bad block. Increment the offset by block size.
		 * The flash programming utility must make sure to start
		 * writing always at a block boundary and skip blocks if any.
		 */
		if (XNandPsu_IsBlockBad(InstancePtr, Block) == XST_SUCCESS) {
			OffsetVar += (u64)InstancePtr->Geometry.BlockSize;
			continue;
		}
		/* Calculate Page and Column address values */
		Page = (u32) (OffsetVar/InstancePtr->Geometry.BytesPerPage);
		Col = (u32) (OffsetVar &
				(InstancePtr->Geometry.BytesPerPage - 1U));
		PartialBytes = 0U;
		/*
		 * Check if partial write.
		 * If column address is > 0 or Length is < page size
		 */
		if ((Col > 0U) ||
			(LengthVar < InstancePtr->Geometry.BytesPerPage)) {
			RemLen = InstancePtr->Geometry.BytesPerPage - Col;
			PartialBytes = ((u32)RemLen < (u32)LengthVar) ?
						(u32)RemLen : (u32)LengthVar;
		}

		Target = (u32) (OffsetVar/InstancePtr->Geometry.TargetSize);
#ifdef __rtems__
		{
#else
		if (Page > InstancePtr->Geometry.NumTargetPages) {
#endif
			Page %= InstancePtr->Geometry.NumTargetPages;
		}
		/* Check if partial read */
		if (PartialBytes > 0U) {
			BufPtr = &InstancePtr->PartialDataBuf[0];
			NumBytes = PartialBytes;
		} else {
			BufPtr = DestBufPtr;
			NumBytes = (InstancePtr->Geometry.BytesPerPage <
					(u32)LengthVar) ?
					InstancePtr->Geometry.BytesPerPage :
					(u32)LengthVar;
		}
#ifdef __rtems__
		if (Page == InstancePtr->PartialDataPageIndex) {
			/*
			 * This is a whole page read for the currently cached
			 * page. It will not be taken care of below, so perform
			 * the copy here.
			 */
			if (PartialBytes == 0U) {
				(void)Xil_MemCpy(DestBufPtr,
						&InstancePtr->PartialDataBuf[0],
						NumBytes);
			}
		} else {
#endif
		/* Read page */
		Status = XNandPsu_ReadPage(InstancePtr, Target, Page, 0U,
								BufPtr);
#ifdef __rtems__
			if (PartialBytes > 0U &&
				InstancePtr->PartialDataPageIndex != XNANDPSU_PAGE_CACHE_UNAVAILABLE) {
				/*
				 * Partial read into page cache. Update the
				 * cached page index.
				 */
				InstancePtr->PartialDataPageIndex = Page;
			}
		}
#endif
		if (Status != XST_SUCCESS) {
			goto Out;
		}
		if (PartialBytes > 0U) {
			(void)Xil_MemCpy(DestBufPtr, BufPtr + Col, NumBytes);
#ifdef __rtems__
			/* The destination buffer is touched by hardware, synchronize */
			if (InstancePtr->Config.IsCacheCoherent == 0) {
				Xil_DCacheFlushRange((INTPTR)(void *)DestBufPtr, NumBytes);
			}
#endif
		}
		DestBufPtr += NumBytes;
		OffsetVar += NumBytes;
		LengthVar -= NumBytes;
	}

	Status = XST_SUCCESS;
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function erases the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Offset is the starting offset of flash to erase.
* @param	Length is the number of bytes to erase.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note
*		The Offset and Length should be aligned to block size boundary
*		to get better results.
*
******************************************************************************/
s32 XNandPsu_Erase(XNandPsu *InstancePtr, u64 Offset, u64 Length)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(Length != 0U);
	Xil_AssertNonvoid((Offset + Length) <=
			InstancePtr->Geometry.DeviceSize);

	s32 Status = XST_FAILURE;
	u32 Target = 0;
	u32 StartBlock;
	u32 NumBlocks = 0;
	u32 Block;
	u32 AlignOff;
	u32 EraseLen;
	u32 BlockRemLen;
	u64 OffsetVar = Offset;
	u64 LengthVar = Length;

	/*
	 * Check if erase operation exceeds flash size when including
	 * bad blocks.
	 */
	Status = XNandPsu_CalculateLength(InstancePtr, OffsetVar, LengthVar);
	if (Status != XST_SUCCESS) {
		goto Out;
	}
	/* Calculate number of blocks to erase */
	StartBlock = (u32) (OffsetVar/InstancePtr->Geometry.BlockSize);

	while (LengthVar > 0U) {
		Block = (u32) (OffsetVar/InstancePtr->Geometry.BlockSize);
		if (XNandPsu_IsBlockBad(InstancePtr, Block) ==
							XST_SUCCESS) {
			OffsetVar += (u64)InstancePtr->Geometry.BlockSize;
			NumBlocks++;
			continue;
		}

		AlignOff = (u32)OffsetVar &
				(InstancePtr->Geometry.BlockSize - (u32)1);
		if (AlignOff > 0U) {
			BlockRemLen = InstancePtr->Geometry.BlockSize -
								AlignOff;
			EraseLen = (BlockRemLen < (u32)LengthVar) ?
						BlockRemLen :(u32)LengthVar;
		} else {
			EraseLen = (InstancePtr->Geometry.BlockSize <
						(u32)LengthVar) ?
					InstancePtr->Geometry.BlockSize:
						(u32)LengthVar;
		}
		NumBlocks++;
		OffsetVar += EraseLen;
		LengthVar -= EraseLen;
	}

	for (Block = StartBlock; Block < (StartBlock + NumBlocks); Block++) {
		Target = Block/InstancePtr->Geometry.NumTargetBlocks;
#ifdef __rtems__
		u32 ModBlock = Block % InstancePtr->Geometry.NumTargetBlocks;
#else
		Block %= InstancePtr->Geometry.NumTargetBlocks;
#endif
		/* Don't erase bad block */
		if (XNandPsu_IsBlockBad(InstancePtr, Block) ==
							XST_SUCCESS)
			continue;
		/* Block Erase */
#ifdef __rtems__
		Status = XNandPsu_EraseBlock(InstancePtr, Target, ModBlock);
#else
		Status = XNandPsu_EraseBlock(InstancePtr, Target, Block);
#endif
		if (Status != XST_SUCCESS)
			goto Out;

		Status = XNandPsu_Device_Ready(InstancePtr, Target);
		if (Status != XST_SUCCESS)
					goto Out;

				}
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Program Page command to flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Page is the page address value to program.
* @param	Col is the column address value to program.
* @param	Buf is the data buffer to program.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_ProgramPage(XNandPsu *InstancePtr, u32 Target, u32 Page,
							u32 Col, u8 *Buf)
{
	u32 PktSize;
	u32 PktCount;
	s32 Status = XST_FAILURE;
	u32 IsrValue;
	u32 AddrCycles = InstancePtr->Geometry.RowAddrCycles +
				InstancePtr->Geometry.ColAddrCycles;

	if (InstancePtr->EccCfg.CodeWordSize > 9U) {
		PktSize = 1024U;
	} else {
		PktSize = 512U;
	}
	PktCount = InstancePtr->Geometry.BytesPerPage/PktSize;

	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_PG_PROG1, ONFI_CMD_PG_PROG2,
					1U, 1U, (u8)AddrCycles);

	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		IsrValue = XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK |
			   XNANDPSU_INTR_STS_EN_DMA_INT_STS_EN_MASK;
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheFlushRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		}
		XNandPsu_Update_DmaAddr(InstancePtr, Buf);
	} else {
		IsrValue = XNANDPSU_INTR_STS_EN_BUFF_WR_RDY_STS_EN_MASK;
	}

		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			   XNANDPSU_INTR_STS_EN_OFFSET, IsrValue);
	/* Program Page Size */
	XNandPsu_SetPageSize(InstancePtr);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, Page, (u16)Col);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set ECC */
	if (InstancePtr->EccMode == XNANDPSU_HWECC) {
		XNandPsu_SetEccSpareCmd(InstancePtr, ONFI_CMD_CHNG_WR_COL,
					InstancePtr->Geometry.ColAddrCycles);
	}
	/* Set Page Program in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_PG_PROG_MASK);


	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 1, 1);

	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Program Page command to flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Page is the page address value to program.
* @param	Buf is the data buffer to program.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_WriteSpareBytes(XNandPsu *InstancePtr, u32 Page, u8 *Buf)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(Page < InstancePtr->Geometry.NumPages);
	Xil_AssertNonvoid(Buf != NULL);

	u32 PktCount = 1U;
	u16 PreEccSpareCol = 0U;
	u16 PreEccSpareWrCnt = 0U;
	u16 PostEccSpareCol = 0U;
	u16 PostEccSpareWrCnt = 0U;
	u32 PostWrite = 0U;
	OnfiCmdFormat Cmd;
	s32 Status = XST_FAILURE;
	u32 RegVal;
	u32 AddrCycles = InstancePtr->Geometry.RowAddrCycles +
				InstancePtr->Geometry.ColAddrCycles;
	u32 Col = InstancePtr->Geometry.BytesPerPage;
	u32 Target = Page/InstancePtr->Geometry.NumTargetPages;
	u32 PktSize = InstancePtr->Geometry.SpareBytesPerPage;
	u32 *BufPtr = (u32 *)(void *)Buf;
	u32 PageVar = Page;

	PageVar %= InstancePtr->Geometry.NumTargetPages;

	if (InstancePtr->EccMode == XNANDPSU_HWECC) {
		/* Calculate ECC free positions before and after ECC code */
		PreEccSpareCol = 0x0U;
		PreEccSpareWrCnt = InstancePtr->EccCfg.EccAddr -
				(u16)InstancePtr->Geometry.BytesPerPage;

		PostEccSpareCol = PreEccSpareWrCnt +
					InstancePtr->EccCfg.EccSize;
		PostEccSpareWrCnt = InstancePtr->Geometry.SpareBytesPerPage -
					PostEccSpareCol;

		PreEccSpareWrCnt = (PreEccSpareWrCnt/4U) * 4U;
		PostEccSpareWrCnt = (PostEccSpareWrCnt/4U) * 4U;

		if (PreEccSpareWrCnt > 0U) {
			PktSize = PreEccSpareWrCnt;
			PktCount = 1U;
			Col = InstancePtr->Geometry.BytesPerPage +
							PreEccSpareCol;
			BufPtr = (u32 *)(void *)Buf;
			if (PostEccSpareWrCnt > 0U) {
				PostWrite = 1U;
			}
		} else if (PostEccSpareWrCnt > 0U) {
			PktSize = PostEccSpareWrCnt;
			PktCount = 1U;
			Col = InstancePtr->Geometry.BytesPerPage +
							PostEccSpareCol;
			BufPtr = (u32 *)(Buf + Col);
		} else {
			/* No free spare bytes available for writing */
			Status = XST_FAILURE;
			goto Out;
		}
	}

	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		RegVal = XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK;
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheFlushRange((INTPTR)(void *)BufPtr, (PktSize * PktCount));
		}
		XNandPsu_Update_DmaAddr(InstancePtr, (u8 *)BufPtr);
	} else {
		RegVal = XNANDPSU_INTR_STS_EN_BUFF_WR_RDY_STS_EN_MASK;
	}

		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			   XNANDPSU_INTR_STS_EN_OFFSET, RegVal);
	/* Program Command hack for change write column */
	if (PostWrite > 0U) {
		Cmd.Command1 = 0x80U;
		Cmd.Command2 = 0x00U;
		XNandPsu_Prepare_Cmd(InstancePtr, Cmd.Command1, Cmd.Command2,
				0U , 1U, (u8)AddrCycles);

	} else {
		XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_PG_PROG1,
				ONFI_CMD_PG_PROG2, 0U , 1U, (u8)AddrCycles);
	}
	/* Program Page Size */
	XNandPsu_SetPageSize(InstancePtr);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, PageVar, (u16)Col);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set Page Program in Program Register */
	if (PostWrite > 0U) {
		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,((u32)XNANDPSU_PROG_PG_PROG_MASK |
				(u32)XNANDPSU_PROG_CHNG_ROW_ADDR_MASK));
	} else {
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_PG_PROG_MASK);
	}

	Status = XNandPsu_Data_ReadWrite(InstancePtr, (u8 *)BufPtr, PktCount,
					 PktSize, 1, 1);

	if (InstancePtr->EccMode == XNANDPSU_HWECC) {
		if (PostWrite > 0U) {
			BufPtr = (u32 *)(Buf + PostEccSpareCol);
			Status = XNandPsu_ChangeWriteColumn(InstancePtr,
					Target,
					PostEccSpareCol, PostEccSpareWrCnt, 1U,
					(u8 *)(void *)BufPtr);
			if (Status != XST_SUCCESS) {
				goto Out;
			}
		}
	}
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Read Page command to flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Page is the page address value to read.
* @param	Col is the column address value to read.
* @param	Buf is the data buffer to fill in.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_ReadPage(XNandPsu *InstancePtr, u32 Target, u32 Page,
							u32 Col, u8 *Buf)
{
	u32 PktSize;
	u32 PktCount;
	s32 Status = XST_FAILURE;
	u32 RegVal;
	u32 AddrCycles = InstancePtr->Geometry.RowAddrCycles +
				InstancePtr->Geometry.ColAddrCycles;

	if (InstancePtr->EccCfg.CodeWordSize > 9U) {
		PktSize = 1024U;
	} else {
		PktSize = 512U;
	}
	PktCount = InstancePtr->Geometry.BytesPerPage/PktSize;

	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_RD1, ONFI_CMD_RD2,
					1U, 1U, (u8)AddrCycles);

	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		RegVal = XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK |
			 XNANDPSU_INTR_STS_EN_DMA_INT_STS_EN_MASK;
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheInvalidateRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		}
		XNandPsu_Update_DmaAddr(InstancePtr, Buf);
	} else {
		RegVal = XNANDPSU_INTR_STS_EN_BUFF_RD_RDY_STS_EN_MASK;
	}
	/* Enable Single bit error and Multi bit error */
	if (InstancePtr->EccMode == XNANDPSU_HWECC)
		RegVal |= XNANDPSU_INTR_STS_EN_MUL_BIT_ERR_STS_EN_MASK |
			 XNANDPSU_INTR_STS_EN_ERR_INTR_STS_EN_MASK;

	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			  XNANDPSU_INTR_STS_EN_OFFSET, RegVal);
	/* Program Page Size */
	XNandPsu_SetPageSize(InstancePtr);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, Page, (u16)Col);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set ECC */
	if (InstancePtr->EccMode == XNANDPSU_HWECC) {
		XNandPsu_SetEccSpareCmd(InstancePtr,
					(ONFI_CMD_CHNG_RD_COL1 |
					(ONFI_CMD_CHNG_RD_COL2 << (u8)8U)),
					InstancePtr->Geometry.ColAddrCycles);
	}

	/* Set Read command in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
				XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_RD_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 0, 1);

#ifdef __rtems__
	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheInvalidateRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		}
	}
#endif

	/* Check ECC Errors */
	if (InstancePtr->EccMode == XNANDPSU_HWECC) {
		/* Hamming Multi Bit Errors */
		if (((u32)XNandPsu_ReadReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_INTR_STS_OFFSET) &
			(u32)XNANDPSU_INTR_STS_MUL_BIT_ERR_STS_EN_MASK) != 0U) {

			XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
				XNANDPSU_INTR_STS_OFFSET,
				XNANDPSU_INTR_STS_MUL_BIT_ERR_STS_EN_MASK);

#ifdef XNANDPSU_DEBUG
			xil_printf("%s: ECC Hamming multi bit error\r\n",
							__func__);
#endif
			InstancePtr->Ecc_Stat_PerPage_flips =
					((XNandPsu_ReadReg(
					InstancePtr->Config.BaseAddress,
					XNANDPSU_ECC_ERR_CNT_OFFSET) &
					0x1FF00U) >> 8U);
			InstancePtr->Ecc_Stats_total_flips +=
					InstancePtr->Ecc_Stat_PerPage_flips;
			Status = XST_FAILURE;
		}
		/* Hamming Single Bit or BCH Errors */
		if (((u32)XNandPsu_ReadReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_INTR_STS_OFFSET) &
			(u32)XNANDPSU_INTR_STS_ERR_INTR_STS_EN_MASK) != 0U) {

			XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
					XNANDPSU_INTR_STS_OFFSET,
					XNANDPSU_INTR_STS_ERR_INTR_STS_EN_MASK);

			if (InstancePtr->EccCfg.IsBCH == 1U) {
				InstancePtr->Ecc_Stat_PerPage_flips =
						((XNandPsu_ReadReg(
						InstancePtr->Config.BaseAddress,
						XNANDPSU_ECC_ERR_CNT_OFFSET)&
						0x1FF00U) >> 8U);
				InstancePtr->Ecc_Stats_total_flips +=
					InstancePtr->Ecc_Stat_PerPage_flips;
				Status = XST_SUCCESS;
			}
		}
	}

	return Status;
}

/*****************************************************************************/
/**
*
* This function reads spare bytes from flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Page is the page address value to read.
* @param	Buf is the data buffer to fill in.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_ReadSpareBytes(XNandPsu *InstancePtr, u32 Page, u8 *Buf)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(Page < InstancePtr->Geometry.NumPages);
	Xil_AssertNonvoid(Buf != NULL);

	u32 PktCount = 1U;
	s32 Status = XST_FAILURE;
	u32 RegVal;
	u32 AddrCycles = InstancePtr->Geometry.RowAddrCycles +
				InstancePtr->Geometry.ColAddrCycles;
	u32 Col = InstancePtr->Geometry.BytesPerPage;
	u32 Target = Page/InstancePtr->Geometry.NumTargetPages;
	u32 PktSize = InstancePtr->Geometry.SpareBytesPerPage;
	u32 PageVar = Page;

	PageVar %= InstancePtr->Geometry.NumTargetPages;

	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		RegVal = XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK;
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheInvalidateRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		}
		XNandPsu_Update_DmaAddr(InstancePtr, Buf);
	} else {
		RegVal = XNANDPSU_INTR_STS_EN_BUFF_RD_RDY_STS_EN_MASK;
	}
		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			   XNANDPSU_INTR_STS_EN_OFFSET, RegVal);
	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_RD1, ONFI_CMD_RD2, 0U,
						1U, (u8)AddrCycles);
	/* Program Page Size */
	XNandPsu_SetPageSize(InstancePtr);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, PageVar, (u16)Col);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set Read command in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
				XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_RD_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 0, 1);

#ifdef __rtems__
	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheInvalidateRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		}
	}
#endif

	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI block erase command to the flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Block is the block to erase.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_EraseBlock(XNandPsu *InstancePtr, u32 Target, u32 Block)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertNonvoid(Target < XNANDPSU_MAX_TARGETS);
#ifdef __rtems__
	Xil_AssertNonvoid(Block < InstancePtr->Geometry.NumTargetBlocks);
#else
	Xil_AssertNonvoid(Block < InstancePtr->Geometry.NumBlocks);
#endif

	s32 Status = XST_FAILURE;
	u32 Page;
	u32 ErasePage;
	u32 EraseCol;
	u32 AddrCycles = InstancePtr->Geometry.RowAddrCycles;

	Page = Block * InstancePtr->Geometry.PagesPerBlock;
	ErasePage = (Page >> 16U) & 0xFFFFU;
	EraseCol = Page & 0xFFFFU;

	/*
	 * Enable Transfer Complete Interrupt in Interrupt Status Enable
	 * Register
	 */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_EN_OFFSET,
			XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK);

	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_BLK_ERASE1,
			ONFI_CMD_BLK_ERASE2, 0U , 0U, (u8)AddrCycles);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, ErasePage, (u16)EraseCol);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set Block Erase in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_BLK_ERASE_MASK);
	/* Poll for Transfer Complete event */
	Status = XNandPsu_WaitFor_Transfer_Complete(InstancePtr);
	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Get Feature command to flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Feature is the feature selector.
* @param	Buf is the buffer to fill feature value.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_GetFeature(XNandPsu *InstancePtr, u32 Target, u8 Feature,
								u8 *Buf)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY)
	Xil_AssertNonvoid(Buf != NULL);
	Xil_AssertNonvoid(Target < XNANDPSU_MAX_TARGETS);

	s32 Status;
	u32 PktSize = 4;
	u32 PktCount = 1;

	if (InstancePtr->DataInterface == XNANDPSU_NVDDR) {
		PktSize = 8U;
	}

	/*
	 * Enable Buffer Read Ready Interrupt in Interrupt Status
	 * Enable Register
	 */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_EN_OFFSET,
			XNANDPSU_INTR_STS_EN_BUFF_RD_RDY_STS_EN_MASK);
	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_GET_FEATURES,
				ONFI_CMD_INVALID, 0U, 0U, 1U);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, 0x0U, Feature);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Set Read Parameter Page in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_GET_FEATURES_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 0, 0);

	return Status;
}

/*****************************************************************************/
/**
*
* This function sends ONFI Set Feature command to flash.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Feature is the feature selector.
* @param	Buf is the feature value to send.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_SetFeature(XNandPsu *InstancePtr, u32 Target, u8 Feature,
								u8 *Buf)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY)
	Xil_AssertNonvoid(Buf != NULL);
	Xil_AssertNonvoid(Target < XNANDPSU_MAX_TARGETS);

	s32 Status;
	u32 PktSize = 4U;
	u32 PktCount = 1U;

	if (InstancePtr->DataInterface == XNANDPSU_NVDDR) {
		PktSize = 8U;
	}

	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_EN_OFFSET, 0U);

	/*
	 * Enable Buffer Write Ready Interrupt in Interrupt Status
	 * Enable Register
	 */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_EN_OFFSET,
			XNANDPSU_INTR_STS_EN_BUFF_WR_RDY_STS_EN_MASK);

	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_SET_FEATURES,
				ONFI_CMD_INVALID, 0U , 0U, 1U);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, 0x0U, Feature);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Set Read Parameter Page in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_SET_FEATURES_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 1, 0);
	return Status;
}

/*****************************************************************************/
/**
*
* This function changes clock frequency of flash controller.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	ClockFreq is the clock frequency to change.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_ChangeClockFreq(XNandPsu *InstancePtr, u32 ClockFreq)
{
	(void) InstancePtr;
	(void) ClockFreq;

	/* Not implemented */
}
/*****************************************************************************/
/**
*
* This function changes the data interface and timing mode.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	NewIntf is the new data interface.
* @param	NewMode is the new timing mode.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
s32 XNandPsu_ChangeTimingMode(XNandPsu *InstancePtr,
				XNandPsu_DataInterface NewIntf,
				XNandPsu_TimingMode NewMode)
{
	/* Assert the input arguments. */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	s32 Status = XST_SUCCESS;
	u32 Target;
	u32 RegVal;
	u8 Buf[4] = {0U};
	u32 *Feature = (u32 *)(void *)&Buf[0];
	u32 SetFeature = 0U;
	u32 NewModeVar = (u32)NewMode;

	/* Check for valid input arguments */
	if(((NewIntf != XNANDPSU_SDR) && (NewIntf != XNANDPSU_NVDDR)) ||
			(NewModeVar > 5U)){
		Status = XST_FAILURE;
		goto Out;
	}

	if(NewIntf == XNANDPSU_NVDDR){
		NewModeVar = NewModeVar | (u32)0x10;
	}
	/* Get current data interface type and timing mode */
	XNandPsu_DataInterface CurIntf = InstancePtr->DataInterface;
	XNandPsu_TimingMode CurMode = InstancePtr->TimingMode;

	/* Check if the flash is in same mode */
	if ((CurIntf == NewIntf) && (CurMode == NewModeVar)) {
		Status = XST_SUCCESS;
		goto Out;
	}

	if ((CurIntf == XNANDPSU_NVDDR) && (NewIntf == XNANDPSU_SDR)) {

		NewModeVar = XNANDPSU_SDR0;

		/* Change the clock frequency */
		XNandPsu_ChangeClockFreq(InstancePtr, XNANDPSU_SDR_CLK);

		/* Update Data Interface Register */
		RegVal = ((NewModeVar % 6U) << ((NewIntf == XNANDPSU_NVDDR) ? 3U : 0U)) |
				((u32)NewIntf << XNANDPSU_DATA_INTF_DATA_INTF_SHIFT);
		XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
					XNANDPSU_DATA_INTF_OFFSET, RegVal);

		for (Target = 0U; Target < InstancePtr->Geometry.NumTargets;
							Target++) {
			Status = XNandPsu_OnfiReset(InstancePtr, Target);
			if (Status != XST_SUCCESS) {
				goto Out;
			}
		}

		/* Set Feature */
		for (Target = 0U; Target < InstancePtr->Geometry.NumTargets;
								Target++) {
			Status = XNandPsu_SetFeature(InstancePtr, Target, 0x01U,
							(u8 *)(void *)&NewModeVar);
			if (Status != XST_SUCCESS) {
				goto Out;
			}
		}

		InstancePtr->DataInterface = NewIntf;
		InstancePtr->TimingMode = NewModeVar;

		for (Target = 0U; Target < InstancePtr->Geometry.NumTargets;
								Target++) {
			Status = XNandPsu_GetFeature(InstancePtr, Target, 0x01U,
								&Buf[0]);
			if (Status != XST_SUCCESS) {
				goto Out;
			}
			/* Check if set_feature was successful */
			if (*Feature != NewModeVar) {
				Status = XST_FAILURE;
				goto Out;
			}
		}

		goto Out;
	}

	SetFeature = NewModeVar;
	if((CurIntf == XNANDPSU_NVDDR) && (NewIntf == XNANDPSU_NVDDR)){
		SetFeature |= SetFeature << 8U;
	}
	/* Set Feature */
	for (Target = 0U; Target < InstancePtr->Geometry.NumTargets;
							Target++) {
		Status = XNandPsu_SetFeature(InstancePtr, Target, 0x01U,
						(u8 *)(void *)&SetFeature);
		if (Status != XST_SUCCESS) {
			goto Out;
		}
	}

	InstancePtr->DataInterface = NewIntf;
	InstancePtr->TimingMode = NewModeVar;
	/* Update Data Interface Register */
	RegVal = ((NewMode % 6U) << ((NewIntf == XNANDPSU_NVDDR) ? 3U : 0U)) |
			((u32)NewIntf << XNANDPSU_DATA_INTF_DATA_INTF_SHIFT);
	XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_DATA_INTF_OFFSET, RegVal);

	/* Get Feature */
	for (Target = 0U; Target < InstancePtr->Geometry.NumTargets;
							Target++) {
		Status = XNandPsu_GetFeature(InstancePtr, Target, 0x01U,
							&Buf[0]);
		if (Status != XST_SUCCESS) {
			goto Out;
		}

		/* Check if set_feature was successful */
		if (*Feature != NewModeVar) {
			Status = XST_FAILURE;
			goto Out;
		}
	}

Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function issues change read column and reads the data into buffer
* specified by user.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Col is the coulmn address.
* @param	PktSize is the number of bytes to read.
* @param	PktCount is the number of transactions to read.
* @param	Buf is the data buffer to fill in.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_ChangeReadColumn(XNandPsu *InstancePtr, u32 Target,
					u32 Col, u32 PktSize, u32 PktCount,
					u8 *Buf)
{
	s32 Status = XST_FAILURE;
	u32 RegVal;
	u32 AddrCycles = InstancePtr->Geometry.ColAddrCycles;

	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		RegVal = XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK |
			 XNANDPSU_INTR_STS_EN_DMA_INT_STS_EN_MASK;
		Xil_DCacheInvalidateRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		XNandPsu_Update_DmaAddr(InstancePtr, Buf);
	} else {
		RegVal = XNANDPSU_INTR_STS_EN_BUFF_RD_RDY_STS_EN_MASK;
	}

		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			   XNANDPSU_INTR_STS_EN_OFFSET, RegVal);
	/* Program Command */
	XNandPsu_Prepare_Cmd(InstancePtr, ONFI_CMD_CHNG_RD_COL1,
			ONFI_CMD_CHNG_RD_COL2, 0U , 1U, (u8)AddrCycles);
	/* Program Page Size */
	XNandPsu_SetPageSize(InstancePtr);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, 0U, (u16)Col);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set Read command in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_RD_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 0, 1);

	return Status;
}

/*****************************************************************************/
/**
*
* This function issues change read column and reads the data into buffer
* specified by user.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chip select value.
* @param	Col is the coulmn address.
* @param	PktSize is the number of bytes to read.
* @param	PktCount is the number of transactions to read.
* @param	Buf is the data buffer to fill in.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_ChangeWriteColumn(XNandPsu *InstancePtr, u32 Target,
					u32 Col, u32 PktSize, u32 PktCount,
					u8 *Buf)
{
	s32 Status = XST_FAILURE;
	OnfiCmdFormat OnfiCommand;
	u32 RegVal;
	u32 AddrCycles = InstancePtr->Geometry.ColAddrCycles;

	if (PktCount == 0U) {
		return XST_SUCCESS;
	}

	if (InstancePtr->DmaMode == XNANDPSU_MDMA) {
		RegVal = XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK |
			 XNANDPSU_INTR_STS_EN_DMA_INT_STS_EN_MASK;
#ifdef __rtems__
		if (InstancePtr->Config.IsCacheCoherent == 0) {
			Xil_DCacheFlushRange((INTPTR)(void *)Buf, (PktSize * PktCount));
		}
#endif
		XNandPsu_Update_DmaAddr(InstancePtr, Buf);
	} else {
		RegVal = XNANDPSU_INTR_STS_EN_BUFF_WR_RDY_STS_EN_MASK;
	}

		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			   XNANDPSU_INTR_STS_EN_OFFSET, RegVal);
	/* Change write column hack */
	OnfiCommand.Command1 = 0x85U;
	OnfiCommand.Command2 = 0x10U;
	XNandPsu_Prepare_Cmd(InstancePtr, OnfiCommand.Command1,
				OnfiCommand.Command2, 0U , 0U, (u8)AddrCycles);

	/* Program Page Size */
	XNandPsu_SetPageSize(InstancePtr);
	/* Program Column, Page, Block address */
	XNandPsu_SetPageColAddr(InstancePtr, 0U, (u16)Col);
	/* Program Packet Size and Packet Count */
	XNandPsu_SetPktSzCnt(InstancePtr, PktSize, PktCount);
	/* Program Memory Address Register2 for chip select */
	XNandPsu_SelectChip(InstancePtr, Target);
	/* Set Page Program in Program Register */
	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_PROG_OFFSET,XNANDPSU_PROG_CHNG_ROW_ADDR_END_MASK);

	Status = XNandPsu_Data_ReadWrite(InstancePtr, Buf, PktCount, PktSize, 1, 0);
	return Status;
}

/*****************************************************************************/
/**
*
* This function initializes extended parameter page ECC information.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	ExtPrm is the Extended parameter page buffer.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_InitExtEcc(XNandPsu *InstancePtr, OnfiExtPrmPage *ExtPrm)
{
	s32 Status = XST_FAILURE;
	u32 Offset = 0U;
	u32 Found = 0U;
	OnfiExtEccBlock *EccBlock;

	if (ExtPrm->Section0Type != 0x2U) {
		Offset += (u32)ExtPrm->Section0Len;
		if (ExtPrm->Section1Type != 0x2U) {
#ifdef XNANDPSU_DEBUG
		xil_printf("%s: Extended ECC section not found\r\n",__func__);
#endif
			Status = XST_FAILURE;
		} else {
			Found = 1U;
		}
	} else {
		Found = 1U;
	}

	if (Found != 0U) {
		EccBlock = (OnfiExtEccBlock *)&ExtPrm->SectionData[Offset];
		Xil_AssertNonvoid(EccBlock != NULL);
		if (EccBlock->CodeWordSize == 0U) {
			Status = XST_FAILURE;
		} else {
			InstancePtr->Geometry.NumBitsECC =
						EccBlock->NumEccBits;
			InstancePtr->Geometry.EccCodeWordSize =
						(u32)EccBlock->CodeWordSize;
			Status = XST_SUCCESS;
		}
	}
	return Status;
}

/*****************************************************************************/
/**
*
* This function prepares command to be written into command register.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Cmd1 is the first Onfi Command.
* @param	Cmd2 is the second Onfi Command.
* @param	EccState is the flag to set Ecc State.
* @param	DmaMode is the flag to set DMA mode.
* @param	AddrCycles is the number of Address Cycles.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
void XNandPsu_Prepare_Cmd(XNandPsu *InstancePtr, u8 Cmd1, u8 Cmd2, u8 EccState,
			u8 DmaMode, u8 AddrCycles)
{
	Xil_AssertVoid(InstancePtr != NULL);

	u32 RegValue = 0U;

	RegValue = (u32)Cmd1 | (((u32)Cmd2 << (u32)XNANDPSU_CMD_CMD2_SHIFT) &
			(u32)XNANDPSU_CMD_CMD2_MASK);

	if ((EccState != 0U) && (InstancePtr->EccMode == XNANDPSU_HWECC)) {
		RegValue |= 1U << XNANDPSU_CMD_ECC_ON_SHIFT;
	}

	if ((DmaMode != 0U) && (InstancePtr->DmaMode == XNANDPSU_MDMA)) {
		RegValue |= XNANDPSU_MDMA << XNANDPSU_CMD_DMA_EN_SHIFT;
	}

	if (AddrCycles != 0U) {
		RegValue |= (u32)AddrCycles <<
				(u32)XNANDPSU_CMD_ADDR_CYCLES_SHIFT;
	}

	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_CMD_OFFSET, RegValue);
}

/*****************************************************************************/
/**
*
* This function Read/Writes data from the nand controller.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Buf is the data buffer.
* @param	PktCount is the number packet chunks.
* @param	PktSize is the size of the packet.
* @param	Operation is 1 for write and 0 for read.
* @param	DmaMode is 1 for Dma and 0 for PIO.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_Data_ReadWrite(XNandPsu *InstancePtr, u8* Buf, u32 PktCount,
				u32 PktSize, u32 Operation, u8 DmaMode)
{
	u32 BufRwCnt = 0U;
	s32 Status = XST_FAILURE;
	u32 Event = XNANDPSU_INTR_STS_BUFF_RD_RDY_STS_EN_MASK;

	if ((DmaMode != 0U) && (InstancePtr->DmaMode == XNANDPSU_MDMA))
		goto DmaDone;

	if (Operation)
		Event = XNANDPSU_INTR_STS_BUFF_WR_RDY_STS_EN_MASK;

	while (BufRwCnt < PktCount) {
		/* Poll for Buffer Write Ready event */
		Status = XNandPsu_PollRegTimeout(InstancePtr,
				XNANDPSU_INTR_STS_OFFSET, Event,
				XNANDPSU_INTR_POLL_TIMEOUT);
		if (Status != XST_SUCCESS) {
			xil_printf("%s: Poll for buf write ready timeout\r\n",
				    __func__);
			goto Out;
		}

		/* Increment Buffer Write Interrupt Count */
		BufRwCnt++;

		if (BufRwCnt == PktCount)
			XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
				XNANDPSU_INTR_STS_EN_OFFSET,
				XNANDPSU_INTR_STS_EN_TRANS_COMP_STS_EN_MASK);

		else
			XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
				XNANDPSU_INTR_STS_EN_OFFSET, 0U);
		/*
	         * Clear Buffer Write Ready Interrupt in Interrupt Status
		 * Register
		 */
		XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_OFFSET, Event);
		/* Write Packet Data to Data Port Register */
		if (Operation)
			XNandPsu_Fifo_Write(InstancePtr, Buf, PktSize);
		else
			XNandPsu_Fifo_Read(InstancePtr, Buf, PktSize);

		Buf += PktSize;

		if (BufRwCnt < PktCount)
			XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
				XNANDPSU_INTR_STS_EN_OFFSET, Event);
		else
			break;
	}

DmaDone:
	Status = XNandPsu_WaitFor_Transfer_Complete(InstancePtr);
Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function writes data to the fifo.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Buf is the buffer pointer.
* @param	Size of the Buffer.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_Fifo_Write(XNandPsu *InstancePtr, u8* Buffer, u32 Size)
{
	u32 *BufPtr = (u32 *)(void *)Buffer;
	u32 Index;

	for (Index = 0U; Index < Size/4U; Index++)
		XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_BUF_DATA_PORT_OFFSET,
				BufPtr[Index]);
}

/*****************************************************************************/
/**
*
* This function reads data from the fifo.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Buf is the buffer pointer.
* @param	Size of the Buffer.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_Fifo_Read(XNandPsu *InstancePtr, u8* Buf, u32 Size)
{
	u32 *BufPtr = (u32 *)(void *)Buf;
	u32 Index;

	for (Index = 0U; Index < Size/4U; Index++)
		BufPtr[Index] = XNandPsu_ReadReg(InstancePtr->Config.BaseAddress,
					XNANDPSU_BUF_DATA_PORT_OFFSET);
}

/*****************************************************************************/
/**
*
* This function configures the given dma address to the controller.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Buf is the buffer pointer.
*
* @return
*		None
*
* @note		None
*
******************************************************************************/
static void XNandPsu_Update_DmaAddr(XNandPsu *InstancePtr, u8* Buf)
{
#if defined(__aarch64__) || defined(__arch64__)
		XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_DMA_SYS_ADDR1_OFFSET,
				(u32) (((INTPTR)Buf >> 32U) & 0xFFFFFFFFU));
#endif
		XNandPsu_WriteReg(InstancePtr->Config.BaseAddress,
				XNANDPSU_DMA_SYS_ADDR0_OFFSET,
				(u32) ((INTPTR)(void *)Buf & 0xFFFFFFFFU));

}

/*****************************************************************************/
/**
*
* This function waits for the device ready stataus.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
* @param	Target is the chipselect value.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		None
*
******************************************************************************/
static s32 XNandPsu_Device_Ready(XNandPsu *InstancePtr, u32 Target)
{
	s32 Status = XST_SUCCESS;
	u16 OnfiStatus = 0U;

	do {
		Status = XNandPsu_OnfiReadStatus(InstancePtr, Target,
							&OnfiStatus);
		if (Status != XST_SUCCESS)
			goto Out;
		if ((OnfiStatus & (1U << 6U)) != 0U) {
			if ((OnfiStatus & (1U << 0U)) != 0U) {
				Status = XST_FAILURE;
				goto Out;
			}
		}
	} while (((OnfiStatus >> 6U) & 0x1U) == 0U);

Out:
	return Status;
}

/*****************************************************************************/
/**
*
* This function waits for the  transfer complete event.
*
* @param	InstancePtr is a pointer to the XNandPsu instance.
*
* @return
*		- XST_SUCCESS if successful.
*		- XST_FAILURE if failed.
*
* @note		Expects that transfer complete event was set before calling
* 		this function.
*
******************************************************************************/
static s32 XNandPsu_WaitFor_Transfer_Complete(XNandPsu *InstancePtr)
{
s32 Status = XST_FAILURE;

	 /* Poll for Transfer Complete event */
	Status = XNandPsu_PollRegTimeout(
			InstancePtr,
			XNANDPSU_INTR_STS_OFFSET,
			XNANDPSU_INTR_STS_TRANS_COMP_STS_EN_MASK,
			XNANDPSU_INTR_POLL_TIMEOUT);
	if (Status != XST_SUCCESS) {
		xil_printf("%s: Poll for xfer complete timeout\r\n", __func__);
		goto Out;
	}

	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
		XNANDPSU_INTR_STS_EN_OFFSET, 0U);

	XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress,
			XNANDPSU_INTR_STS_OFFSET,
			XNANDPSU_INTR_STS_TRANS_COMP_STS_EN_MASK);
#if defined  (XCLOCKING)
	Xil_ClockDisable(InstancePtr->Config.RefClk);
#endif
Out:
	return Status;
}
/** @} */