summaryrefslogblamecommitdiffstats
path: root/bsps/shared/grlib/l2c/l2c.c
blob: 4a443ed7cff4d87599448873129d6349851e8fe0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
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











                                                           
                   
                          

                             

                        
                        
                
                      





























































































































































































































                                                                                     

                                                    









                                                          

                              































































































































































































































                                                                               
                                                                           



































































































































































































                                                                                       



                                                                  





















































































































































































































                                                                                          
                 

                                                                  


































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                           
/*
 *  GRLIB L2CACHE Driver
 *
 *  COPYRIGHT (c) 2017
 *  Cobham Gaisler AB
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <stdlib.h>
#include <string.h>
#include <drvmgr/drvmgr.h>
#include <grlib/ambapp_bus.h>
#include <grlib/ambapp.h>
#include <rtems.h>
#include <rtems/bspIo.h>
#include <grlib/grlib.h>
#include <bsp.h>
#include <grlib/l2c.h>

/*#define STATIC*/
#define STATIC static

/*#define INLINE*/
#define INLINE inline

#define UNUSED __attribute__((unused))

/*#define DEBUG 1*/

#ifdef DEBUG
#define DBG(x...) printf(x)
#else
#define DBG(x...) 
#endif

/*
 * L2CACHE CTRL register fields 
 */
#define L2C_CTRL_EN (0x1 << L2C_CTRL_EN_BIT)
#define L2C_CTRL_EDAC (0x1 << L2C_CTRL_EDAC_BIT)
#define L2C_CTRL_REPL (0x3 << L2C_CTRL_REPL_BIT)
#define L2C_CTRL_IWAY (0xf << L2C_CTRL_IWAY_BIT)
#define L2C_CTRL_LOCK (0xf << L2C_CTRL_LOCK_BIT)
#define L2C_CTRL_HPRHB (0x1 << L2C_CTRL_HPRHB_BIT)
#define L2C_CTRL_HPB (0x1 << L2C_CTRL_HPB_BIT)
#define L2C_CTRL_UC (0x1 << L2C_CTRL_UC_BIT)
#define L2C_CTRL_HC (0x1 << L2C_CTRL_HC_BIT)
#define L2C_CTRL_WP (0x1 << L2C_CTRL_WP_BIT)
#define L2C_CTRL_HP (0x1 << L2C_CTRL_HP_BIT)

#define L2C_CTRL_EN_BIT 31
#define L2C_CTRL_EDAC_BIT 30
#define L2C_CTRL_REPL_BIT 28
#define L2C_CTRL_IWAY_BIT 12
#define L2C_CTRL_LOCK_BIT 8
#define L2C_CTRL_HPRHB_BIT 5
#define L2C_CTRL_HPB_BIT 4
#define L2C_CTRL_UC_BIT 3
#define L2C_CTRL_HC_BIT 2
#define L2C_CTRL_WP_BIT 1
#define L2C_CTRL_HP_BIT 0

/*
 * L2CACHE STATUS register fields 
 */
#define L2C_STAT_LS (0x1 << L2C_STAT_LS_BIT)
#define L2C_STAT_AT (0x1 << L2C_STAT_AT_BIT)
#define L2C_STAT_MP (0x1 << L2C_STAT_MP_BIT)
#define L2C_STAT_MTRR (0x3f << L2C_STAT_MTRR_BIT)
#define L2C_STAT_BBUSW (0x7 << L2C_STAT_BBUSW_BIT)
#define L2C_STAT_WAYSIZE (0x7ff << L2C_STAT_WAYSIZE_BIT)
#define L2C_STAT_WAY (0x3 << L2C_STAT_WAY_BIT)

#define L2C_STAT_LS_BIT 24
#define L2C_STAT_AT_BIT 23
#define L2C_STAT_MP_BIT 22
#define L2C_STAT_MTRR_BIT 16
#define L2C_STAT_BBUSW_BIT 13
#define L2C_STAT_WAYSIZE_BIT 2
#define L2C_STAT_WAY_BIT 0

/*
 * L2CACHE MTRR register fields 
 */
#define L2C_MTRR_ADDR (0x3fff << L2C_MTRR_ADDR_BIT)
#define L2C_MTRR_ACC (0x3 << L2C_MTRR_ACC_BIT)
#define L2C_MTRR_MASK (0x3fff << L2C_MTRR_MASK_BIT)
#define L2C_MTRR_WP (0x1 << L2C_MTRR_WP_BIT)
#define L2C_MTRR_AC (0x1 << L2C_MTRR_AC_BIT)

#define L2C_MTRR_ADDR_BIT 18
#define L2C_MTRR_ACC_BIT 16
#define L2C_MTRR_MASK_BIT 2
#define L2C_MTRR_WP_BIT 1
#define L2C_MTRR_AC_BIT 0

#define L2C_MTRR_UNCACHED 0
#define L2C_MTRR_WRITETHROUGH (0x1 << L2C_MTRR_ACC_BIT)
#define L2C_MTRR_WRITEPROT_ENABLE L2C_MTRR_WP
#define L2C_MTRR_WRITEPROT_DISABLE 0
#define L2C_MTRR_ACCESSCONTROL_ENABLE L2C_MTRR_AC
#define L2C_MTRR_ACCESSCONTROL_DISABLE 0

#define REG_WRITE(addr, val) (*(volatile unsigned int *)(addr) = (unsigned int)(val))
#define REG_READ(addr) (*(volatile unsigned int *)(addr))

/*
 * L2CACHE FLUSHMEM register fields 
 */
#define L2C_FLUSH_ADDR (0x7ffffff << L2C_FLUSH_ADDR_BIT)
#define L2C_FLUSH_DI (0x1 << L2C_FLUSH_DI_BIT)
#define L2C_FLUSH_FMODE (0x7 << L2C_FLUSH_FMODE_BIT)

#define L2C_FLUSH_ADDR_BIT 5
#define L2C_FLUSH_DI_BIT 3
#define L2C_FLUSH_FMODE_BIT 0

#define L2C_FLUSH_FMODE_INV_ONE (0x1 << L2C_FLUSH_FMODE_BIT)
#define L2C_FLUSH_FMODE_WB_ONE (0x2 << L2C_FLUSH_FMODE_BIT)
#define L2C_FLUSH_FMODE_INV_WB_ONE (0x3 << L2C_FLUSH_FMODE_BIT)
#define L2C_FLUSH_FMODE_INV_ALL (0x5 << L2C_FLUSH_FMODE_BIT)
#define L2C_FLUSH_FMODE_WB_ALL (0x6 << L2C_FLUSH_FMODE_BIT)
#define L2C_FLUSH_FMODE_INV_WB_ALL (0x7 << L2C_FLUSH_FMODE_BIT)

/*
 * L2CACHE FLUSSETINDEX register fields 
 */
#define L2C_FLUSHSI_INDEX (0xffff << L2C_FLUSHSI_INDEX_BIT)
#define L2C_FLUSHSI_TAG (0x3fffff << L2C_FLUSHSI_TAG_BIT)
#define L2C_FLUSHSI_FL (0x1 << L2C_FLUSHSI_FL_BIT)
#define L2C_FLUSHSI_VB (0x1 << L2C_FLUSHSI_VB_BIT)
#define L2C_FLUSHSI_DB (0x1 << L2C_FLUSHSI_DB_BIT)
#define L2C_FLUSHSI_WAY (0x3 << L2C_FLUSHSI_WAY_BIT)
#define L2C_FLUSHSI_DI (0x1 << L2C_FLUSHSI_DI_BIT)
#define L2C_FLUSHSI_WF (0x1 << L2C_FLUSHSI_WF_BIT)
#define L2C_FLUSHSI_FMODE (0x3 << L2C_FLUSHSI_FMODE_BIT)

#define L2C_FLUSHSI_INDEX_BIT 16
#define L2C_FLUSHSI_TAG_BIT 10
#define L2C_FLUSHSI_FL_BIT 9
#define L2C_FLUSHSI_VB_BIT 8
#define L2C_FLUSHSI_DB_BIT 7
#define L2C_FLUSHSI_WAY_BIT 4
#define L2C_FLUSHSI_DI_BIT 3
#define L2C_FLUSHSI_WF_BIT 2
#define L2C_FLUSHSI_FMODE_BIT 0

#define L2C_FLUSHSI_FMODE_SET_INV_ONE (0x1 << L2C_FLUSHSI_FMODE_BIT)
#define L2C_FLUSHSI_FMODE_SET_WB_ONE (0x2 << L2C_FLUSHSI_FMODE_BIT)
#define L2C_FLUSHSI_FMODE_SET_INV_WB_ONE (0x3 << L2C_FLUSHSI_FMODE_BIT)
#define L2C_FLUSHSI_FMODE_WAY_UPDATE (0x1 << L2C_FLUSHSI_FMODE_BIT)
#define L2C_FLUSHSI_FMODE_WAY_WB (0x2 << L2C_FLUSHSI_FMODE_BIT)
#define L2C_FLUSHSI_FMODE_WAY_UPDATE_WB_ALL (0x3 << L2C_FLUSHSI_FMODE_BIT)

/*
 * L2CACHE ERROR register fields 
 */
#define L2C_ERROR_AHBM (0xf << L2C_ERROR_AHBM_BIT)
#define L2C_ERROR_SCRUB (0x1 << L2C_ERROR_SCRUB_BIT)
#define L2C_ERROR_TYPE (0x7 << L2C_ERROR_TYPE_BIT)
#define L2C_ERROR_TAG (0x1 << L2C_ERROR_TAG_BIT)
#define L2C_ERROR_COR (0x1 << L2C_ERROR_COR_BIT)
#define L2C_ERROR_MULTI (0x1 << L2C_ERROR_MULTI_BIT)
#define L2C_ERROR_VALID (0x1 << L2C_ERROR_VALID_BIT)
#define L2C_ERROR_DISERESP (0x1 << L2C_ERROR_DISERESP_BIT)
#define L2C_ERROR_CEC (0x7 << L2C_ERROR_CEC_BIT)
#define L2C_ERROR_IRQP (0xf << L2C_ERROR_IRQP_BIT)
#define L2C_ERROR_IRQM (0xf << L2C_ERROR_IRQM_BIT)
#define L2C_ERROR_IRQM_BCKEND (0x1 << L2C_ERROR_IRQM_BCKEND_BIT)
#define L2C_ERROR_IRQM_WPROT (0x1 << L2C_ERROR_IRQM_WPROT_BIT)
#define L2C_ERROR_IRQM_UNCORR (0x1 << L2C_ERROR_IRQM_UNCORR_BIT)
#define L2C_ERROR_IRQM_CORR (0x1 << L2C_ERROR_IRQM_CORR_BIT)
#define L2C_ERROR_SCB (0x3 << L2C_ERROR_SCB_BIT)
#define L2C_ERROR_STCB (0x3 << L2C_ERROR_STCB_BIT)
#define L2C_ERROR_XCB (0x1 << L2C_ERROR_XCB_BIT)
#define L2C_ERROR_RCB (0x1 << L2C_ERROR_RCB_BIT)
#define L2C_ERROR_COMP (0x1 << L2C_ERROR_COMP_BIT)
#define L2C_ERROR_RST (0x1 << L2C_ERROR_RST_BIT)

#define L2C_ERROR_AHBM_BIT 28
#define L2C_ERROR_SCRUB_BIT 27
#define L2C_ERROR_TYPE_BIT 24
#define L2C_ERROR_TAG_BIT 23
#define L2C_ERROR_COR_BIT 22
#define L2C_ERROR_MULTI_BIT 21
#define L2C_ERROR_VALID_BIT 20
#define L2C_ERROR_DISERESP_BIT 19
#define L2C_ERROR_CEC_BIT 16
#define L2C_ERROR_IRQP_BIT 12
#define L2C_ERROR_IRQM_BCKEND_BIT 11
#define L2C_ERROR_IRQM_WPROT_BIT 10
#define L2C_ERROR_IRQM_UNCORR_BIT 9
#define L2C_ERROR_IRQM_CORR_BIT 8
#define L2C_ERROR_IRQM_BIT 8
#define L2C_ERROR_SCB_BIT 6
#define L2C_ERROR_STCB_BIT 4
#define L2C_ERROR_XCB_BIT 3
#define L2C_ERROR_RCB_BIT 2
#define L2C_ERROR_COMP_BIT 1
#define L2C_ERROR_RST_BIT 0

/*
 * L2CACHE DATA CHECK BITS register fields 
 */
#define L2C_DCB_CB (0xfffffff << L2C_DCB_CB_BIT)

#define L2C_DCB_CB_BIT 0 

/*
 * L2CACHE SCRUB register fields 
 */
#define L2C_SCRUB_INDEX (0xffff << L2C_SCRUB_INDEX_BIT)
#define L2C_SCRUB_WAY (0x3 << L2C_SCRUB_WAY_BIT)
#define L2C_SCRUB_PEN (0x1 << L2C_SCRUB_PEN_BIT)
#define L2C_SCRUB_EN (0x1 << L2C_SCRUB_EN_BIT)

#define L2C_SCRUB_INDEX_BIT 16 
#define L2C_SCRUB_WAY_BIT 2
#define L2C_SCRUB_PEN_BIT 1
#define L2C_SCRUB_EN_BIT 0

/*
 * L2CACHE SCRUBDELAY register fields 
 */
#define L2C_SCRUB_DEL (0xffff << L2C_SCRUB_DEL_BIT)

#define L2C_SCRUB_DEL_BIT 0 

/*
 * L2CACHE ERROR INJECT register fields 
 */
#define L2C_ERRINJ_ADDR (0x3fffffff << L2C_ERRINJ_ADDR_BIT)
#define L2C_ERRINJ_EN (0x1 << L2C_ERRINJ_EN_BIT)

#define L2C_ERRINJ_ADDR_BIT 2
#define L2C_ERRINJ_EN_BIT 0

/*
 * L2CACHE ACCESS CONTROL register fields 
 */
#define L2C_ACCCTRL_DSC (0x1 << L2C_ACCCTRL_DSC_BIT)
#define L2C_ACCCTRL_SH (0x1 << L2C_ACCCTRL_SH_BIT)
#define L2C_ACCCTRL_SPLITQ (0x1 << L2C_ACCCTRL_SPLITQ_BIT)
#define L2C_ACCCTRL_NHM (0x1 << L2C_ACCCTRL_NHM_BIT)
#define L2C_ACCCTRL_BERR (0x1 << L2C_ACCCTRL_BERR_BIT)
#define L2C_ACCCTRL_OAPM (0x1 << L2C_ACCCTRL_OAPM_BIT)
#define L2C_ACCCTRL_FLINE (0x1 << L2C_ACCCTRL_FLINE_BIT)
#define L2C_ACCCTRL_DBPF (0x1 << L2C_ACCCTRL_DBPF_BIT)
#define L2C_ACCCTRL_128WF (0x1 << L2C_ACCCTRL_128WF_BIT)
#define L2C_ACCCTRL_DBPWS (0x1 << L2C_ACCCTRL_DBPWS_BIT)
#define L2C_ACCCTRL_SPLIT (0x1 << L2C_ACCCTRL_SPLIT_BIT)

#define L2C_ACCCTRL_DSC_BIT 14
#define L2C_ACCCTRL_SH_BIT 13
#define L2C_ACCCTRL_SPLITQ_BIT 10
#define L2C_ACCCTRL_NHM_BIT 9
#define L2C_ACCCTRL_BERR_BIT 8
#define L2C_ACCCTRL_OAPM_BIT 7
#define L2C_ACCCTRL_FLINE_BIT 6
#define L2C_ACCCTRL_DBPF_BIT 5
#define L2C_ACCCTRL_128WF_BIT 4
#define L2C_ACCCTRL_DBPWS_BIT 2
#define L2C_ACCCTRL_SPLIT_BIT 1

#ifdef TEST_L2CACHE
/*
 * L2CACHE TAG fields 
 */
#define L2C_TAG_TAG (0xfffffc << L2C_TAG_TAG_BIT)
#define L2C_TAG_VALID (0x3 << L2C_TAG_VALID_BIT)
#define L2C_TAG_DIRTY (0x3 << L2C_TAG_DIRTY_BIT)
#define L2C_TAG_LRU (0x3 << L2C_TAG_LRU_BIT)

#define L2C_TAG_TAG_BIT 10
#define L2C_TAG_VALID_BIT 8
#define L2C_TAG_DIRTY_BIT 6
#define L2C_TAG_LRU_BIT 0

#endif /* TEST_L2CACHE */

#define DEVNAME_LEN 9
/*
 * L2CACHE Driver private data struture
 */
struct l2cache_priv {
	struct drvmgr_dev	*dev;
	char devname[DEVNAME_LEN];

	/* L2CACHE control registers */
	struct l2c_regs	*regs;

	/* L2CACHE status */
	int ways;
	int waysize;
	int linesize;
	int index;
	int mtrr;
	int ft_support;
	int split_support;

	/* User defined ISR */
	l2cache_isr_t isr;
	void *isr_arg;
};

/*
 * L2CACHE internal prototypes 
 */
/* -Register access functions */
STATIC INLINE int l2cache_reg_ctrl_enable(void);
STATIC INLINE int l2cache_reg_ctrl_disable(void);
STATIC INLINE int l2cache_reg_ctrl_locked_set(int locked);
STATIC INLINE int l2cache_reg_ctrl_edac_set(int edac);
STATIC INLINE int l2cache_reg_ctrl_repl(int policy);
STATIC INLINE int l2cache_reg_ctrl_iway(int way);
STATIC INLINE int l2cache_reg_ctrl_writep(int policy);
STATIC INLINE unsigned int l2cache_reg_ctrl(void);
STATIC INLINE unsigned int l2cache_reg_status(void);
STATIC INLINE int l2cache_reg_mtrr_set(int index, unsigned int addr, 
		unsigned int mask, int options);
UNUSED STATIC INLINE unsigned int l2cache_reg_mtrr_get(int index);
STATIC INLINE int l2cache_reg_flushmem(unsigned int addr, int options);
STATIC INLINE int l2cache_reg_flushline(int way, int index, int options);
STATIC INLINE int l2cache_reg_flushway(unsigned int tag, int way, int options);
STATIC INLINE unsigned int l2cache_reg_error(void);
STATIC INLINE int l2cache_reg_error_reset(void);
STATIC INLINE int l2cache_reg_error_irqmask(int mask);
STATIC INLINE unsigned int l2cache_reg_error_addr(void);
STATIC INLINE unsigned int l2cache_reg_scrub(void);
STATIC INLINE int l2cache_reg_scrub_enable(int delay);
STATIC INLINE int l2cache_reg_scrub_disable(void);
STATIC INLINE unsigned int l2cache_reg_scrub_delay(void);
STATIC INLINE int l2cache_reg_scrub_line(int way, int index);
STATIC INLINE unsigned int l2cache_reg_accctrl(void);
STATIC INLINE int l2cache_reg_accctrl_split_disable(void);
STATIC INLINE int l2cache_reg_accctrl_split_enable(void);
#ifdef TEST_L2CACHE
STATIC INLINE int l2cache_reg_error_dcb(unsigned int cb);
STATIC INLINE int l2cache_reg_error_inject(unsigned int addr);
STATIC INLINE unsigned int l2cache_reg_diagtag(int way, int index);
STATIC INLINE unsigned int l2cache_reg_diagdata(int way, int index, int word);
STATIC unsigned int log2int(unsigned int v);
#endif /* TEST_L2CACHE */

/* -Control functions */
STATIC int l2cache_ctrl_status(void);
STATIC void l2cache_flushwait(void);

/* -Init function */
STATIC int l2cache_init(struct l2cache_priv *priv);

/* -Init function called by drvmgr */
int l2cache_init1(struct drvmgr_dev *dev);

/* -IRQ handler */
void l2cache_isr(void *arg);

/*
 * L2CACHE static members 
 */
static struct l2cache_priv *l2cachepriv = NULL;
#ifdef DEBUG
static char * repl_names[4] = {"LRU","Random","Master-Idx-1","Master-IDx-2"};
#endif

/* L2CACHE DRIVER */

struct drvmgr_drv_ops l2cache_ops = 
{
	.init = {l2cache_init1, NULL, NULL, NULL},
	.remove = NULL,
	.info = NULL
};

struct amba_dev_id l2cache_ids[] = 
{
	{VENDOR_GAISLER, GAISLER_L2CACHE},
	{0, 0}		/* Mark end of table */
};

struct amba_drv_info l2cache_info =
{
	{
		DRVMGR_OBJ_DRV,					/* Driver */
		NULL,				/* Next driver */
		NULL,				/* Device list */
		DRIVER_AMBAPP_GAISLER_L2CACHE_ID,/* Driver ID */
		"L2CACHE_DRV",			/* Driver Name */
		DRVMGR_BUS_TYPE_AMBAPP,		/* Bus Type */
		&l2cache_ops,
		NULL,				/* Funcs */
		0,				/* No devices yet */
		sizeof(struct l2cache_priv),	/* Make drvmgr alloc private */
	},
	&l2cache_ids[0]
};

void l2cache_register_drv(void)
{
	DBG("Registering L2CACHE driver\n");
	drvmgr_drv_register(&l2cache_info.general);
}

/* Initializes the L2CACHE core and driver
 *
 * Return values
 *	0			  Successful initalization
 */
STATIC int l2cache_init(struct l2cache_priv *priv)
{
	struct ambapp_ahb_info *ahb;
	struct amba_dev_info *ainfo = priv->dev->businfo;

	/* Find L2CACHE core from Plug&Play information */
	ahb = ainfo->info.ahb_slv;

	/* Found L2CACHE core, init private structure */
	priv->regs = (struct l2c_regs *)ahb->start[1];

	/* Initialize L2CACHE status */
	unsigned int status = l2cache_reg_status();
	priv->ways = (status & L2C_STAT_WAY) + 1;
	priv->waysize = 
		((status & L2C_STAT_WAYSIZE) >> L2C_STAT_WAYSIZE_BIT) * 1024;
	priv->linesize = ((status & L2C_STAT_LS)? 64 : 32);
	priv->index = ((priv->waysize)/(priv->linesize));
	priv->mtrr = (status & L2C_STAT_MTRR) >> L2C_STAT_MTRR_BIT;
	priv->ft_support = (status & L2C_STAT_MP) >> L2C_STAT_MP_BIT;

	/* Probe split support. */
	int split_old = 0;
	int split_new = 0;
	split_old = (l2cache_reg_accctrl() & L2C_ACCCTRL_SPLIT);
	if (split_old){
		l2cache_reg_accctrl_split_disable();
	}else{
		l2cache_reg_accctrl_split_enable();
	}
	split_new = (l2cache_reg_accctrl() & L2C_ACCCTRL_SPLIT);
	if (split_old){
		l2cache_reg_accctrl_split_enable();
	}else{
		l2cache_reg_accctrl_split_disable();
	}
	priv->split_support = 
		((split_new ^ split_old) >> L2C_ACCCTRL_SPLIT_BIT) & 1;

	DBG("L2CACHE driver initialized\n");

	return 0;
}

/* Called when a core is found with the AMBA device and vendor ID 
 * given in l2cache_ids[]. IRQ, Console does not work here
 */
int l2cache_init1(struct drvmgr_dev *dev)
{
	int status;
	struct l2cache_priv *priv;

	DBG("L2CACHE[%d] on bus %s\n", dev->minor_drv, dev->parent->dev->name);

	if (l2cachepriv) {
		DBG("Driver only supports one L2CACHE core\n");
		return DRVMGR_FAIL;
	}

	priv = dev->priv;
	if (!priv)
		return DRVMGR_NOMEM;

	priv->dev = dev;
	strncpy(&priv->devname[0], "l2cache0", DEVNAME_LEN);
	l2cachepriv = priv;

	/* Initialize L2CACHE Hardware */
	status = l2cache_init(priv);
	if (status) {
		printk("Failed to initialize l2cache driver %d\n", status);
		return -1;
	}

	return DRVMGR_OK;
}

STATIC INLINE int l2cache_reg_ctrl_enable(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	REG_WRITE(&priv->regs->control, (ctrl | L2C_CTRL_EN));
	return 0;
}

STATIC INLINE int l2cache_reg_ctrl_disable(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	REG_WRITE(&priv->regs->control, (ctrl & ~(L2C_CTRL_EN)));
	return 0;
}

STATIC INLINE int l2cache_reg_ctrl_repl(int policy)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	REG_WRITE(&priv->regs->control, 
		((ctrl & ~(L2C_CTRL_REPL)) | 
		 ((policy << L2C_CTRL_REPL_BIT) & L2C_CTRL_REPL))
	);
	return 0;
}

STATIC INLINE int l2cache_reg_ctrl_iway(int way)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	REG_WRITE(&priv->regs->control, 
		((ctrl & ~(L2C_CTRL_IWAY)) | 
		 ((way << L2C_CTRL_IWAY_BIT) & L2C_CTRL_IWAY))
	);
	return 0;
}

STATIC INLINE int l2cache_reg_ctrl_writep(int policy)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	REG_WRITE(&priv->regs->control, 
		((ctrl & ~(L2C_CTRL_WP)) | ((policy << L2C_CTRL_WP_BIT) & L2C_CTRL_WP))
	);
	return 0;
}

STATIC INLINE int l2cache_reg_ctrl_locked_set(int locked)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	ctrl = (ctrl & ~(L2C_CTRL_LOCK));
	REG_WRITE(&priv->regs->control, 
			ctrl | 
			((locked << L2C_CTRL_LOCK_BIT) & L2C_CTRL_LOCK));
	return 0;
}

STATIC INLINE int l2cache_reg_ctrl_edac_set(int edac)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->control);
	REG_WRITE(&priv->regs->control, 
			(ctrl & ~(L2C_CTRL_EDAC)) | 
			(edac? L2C_CTRL_EDAC:0));
	return 0;
}

STATIC INLINE unsigned int l2cache_reg_ctrl(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->control);
}

STATIC INLINE unsigned int l2cache_reg_status(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->status);
}

STATIC INLINE int l2cache_reg_mtrr_set(int index, unsigned int addr, 
		unsigned int mask, int options)
{
	struct l2cache_priv *priv = l2cachepriv;

	/* Set mtrr */
	addr = addr & L2C_MTRR_ADDR;
	mask = (mask >> 16) & L2C_MTRR_MASK; 
	options = ((options & ~(L2C_MTRR_ADDR)) & ~(L2C_MTRR_MASK));
	unsigned int mtrr = 0 | addr | mask | options;
	REG_WRITE(&priv->regs->mtrr[index], mtrr);
	return 0;
}

UNUSED STATIC INLINE unsigned int l2cache_reg_mtrr_get(int index)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->mtrr[index]);
}

STATIC INLINE int l2cache_reg_flushmem(unsigned int addr, int options)
{
	struct l2cache_priv *priv = l2cachepriv;

	options = (options & ~(L2C_FLUSH_ADDR));
	REG_WRITE(&priv->regs->flush_mem_addr, (addr & L2C_FLUSH_ADDR) | options);
	return 0;
}

STATIC INLINE int l2cache_reg_flushline(int way, int index, int options)
{
	struct l2cache_priv *priv = l2cachepriv;

	options = 0 | (options & (L2C_FLUSHSI_FMODE));
	REG_WRITE(&priv->regs->flush_set_index, 
			((index << L2C_FLUSHSI_INDEX_BIT) & L2C_FLUSHSI_INDEX) |
			((way << L2C_FLUSHSI_WAY_BIT) & L2C_FLUSHSI_WAY) | 
			options
	);
	return 0;
}

STATIC INLINE int l2cache_reg_flushway(unsigned int tag, int way, int options)
{
	struct l2cache_priv *priv = l2cachepriv;

	options = (options & ~(L2C_FLUSHSI_TAG | L2C_FLUSHSI_WAY)) 
		| L2C_FLUSHSI_WF;
	REG_WRITE(&priv->regs->flush_set_index, 
			(tag & L2C_FLUSHSI_TAG) | 
			( (way << L2C_FLUSHSI_WAY_BIT) & L2C_FLUSHSI_WAY) | 
			options);
	return 0;
}

STATIC INLINE unsigned int l2cache_reg_error(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->error_status_control);
}

STATIC INLINE int l2cache_reg_error_reset(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->error_status_control);
	REG_WRITE(&priv->regs->error_status_control, ctrl | L2C_ERROR_RST);
	return 0;
}

STATIC INLINE int l2cache_reg_error_irqmask(int mask)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->error_status_control);
	REG_WRITE(&priv->regs->error_status_control, 
			(ctrl & ~(L2C_ERROR_IRQM)) | (mask & L2C_ERROR_IRQM));
	return 0;
}

STATIC INLINE unsigned int l2cache_reg_error_addr(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->error_addr);
}

STATIC INLINE unsigned int l2cache_reg_scrub(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->scrub_control_status);
}

STATIC INLINE int l2cache_reg_scrub_enable(int delay)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int accc = REG_READ(&priv->regs->access_control);
	REG_WRITE(&priv->regs->access_control,
			accc | L2C_ACCCTRL_DSC | L2C_ACCCTRL_SH);

	unsigned int ctrl = REG_READ(&priv->regs->scrub_control_status);
	REG_WRITE(&priv->regs->scrub_delay, 
			(delay << L2C_SCRUB_DEL_BIT) & L2C_SCRUB_DEL);
	REG_WRITE(&priv->regs->scrub_control_status,  ctrl | L2C_SCRUB_EN);
	return 0;
}

STATIC INLINE int l2cache_reg_scrub_disable(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	unsigned int ctrl = REG_READ(&priv->regs->scrub_control_status);
	REG_WRITE(&priv->regs->scrub_control_status,  ctrl & ~(L2C_SCRUB_EN));
	return 0;
}

STATIC INLINE int l2cache_reg_scrub_line(int way, int index)
{
	struct l2cache_priv *priv = l2cachepriv;

	REG_WRITE(&priv->regs->scrub_control_status, 
			((index << L2C_SCRUB_INDEX_BIT) & L2C_SCRUB_INDEX) |
			((way << L2C_SCRUB_WAY_BIT) & L2C_SCRUB_WAY) |
			L2C_SCRUB_PEN);
	return 0;
}

STATIC INLINE unsigned int l2cache_reg_scrub_delay(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->scrub_delay);
}

STATIC INLINE unsigned int l2cache_reg_accctrl(void){
	struct l2cache_priv *priv = l2cachepriv;

	return REG_READ(&priv->regs->access_control);
}

STATIC INLINE int l2cache_reg_accctrl_split_disable(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	/* Disable split */
	unsigned int ctrl = REG_READ(&priv->regs->access_control);
	REG_WRITE(&priv->regs->access_control, (ctrl & ~(L2C_ACCCTRL_SPLIT)));
	return 0;
}

STATIC INLINE int l2cache_reg_accctrl_split_enable(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	/* Enable split */
	unsigned int ctrl = REG_READ(&priv->regs->access_control);
	REG_WRITE(&priv->regs->access_control, (ctrl | (L2C_ACCCTRL_SPLIT)));
	return 0;
}

STATIC INLINE int l2cache_ctrl_status(void)
{
	return ((l2cache_reg_ctrl() >> L2C_CTRL_EN_BIT) & 0x1);
}

STATIC void l2cache_flushwait(void)
{
	/* Read any L2cache register to wait until flush is done */
	/* The L2 will block any access until the flush is done */
	/* Force read operation */
	//asm volatile ("" : : "r" (l2cache_reg_status()));
	(void) l2cache_reg_status();
	return;
}

#ifdef TEST_L2CACHE
STATIC INLINE int l2cache_reg_error_dcb(unsigned int cb)
{
	struct l2cache_priv *priv = l2cachepriv;

	REG_WRITE(&priv->regs->data_check_bit, (cb & L2C_DCB_CB)); 
	return 0;
}

STATIC INLINE int l2cache_reg_error_inject(unsigned int addr)
{
	struct l2cache_priv *priv = l2cachepriv;

	REG_WRITE(&priv->regs->error_injection, 
			(addr & L2C_ERRINJ_ADDR) | L2C_ERRINJ_EN); 
	return 0;
}

STATIC INLINE unsigned int l2cache_reg_diagtag(int way, int index)
{
	struct l2cache_priv *priv = l2cachepriv;

	int offset = (index*8 + way);
	return REG_READ(&priv->regs->diag_iface_tag[offset]);
}

STATIC INLINE unsigned int l2cache_reg_diagdata(int way, int index, int word)
{
	struct l2cache_priv *priv = l2cachepriv;

	int offset = (index*(priv->linesize/4) + way*0x20000 + word);
	return REG_READ(&priv->regs->diag_iface_data[offset]);
}

STATIC unsigned int log2int(unsigned int v)
{
	unsigned r = 0;
	while (v >>= 1) {
		r++;
	}
	return r;
}

/* Return the index for a given addr */
int l2cache_get_index( uint32_t addr)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	return (addr % priv->waysize)/(priv->linesize);
}

/* Return the tag for a given addr */
uint32_t l2cache_get_tag( uint32_t addr)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	uint32_t tmp;
	int i = log2int(priv->waysize);
	tmp = (addr >> i);
	tmp = (tmp << i);
	return tmp;
}

int l2cache_lookup(uint32_t addr, int * way)
{
	struct l2cache_priv * priv = l2cachepriv;
	int i;
	struct l2cache_tag gottag;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	uint32_t exptag = l2cache_get_tag(addr);
	int index = l2cache_get_index(addr);

	/* Check all tags in the set */ 
	for(i=0; i< priv->ways; i++){
		ret = l2cache_diag_tag(i, index, &gottag);
		if (ret != L2CACHE_ERR_OK){
			return ret;
		}
		/*DBG("L2CACHE gottag: way=%d, valid=%d, tag=0x%08x.\n",
		 *		i, gottag.valid, gottag.tag);*/
		/* Check if valid */
		if (gottag.valid){
			/* Check if they are the same */
			if (gottag.tag == exptag){
				/* HIT! */
				if (way){
					*way = i;
				}
				DBG("L2CACHE lookup: index=%d, tag=0x%08x HIT way=%d.\n", 
						index, (unsigned int) exptag, i);
				return L2CACHE_HIT;
			}
		}
	}
	DBG("L2CACHE lookup: index=%d, tag=0x%08x MISS.\n", 
			index, (unsigned int) exptag);
	/* MISS! */
	return L2CACHE_MISS;
}

/* Diagnostic Accesses */
#define l2cache_tag_valid(val) ((val & L2C_TAG_VALID) >> L2C_TAG_VALID_BIT)
#define l2cache_tag_dirty(val) ((val & L2C_TAG_DIRTY) >> L2C_TAG_DIRTY_BIT)
#define l2cache_tag_lru(val) ((val & L2C_TAG_LRU) >> L2C_TAG_LRU_BIT)
int l2cache_diag_tag( int way, int index, struct l2cache_tag * tag)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (way >= priv->ways){
		DBG("L2CACHE has only %d ways.\n", priv->ways);
		return L2CACHE_ERR_EINVAL;
	}

	if (index >= priv->index){
		DBG("L2CACHE has only %d lines.\n", priv->index);
		return L2CACHE_ERR_EINVAL;
	}

	if (tag){
		unsigned int val = l2cache_reg_diagtag(way,index);

		tag->tag   = l2cache_get_tag(val);
		tag->valid = l2cache_tag_valid(val);
		tag->dirty = l2cache_tag_dirty(val);
		tag->lru   = l2cache_tag_lru(val);
	}else{
		return L2CACHE_ERR_EINVAL;
	}
	return L2CACHE_ERR_OK;
}

int l2cache_diag_line( int way, int index, struct l2cache_dataline * dataline)
{
	struct l2cache_priv * priv = l2cachepriv;
	int i;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (way >= priv->ways){
		DBG("L2CACHE has only %d ways.\n", priv->ways);
		return L2CACHE_ERR_EINVAL;
	}

	if (index >= priv->index){
		DBG("L2CACHE has only %d lines.\n", priv->index);
		return L2CACHE_ERR_EINVAL;
	}

	if (dataline){
		dataline->words = (priv->linesize/4);
		for (i=0; i< (priv->linesize/4); i++){
			dataline->data[i] = l2cache_reg_diagdata(way,index,i);
		}
	}else{
		return L2CACHE_ERR_EINVAL;
	}
	return L2CACHE_ERR_OK;
}

/* Inject an error on a given addr */
int l2cache_error_inject_address( uint32_t addr, uint32_t mask)
{
	struct l2cache_priv * priv = l2cachepriv;
	int word;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->ft_support){
		DBG("L2CACHE does not have EDAC support.\n");
		return L2CACHE_ERR_ERROR;
	}

	if (addr & 0x3){
		DBG("Address not aligned to 32-bit.\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Get word index */
	word = (addr % priv->linesize)/4;

	/* Shift mask to proper word */
	mask = (mask << (7*(priv->ways - word - 1)));

	/* Write DCB mask to XOR */
	l2cache_reg_error_dcb(mask);

	/* Inject error */
	l2cache_reg_error_inject(addr);

	DBG("L2CACHE error injected in 0x%08x (0x%08x).\n", 
			(unsigned int) addr, (unsigned int) mask);

	return L2CACHE_ERR_OK;
}

#endif /* TEST_L2CACHE */

/* L2CACHE Interrupt handler, called when there may be a L2CACHE interrupt.
 */
void l2cache_isr(void *arg)
{
	struct l2cache_priv *priv = arg;
	unsigned int sts = l2cache_reg_error();
	unsigned int addr = l2cache_reg_error_addr();

	/* Make sure that the interrupt is pending and unmasked,
	 * otherwise it migth have been other core
	 * sharing the same interrupt line */
	if ( ((sts & L2C_ERROR_IRQP) >> L2C_ERROR_IRQP_BIT) &
			((sts & L2C_ERROR_IRQM) >> L2C_ERROR_IRQM_BIT)){
		/* Reset error status */
		l2cache_reg_error_reset();
		/* Execute user IRQ (ther will always be one ISR */
		/* Give cacheline address */
		(priv->isr)(priv->isr_arg, (addr & ~(0x1f)), sts);
	}
}

/* Enable L2CACHE:
 */
int l2cache_enable(int flush)
{
	int ret;

	/* Flush checks flus parameter and INIT state */
	ret = l2cache_flush(flush);
	if (ret < 0){
		return ret;
	}

	l2cache_reg_ctrl_enable();

	DBG("L2CACHE enabled\n");
	return L2CACHE_ERR_OK;
}

/* Disable L2CACHE:
 */
int l2cache_disable(int flush)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if ((flush < 0) || 
			(flush > 
			 (L2CACHE_OPTIONS_FLUSH_INVALIDATE | L2CACHE_OPTIONS_FLUSH_WAIT))
			){
		DBG("L2CACHE wrong flush option.\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Flush & invalidate all cache. Also disable L2C */
	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			l2cache_reg_ctrl_disable();
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
			l2cache_reg_flushmem(0, L2C_FLUSH_FMODE_INV_WB_ALL | L2C_FLUSH_DI);
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			l2cache_reg_flushmem(0, L2C_FLUSH_FMODE_WB_ALL | L2C_FLUSH_DI);
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
		default:
			l2cache_reg_flushmem(0, L2C_FLUSH_FMODE_INV_ALL | L2C_FLUSH_DI);
			break;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}

	DBG("L2CACHE disabled\n");
	return L2CACHE_ERR_OK;
}

/* Status L2CACHE:
 */
int l2cache_status(void)
{
	struct l2cache_priv * priv = l2cachepriv;
	int status;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	unsigned int ctrl = l2cache_reg_ctrl();
	int locked = (ctrl & L2C_CTRL_LOCK) >> L2C_CTRL_LOCK_BIT;
	int enabled = ((ctrl & L2C_CTRL_EN) >> L2C_CTRL_EN_BIT) & 0x1;
	int edac = (ctrl & L2C_CTRL_EDAC) >> L2C_CTRL_EDAC_BIT;
	int repl = (ctrl & L2C_CTRL_REPL) >> L2C_CTRL_REPL_BIT;
	int writep = (ctrl & L2C_CTRL_WP) >> L2C_CTRL_WP_BIT;

	unsigned int acc = l2cache_reg_accctrl();
	int split = (acc & L2C_ACCCTRL_SPLIT) >> L2C_ACCCTRL_SPLIT_BIT;

	unsigned int err = l2cache_reg_error();
	int interrupts = (err & L2C_ERROR_IRQM) >> L2C_ERROR_IRQM_BIT;

	unsigned int scr = l2cache_reg_scrub();
	int scrub = (scr & L2C_SCRUB_EN) >> L2C_SCRUB_EN_BIT;

	unsigned int dly = l2cache_reg_scrub_delay();
	int delay = (dly & L2C_SCRUB_DEL) >> L2C_SCRUB_DEL_BIT;

	status = 0|
		(enabled? L2CACHE_STATUS_ENABLED: 0) |
		(split? L2CACHE_STATUS_SPLIT_ENABLED: 0) |
		(edac? L2CACHE_STATUS_EDAC_ENABLED: 0) |
		((repl & 0x3) << L2CACHE_STATUS_REPL_BIT) |
		(writep? L2CACHE_STATUS_WRITETHROUGH: 0) |
		((locked & 0xf) << L2CACHE_STATUS_LOCK_BIT) |
		((interrupts & 0xf) << L2CACHE_STATUS_INT_BIT) |
		(scrub? L2CACHE_STATUS_SCRUB_ENABLED: 0) |
		((delay & 0xffff) << L2CACHE_STATUS_SCRUB_DELAY_BIT);

	return status;
}

/* Flush L2CACHE:
 */
int l2cache_flush(int flush)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if ((flush < 0) || 
			(flush > 
			 (L2CACHE_OPTIONS_FLUSH_INVALIDATE | L2CACHE_OPTIONS_FLUSH_WAIT))
			){
		DBG("L2CACHE wrong flush option.\n");
		return L2CACHE_ERR_EINVAL;
	}

	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
			l2cache_reg_flushmem(0, L2C_FLUSH_FMODE_INV_WB_ALL);
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			l2cache_reg_flushmem(0, L2C_FLUSH_FMODE_WB_ALL);
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
		default:
			l2cache_reg_flushmem(0, L2C_FLUSH_FMODE_INV_ALL);
			break;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}

	DBG("L2CACHE flushed\n");
	return L2CACHE_ERR_OK;
}

/* Flush L2CACHE address:
 */
int l2cache_flush_address(uint32_t addr, int size, int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	uint32_t endaddr;
	int options;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if ((flush < 0) || 
			(flush > 
			 (L2CACHE_OPTIONS_FLUSH_INVALIDATE | L2CACHE_OPTIONS_FLUSH_WAIT))
			){
		DBG("L2CACHE wrong flush option.\n");
		return L2CACHE_ERR_EINVAL;
	}

	if (size <= 0){
		DBG("L2CACHE wrong size.\n");
		return L2CACHE_ERR_EINVAL;
	}

	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
			options=L2C_FLUSH_FMODE_INV_WB_ONE;
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			options=L2C_FLUSH_FMODE_WB_ONE;
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
		default:
			options=L2C_FLUSH_FMODE_INV_ONE;
			break;
	}

	if ( (flush & 0x3) == L2CACHE_OPTIONS_FLUSH_NONE){
		return L2CACHE_ERR_OK;
	}

	/* Get the end address */
	endaddr = (addr + size);

	/* Start on first cacheline address */
	addr = addr - (addr % priv->linesize);
	while( addr < endaddr){
		/* Flush address */
		l2cache_reg_flushmem(addr, options);
		/* Update next line */
		addr += priv->linesize;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}
	
	DBG("L2CACHE address range flushed\n");
	return L2CACHE_ERR_OK;
}

/* Flush L2CACHE line:
 */
int l2cache_flush_line(int way, int index, int flush)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if ((flush < 0) || 
			(flush > 
			 (L2CACHE_OPTIONS_FLUSH_INVALIDATE | L2CACHE_OPTIONS_FLUSH_WAIT))
			){
		DBG("L2CACHE wrong flush option.\n");
		return L2CACHE_ERR_EINVAL;
	}

	if ((index < 0) || (index >= priv->index)){
		DBG("L2CACHE only has %d lines.\n", priv->index);
		return L2CACHE_ERR_EINVAL;
	}

	if ((way < 0 ) || (way >= priv->ways)){
		DBG("L2CACHE only has %d ways.\n", priv->ways);
		return L2CACHE_ERR_EINVAL;
	}

	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
			l2cache_reg_flushline(way, index, 
					L2C_FLUSHSI_FMODE_SET_INV_WB_ONE);
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			l2cache_reg_flushline(way, index, L2C_FLUSHSI_FMODE_SET_WB_ONE);
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
		default:
			l2cache_reg_flushline(way, index, L2C_FLUSHSI_FMODE_SET_INV_ONE);
			break;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}

	DBG("L2CACHE line [%d,%d] flushed\n", way, index);
	return L2CACHE_ERR_OK;
}

/* Flush L2CACHE way:
 */
int l2cache_flush_way(int way, int flush)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if ((flush < 0) || 
			(flush > 
			 (L2CACHE_OPTIONS_FLUSH_INVALIDATE | L2CACHE_OPTIONS_FLUSH_WAIT))
			){
		DBG("L2CACHE wrong flush option.\n");
		return L2CACHE_ERR_EINVAL;
	}

	if ((way < 0 ) || (way >= priv->ways)){
		DBG("L2CACHE only has %d ways.\n", priv->ways);
		return L2CACHE_ERR_EINVAL;
	}

	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
			l2cache_reg_flushway(0, way, L2C_FLUSHSI_FMODE_WAY_UPDATE);
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			l2cache_reg_flushway(0, way, L2C_FLUSHSI_FMODE_WAY_WB);
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
		default:
			l2cache_reg_flushway(0, way, L2C_FLUSHSI_FMODE_WAY_UPDATE_WB_ALL);
			break;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}

	DBG("L2CACHE way [%d] flushed\n",way);
	return L2CACHE_ERR_OK;
}

/* Fill L2CACHE way:
 */
int l2cache_fill_way(int way, uint32_t tag, int options, int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int flags;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if ((way < 0 ) || (way >= priv->ways)){
		DBG("L2CACHE only has %d ways.\n", priv->ways);
		return L2CACHE_ERR_EINVAL;
	}

	/* Check input parameters */
	if (tag & 0x000003ff){
		DBG("Only using bits 31:10 of Addr/Mask\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Perform the Way-flush */
	flags = ((options & L2CACHE_OPTIONS_FETCH)? L2C_FLUSHSI_FL:0) |
			 ((options & L2CACHE_OPTIONS_VALID)? L2C_FLUSHSI_VB:0) | 
			 ((options & L2CACHE_OPTIONS_DIRTY)? L2C_FLUSHSI_DB:0);

	/*DBG("L2CACHE lock way: Locked=%d, way=%d, option=0x%04x\n", 
	 *		locked, way, flags);*/

	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
			l2cache_reg_flushway(tag, way, 
					flags | L2C_FLUSHSI_FMODE_WAY_UPDATE);
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			l2cache_reg_flushway(tag, way, flags | L2C_FLUSHSI_FMODE_WAY_WB);
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
		default:
			l2cache_reg_flushway(tag, way, 
					flags | L2C_FLUSHSI_FMODE_WAY_UPDATE_WB_ALL);
			break;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}

	DBG("Way[%d] filled with Tag 0x%08x\n", way, (unsigned int) tag);

	return L2CACHE_ERR_OK;
}

/* Lock L2CACHE way:
 */
int l2cache_lock_way(uint32_t tag, int options, int flush, int enable)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int way;
	int locked;
	int flags;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	locked = L2CACHE_LOCKED_WAYS(l2cache_status());
	if (locked >= priv->ways){
		DBG("L2CACHE only has %d ways.\n", priv->ways);
		return L2CACHE_ERR_TOOMANY;
	}

	/* Check input parameters */
	if (tag & 0x000003ff){
		DBG("Only using bits 31:10 of Addr/Mask\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Check L2C status */
	enabled = l2cache_ctrl_status();
	
	/* Disable L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	/* Increase number of locked ways */
	locked++;
	way = priv->ways - locked;
	l2cache_reg_ctrl_locked_set(locked);

	/* Perform the Way-flush */
	flags = ((options & L2CACHE_OPTIONS_FETCH)? L2C_FLUSHSI_FL:0) |
			 ((options & L2CACHE_OPTIONS_VALID)? L2C_FLUSHSI_VB:0) | 
			 ((options & L2CACHE_OPTIONS_DIRTY)? L2C_FLUSHSI_DB:0);

	/*DBG("L2CACHE lock way: Locked=%d, way=%d, option=0x%04x\n",
	 *		locked, way, flags);*/

	switch(flush & 0x3){
		case L2CACHE_OPTIONS_FLUSH_NONE:
			break;
		case L2CACHE_OPTIONS_FLUSH_INVALIDATE:
			l2cache_reg_flushway(tag, way, 
					flags | L2C_FLUSHSI_FMODE_WAY_UPDATE);
			break;
		case L2CACHE_OPTIONS_FLUSH_WRITEBACK:
			l2cache_reg_flushway(tag, way, flags | L2C_FLUSHSI_FMODE_WAY_WB);
			break;
		case L2CACHE_OPTIONS_FLUSH_INV_WBACK:
		default:
			l2cache_reg_flushway(tag, way, 
					flags | L2C_FLUSHSI_FMODE_WAY_UPDATE_WB_ALL);
			break;
	}

	/* Reenable L2C if required */
	switch(enable){
		case L2CACHE_OPTIONS_ENABLE:
			l2cache_reg_ctrl_enable();
			break;
		case L2CACHE_OPTIONS_DISABLE:
			break;
		case L2CACHE_OPTIONS_NONE:
		default:
			if (enabled) {
				l2cache_reg_ctrl_enable();
			}
			break;
	}

	if (flush & L2CACHE_OPTIONS_FLUSH_WAIT){
		l2cache_flushwait();
	}

	DBG("Way[%d] locked with Tag 0x%08x\n", way, (unsigned int) tag);

	return L2CACHE_ERR_OK;
}

/* Unlock L2CACHE waw:
 */
int l2cache_unlock()
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	/* Set number of locked ways to 0*/
	l2cache_reg_ctrl_locked_set(0);

	DBG("L2CACHE ways unlocked\n");

	return L2CACHE_ERR_OK;
}

/* Setup L2CACHE:
 * Parameters:
 * -options: Can be:  
 */
int l2cache_mtrr_enable(int index, uint32_t addr, uint32_t mask, int options, 
		int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int flags;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (index < 0){
		DBG("Wrong index\n");
		return L2CACHE_ERR_EINVAL;
	}

	if (index >= priv->mtrr){
		DBG("Not enough MTRR registers\n");
		return L2CACHE_ERR_TOOMANY;
	}

	/* Check input parameters */
	if ((addr & 0x0003ffff) || (mask & 0x0003ffff)){
		DBG("Only using bits 31:18 of Addr/Mask\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Check L2C status */
	enabled = l2cache_ctrl_status();

	/* Disable L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	/* Decode options */
	flags = 0 |
		(options & L2CACHE_OPTIONS_MTRR_ACCESS_WRITETHROUGH? 
			L2C_MTRR_WRITETHROUGH	: 
			L2C_MTRR_UNCACHED)		|
		(options & L2CACHE_OPTIONS_MTRR_WRITEPROT_ENABLE? 
			L2C_MTRR_WRITEPROT_ENABLE	: 
			L2C_MTRR_WRITEPROT_DISABLE) |
		L2C_MTRR_ACCESSCONTROL_ENABLE;

	/* Configure mtrr */
	l2cache_reg_mtrr_set(index, addr, mask, flags); 

	/* Enable cache again (if needed) */
	if (enabled){
		l2cache_reg_ctrl_enable();
	}

	DBG("MTRR[%d] succesfully configured for 0x%08x (mask 0x%08x), "
			"access=%s, wprot=%s\n", 
		index, (unsigned int) addr, (unsigned int) mask, 
		(options & L2CACHE_OPTIONS_MTRR_ACCESS_WRITETHROUGH? 
				"WRITETHROUGH":"UNCACHED"),
		(options & L2CACHE_OPTIONS_MTRR_WRITEPROT_ENABLE? "ENABLE":"DISABLE")
		);

	return L2CACHE_ERR_OK;
}

/* Setup L2CACHE:
 * Parameters:
 * -options: Can be:  
 */
int l2cache_mtrr_disable(int index)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (index < 0){
		DBG("Wrong index\n");
		return L2CACHE_ERR_EINVAL;
	}

	if (index >= priv->mtrr){
		DBG("Not enough MTRR registers\n");
		return L2CACHE_ERR_TOOMANY;
	}

	/* Configure mtrr */
	l2cache_reg_mtrr_set(index, 0, 0, L2C_MTRR_ACCESSCONTROL_DISABLE); 

	DBG("MTRR[%d] disabled\n", index);

	return L2CACHE_ERR_OK;
}

/* Print L2CACHE status
 * DEBUG function
 */
int l2cache_print(void)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	#ifdef DEBUG
	int status = l2cache_status();
	if (status < 0){
		return status;
	}
	printf("L2cache: Ways:%d. Waysize:%d, Linesize:%d, Lines:%d\n"
		   "		 MTRR:%d, FT:%s, Locked:%d, Split:%s\n"
		   "		 REPL:%s, WP:%s, EDAC:%s, Enabled:%s\n"
		   "		 Scrub:%s, S-Delay:%d\n",
		priv->ways, 
		priv->waysize, 
		priv->linesize, 
		(priv->index * priv->ways), 
		priv->mtrr, 
		(priv->ft_support? "Available":"N/A"),
		L2CACHE_LOCKED_WAYS(status),
		(priv->split_support? (L2CACHE_SPLIT_ENABLED(status)? 
							   "Enabled":"Disabled"):"N/A"),
		repl_names[L2CACHE_REPL(status)],
		(L2CACHE_WRITETHROUGH(status)? "Write-through":"Write-back"),
		(L2CACHE_EDAC_ENABLED(status)? "Enabled":"Disabled"),
		(L2CACHE_ENABLED(status)? "Yes":"No"),
		(L2CACHE_SCRUB_ENABLED(status)? "Enabled":"Disabled"),
		L2CACHE_SCRUB_DELAY(status)
	);
	if (l2cache_ctrl_status()){
		printf("L2cache enabled.\n");
	}else{
		printf("L2cache disabled.\n");
	}
	#endif
	return L2CACHE_ERR_OK;
}

int l2cache_split_enable(void)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->split_support){
		DBG("L2CACHE does not have split support.\n");
		return L2CACHE_ERR_ERROR;
	}

	l2cache_reg_accctrl_split_enable();
	DBG("L2CACHE split is now enabled\n");

	return L2CACHE_ERR_OK;
}

int l2cache_split_disable(void)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->split_support){
		DBG("L2CACHE does not have split support.\n");
		return L2CACHE_ERR_ERROR;
	}

	l2cache_reg_accctrl_split_disable();
	DBG("L2CACHE split is now disabled\n");

	return L2CACHE_ERR_OK;
}

int l2cache_edac_enable(int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->ft_support){
		DBG("L2CACHE does not have EDAC support.\n");
		return L2CACHE_ERR_ERROR;
	}

	/* Check that L2C is enabled */
	enabled = l2cache_ctrl_status();

	/* Disable&Flush L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	/* Clear error register */
	l2cache_reg_error_reset();

	/* Enable EDAC */
	l2cache_reg_ctrl_edac_set(1);

	/* Enable cache again */
	if (enabled){
		l2cache_reg_ctrl_enable();
	}

	DBG("L2CACHE EDAC is now enabled\n");

	return L2CACHE_ERR_OK;
}

int l2cache_edac_disable(int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->ft_support){
		DBG("L2CACHE does not have EDAC support.\n");
		return L2CACHE_ERR_ERROR;
	}

	/* Check that L2C is enabled */
	enabled = l2cache_ctrl_status();

	/* Disable&Flush L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	/* Disable EDAC */
	l2cache_reg_ctrl_edac_set(0);

	/* Clear error register */
	l2cache_reg_error_reset();

	/* Enable cache again */
	if (enabled){
		l2cache_reg_ctrl_enable();
	}

	DBG("L2CACHE EDAC is now disabled\n");

	return L2CACHE_ERR_OK;
}

int l2cache_scrub_enable(int delay)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->ft_support){
		DBG("L2CACHE does not have EDAC support.\n");
		return L2CACHE_ERR_ERROR;
	}

	/* Enable Scrub */
	l2cache_reg_scrub_enable(delay);

	DBG("L2CACHE Scrub is now enabled\n");

	return L2CACHE_ERR_OK;
}

int l2cache_scrub_disable(void)
{
	struct l2cache_priv * priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->ft_support){
		DBG("L2CACHE does not have EDAC support.\n");
		return L2CACHE_ERR_ERROR;
	}

	/* Disable Scrub */
	l2cache_reg_scrub_disable();

	DBG("L2CACHE Scrub is now disabled\n");

	return L2CACHE_ERR_OK;
}

int l2cache_scrub_line(int way, int index)
{
	struct l2cache_priv * priv = l2cachepriv;
	unsigned int scrub;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (!priv->ft_support){
		DBG("L2CACHE does not have EDAC support.\n");
		return L2CACHE_ERR_ERROR;
	}

	if ((index < 0) || (index >= priv->index)){
		DBG("L2CACHE only has %d lines.\n", priv->index);
		return L2CACHE_ERR_EINVAL;
	}

	if ((way < 0) || (way >= priv->ways)){
		DBG("L2CACHE only has %d ways.\n", priv->ways);
		return L2CACHE_ERR_EINVAL;
	}

	/* Check pending bit */
	scrub = l2cache_reg_scrub();
	if ( (scrub & L2C_SCRUB_PEN) || (scrub & L2C_SCRUB_EN) ){
		DBG("L2CACHE already scrubbing.\n");
		return L2CACHE_ERR_ERROR;
	}

	/* Scrub line */
	l2cache_reg_scrub_line(way, index);

	DBG("L2CACHE Scrub line [%d,%d]\n",way,index);

	return L2CACHE_ERR_OK;
}

int l2cache_writethrough(int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	/* Check that L2C is enabled */
	enabled = l2cache_ctrl_status();

	/* Disable&Flush L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	/* Configure writethrough */
	l2cache_reg_ctrl_writep(1);

	/* Enable cache again */
	if (enabled){
		l2cache_reg_ctrl_enable();
	}

	DBG("L2CACHE now is writethrough\n");

	return L2CACHE_ERR_OK;
}

int l2cache_writeback(int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int ret;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	/* Check that L2C is enabled */
	enabled = l2cache_ctrl_status();

	/* Disable&Flush L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	/* Configure writeback */
	l2cache_reg_ctrl_writep(0);

	/* Enable cache again */
	if (enabled){
		l2cache_reg_ctrl_enable();
	}

	DBG("L2CACHE now is writeback\n");

	return L2CACHE_ERR_OK;
}

int l2cache_replacement(int options, int flush)
{
	struct l2cache_priv * priv = l2cachepriv;
	int enabled;
	int ret;
	int way;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	/* Check that L2C is enabled */
	enabled = l2cache_ctrl_status();

	/* Disable&Flush L2C */
	ret = l2cache_disable(flush);
	if (ret < 0){
		return ret;
	}

	if ( (options & 0x3) == L2CACHE_OPTIONS_REPL_MASTERIDX_IDX){
		/* Set iway */
		way = (options >> 2) & 0x3;
		l2cache_reg_ctrl_iway(way);
	}

	/* Configure writeback */
	l2cache_reg_ctrl_repl(options & 0x3);

	/* Enable cache again */
	if (enabled){
		l2cache_reg_ctrl_enable();
	}

	DBG("L2CACHE replacement set to %d\n", (options & 0x3));

	return L2CACHE_ERR_OK;

}

int l2cache_isr_register(l2cache_isr_t isr, void * arg, int options)
{
	struct l2cache_priv *priv = l2cachepriv;
	unsigned int mask;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (isr == NULL){
		DBG("L2CACHE wrong isr.\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Get mask */
	mask = 0 |
		((options & L2CACHE_INTERRUPT_BACKENDERROR)? L2C_ERROR_IRQM_BCKEND:0) |
		((options & L2CACHE_INTERRUPT_WPROTHIT)? L2C_ERROR_IRQM_WPROT:0) |
		((options & L2CACHE_INTERRUPT_CORRERROR)? L2C_ERROR_IRQM_CORR:0) |
		((options & L2CACHE_INTERRUPT_UNCORRERROR)? L2C_ERROR_IRQM_UNCORR:0);

	/* Clear previous interrupts and mask them*/
	l2cache_reg_error_reset();
	l2cache_reg_error_irqmask(0);

	/* First time registering an ISR */
	if (priv->isr == NULL){
		/* Install and Enable L2CACHE interrupt handler */
		drvmgr_interrupt_register(priv->dev, 0, priv->devname, l2cache_isr, 
				priv);
	}

	/* Install user ISR */
	priv->isr=isr;
	priv->isr_arg=arg;

	/* Now it is safe to unmask interrupts */
	l2cache_reg_error_irqmask(mask);

	return L2CACHE_ERR_OK;
}

int l2cache_isr_unregister(void)
{
	struct l2cache_priv *priv = l2cachepriv;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (priv->isr == NULL){
		DBG("L2CACHE wrong isr.\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Clear previous interrupts and mask them*/
	l2cache_reg_error_reset();
	l2cache_reg_error_irqmask(0);

	/* Uninstall and disable L2CACHE interrupt handler */
	drvmgr_interrupt_unregister(priv->dev, 0, l2cache_isr, priv);

	/* Uninstall user ISR */
	priv->isr=NULL;
	priv->isr_arg=NULL;

	return L2CACHE_ERR_OK;
}

int l2cache_interrupt_unmask(int options)
{
	struct l2cache_priv *priv = l2cachepriv;
	unsigned int mask, irq;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	if (priv->isr == NULL){
		DBG("L2CACHE wrong isr.\n");
		return L2CACHE_ERR_EINVAL;
	}

	/* Unmask interrupts in  L2CACHE */
	mask = 0 |
		((options & L2CACHE_INTERRUPT_BACKENDERROR)? L2C_ERROR_IRQM_BCKEND:0) |
		((options & L2CACHE_INTERRUPT_WPROTHIT)? L2C_ERROR_IRQM_WPROT:0) |
		((options & L2CACHE_INTERRUPT_CORRERROR)? L2C_ERROR_IRQM_CORR:0) |
		((options & L2CACHE_INTERRUPT_UNCORRERROR)? L2C_ERROR_IRQM_UNCORR:0);

	/* Clear previous interrupts*/
	l2cache_reg_error_reset();

	/* Get previous mask */
	irq = ((l2cache_reg_error() & L2C_ERROR_IRQM) >> L2C_ERROR_IRQM_BIT);

	/* Set new mask */
	l2cache_reg_error_irqmask(irq | mask);

	return L2CACHE_ERR_OK;
}

int l2cache_interrupt_mask(int options)
{
	struct l2cache_priv *priv = l2cachepriv;
	unsigned int mask, irq;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	/* Mask interrupts in  L2CACHE */
	mask = 0 |
		((options & L2CACHE_INTERRUPT_BACKENDERROR)? L2C_ERROR_IRQM_BCKEND:0) |
		((options & L2CACHE_INTERRUPT_WPROTHIT)? L2C_ERROR_IRQM_WPROT:0) |
		((options & L2CACHE_INTERRUPT_CORRERROR)? L2C_ERROR_IRQM_CORR:0) |
		((options & L2CACHE_INTERRUPT_UNCORRERROR)? L2C_ERROR_IRQM_UNCORR:0);

	/* Clear previous interrupts */
	l2cache_reg_error_reset();

	/* Get previous mask */
	irq = ((l2cache_reg_error() & L2C_ERROR_IRQM) >> L2C_ERROR_IRQM_BIT);

	/* Set new mask */
	l2cache_reg_error_irqmask(irq & ~(mask));

	return L2CACHE_ERR_OK;
}

int l2cache_error_status(uint32_t * addr, uint32_t * status)
{
	struct l2cache_priv *priv = l2cachepriv;
	unsigned int sts;
	unsigned int erraddr;

	if (priv == NULL){
		DBG("L2CACHE not initialized.\n");
		return L2CACHE_ERR_NOINIT;
	}

	/* Get error register */
	sts = priv->regs->error_status_control;
	erraddr = priv->regs->error_addr;

	/* Check if an error occurred */
	if (sts & L2C_ERROR_VALID){
		/* Reset error register */
		l2cache_reg_error_reset();

		/* Update user variables if needed */
		if (addr != NULL){
			*addr = (erraddr & ~(0x1f));
		}

		if(status != NULL){
			*status = sts;
		}

		/* Return status */
		if (sts & L2C_ERROR_MULTI){
			return L2CACHE_STATUS_MULTIPLEERRORS;
		}else{
			return L2CACHE_STATUS_NEWERROR;
		}
	}else{
		/* Return status */
		return L2CACHE_STATUS_NOERROR;
	}
}