aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/brcm63xx/patches-4.9/340-MIPS-BCM63XX-add-pcie-support-for-BCM63268.patch
blob: 4e8a0907918d2f6f68fecd81a5379040f8788ca5 (plain)
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
From 5c290c81dbdb4433600593fe80c88eb4af86e791 Mon Sep 17 00:00:00 2001
From: Jonas Gorski <jogo@openwrt.org>
Date: Sun, 8 Dec 2013 03:22:40 +0100
Subject: [PATCH 50/53] MIPS: BCM63XX: add pcie support for BCM63268

---
 arch/mips/bcm63xx/reset.c                       | 3 ++-
 arch/mips/include/asm/mach-bcm63xx/bcm63xx_io.h | 5 +++++
 arch/mips/pci/pci-bcm63xx.c                     | 4 ++++
 3 files changed, 11 insertions(+), 1 deletion(-)

--- a/arch/mips/bcm63xx/reset.c
+++ b/arch/mips/bcm63xx/reset.c
@@ -136,7 +136,8 @@
 #define BCM63268_RESET_PCM	SOFTRESET_63268_PCM_MASK
 #define BCM63268_RESET_MPI	0
 #define BCM63268_RESET_PCIE	(SOFTRESET_63268_PCIE_MASK | \
-				 SOFTRESET_63268_PCIE_CORE_MASK)
+				 SOFTRESET_63268_PCIE_CORE_MASK | \
+				 SOFTRESET_63268_PCIE_HARD_MASK)
 #define BCM63268_RESET_PCIE_EXT	SOFTRESET_63268_PCIE_EXT_MASK
 
 /*
--- a/arch/mips/include/asm/mach-bcm63xx/bcm63xx_io.h
+++ b/arch/mips/include/asm/mach-bcm63xx/bcm63xx_io.h
@@ -45,6 +45,11 @@
 #define BCM_PCIE_MEM_END_PA_6328	(BCM_PCIE_MEM_BASE_PA_6328 +	\
 					BCM_PCIE_MEM_SIZE_6328 - 1)
 
+#define BCM_PCIE_MEM_BASE_PA_63268	0x11000000
+#define BCM_PCIE_MEM_SIZE_63268		(15 * 1024 * 1024)
+#define BCM_PCIE_MEM_END_PA_63268	(BCM_PCIE_MEM_BASE_PA_63268 +	\
+					BCM_PCIE_MEM_SIZE_63268 - 1)
+
 /*
  * Internal registers are accessed through KSEG3
  */
--- a/arch/mips/pci/pci-bcm63xx.c
+++ b/arch/mips/pci/pci-bcm63xx.c
@@ -337,11 +337,15 @@ static int __init bcm63xx_pci_init(void)
 	if (BCMCPU_IS_6328() || BCMCPU_IS_6362()) {
 		bcm_pcie_mem_resource.start = BCM_PCIE_MEM_BASE_PA_6328;
 		bcm_pcie_mem_resource.end = BCM_PCIE_MEM_END_PA_6328;
+	} else if (BCMCPU_IS_63268()) {
+		bcm_pcie_mem_resource.start = BCM_PCIE_MEM_BASE_PA_63268;
+		bcm_pcie_mem_resource.end = BCM_PCIE_MEM_END_PA_63268;
 	}
 
 	switch (bcm63xx_get_cpu_id()) {
 	case BCM6328_CPU_ID:
 	case BCM6362_CPU_ID:
+	case BCM63268_CPU_ID:
 		return bcm63xx_register_pcie();
 	case BCM3368_CPU_ID:
 	case BCM6348_CPU_ID:
href='#n277'>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
/*******************************************************************************
Copyright (C) Marvell International Ltd. and its affiliates

This software file (the "File") is owned and distributed by Marvell 
International Ltd. and/or its affiliates ("Marvell") under the following
alternative licensing terms.  Once you have made an election to distribute the
File under one of the following license alternatives, please (i) delete this
introductory statement regarding license alternatives, (ii) delete the two
license alternatives that you have not elected to use and (iii) preserve the
Marvell copyright notice above.

********************************************************************************
Marvell Commercial License Option

If you received this File from Marvell and you have entered into a commercial
license agreement (a "Commercial License") with Marvell, the File is licensed
to you under the terms of the applicable Commercial License.

********************************************************************************
Marvell GPL License Option

If you received this File from Marvell, you may opt to use, redistribute and/or 
modify this File in accordance with the terms and conditions of the General 
Public License Version 2, June 1991 (the "GPL License"), a copy of which is 
available along with the File in the license.txt file or by writing to the Free 
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or 
on the worldwide web at http://www.gnu.org/licenses/gpl.txt. 

THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED 
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY 
DISCLAIMED.  The GPL License provides additional details about this warranty 
disclaimer.
********************************************************************************
Marvell BSD License Option

If you received this File from Marvell, you may opt to use, redistribute and/or 
modify this File under the following licensing terms. 
Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    *   Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer. 

    *   Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution. 

    *   Neither the name of Marvell nor the names of its contributors may be 
        used to endorse or promote products derived from this software without 
        specific prior written permission. 
    
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
* mvEth.c - Marvell's Gigabit Ethernet controller low level driver
*
* DESCRIPTION:
*       This file introduce OS independent APIs to Marvell's Gigabit Ethernet
*       controller. This Gigabit Ethernet Controller driver API controls
*       1) Operations (i.e. port Init, Finish, Up, Down, PhyReset etc').
*       2) Data flow (i.e. port Send, Receive etc').
*       3) MAC Filtering functions (ethSetMcastAddr, ethSetRxFilterMode, etc.)
*       4) MIB counters support (ethReadMibCounter)
*       5) Debug functions (ethPortRegs, ethPortCounters, ethPortQueues, etc.)
*       Each Gigabit Ethernet port is controlled via ETH_PORT_CTRL struct.
*       This struct includes configuration information as well as driver
*       internal data needed for its operations.
*
*       Supported Features:
*       - OS independent. All required OS services are implemented via external 
*       OS dependent components (like osLayer or ethOsg)
*       - The user is free from Rx/Tx queue managing.
*       - Simple Gigabit Ethernet port operation API.
*       - Simple Gigabit Ethernet port data flow API.
*       - Data flow and operation API support per queue functionality.
*       - Support cached descriptors for better performance.
*       - PHY access and control API.
*       - Port Configuration API.
*       - Full control over Special and Other Multicast MAC tables.
*
*******************************************************************************/
/* includes */
#include "mvTypes.h"
#include "mv802_3.h"
#include "mvDebug.h"
#include "mvCommon.h"
#include "mvOs.h"
#include "ctrlEnv/mvCtrlEnvLib.h"
#include "eth-phy/mvEthPhy.h"
#include "eth/mvEth.h"
#include "eth/gbe/mvEthGbe.h"
#include "cpu/mvCpu.h"

#ifdef INCLUDE_SYNC_BARR
#include "sys/mvCpuIf.h"
#endif

#ifdef MV_RT_DEBUG
#   define ETH_DEBUG
#endif


/* locals */
MV_BOOL         ethDescInSram;
MV_BOOL         ethDescSwCoher;

/* This array holds the control structure of each port */
ETH_PORT_CTRL* ethPortCtrl[MV_ETH_MAX_PORTS];

/* Ethernet Port Local routines */

static void    ethInitRxDescRing(ETH_PORT_CTRL* pPortCtrl, int queue);

static void    ethInitTxDescRing(ETH_PORT_CTRL* pPortCtrl, int queue);

static void    ethSetUcastTable(int portNo, int queue);

static MV_BOOL ethSetUcastAddr (int ethPortNum, MV_U8 lastNibble, int queue);
static MV_BOOL ethSetSpecialMcastAddr(int ethPortNum, MV_U8 lastByte, int queue);
static MV_BOOL ethSetOtherMcastAddr(int ethPortNum, MV_U8 crc8, int queue);

static void    ethFreeDescrMemory(ETH_PORT_CTRL* pEthPortCtrl, MV_BUF_INFO* pDescBuf);
static MV_U8*  ethAllocDescrMemory(ETH_PORT_CTRL* pEthPortCtrl, int size, 
                                   MV_ULONG* pPhysAddr, MV_U32 *memHandle);

static MV_U32 mvEthMruGet(MV_U32 maxRxPktSize);

static void mvEthPortSgmiiConfig(int port);



/******************************************************************************/
/*                      EthDrv Initialization functions                       */
/******************************************************************************/

/*******************************************************************************
* mvEthHalInit - Initialize the Giga Ethernet unit
*
* DESCRIPTION:
*       This function initialize the Giga Ethernet unit.
*       1) Configure Address decode windows of the unit
*       2) Set registers to HW default values. 
*       3) Clear and Disable interrupts
*
* INPUT:  NONE
*
* RETURN: NONE
*
* NOTE: this function is called once in the boot process.
*******************************************************************************/
void    mvEthHalInit(void)
{
    int port;

    /* Init static data structures */
    for (port=0; port<MV_ETH_MAX_PORTS; port++)
    {
        ethPortCtrl[port] = NULL;
    }
    /* Power down all existing ports */
    for(port=0; port<mvCtrlEthMaxPortGet(); port++)
    {

#if defined (MV78200)
	    /* Skip ports mapped to another CPU*/
	if (MV_FALSE == mvSocUnitIsMappedToThisCpu(GIGA0+port))
	{
		    continue;
	}		
#endif

	/* Skip power down ports */
	if (MV_FALSE == mvCtrlPwrClckGet(ETH_GIG_UNIT_ID, port)) continue;

        /* Disable Giga Ethernet Unit interrupts */
        MV_REG_WRITE(ETH_UNIT_INTR_MASK_REG(port), 0);

        /* Clear ETH_UNIT_INTR_CAUSE_REG register */
        MV_REG_WRITE(ETH_UNIT_INTR_CAUSE_REG(port), 0);

    }

    mvEthMemAttrGet(&ethDescInSram, &ethDescSwCoher);

#if defined(ETH_DESCR_IN_SRAM)
    if(ethDescInSram == MV_FALSE)
    {
        mvOsPrintf("ethDrv: WARNING! Descriptors will be allocated in DRAM instead of SRAM.\n");
    }
#endif /* ETH_DESCR_IN_SRAM */
}

/*******************************************************************************
* mvEthMemAttrGet - Define properties (SRAM/DRAM, SW_COHER / HW_COHER / UNCACHED) 
*                       of of memory location for RX and TX descriptors.
*
* DESCRIPTION:
*       This function allocates memory for RX and TX descriptors.
*       - If ETH_DESCR_IN_SRAM defined, allocate from SRAM memory.
*       - If ETH_DESCR_IN_SDRAM defined, allocate from SDRAM memory.
*
* INPUT:
*   MV_BOOL* pIsSram - place of descriptors: 
*                      MV_TRUE  - in SRAM
*                      MV_FALSE - in DRAM
*   MV_BOOL* pIsSwCoher - cache coherency of descriptors:
*                      MV_TRUE  - driver is responsible for cache coherency
*                      MV_FALSE - driver is not responsible for cache coherency
*
* RETURN:
*
*******************************************************************************/
void   mvEthMemAttrGet(MV_BOOL* pIsSram, MV_BOOL* pIsSwCoher)
{
    MV_BOOL isSram, isSwCoher;

    isSram = MV_FALSE;
#if (ETHER_DRAM_COHER == MV_CACHE_COHER_SW) 
    isSwCoher = MV_TRUE;
#else 
    isSwCoher = MV_FALSE;
#endif

#if defined(ETH_DESCR_IN_SRAM)
    if( mvCtrlSramSizeGet() > 0)
    {
        isSram = MV_TRUE;
        #if (INTEG_SRAM_COHER == MV_CACHE_COHER_SW) 
            isSwCoher = MV_TRUE;
        #else 
            isSwCoher = MV_FALSE;
        #endif
    }
#endif /* ETH_DESCR_IN_SRAM */

    if(pIsSram != NULL)
        *pIsSram = isSram;

    if(pIsSwCoher != NULL)
        *pIsSwCoher = isSwCoher;
}



/******************************************************************************/
/*                      Port Initialization functions                         */
/******************************************************************************/

/*******************************************************************************
* mvEthPortInit - Initialize the Ethernet port driver
*
* DESCRIPTION:
*       This function initialize the ethernet port.
*       1) Allocate and initialize internal port Control structure.
*       2) Create RX and TX descriptor rings for default RX and TX queues
*       3) Disable RX and TX operations, clear cause registers and 
*          mask all interrupts.
*       4) Set all registers to default values and clean all MAC tables. 
*
* INPUT:
*       int             portNo          - Ethernet port number
*       ETH_PORT_INIT   *pEthPortInit   - Ethernet port init structure
*
* RETURN:
*       void* - ethernet port handler, that should be passed to the most other
*               functions dealing with this port.
*
* NOTE: This function is called once per port when loading the eth module.
*******************************************************************************/
void*   mvEthPortInit(int portNo, MV_ETH_PORT_INIT *pEthPortInit)
{
    int             queue, descSize;
    ETH_PORT_CTRL*  pPortCtrl;

    /* Check validity of parameters */
    if( (portNo >= (int)mvCtrlEthMaxPortGet()) || 
        (pEthPortInit->rxDefQ   >= MV_ETH_RX_Q_NUM)  ||
        (pEthPortInit->maxRxPktSize < 1518) )
    {
        mvOsPrintf("EthPort #%d: Bad initialization parameters\n", portNo);
        return NULL;
    }
    if( (pEthPortInit->rxDescrNum[pEthPortInit->rxDefQ]) == 0)
    {
        mvOsPrintf("EthPort #%d: rxDefQ (%d) must be created\n", 
                    portNo, pEthPortInit->rxDefQ);
        return NULL;
    }

    pPortCtrl = (ETH_PORT_CTRL*)mvOsMalloc( sizeof(ETH_PORT_CTRL) );
    if(pPortCtrl == NULL)   
    {
       mvOsPrintf("EthDrv: Can't allocate %dB for port #%d control structure!\n",
                   (int)sizeof(ETH_PORT_CTRL), portNo);
       return NULL;
    }

    memset(pPortCtrl, 0, sizeof(ETH_PORT_CTRL) );
    ethPortCtrl[portNo] = pPortCtrl;

    pPortCtrl->portState = MV_UNDEFINED_STATE;

    pPortCtrl->portNo = portNo;

    pPortCtrl->osHandle = pEthPortInit->osHandle;

    /* Copy Configuration parameters */
    pPortCtrl->portConfig.maxRxPktSize = pEthPortInit->maxRxPktSize;
    pPortCtrl->portConfig.rxDefQ = pEthPortInit->rxDefQ;
    pPortCtrl->portConfig.ejpMode = 0;

    for( queue=0; queue<MV_ETH_RX_Q_NUM; queue++ )
    {
        pPortCtrl->rxQueueConfig[queue].descrNum = pEthPortInit->rxDescrNum[queue];
    }
    for( queue=0; queue<MV_ETH_TX_Q_NUM; queue++ )
    {
        pPortCtrl->txQueueConfig[queue].descrNum = pEthPortInit->txDescrNum[queue];
    }

    mvEthPortDisable(pPortCtrl);

    /* Set the board information regarding PHY address */
    mvEthPhyAddrSet(pPortCtrl, mvBoardPhyAddrGet(portNo) );

    /* Create all requested RX queues */
    for(queue=0; queue<MV_ETH_RX_Q_NUM; queue++)
    {
        if(pPortCtrl->rxQueueConfig[queue].descrNum == 0)
            continue;

        /* Allocate memory for RX descriptors */
        descSize = ((pPortCtrl->rxQueueConfig[queue].descrNum * ETH_RX_DESC_ALIGNED_SIZE) +
                                                        CPU_D_CACHE_LINE_SIZE);
 
        pPortCtrl->rxQueue[queue].descBuf.bufVirtPtr = 
                        ethAllocDescrMemory(pPortCtrl, descSize, 
					    &pPortCtrl->rxQueue[queue].descBuf.bufPhysAddr,
					    &pPortCtrl->rxQueue[queue].descBuf.memHandle);
        pPortCtrl->rxQueue[queue].descBuf.bufSize = descSize;
        if(pPortCtrl->rxQueue[queue].descBuf.bufVirtPtr == NULL)
        {
            mvOsPrintf("EthPort #%d, rxQ=%d: Can't allocate %d bytes in %s for %d RX descr\n", 
                        pPortCtrl->portNo, queue, descSize, 
                        ethDescInSram ? "SRAM" : "DRAM",
                        pPortCtrl->rxQueueConfig[queue].descrNum);
            return NULL;
        }

        ethInitRxDescRing(pPortCtrl, queue);
    }
    /* Create TX queues */
    for(queue=0; queue<MV_ETH_TX_Q_NUM; queue++)
    {
        if(pPortCtrl->txQueueConfig[queue].descrNum == 0)
            continue;

        /* Allocate memory for TX descriptors */
        descSize = ((pPortCtrl->txQueueConfig[queue].descrNum * ETH_TX_DESC_ALIGNED_SIZE) +
                                                        CPU_D_CACHE_LINE_SIZE);
 
        pPortCtrl->txQueue[queue].descBuf.bufVirtPtr = 
		ethAllocDescrMemory(pPortCtrl, descSize,
				    &pPortCtrl->txQueue[queue].descBuf.bufPhysAddr,
				    &pPortCtrl->txQueue[queue].descBuf.memHandle);
        pPortCtrl->txQueue[queue].descBuf.bufSize = descSize;
        if(pPortCtrl->txQueue[queue].descBuf.bufVirtPtr == NULL)
        {
            mvOsPrintf("EthPort #%d, txQ=%d: Can't allocate %d bytes in %s for %d TX descr\n", 
                        pPortCtrl->portNo, queue, descSize, ethDescInSram ? "SRAM" : "DRAM",
                        pPortCtrl->txQueueConfig[queue].descrNum);
            return NULL;
        }

        ethInitTxDescRing(pPortCtrl, queue);
    }
    mvEthDefaultsSet(pPortCtrl);

    pPortCtrl->portState = MV_IDLE;
    return pPortCtrl;
}

/*******************************************************************************
* ethPortFinish - Finish the Ethernet port driver
*
* DESCRIPTION:
*       This function finish the ethernet port.
*       1) Down ethernet port if needed.
*       2) Delete RX and TX descriptor rings for all created RX and TX queues
*       3) Free internal port Control structure.
*
* INPUT:
*       void*   pEthPortHndl  - Ethernet port handler
*
* RETURN:   NONE.
*
*******************************************************************************/
void    mvEthPortFinish(void* pPortHndl)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    int             queue, portNo  = pPortCtrl->portNo;

    if(pPortCtrl->portState == MV_ACTIVE)
    {
        mvOsPrintf("ethPort #%d: Warning !!! Finish port in Active state\n",
                 portNo);
        mvEthPortDisable(pPortHndl);
    }

    /* Free all allocated RX queues */
    for(queue=0; queue<MV_ETH_RX_Q_NUM; queue++)
    {
        ethFreeDescrMemory(pPortCtrl, &pPortCtrl->rxQueue[queue].descBuf);
    }

    /* Free all allocated TX queues */
    for(queue=0; queue<MV_ETH_TX_Q_NUM; queue++)
    {
        ethFreeDescrMemory(pPortCtrl, &pPortCtrl->txQueue[queue].descBuf);
    }

    /* Free port control structure */
    mvOsFree(pPortCtrl);

    ethPortCtrl[portNo] = NULL;
}

/*******************************************************************************
* mvEthDefaultsSet - Set defaults to the ethernet port
*
* DESCRIPTION:
*       This function set default values to the ethernet port.
*       1) Clear Cause registers and Mask all interrupts
*       2) Clear all MAC tables
*       3) Set defaults to all registers
*       4) Reset all created RX and TX descriptors ring
*       5) Reset PHY
*
* INPUT:
*       void*   pEthPortHndl  - Ethernet port handler
*
* RETURN:   MV_STATUS  
*               MV_OK - Success, Others - Failure
* NOTE:
*   This function update all the port configuration except those set
*   Initialy by the OsGlue by MV_ETH_PORT_INIT.
*   This function can be called after portDown to return the port setting 
*   to defaults.
*******************************************************************************/
MV_STATUS   mvEthDefaultsSet(void* pPortHndl)
{
    int                 ethPortNo, queue;
    ETH_PORT_CTRL*      pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    ETH_QUEUE_CTRL*     pQueueCtrl;
    MV_U32              txPrio;
    MV_U32              portCfgReg, portCfgExtReg, portSerialCtrlReg, portSerialCtrl1Reg, portSdmaCfgReg;
    MV_BOARD_MAC_SPEED  boardMacCfg;

    ethPortNo = pPortCtrl->portNo;

    /* Clear Cause registers */
    MV_REG_WRITE(ETH_INTR_CAUSE_REG(ethPortNo),0);
    MV_REG_WRITE(ETH_INTR_CAUSE_EXT_REG(ethPortNo),0);

    /* Mask all interrupts */
    MV_REG_WRITE(ETH_INTR_MASK_REG(ethPortNo),0);
    MV_REG_WRITE(ETH_INTR_MASK_EXT_REG(ethPortNo),0);

    portCfgReg  =   PORT_CONFIG_VALUE;
    portCfgExtReg  =  PORT_CONFIG_EXTEND_VALUE;

    boardMacCfg = mvBoardMacSpeedGet(ethPortNo);

    if(boardMacCfg == BOARD_MAC_SPEED_100M)
    {
        portSerialCtrlReg = PORT_SERIAL_CONTROL_100MB_FORCE_VALUE;
    }
    else if(boardMacCfg == BOARD_MAC_SPEED_1000M)
    {
        portSerialCtrlReg = PORT_SERIAL_CONTROL_1000MB_FORCE_VALUE;
    }
    else
    {
        portSerialCtrlReg =  PORT_SERIAL_CONTROL_VALUE;
    }

    /* build PORT_SDMA_CONFIG_REG */
    portSdmaCfgReg = ETH_TX_INTR_COAL_MASK(0);
    portSdmaCfgReg |= ETH_TX_BURST_SIZE_MASK(ETH_BURST_SIZE_16_64BIT_VALUE);

#if ( (ETHER_DRAM_COHER == MV_CACHE_COHER_HW_WB) ||  \
      (ETHER_DRAM_COHER == MV_CACHE_COHER_HW_WT) )
    /* some devices have restricted RX burst size when using HW coherency */
    portSdmaCfgReg |= ETH_RX_BURST_SIZE_MASK(ETH_BURST_SIZE_4_64BIT_VALUE);
#else
    portSdmaCfgReg |= ETH_RX_BURST_SIZE_MASK(ETH_BURST_SIZE_16_64BIT_VALUE);
#endif

#if defined(MV_CPU_BE)
    /* big endian */
# if defined(MV_ARM)
    portSdmaCfgReg |= (ETH_RX_NO_DATA_SWAP_MASK |
                       ETH_TX_NO_DATA_SWAP_MASK |
                       ETH_DESC_SWAP_MASK);
# elif defined(MV_PPC)
    portSdmaCfgReg |= (ETH_RX_DATA_SWAP_MASK |
                       ETH_TX_DATA_SWAP_MASK |
                       ETH_NO_DESC_SWAP_MASK);
# else
# error "Giga Ethernet Swap policy is not defined for the CPU_ARCH"
# endif /* MV_ARM / MV_PPC */

#else /* MV_CPU_LE */
    /* little endian */
    portSdmaCfgReg |= (ETH_RX_NO_DATA_SWAP_MASK | 
                       ETH_TX_NO_DATA_SWAP_MASK | 
                       ETH_NO_DESC_SWAP_MASK);
#endif /* MV_CPU_BE / MV_CPU_LE */

    pPortCtrl->portRxQueueCmdReg = 0;
    pPortCtrl->portTxQueueCmdReg = 0;

#if (MV_ETH_VERSION >= 4) 
    if(pPortCtrl->portConfig.ejpMode == MV_TRUE)
    {
        MV_REG_WRITE(ETH_TXQ_CMD_1_REG(ethPortNo), ETH_TX_EJP_ENABLE_MASK);
    }
    else
    {
        MV_REG_WRITE(ETH_TXQ_CMD_1_REG(ethPortNo), 0)
    }
#endif /* (MV_ETH_VERSION >= 4) */

    ethSetUcastTable(ethPortNo, -1);
    mvEthSetSpecialMcastTable(ethPortNo, -1);
    mvEthSetOtherMcastTable(ethPortNo, -1);

    portSerialCtrlReg &= ~ETH_MAX_RX_PACKET_SIZE_MASK;

    portSerialCtrlReg |= mvEthMruGet(pPortCtrl->portConfig.maxRxPktSize);

    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(ethPortNo), portSerialCtrlReg);

    /* Update value of PortConfig register accordingly with all RxQueue types */
    pPortCtrl->portConfig.rxArpQ = pPortCtrl->portConfig.rxDefQ;
    pPortCtrl->portConfig.rxBpduQ = pPortCtrl->portConfig.rxDefQ; 
    pPortCtrl->portConfig.rxTcpQ = pPortCtrl->portConfig.rxDefQ; 
    pPortCtrl->portConfig.rxUdpQ = pPortCtrl->portConfig.rxDefQ; 

    portCfgReg &= ~ETH_DEF_RX_QUEUE_ALL_MASK;
    portCfgReg |= ETH_DEF_RX_QUEUE_MASK(pPortCtrl->portConfig.rxDefQ);
    
    portCfgReg &= ~ETH_DEF_RX_ARP_QUEUE_ALL_MASK;
    portCfgReg |= ETH_DEF_RX_ARP_QUEUE_MASK(pPortCtrl->portConfig.rxArpQ); 

    portCfgReg &= ~ETH_DEF_RX_BPDU_QUEUE_ALL_MASK;
    portCfgReg |= ETH_DEF_RX_BPDU_QUEUE_MASK(pPortCtrl->portConfig.rxBpduQ);

    portCfgReg &= ~ETH_DEF_RX_TCP_QUEUE_ALL_MASK;
    portCfgReg |= ETH_DEF_RX_TCP_QUEUE_MASK(pPortCtrl->portConfig.rxTcpQ);

    portCfgReg &= ~ETH_DEF_RX_UDP_QUEUE_ALL_MASK;
    portCfgReg |= ETH_DEF_RX_UDP_QUEUE_MASK(pPortCtrl->portConfig.rxUdpQ);

    /* Assignment of Tx CTRP of given queue */
    txPrio = 0;

    for(queue=0; queue<MV_ETH_TX_Q_NUM; queue++)
    {
        pQueueCtrl = &pPortCtrl->txQueue[queue];

        if(pQueueCtrl->pFirstDescr != NULL)
        {
            ethResetTxDescRing(pPortCtrl, queue);

            MV_REG_WRITE(ETH_TXQ_TOKEN_COUNT_REG(ethPortNo, queue),
                         0x3fffffff);
            MV_REG_WRITE(ETH_TXQ_TOKEN_CFG_REG(ethPortNo, queue), 
                         0x03ffffff);
        }
        else
        {
            MV_REG_WRITE(ETH_TXQ_TOKEN_COUNT_REG(ethPortNo, queue),  0x0);
            MV_REG_WRITE(ETH_TXQ_TOKEN_CFG_REG(ethPortNo, queue), 0x0);
        }
    }

    /* Assignment of Rx CRDP of given queue */
    for(queue=0; queue<MV_ETH_RX_Q_NUM; queue++)
    {
        ethResetRxDescRing(pPortCtrl, queue);
    }

    /* Allow receiving packes with odd number of preamble nibbles */
    portSerialCtrl1Reg = MV_REG_READ(ETH_PORT_SERIAL_CTRL_1_REG(ethPortNo));
    portSerialCtrl1Reg |= ETH_EN_MII_ODD_PRE_MASK;
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_1_REG(ethPortNo), portSerialCtrl1Reg);

    /* Assign port configuration and command. */
    MV_REG_WRITE(ETH_PORT_CONFIG_REG(ethPortNo), portCfgReg);

    MV_REG_WRITE(ETH_PORT_CONFIG_EXTEND_REG(ethPortNo), portCfgExtReg);

    /* Assign port SDMA configuration */
    MV_REG_WRITE(ETH_SDMA_CONFIG_REG(ethPortNo), portSdmaCfgReg);
    
    /* Turn off the port/queue bandwidth limitation */
    MV_REG_WRITE(ETH_MAX_TRANSMIT_UNIT_REG(ethPortNo), 0x0);

    return MV_OK;
}

/*******************************************************************************
* ethPortUp - Start the Ethernet port RX and TX activity.
*
* DESCRIPTION:
*       This routine start Rx and Tx activity:
*
*       Note: Each Rx and Tx queue descriptor's list must be initialized prior
*       to calling this function (use etherInitTxDescRing for Tx queues and
*       etherInitRxDescRing for Rx queues).
*
* INPUT:
*       void*   pEthPortHndl  - Ethernet port handler
*
* RETURN:   MV_STATUS
*           MV_OK - Success, Others - Failure.
*
* NOTE : used for port link up.
*******************************************************************************/
MV_STATUS   mvEthPortUp(void* pEthPortHndl)
{
    int             ethPortNo;
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;

    ethPortNo = pPortCtrl->portNo;
    
    if( (pPortCtrl->portState != MV_ACTIVE) && 
        (pPortCtrl->portState != MV_PAUSED) )
    {
        mvOsPrintf("ethDrv port%d: Unexpected port state %d\n", 
                        ethPortNo, pPortCtrl->portState);
        return MV_BAD_STATE;
    }
    
    ethPortNo = pPortCtrl->portNo;

    /* Enable port RX. */
    MV_REG_WRITE(ETH_RX_QUEUE_COMMAND_REG(ethPortNo), pPortCtrl->portRxQueueCmdReg);

    /* Enable port TX. */
    MV_REG_VALUE(ETH_TX_QUEUE_COMMAND_REG(ethPortNo)) = pPortCtrl->portTxQueueCmdReg;

    pPortCtrl->portState = MV_ACTIVE;

    return MV_OK;
}

/*******************************************************************************
* ethPortDown - Stop the Ethernet port activity.
*
* DESCRIPTION:
*
* INPUT:
*       void*   pEthPortHndl  - Ethernet port handler
*
* RETURN:   MV_STATUS
*               MV_OK - Success, Others - Failure.
*
* NOTE : used for port link down.
*******************************************************************************/
MV_STATUS   mvEthPortDown(void* pEthPortHndl)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;
    int             ethPortNum = pPortCtrl->portNo;
    unsigned int    regData;
    volatile int    uDelay, mDelay;

    /* Stop Rx port activity. Check port Rx activity. */
    regData = (MV_REG_READ(ETH_RX_QUEUE_COMMAND_REG(ethPortNum))) & ETH_RXQ_ENABLE_MASK;
    if(regData != 0)
    {
        /* Issue stop command for active channels only */
        MV_REG_WRITE(ETH_RX_QUEUE_COMMAND_REG(ethPortNum), (regData << ETH_RXQ_DISABLE_OFFSET));
    }

    /* Stop Tx port activity. Check port Tx activity. */
    regData = (MV_REG_READ(ETH_TX_QUEUE_COMMAND_REG(ethPortNum))) & ETH_TXQ_ENABLE_MASK;
    if(regData != 0)
    {
        /* Issue stop command for active channels only */
        MV_REG_WRITE(ETH_TX_QUEUE_COMMAND_REG(ethPortNum), 
                            (regData << ETH_TXQ_DISABLE_OFFSET) ); 
    }

    /* Force link down */
/*
    regData = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(ethPortNum));
    regData &= ~(ETH_DO_NOT_FORCE_LINK_FAIL_MASK);
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(ethPortNum), regData);
*/
    /* Wait for all Rx activity to terminate. */
    mDelay = 0;
    do
    {
        if(mDelay >= RX_DISABLE_TIMEOUT_MSEC)
        {
            mvOsPrintf("ethPort_%d: TIMEOUT for RX stopped !!! rxQueueCmd - 0x08%x\n", 
                        ethPortNum, regData);
            break;
        }
        mvOsDelay(1);
        mDelay++;
        
        /* Check port RX Command register that all Rx queues are stopped */
        regData = MV_REG_READ(ETH_RX_QUEUE_COMMAND_REG(ethPortNum));
    }
    while(regData & 0xFF);

    /* Wait for all Tx activity to terminate. */
    mDelay = 0;
    do
    {
        if(mDelay >= TX_DISABLE_TIMEOUT_MSEC)
        {
            mvOsPrintf("ethPort_%d: TIMEOUT for TX stoped !!! txQueueCmd - 0x08%x\n", 
                        ethPortNum, regData);
            break;
        }
        mvOsDelay(1);
        mDelay++;

        /* Check port TX Command register that all Tx queues are stopped */
        regData = MV_REG_READ(ETH_TX_QUEUE_COMMAND_REG(ethPortNum));
    }
    while(regData & 0xFF);
    
    /* Double check to Verify that TX FIFO is Empty */
    mDelay = 0;
    while(MV_TRUE)
    {
        do
        {
            if(mDelay >= TX_FIFO_EMPTY_TIMEOUT_MSEC)
            {
                mvOsPrintf("\n ethPort_%d: TIMEOUT for TX FIFO empty !!! portStatus - 0x08%x\n", 
                            ethPortNum, regData);
                break;
            }
            mvOsDelay(1);
            mDelay++;

            regData = MV_REG_READ(ETH_PORT_STATUS_REG(ethPortNum));
        }
        while( ((regData & ETH_TX_FIFO_EMPTY_MASK) == 0) || 
               ((regData & ETH_TX_IN_PROGRESS_MASK) != 0) );

        if(mDelay >= TX_FIFO_EMPTY_TIMEOUT_MSEC)
            break;

        /* Double check */
        regData = MV_REG_READ(ETH_PORT_STATUS_REG(ethPortNum));
        if( ((regData & ETH_TX_FIFO_EMPTY_MASK) != 0) && 
            ((regData & ETH_TX_IN_PROGRESS_MASK) == 0) )
        {
            break;
        }
        else
            mvOsPrintf("ethPort_%d: TX FIFO Empty double check failed. %d msec, portStatus=0x%x\n",
                                ethPortNum, mDelay, regData);     
    }

    /* Do NOT force link down */
/*
    regData = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(ethPortNum));
    regData |= (ETH_DO_NOT_FORCE_LINK_FAIL_MASK);
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(ethPortNum), regData);
*/
    /* Wait about 2500 tclk cycles */
    uDelay = (PORT_DISABLE_WAIT_TCLOCKS/(mvBoardTclkGet()/1000000));
    mvOsUDelay(uDelay);

    pPortCtrl->portState = MV_PAUSED;

    return MV_OK;   
}


/*******************************************************************************
* ethPortEnable - Enable the Ethernet port and Start RX and TX.
*
* DESCRIPTION:
*       This routine enable the Ethernet port and Rx and Tx activity:
*
*       Note: Each Rx and Tx queue descriptor's list must be initialized prior
*       to calling this function (use etherInitTxDescRing for Tx queues and
*       etherInitRxDescRing for Rx queues).
*
* INPUT:
*       void*   pEthPortHndl  - Ethernet port handler
*
* RETURN:   MV_STATUS
*               MV_OK - Success, Others - Failure.
*
* NOTE: main usage is to enable the port after ifconfig up.
*******************************************************************************/
MV_STATUS   mvEthPortEnable(void* pEthPortHndl)
{
    int             ethPortNo;
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;
    MV_U32      portSerialCtrlReg;

    ethPortNo = pPortCtrl->portNo;

    /* Enable port */
    portSerialCtrlReg = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(ethPortNo));
    portSerialCtrlReg |= (ETH_DO_NOT_FORCE_LINK_FAIL_MASK | ETH_PORT_ENABLE_MASK);

    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(ethPortNo), portSerialCtrlReg);

    mvEthMibCountersClear(pEthPortHndl);

    pPortCtrl->portState = MV_PAUSED;

    /* If Link is UP, Start RX and TX traffic */
    if( MV_REG_READ( ETH_PORT_STATUS_REG(ethPortNo) ) & ETH_LINK_UP_MASK)
        return( mvEthPortUp(pEthPortHndl) );
    
    return MV_NOT_READY;
}


/*******************************************************************************
* mvEthPortDisable - Stop RX and TX activities and Disable the Ethernet port.
*
* DESCRIPTION:
*
* INPUT:
*       void*   pEthPortHndl  - Ethernet port handler
*
* RETURN:   MV_STATUS
*               MV_OK - Success, Others - Failure.
*
* NOTE: main usage is to disable the port after ifconfig down.
*******************************************************************************/
MV_STATUS   mvEthPortDisable(void* pEthPortHndl)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;
    int             ethPortNum = pPortCtrl->portNo;
    unsigned int    regData;
    volatile int    mvDelay;

    if(pPortCtrl->portState == MV_ACTIVE)           
    {    
        /* Stop RX and TX activities */
        mvEthPortDown(pEthPortHndl);
    }

    /* Reset the Enable bit in the Serial Control Register */
    regData = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(ethPortNum));
    regData &= ~(ETH_PORT_ENABLE_MASK);
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(ethPortNum), regData);

    /* Wait about 2500 tclk cycles */
    mvDelay = (PORT_DISABLE_WAIT_TCLOCKS*(mvCpuPclkGet()/mvBoardTclkGet()));
    for(mvDelay; mvDelay>0; mvDelay--);

    pPortCtrl->portState = MV_IDLE;
    return MV_OK;
}

/*******************************************************************************
* mvEthPortForceTxDone - Get next buffer from TX queue in spite of buffer ownership.
*
* DESCRIPTION:
*       This routine used to free buffers attached to the Tx ring and should
*       be called only when Giga Ethernet port is Down
*
* INPUT:
*       void*       pEthPortHndl    - Ethernet Port handler.
*       int         txQueue         - Number of TX queue.
*
* OUTPUT:
*       MV_PKT_INFO *pPktInfo       - Pointer to packet was sent.
*
* RETURN:
*       MV_EMPTY    - There is no more buffers in this queue.
*       MV_OK       - Buffer detached from the queue and pPktInfo structure 
*                   filled with relevant information.
*
*******************************************************************************/
MV_PKT_INFO*    mvEthPortForceTxDone(void* pEthPortHndl, int txQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;
    ETH_QUEUE_CTRL* pQueueCtrl;
    MV_PKT_INFO*    pPktInfo;
    ETH_TX_DESC*    pTxDesc;
    int             port = pPortCtrl->portNo;
       
    pQueueCtrl = &pPortCtrl->txQueue[txQueue];

    while( (pQueueCtrl->pUsedDescr != pQueueCtrl->pCurrentDescr) ||
           (pQueueCtrl->resource == 0) )
    {
        /* Free next descriptor */
        pQueueCtrl->resource++;
        pTxDesc = (ETH_TX_DESC*)pQueueCtrl->pUsedDescr;

	/* pPktInfo is available only in descriptors which are last descriptors */
        pPktInfo = (MV_PKT_INFO*)pTxDesc->returnInfo;
	if (pPktInfo)
        	pPktInfo->status = pTxDesc->cmdSts;

        pTxDesc->cmdSts = 0x0;
        pTxDesc->returnInfo = 0x0;
        ETH_DESCR_FLUSH_INV(pPortCtrl, pTxDesc);

        pQueueCtrl->pUsedDescr = TX_NEXT_DESC_PTR(pTxDesc, pQueueCtrl);

        if (pPktInfo)
		if (pPktInfo->status  & ETH_TX_LAST_DESC_MASK) 
            		return pPktInfo;
    }   
    MV_REG_WRITE( ETH_TX_CUR_DESC_PTR_REG(port, txQueue), 
                    (MV_U32)ethDescVirtToPhy(pQueueCtrl, pQueueCtrl->pCurrentDescr) );
    return NULL;
}

        

/*******************************************************************************
* mvEthPortForceRx - Get next buffer from RX queue in spite of buffer ownership.
*
* DESCRIPTION:
*       This routine used to free buffers attached to the Rx ring and should
*       be called only when Giga Ethernet port is Down
*
* INPUT:
*       void*       pEthPortHndl    - Ethernet Port handler.
*       int         rxQueue         - Number of Rx queue.
*
* OUTPUT:
*       MV_PKT_INFO *pPktInfo       - Pointer to received packet.
*
* RETURN:
*       MV_EMPTY    - There is no more buffers in this queue.
*       MV_OK       - Buffer detached from the queue and pBufInfo structure 
*                   filled with relevant information.
*
*******************************************************************************/
MV_PKT_INFO*    mvEthPortForceRx(void* pEthPortHndl, int rxQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;
    ETH_QUEUE_CTRL* pQueueCtrl;
    ETH_RX_DESC*    pRxDesc;
    MV_PKT_INFO*    pPktInfo;
    int             port = pPortCtrl->portNo;
       
    pQueueCtrl = &pPortCtrl->rxQueue[rxQueue];

    if(pQueueCtrl->resource == 0)
    {
        MV_REG_WRITE( ETH_RX_CUR_DESC_PTR_REG(port, rxQueue), 
                    (MV_U32)ethDescVirtToPhy(pQueueCtrl, pQueueCtrl->pCurrentDescr) );

        return NULL;
    }
    /* Free next descriptor */
    pQueueCtrl->resource--;
    pRxDesc = (ETH_RX_DESC*)pQueueCtrl->pCurrentDescr;
    pPktInfo = (MV_PKT_INFO*)pRxDesc->returnInfo;

    pPktInfo->status  = pRxDesc->cmdSts;
    pRxDesc->cmdSts = 0x0;
    pRxDesc->returnInfo = 0x0;
    ETH_DESCR_FLUSH_INV(pPortCtrl, pRxDesc);

    pQueueCtrl->pCurrentDescr = RX_NEXT_DESC_PTR(pRxDesc, pQueueCtrl);
    return pPktInfo;    
}


/******************************************************************************/
/*                          Port Configuration functions                      */
/******************************************************************************/
/*******************************************************************************
* mvEthMruGet - Get MRU configuration for Max Rx packet size.
*
* INPUT:
*           MV_U32 maxRxPktSize - max  packet size.
*
* RETURN:   MV_U32 - MRU configuration.
*
*******************************************************************************/
static MV_U32 mvEthMruGet(MV_U32 maxRxPktSize)
{
    MV_U32 portSerialCtrlReg = 0;

    if(maxRxPktSize > 9192)
        portSerialCtrlReg |= ETH_MAX_RX_PACKET_9700BYTE;
    else if(maxRxPktSize > 9022)
        portSerialCtrlReg |= ETH_MAX_RX_PACKET_9192BYTE;
    else if(maxRxPktSize > 1552)
        portSerialCtrlReg |= ETH_MAX_RX_PACKET_9022BYTE;
    else if(maxRxPktSize > 1522)
        portSerialCtrlReg |= ETH_MAX_RX_PACKET_1552BYTE;
    else if(maxRxPktSize > 1518)
        portSerialCtrlReg |= ETH_MAX_RX_PACKET_1522BYTE;
    else
        portSerialCtrlReg |= ETH_MAX_RX_PACKET_1518BYTE;

    return portSerialCtrlReg;
}

/*******************************************************************************
* mvEthRxCoalSet  - Sets coalescing interrupt mechanism on RX path
*
* DESCRIPTION:
*       This routine sets the RX coalescing interrupt mechanism parameter.
*       This parameter is a timeout counter, that counts in 64 tClk
*       chunks, that when timeout event occurs a maskable interrupt occurs.
*       The parameter is calculated using the tCLK frequency of the
*       MV-64xxx chip, and the required number is in micro seconds.
*
* INPUT:
*       void*           pPortHndl   - Ethernet Port handler.
*       MV_U32          uSec        - Number of micro seconds between 
*                                   RX interrupts
*
* RETURN:
*       None.
*
* COMMENT:     
*   1 sec           - TCLK_RATE clocks
*   1 uSec          - TCLK_RATE / 1,000,000 clocks
*
*   Register Value for N micro seconds -  ((N * ( (TCLK_RATE / 1,000,000)) / 64)
*
* RETURN:
*       None.
*
*******************************************************************************/
MV_U32    mvEthRxCoalSet (void* pPortHndl, MV_U32 uSec) 
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    MV_U32          coal = ((uSec * (mvBoardTclkGet() / 1000000)) / 64);
    MV_U32          portSdmaCfgReg;

    portSdmaCfgReg =  MV_REG_READ(ETH_SDMA_CONFIG_REG(pPortCtrl->portNo));
    portSdmaCfgReg &= ~ETH_RX_INTR_COAL_ALL_MASK;

    portSdmaCfgReg |= ETH_RX_INTR_COAL_MASK(coal);

#if (MV_ETH_VERSION >= 2)
    /* Set additional bit if needed ETH_RX_INTR_COAL_MSB_BIT (25) */
    if(ETH_RX_INTR_COAL_MASK(coal) > ETH_RX_INTR_COAL_ALL_MASK)
        portSdmaCfgReg |= ETH_RX_INTR_COAL_MSB_MASK;
#endif /* MV_ETH_VERSION >= 2 */

    MV_REG_WRITE (ETH_SDMA_CONFIG_REG(pPortCtrl->portNo), portSdmaCfgReg);
    return coal;
}

/*******************************************************************************
* mvEthTxCoalSet - Sets coalescing interrupt mechanism on TX path
*
* DESCRIPTION:
*       This routine sets the TX coalescing interrupt mechanism parameter.
*       This parameter is a timeout counter, that counts in 64 tClk
*       chunks, that when timeout event occurs a maskable interrupt
*       occurs.
*       The parameter is calculated using the tCLK frequency of the
*       MV-64xxx chip, and the required number is in micro seconds.
*
* INPUT:
*       void*           pPortHndl    - Ethernet Port handler.
*       MV_U32          uSec        - Number of micro seconds between 
*                                   RX interrupts
*
* RETURN:
*       None.
*
* COMMENT:     
*   1 sec           - TCLK_RATE clocks
*   1 uSec          - TCLK_RATE / 1,000,000 clocks
*
*   Register Value for N micro seconds -  ((N * ( (TCLK_RATE / 1,000,000)) / 64)
*
*******************************************************************************/
MV_U32    mvEthTxCoalSet(void* pPortHndl, MV_U32 uSec) 
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    MV_U32          coal = ((uSec * (mvBoardTclkGet() / 1000000)) / 64);
    MV_U32          regVal;

    regVal = MV_REG_READ(ETH_TX_FIFO_URGENT_THRESH_REG(pPortCtrl->portNo));
    regVal &= ~ETH_TX_INTR_COAL_ALL_MASK;
    regVal |= ETH_TX_INTR_COAL_MASK(coal);

    /* Set TX Coalescing mechanism */
    MV_REG_WRITE (ETH_TX_FIFO_URGENT_THRESH_REG(pPortCtrl->portNo), regVal); 
    return coal;
}

/*******************************************************************************
* mvEthCoalGet - Gets RX and TX coalescing values in micro seconds
*
* DESCRIPTION:
*       This routine gets the RX and TX coalescing interrupt values.
*       The parameter is calculated using the tCLK frequency of the
*       MV-64xxx chip, and the returned numbers are in micro seconds.
*
* INPUTs:
*       void*   pPortHndl   - Ethernet Port handler.
*
* OUTPUTs:
*       MV_U32* pRxCoal     - Number of micro seconds between RX interrupts
*       MV_U32* pTxCoal     - Number of micro seconds between TX interrupts
*
* RETURN:
*       MV_STATUS   MV_OK  - success
*                   Others - failure.
*
* COMMENT:     
*   1 sec           - TCLK_RATE clocks
*   1 uSec          - TCLK_RATE / 1,000,000 clocks
*
*   Register Value for N micro seconds -  ((N * ( (TCLK_RATE / 1,000,000)) / 64)
*
*******************************************************************************/
MV_STATUS   mvEthCoalGet(void* pPortHndl, MV_U32* pRxCoal, MV_U32* pTxCoal)
{
    MV_U32  regVal, coal, usec;

    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;

    /* get TX Coalescing */
    regVal = MV_REG_READ (ETH_TX_FIFO_URGENT_THRESH_REG(pPortCtrl->portNo));
    coal = ((regVal & ETH_TX_INTR_COAL_ALL_MASK) >> ETH_TX_INTR_COAL_OFFSET);

    usec = (coal * 64) / (mvBoardTclkGet() / 1000000);
    if(pTxCoal != NULL)
        *pTxCoal = usec;

    /* Get RX Coalescing */
    regVal =  MV_REG_READ(ETH_SDMA_CONFIG_REG(pPortCtrl->portNo));
    coal = ((regVal & ETH_RX_INTR_COAL_ALL_MASK) >> ETH_RX_INTR_COAL_OFFSET);

#if (MV_ETH_VERSION >= 2)
    if(regVal & ETH_RX_INTR_COAL_MSB_MASK)
    {
        /* Add MSB */
        coal |= (ETH_RX_INTR_COAL_ALL_MASK + 1);
    }
#endif /* MV_ETH_VERSION >= 2 */

    usec = (coal * 64) / (mvBoardTclkGet() / 1000000);
    if(pRxCoal != NULL)
        *pRxCoal = usec;

    return MV_OK;
}

/*******************************************************************************
* mvEthMaxRxSizeSet - 
*
* DESCRIPTION:
*       Change maximum receive size of the port. This configuration will take place 
*       after next call of ethPortSetDefaults() function.
*
* INPUT:
*
* RETURN:
*******************************************************************************/
MV_STATUS   mvEthMaxRxSizeSet(void* pPortHndl, int maxRxSize)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    MV_U32      portSerialCtrlReg;

    if((maxRxSize < 1518) || (maxRxSize & ~ETH_RX_BUFFER_MASK))
       return MV_BAD_PARAM;
    
    pPortCtrl->portConfig.maxRxPktSize = maxRxSize;

    portSerialCtrlReg =  MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(pPortCtrl->portNo));
    portSerialCtrlReg &= ~ETH_MAX_RX_PACKET_SIZE_MASK;
    portSerialCtrlReg |= mvEthMruGet(pPortCtrl->portConfig.maxRxPktSize);
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(pPortCtrl->portNo), portSerialCtrlReg);

    return MV_OK;
}


/******************************************************************************/
/*                      MAC Filtering functions                               */
/******************************************************************************/

/*******************************************************************************
* mvEthRxFilterModeSet - Configure Fitering mode of Ethernet port
*
* DESCRIPTION:
*       This routine used to free buffers attached to the Rx ring and should
*       be called only when Giga Ethernet port is Down
*
* INPUT:
*       void*       pEthPortHndl    - Ethernet Port handler.
*       MV_BOOL     isPromisc       - Promiscous mode
*                                   MV_TRUE  - accept all Broadcast, Multicast 
*                                              and Unicast packets
*                                   MV_FALSE - accept all Broadcast, 
*                                              specially added Multicast and
*                                              single Unicast packets
*
* RETURN:   MV_STATUS   MV_OK - Success, Other - Failure
*
*******************************************************************************/
MV_STATUS   mvEthRxFilterModeSet(void* pEthPortHndl, MV_BOOL isPromisc)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pEthPortHndl;
    int             queue;
    MV_U32      portCfgReg;

    portCfgReg = MV_REG_READ(ETH_PORT_CONFIG_REG(pPortCtrl->portNo));
    /* Set / Clear UPM bit in port configuration register */
    if(isPromisc)
    {
        /* Accept all multicast packets to RX default queue */
        queue = pPortCtrl->portConfig.rxDefQ;
        portCfgReg |= ETH_UNICAST_PROMISCUOUS_MODE_MASK;
        memset(pPortCtrl->mcastCount, 1, sizeof(pPortCtrl->mcastCount));
        MV_REG_WRITE(ETH_MAC_ADDR_LOW_REG(pPortCtrl->portNo),0xFFFF);
        MV_REG_WRITE(ETH_MAC_ADDR_HIGH_REG(pPortCtrl->portNo),0xFFFFFFFF);
    }
    else
    {
        /* Reject all Multicast addresses */
        queue = -1;
        portCfgReg &= ~ETH_UNICAST_PROMISCUOUS_MODE_MASK;
        /* Clear all mcastCount */
        memset(pPortCtrl->mcastCount, 0, sizeof(pPortCtrl->mcastCount));
    }
    MV_REG_WRITE(ETH_PORT_CONFIG_REG(pPortCtrl->portNo), portCfgReg);        

    /* Set Special Multicast and Other Multicast tables */
    mvEthSetSpecialMcastTable(pPortCtrl->portNo, queue);
    mvEthSetOtherMcastTable(pPortCtrl->portNo, queue);
    ethSetUcastTable(pPortCtrl->portNo, queue);

    return MV_OK;
}

/*******************************************************************************
* mvEthMacAddrSet - This function Set the port Unicast address.
*
* DESCRIPTION:
*       This function Set the port Ethernet MAC address. This address
*       will be used to send Pause frames if enabled. Packets with this
*       address will be accepted and dispatched to default RX queue
*
* INPUT:
*       void*   pEthPortHndl    - Ethernet port handler.
*       char*   pAddr           - Address to be set
*
* RETURN:   MV_STATUS
*               MV_OK - Success,  Other - Faulure
*
*******************************************************************************/
MV_STATUS   mvEthMacAddrSet(void* pPortHndl, unsigned char *pAddr, int queue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    unsigned int    macH;
    unsigned int    macL;

    if(queue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethDrv: RX queue #%d is out of range\n", queue);
        return MV_BAD_PARAM;
    }

    if(queue != -1)
    {
        macL =  (pAddr[4] << 8) | (pAddr[5]);
        macH =  (pAddr[0] << 24)| (pAddr[1] << 16) |
                (pAddr[2] << 8) | (pAddr[3] << 0);

        MV_REG_WRITE(ETH_MAC_ADDR_LOW_REG(pPortCtrl->portNo),  macL);
        MV_REG_WRITE(ETH_MAC_ADDR_HIGH_REG(pPortCtrl->portNo), macH);
    }

    /* Accept frames of this address */
    ethSetUcastAddr(pPortCtrl->portNo, pAddr[5], queue);

    return MV_OK;
}

/*******************************************************************************
* mvEthMacAddrGet - This function returns the port Unicast address.
*
* DESCRIPTION:
*       This function returns the port Ethernet MAC address.
*
* INPUT:
*       int     portNo          - Ethernet port number.
*       char*   pAddr           - Pointer where address will be written to
*
* RETURN:   MV_STATUS
*               MV_OK - Success,  Other - Faulure
*
*******************************************************************************/
MV_STATUS   mvEthMacAddrGet(int portNo, unsigned char *pAddr)
{
    unsigned int    macH;
    unsigned int    macL;

    if(pAddr == NULL)
    {
        mvOsPrintf("mvEthMacAddrGet: NULL pointer.\n");
        return MV_BAD_PARAM;
    }

    macH = MV_REG_READ(ETH_MAC_ADDR_HIGH_REG(portNo));
    macL = MV_REG_READ(ETH_MAC_ADDR_LOW_REG(portNo));
    pAddr[0] = (macH >> 24) & 0xff;
    pAddr[1] = (macH >> 16) & 0xff;
    pAddr[2] = (macH >> 8) & 0xff;
    pAddr[3] = macH  & 0xff;
    pAddr[4] = (macL >> 8) & 0xff;
    pAddr[5] = macL  & 0xff;

    return MV_OK;
}

/*******************************************************************************
* mvEthMcastCrc8Get - Calculate CRC8 of MAC address.
*
* DESCRIPTION:
*
* INPUT:
*       MV_U8*  pAddr           - Address to calculate CRC-8
*
* RETURN: MV_U8 - CRC-8 of this MAC address
*
*******************************************************************************/
MV_U8   mvEthMcastCrc8Get(MV_U8* pAddr)
{
    unsigned int    macH;
    unsigned int    macL;
    int             macArray[48];
    int             crc[8];
    int             i;
    unsigned char   crcResult = 0;

        /* Calculate CRC-8 out of the given address */
    macH =  (pAddr[0] << 8) | (pAddr[1]);
    macL =  (pAddr[2] << 24)| (pAddr[3] << 16) |
            (pAddr[4] << 8) | (pAddr[5] << 0);

    for(i=0; i<32; i++)
        macArray[i] = (macL >> i) & 0x1;

    for(i=32; i<48; i++)
        macArray[i] = (macH >> (i - 32)) & 0x1;

    crc[0] = macArray[45] ^ macArray[43] ^ macArray[40] ^ macArray[39] ^
             macArray[35] ^ macArray[34] ^ macArray[31] ^ macArray[30] ^
             macArray[28] ^ macArray[23] ^ macArray[21] ^ macArray[19] ^
             macArray[18] ^ macArray[16] ^ macArray[14] ^ macArray[12] ^
             macArray[8]  ^ macArray[7]  ^ macArray[6]  ^ macArray[0];

    crc[1] = macArray[46] ^ macArray[45] ^ macArray[44] ^ macArray[43] ^
             macArray[41] ^ macArray[39] ^ macArray[36] ^ macArray[34] ^
             macArray[32] ^ macArray[30] ^ macArray[29] ^ macArray[28] ^
             macArray[24] ^ macArray[23] ^ macArray[22] ^ macArray[21] ^
             macArray[20] ^ macArray[18] ^ macArray[17] ^ macArray[16] ^
             macArray[15] ^ macArray[14] ^ macArray[13] ^ macArray[12] ^
             macArray[9]  ^ macArray[6]  ^ macArray[1]  ^ macArray[0];

    crc[2] = macArray[47] ^ macArray[46] ^ macArray[44] ^ macArray[43] ^
             macArray[42] ^ macArray[39] ^ macArray[37] ^ macArray[34] ^
             macArray[33] ^ macArray[29] ^ macArray[28] ^ macArray[25] ^
             macArray[24] ^ macArray[22] ^ macArray[17] ^ macArray[15] ^
             macArray[13] ^ macArray[12] ^ macArray[10] ^ macArray[8]  ^
             macArray[6]  ^ macArray[2]  ^ macArray[1]  ^ macArray[0];

    crc[3] = macArray[47] ^ macArray[45] ^ macArray[44] ^ macArray[43] ^
             macArray[40] ^ macArray[38] ^ macArray[35] ^ macArray[34] ^
             macArray[30] ^ macArray[29] ^ macArray[26] ^ macArray[25] ^
             macArray[23] ^ macArray[18] ^ macArray[16] ^ macArray[14] ^
             macArray[13] ^ macArray[11] ^ macArray[9]  ^ macArray[7]  ^
             macArray[3]  ^ macArray[2]  ^ macArray[1];

    crc[4] = macArray[46] ^ macArray[45] ^ macArray[44] ^ macArray[41] ^
             macArray[39] ^ macArray[36] ^ macArray[35] ^ macArray[31] ^
             macArray[30] ^ macArray[27] ^ macArray[26] ^ macArray[24] ^
             macArray[19] ^ macArray[17] ^ macArray[15] ^ macArray[14] ^
             macArray[12] ^ macArray[10] ^ macArray[8]  ^ macArray[4]  ^
             macArray[3]  ^ macArray[2];

    crc[5] = macArray[47] ^ macArray[46] ^ macArray[45] ^ macArray[42] ^
             macArray[40] ^ macArray[37] ^ macArray[36] ^ macArray[32] ^
             macArray[31] ^ macArray[28] ^ macArray[27] ^ macArray[25] ^
             macArray[20] ^ macArray[18] ^ macArray[16] ^ macArray[15] ^
             macArray[13] ^ macArray[11] ^ macArray[9]  ^ macArray[5]  ^
             macArray[4]  ^ macArray[3];

    crc[6] = macArray[47] ^ macArray[46] ^ macArray[43] ^ macArray[41] ^
             macArray[38] ^ macArray[37] ^ macArray[33] ^ macArray[32] ^
             macArray[29] ^ macArray[28] ^ macArray[26] ^ macArray[21] ^
             macArray[19] ^ macArray[17] ^ macArray[16] ^ macArray[14] ^
             macArray[12] ^ macArray[10] ^ macArray[6]  ^ macArray[5]  ^
             macArray[4];

    crc[7] = macArray[47] ^ macArray[44] ^ macArray[42] ^ macArray[39] ^
             macArray[38] ^ macArray[34] ^ macArray[33] ^ macArray[30] ^
             macArray[29] ^ macArray[27] ^ macArray[22] ^ macArray[20] ^
             macArray[18] ^ macArray[17] ^ macArray[15] ^ macArray[13] ^
             macArray[11] ^ macArray[7]  ^ macArray[6]  ^ macArray[5];

    for(i=0; i<8; i++)
        crcResult = crcResult | (crc[i] << i);

    return crcResult;
}
/*******************************************************************************
* mvEthMcastAddrSet - Multicast address settings.
*
* DESCRIPTION:
*       This API controls the MV device MAC multicast support.
*       The MV device supports multicast using two tables:
*       1) Special Multicast Table for MAC addresses of the form
*          0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
*          The MAC DA[7:0] bits are used as a pointer to the Special Multicast
*          Table entries in the DA-Filter table.
*          In this case, the function calls ethPortSmcAddr() routine to set the
*          Special Multicast Table.
*       2) Other Multicast Table for multicast of another type. A CRC-8bit
*          is used as an index to the Other Multicast Table entries in the
*          DA-Filter table.
*          In this case, the function calculates the CRC-8bit value and calls
*          ethPortOmcAddr() routine to set the Other Multicast Table.
*
* INPUT:
*       void*   pEthPortHndl    - Ethernet port handler.
*       MV_U8*  pAddr           - Address to be set
*       int     queue           - RX queue to capture all packets with this 
*                               Multicast MAC address.
*                               -1 means delete this Multicast address.
*
* RETURN: MV_STATUS
*       MV_TRUE - Success, Other - Failure
*
*******************************************************************************/
MV_STATUS   mvEthMcastAddrSet(void* pPortHndl, MV_U8 *pAddr, int queue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    unsigned char   crcResult = 0;

    if(queue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethPort %d: RX queue #%d is out of range\n", 
                    pPortCtrl->portNo, queue);
        return MV_BAD_PARAM;
    }

    if((pAddr[0] == 0x01) &&
       (pAddr[1] == 0x00) &&
       (pAddr[2] == 0x5E) &&
       (pAddr[3] == 0x00) &&
       (pAddr[4] == 0x00))
    {
        ethSetSpecialMcastAddr(pPortCtrl->portNo, pAddr[5], queue);
    }
    else
    {
        crcResult = mvEthMcastCrc8Get(pAddr);

        /* Check Add counter for this CRC value */
        if(queue == -1)
        {
            if(pPortCtrl->mcastCount[crcResult] == 0)
            {
                mvOsPrintf("ethPort #%d: No valid Mcast for crc8=0x%02x\n",
                            pPortCtrl->portNo, (unsigned)crcResult);
                return MV_NO_SUCH;
            }

            pPortCtrl->mcastCount[crcResult]--;
            if(pPortCtrl->mcastCount[crcResult] != 0)
            {
                mvOsPrintf("ethPort #%d: After delete there are %d valid Mcast for crc8=0x%02x\n",
                            pPortCtrl->portNo, pPortCtrl->mcastCount[crcResult],
                            (unsigned)crcResult);
                return MV_NO_CHANGE;
            }
        }
        else
        {
            pPortCtrl->mcastCount[crcResult]++;
            if(pPortCtrl->mcastCount[crcResult] > 1)
            {
                mvOsPrintf("ethPort #%d: Valid Mcast for crc8=0x%02x already exists\n",
                                pPortCtrl->portNo, (unsigned)crcResult);
                return MV_NO_CHANGE;
            }
        }
        ethSetOtherMcastAddr(pPortCtrl->portNo, crcResult, queue);
    }
    return MV_OK;
}

/*******************************************************************************
* ethSetUcastTable - Unicast address settings.
*
* DESCRIPTION:
*      Set all entries in the Unicast MAC Table queue==-1 means reject all 
* INPUT:
*
* RETURN: 
*
*******************************************************************************/
static void    ethSetUcastTable(int portNo, int queue)
{
    int     offset;
    MV_U32  regValue;

    if(queue == -1)
    {
        regValue = 0;
    }
    else
    {
        regValue = (((0x01 | (queue<<1)) << 0)  |
                    ((0x01 | (queue<<1)) << 8)  |
                    ((0x01 | (queue<<1)) << 16) |
                    ((0x01 | (queue<<1)) << 24));
    }

    for (offset=0; offset<=0xC; offset+=4)
        MV_REG_WRITE((ETH_DA_FILTER_UCAST_BASE(portNo) + offset), regValue);
}

/*******************************************************************************
* mvEthSetSpecialMcastTable - Special Multicast address settings.
*
* DESCRIPTION:
*   Set all entries to the Special Multicast MAC Table. queue==-1 means reject all
* INPUT:
*
* RETURN: 
*
*******************************************************************************/
MV_VOID    mvEthSetSpecialMcastTable(int portNo, int queue)
{
    int     offset;
    MV_U32  regValue;

    if(queue == -1)
    {
        regValue = 0;
    }
    else
    {
        regValue = (((0x01 | (queue<<1)) << 0)  |
                    ((0x01 | (queue<<1)) << 8)  |
                    ((0x01 | (queue<<1)) << 16) |
                    ((0x01 | (queue<<1)) << 24));
    }

    for (offset=0; offset<=0xFC; offset+=4)
    {
        MV_REG_WRITE((ETH_DA_FILTER_SPEC_MCAST_BASE(portNo) + 
                      offset), regValue);
    }
}

/*******************************************************************************
* mvEthSetOtherMcastTable - Other Multicast address settings.
*
* DESCRIPTION:
*   Set all entries to the Other Multicast MAC Table. queue==-1 means reject all
* INPUT:
*
* RETURN: 
*
*******************************************************************************/
MV_VOID    mvEthSetOtherMcastTable(int portNo, int queue)
{
    int     offset;
    MV_U32  regValue;

    if(queue == -1)
    {
        regValue = 0;
    }
    else
    {
        regValue = (((0x01 | (queue<<1)) << 0)  |
                    ((0x01 | (queue<<1)) << 8)  |
                    ((0x01 | (queue<<1)) << 16) |
                    ((0x01 | (queue<<1)) << 24));
    }

    for (offset=0; offset<=0xFC; offset+=4)
    {
        MV_REG_WRITE((ETH_DA_FILTER_OTH_MCAST_BASE(portNo) + 
                      offset), regValue);
    }
}

/*******************************************************************************
* ethSetUcastAddr - This function Set the port unicast address table
*
* DESCRIPTION:
*       This function locates the proper entry in the Unicast table for the
*       specified MAC nibble and sets its properties according to function
*       parameters.
*
* INPUT:
*       int     ethPortNum  - Port number.
*       MV_U8   lastNibble  - Unicast MAC Address last nibble.
*       int     queue       - Rx queue number for this MAC address.
*                           value "-1" means remove address
*
* OUTPUT:
*       This function add/removes MAC addresses from the port unicast address
*       table.
*
* RETURN:
*       MV_TRUE is output succeeded.
*       MV_FALSE if option parameter is invalid.
*
*******************************************************************************/
static MV_BOOL ethSetUcastAddr(int portNo, MV_U8 lastNibble, int queue)
{
    unsigned int unicastReg;
    unsigned int tblOffset;
    unsigned int regOffset;

    /* Locate the Unicast table entry */
    lastNibble  = (0xf & lastNibble);
    tblOffset = (lastNibble / 4) * 4; /* Register offset from unicast table base*/
    regOffset = lastNibble % 4;     /* Entry offset within the above register */


    unicastReg = MV_REG_READ( (ETH_DA_FILTER_UCAST_BASE(portNo) + 
                               tblOffset));
                 

    if(queue == -1)
    {
        /* Clear accepts frame bit at specified unicast DA table entry */
        unicastReg &= ~(0xFF << (8*regOffset));
    }
    else
    {
        unicastReg &= ~(0xFF << (8*regOffset));
        unicastReg |= ((0x01 | (queue<<1)) << (8*regOffset));
    }
    MV_REG_WRITE( (ETH_DA_FILTER_UCAST_BASE(portNo) + tblOffset),
                  unicastReg);

    return MV_TRUE;
}

/*******************************************************************************
* ethSetSpecialMcastAddr - Special Multicast address settings.
*
* DESCRIPTION:
*       This routine controls the MV device special MAC multicast support.
*       The Special Multicast Table for MAC addresses supports MAC of the form
*       0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
*       The MAC DA[7:0] bits are used as a pointer to the Special Multicast
*       Table entries in the DA-Filter table.
*       This function set the Special Multicast Table appropriate entry
*       according to the argument given.
*
* INPUT:
*       int     ethPortNum      Port number.
*       unsigned char   mcByte      Multicast addr last byte (MAC DA[7:0] bits).
*       int          queue      Rx queue number for this MAC address.
*       int             option      0 = Add, 1 = remove address.
*
* OUTPUT:
*       See description.
*
* RETURN:
*       MV_TRUE is output succeeded.
*       MV_FALSE if option parameter is invalid.
*
*******************************************************************************/
static MV_BOOL ethSetSpecialMcastAddr(int ethPortNum, MV_U8 lastByte, int queue)
{
    unsigned int smcTableReg;
    unsigned int tblOffset;
    unsigned int regOffset;

    /* Locate the SMC table entry */
    tblOffset = (lastByte / 4);     /* Register offset from SMC table base    */
    regOffset = lastByte % 4;       /* Entry offset within the above register */

    smcTableReg = MV_REG_READ((ETH_DA_FILTER_SPEC_MCAST_BASE(ethPortNum) + tblOffset*4));
    
    if(queue == -1)
    {
        /* Clear accepts frame bit at specified Special DA table entry */
        smcTableReg &= ~(0xFF << (8 * regOffset));
    }
    else
    {
        smcTableReg &= ~(0xFF << (8 * regOffset));
        smcTableReg |= ((0x01 | (queue<<1)) << (8 * regOffset));
    }
    MV_REG_WRITE((ETH_DA_FILTER_SPEC_MCAST_BASE(ethPortNum) + 
                  tblOffset*4), smcTableReg);

    return MV_TRUE;
}

/*******************************************************************************
* ethSetOtherMcastAddr - Multicast address settings.
*
* DESCRIPTION:
*       This routine controls the MV device Other MAC multicast support.
*       The Other Multicast Table is used for multicast of another type.
*       A CRC-8bit is used as an index to the Other Multicast Table entries
*       in the DA-Filter table.
*       The function gets the CRC-8bit value from the calling routine and
*       set the Other Multicast Table appropriate entry according to the
*       CRC-8 argument given.
*
* INPUT:
*       int     ethPortNum  Port number.
*       MV_U8   crc8        A CRC-8bit (Polynomial: x^8+x^2+x^1+1).
*       int     queue       Rx queue number for this MAC address.
*
* OUTPUT:
*       See description.
*
* RETURN:
*       MV_TRUE is output succeeded.
*       MV_FALSE if option parameter is invalid.
*
*******************************************************************************/
static MV_BOOL ethSetOtherMcastAddr(int ethPortNum, MV_U8 crc8, int queue)
{
    unsigned int omcTableReg;
    unsigned int tblOffset;
    unsigned int regOffset;

    /* Locate the OMC table entry */
    tblOffset = (crc8 / 4) * 4;     /* Register offset from OMC table base    */
    regOffset = crc8 % 4;           /* Entry offset within the above register */

    omcTableReg = MV_REG_READ(
        (ETH_DA_FILTER_OTH_MCAST_BASE(ethPortNum) + tblOffset));

    if(queue == -1)
    {
        /* Clear accepts frame bit at specified Other DA table entry */
        omcTableReg &= ~(0xFF << (8 * regOffset));
    }
    else
    {
        omcTableReg &= ~(0xFF << (8 * regOffset));
        omcTableReg |= ((0x01 | (queue<<1)) << (8 * regOffset));
    }

    MV_REG_WRITE((ETH_DA_FILTER_OTH_MCAST_BASE(ethPortNum) + tblOffset), 
                    omcTableReg);

    return MV_TRUE;
}


/******************************************************************************/
/*                      MIB Counters functions                                */
/******************************************************************************/


/*******************************************************************************
* mvEthMibCounterRead - Read a MIB counter
*
* DESCRIPTION:
*       This function reads a MIB counter of a specific ethernet port.
*       NOTE - Read from ETH_MIB_GOOD_OCTETS_RECEIVED_LOW or 
*              ETH_MIB_GOOD_OCTETS_SENT_LOW counters will return 64 bits value,
*              so pHigh32 pointer should not be NULL in this case.
*
* INPUT:
*       int           ethPortNum  - Ethernet Port number.
*       unsigned int  mibOffset   - MIB counter offset.
*
* OUTPUT:
*       MV_U32*       pHigh32 - pointer to place where 32 most significant bits
*                             of the counter will be stored.
*
* RETURN:
*       32 low sgnificant bits of MIB counter value.
*
*******************************************************************************/
MV_U32  mvEthMibCounterRead(void* pPortHandle, unsigned int mibOffset, 
                            MV_U32* pHigh32)
{
    int             portNo;
    MV_U32          valLow32, valHigh32;
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;

    portNo = pPortCtrl->portNo;

    valLow32 = MV_REG_READ(ETH_MIB_COUNTERS_BASE(portNo) + mibOffset);
    
    /* Implement FEr ETH. Erroneous Value when Reading the Upper 32-bits    */
    /* of a 64-bit MIB Counter.                                             */
    if( (mibOffset == ETH_MIB_GOOD_OCTETS_RECEIVED_LOW) || 
        (mibOffset == ETH_MIB_GOOD_OCTETS_SENT_LOW) )
    {
        valHigh32 = MV_REG_READ(ETH_MIB_COUNTERS_BASE(portNo) + mibOffset + 4);
        if(pHigh32 != NULL)
            *pHigh32 = valHigh32;
    }
    return valLow32;
}

/*******************************************************************************
* mvEthMibCountersClear - Clear all MIB counters
*
* DESCRIPTION:
*       This function clears all MIB counters
*
* INPUT:
*       int           ethPortNum  - Ethernet Port number.
*
*
* RETURN:   void
*
*******************************************************************************/
void  mvEthMibCountersClear(void* pPortHandle)
{
    int             i, portNo;
    unsigned int    dummy;
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;

    portNo = pPortCtrl->portNo;

    /* Perform dummy reads from MIB counters */
    for(i=ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i<ETH_MIB_LATE_COLLISION; i+=4)
        dummy = MV_REG_READ((ETH_MIB_COUNTERS_BASE(portNo) + i));
}
    

/******************************************************************************/
/*                        RX Dispatching configuration routines               */
/******************************************************************************/

int     mvEthTosToRxqGet(void* pPortHandle, int tos)
{
    MV_U32          regValue;
    int             regIdx, regOffs, rxq;
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;

    if(tos > 0xFF)
    {
        mvOsPrintf("eth_%d: tos=0x%x is out of range\n", pPortCtrl->portNo, tos);
        return -1;
    }
    regIdx  = mvOsDivide(tos>>2, 10);
    regOffs = mvOsReminder(tos>>2, 10);
    
    regValue = MV_REG_READ(ETH_DIFF_SERV_PRIO_REG(pPortCtrl->portNo, regIdx) );
    rxq = (regValue >> (regOffs*3));
    rxq &= 0x7;

    return rxq;
}

/*******************************************************************************
* mvEthTosToRxqSet - Map packets with special TOS value to special RX queue
*
* DESCRIPTION:
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*       int     tos         - TOS value in the IP header of the packet
*       int     rxq         - RX Queue for packets with the configured TOS value
*                           Negative value (-1) means no special processing for these packets, 
*                           so they will be processed as regular packets.
*
* RETURN:   MV_STATUS
*******************************************************************************/
MV_STATUS   mvEthTosToRxqSet(void* pPortHandle, int tos, int rxq)
{
    MV_U32          regValue;
    int             regIdx, regOffs;
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;

    if( (rxq < 0) || (rxq >= MV_ETH_RX_Q_NUM) )
    {
        mvOsPrintf("eth_%d: RX queue #%d is out of range\n", pPortCtrl->portNo, rxq);
        return MV_BAD_PARAM;
    }
    if(tos > 0xFF)
    {
        mvOsPrintf("eth_%d: tos=0x%x is out of range\n", pPortCtrl->portNo, tos);
        return MV_BAD_PARAM;
    }
    regIdx  = mvOsDivide(tos>>2, 10);
    regOffs = mvOsReminder(tos>>2, 10);
    
    regValue = MV_REG_READ(ETH_DIFF_SERV_PRIO_REG(pPortCtrl->portNo, regIdx) );
    regValue &= ~(0x7 << (regOffs*3));
    regValue |= (rxq << (regOffs*3));

    MV_REG_WRITE(ETH_DIFF_SERV_PRIO_REG(pPortCtrl->portNo, regIdx), regValue);
    return MV_OK;
}

/*******************************************************************************
* mvEthVlanPrioRxQueue - Configure RX queue to capture VLAN tagged packets with 
*                        special priority bits [0-2]
*
* DESCRIPTION:
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*       int     bpduQueue   - Special queue to capture VLAN tagged packets with special
*                           priority.
*                           Negative value (-1) means no special processing for these packets, 
*                           so they will be processed as regular packets.
*
* RETURN:   MV_STATUS
*       MV_OK       - Success
*       MV_FAIL     - Failed. 
*
*******************************************************************************/
MV_STATUS   mvEthVlanPrioRxQueue(void* pPortHandle, int vlanPrio, int vlanPrioQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    MV_U32          vlanPrioReg;

    if(vlanPrioQueue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethDrv: RX queue #%d is out of range\n", vlanPrioQueue);
        return MV_BAD_PARAM;
    }
    if(vlanPrio >= 8)
    {
        mvOsPrintf("ethDrv: vlanPrio=%d is out of range\n", vlanPrio);
        return MV_BAD_PARAM;
    }
  
    vlanPrioReg = MV_REG_READ(ETH_VLAN_TAG_TO_PRIO_REG(pPortCtrl->portNo));
    vlanPrioReg &= ~(0x7 << (vlanPrio*3));
    vlanPrioReg |= (vlanPrioQueue << (vlanPrio*3));
    MV_REG_WRITE(ETH_VLAN_TAG_TO_PRIO_REG(pPortCtrl->portNo), vlanPrioReg);

    return MV_OK;
}


/*******************************************************************************
* mvEthBpduRxQueue - Configure RX queue to capture BPDU packets.
*
* DESCRIPTION:
*       This function defines processing of BPDU packets. 
*   BPDU packets can be accepted and captured to one of RX queues 
*   or can be processing as regular Multicast packets. 
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*       int     bpduQueue   - Special queue to capture BPDU packets (DA is equal to 
*                           01-80-C2-00-00-00 through 01-80-C2-00-00-FF, 
*                           except for the Flow-Control Pause packets). 
*                           Negative value (-1) means no special processing for BPDU, 
*                           packets so they will be processed as regular Multicast packets.
*
* RETURN:   MV_STATUS
*       MV_OK       - Success
*       MV_FAIL     - Failed. 
*
*******************************************************************************/
MV_STATUS   mvEthBpduRxQueue(void* pPortHandle, int bpduQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    MV_U32      portCfgReg;
    MV_U32      portCfgExtReg;

    if(bpduQueue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethDrv: RX queue #%d is out of range\n", bpduQueue);
        return MV_BAD_PARAM;
    }
  
    portCfgExtReg = MV_REG_READ(ETH_PORT_CONFIG_EXTEND_REG(pPortCtrl->portNo));

    portCfgReg = MV_REG_READ(ETH_PORT_CONFIG_REG(pPortCtrl->portNo));
    if(bpduQueue >= 0)
    {
        pPortCtrl->portConfig.rxBpduQ = bpduQueue;

        portCfgReg &= ~ETH_DEF_RX_BPDU_QUEUE_ALL_MASK;
        portCfgReg |= ETH_DEF_RX_BPDU_QUEUE_MASK(pPortCtrl->portConfig.rxBpduQ);

        MV_REG_WRITE(ETH_PORT_CONFIG_REG(pPortCtrl->portNo), portCfgReg);

        portCfgExtReg |= ETH_CAPTURE_SPAN_BPDU_ENABLE_MASK;
    }
    else
    {
        pPortCtrl->portConfig.rxBpduQ = -1;
        /* no special processing for BPDU packets */
        portCfgExtReg &= (~ETH_CAPTURE_SPAN_BPDU_ENABLE_MASK);
    }

    MV_REG_WRITE(ETH_PORT_CONFIG_EXTEND_REG(pPortCtrl->portNo),  portCfgExtReg);

    return MV_OK;
}


/*******************************************************************************
* mvEthArpRxQueue - Configure RX queue to capture ARP packets.
*
* DESCRIPTION:
*       This function defines processing of ARP (type=0x0806) packets. 
*   ARP packets can be accepted and captured to one of RX queues 
*   or can be processed as other Broadcast packets. 
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*       int     arpQueue    - Special queue to capture ARP packets (type=0x806). 
*                           Negative value (-1) means discard ARP packets
*
* RETURN:   MV_STATUS
*       MV_OK       - Success
*       MV_FAIL     - Failed. 
*
*******************************************************************************/
MV_STATUS   mvEthArpRxQueue(void* pPortHandle, int arpQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    MV_U32      portCfgReg;

    if(arpQueue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethDrv: RX queue #%d is out of range\n", arpQueue);
        return MV_BAD_PARAM;
    }

    portCfgReg = MV_REG_READ(ETH_PORT_CONFIG_REG(pPortCtrl->portNo));

    if(arpQueue >= 0)
    {
        pPortCtrl->portConfig.rxArpQ = arpQueue;
        portCfgReg &= ~ETH_DEF_RX_ARP_QUEUE_ALL_MASK;
        portCfgReg |= ETH_DEF_RX_ARP_QUEUE_MASK(pPortCtrl->portConfig.rxArpQ);

        portCfgReg &= (~ETH_REJECT_ARP_BCAST_MASK);
    }
    else
    {
        pPortCtrl->portConfig.rxArpQ = -1;
        portCfgReg |= ETH_REJECT_ARP_BCAST_MASK;
    }

    MV_REG_WRITE(ETH_PORT_CONFIG_REG(pPortCtrl->portNo), portCfgReg);

    return MV_OK;
}


/*******************************************************************************
* mvEthTcpRxQueue - Configure RX queue to capture TCP packets.
*
* DESCRIPTION:
*       This function defines processing of TCP packets. 
*   TCP packets can be accepted and captured to one of RX queues 
*   or can be processed as regular Unicast packets. 
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*       int     tcpQueue    - Special queue to capture TCP packets. Value "-1" 
*                           means no special processing for TCP packets, 
*                           so they will be processed as regular
*
* RETURN:   MV_STATUS
*       MV_OK       - Success
*       MV_FAIL     - Failed. 
*
*******************************************************************************/
MV_STATUS   mvEthTcpRxQueue(void* pPortHandle, int tcpQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    MV_U32      portCfgReg;

    if(tcpQueue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethDrv: RX queue #%d is out of range\n", tcpQueue);
        return MV_BAD_PARAM;
    }
    portCfgReg = MV_REG_READ(ETH_PORT_CONFIG_REG(pPortCtrl->portNo));

    if(tcpQueue >= 0)
    {
        pPortCtrl->portConfig.rxTcpQ = tcpQueue;
        portCfgReg &= ~ETH_DEF_RX_TCP_QUEUE_ALL_MASK;
        portCfgReg |= ETH_DEF_RX_TCP_QUEUE_MASK(pPortCtrl->portConfig.rxTcpQ);

        portCfgReg |= ETH_CAPTURE_TCP_FRAMES_ENABLE_MASK;
    }
    else
    {
        pPortCtrl->portConfig.rxTcpQ = -1;
        portCfgReg &= (~ETH_CAPTURE_TCP_FRAMES_ENABLE_MASK);
    }

    MV_REG_WRITE(ETH_PORT_CONFIG_REG(pPortCtrl->portNo), portCfgReg);

    return MV_OK;
}


/*******************************************************************************
* mvEthUdpRxQueue - Configure RX queue to capture UDP packets.
*
* DESCRIPTION:
*       This function defines processing of UDP packets. 
*   TCP packets can be accepted and captured to one of RX queues 
*   or can be processed as regular Unicast packets. 
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*       int     udpQueue    - Special queue to capture UDP packets. Value "-1" 
*                           means no special processing for UDP packets, 
*                           so they will be processed as regular
*
* RETURN:   MV_STATUS
*       MV_OK       - Success
*       MV_FAIL     - Failed. 
*
*******************************************************************************/
MV_STATUS   mvEthUdpRxQueue(void* pPortHandle, int udpQueue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    MV_U32          portCfgReg;

    if(udpQueue >= MV_ETH_RX_Q_NUM)
    {
        mvOsPrintf("ethDrv: RX queue #%d is out of range\n", udpQueue);
        return MV_BAD_PARAM;
    }

    portCfgReg = MV_REG_READ(ETH_PORT_CONFIG_REG(pPortCtrl->portNo));

    if(udpQueue >= 0)
    {
        pPortCtrl->portConfig.rxUdpQ = udpQueue;
        portCfgReg &= ~ETH_DEF_RX_UDP_QUEUE_ALL_MASK;
        portCfgReg |= ETH_DEF_RX_UDP_QUEUE_MASK(pPortCtrl->portConfig.rxUdpQ);

        portCfgReg |= ETH_CAPTURE_UDP_FRAMES_ENABLE_MASK;        
    }
    else
    {
        pPortCtrl->portConfig.rxUdpQ = -1;
        portCfgReg &= ~ETH_CAPTURE_UDP_FRAMES_ENABLE_MASK;
    }

    MV_REG_WRITE(ETH_PORT_CONFIG_REG(pPortCtrl->portNo), portCfgReg);

    return MV_OK;
}


/******************************************************************************/
/*                          Speed, Duplex, FlowControl routines               */
/******************************************************************************/

/*******************************************************************************
* mvEthSpeedDuplexSet - Set Speed and Duplex of the port.
*
* DESCRIPTION:
*       This function configure the port to work with desirable Duplex and Speed.
*       Changing of these parameters are allowed only when port is disabled.
*       This function disable the port if was enabled, change duplex and speed 
*       and, enable the port back if needed.
*
* INPUT:
*       void*           pPortHandle - Pointer to port specific handler;
*       ETH_PORT_SPEED  speed       - Speed of the port.
*       ETH_PORT_SPEED  duplex      - Duplex of the port.
*
* RETURN:   MV_STATUS
*       MV_OK           - Success
*       MV_OUT_OF_RANGE - Failed. Port is out of valid range
*       MV_NOT_FOUND    - Failed. Port is not initialized.
*       MV_BAD_PARAM    - Input parameters (speed/duplex) in conflict.
*       MV_BAD_VALUE    - Value of one of input parameters (speed, duplex) 
*                       is not valid
*
*******************************************************************************/
MV_STATUS   mvEthSpeedDuplexSet(void* pPortHandle, MV_ETH_PORT_SPEED speed, 
                                MV_ETH_PORT_DUPLEX duplex)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;
    MV_U32      portSerialCtrlReg;
    
    if( (port < 0) || (port >= (int)mvCtrlEthMaxPortGet()) )
        return MV_OUT_OF_RANGE;

    pPortCtrl = ethPortCtrl[port];
    if(pPortCtrl == NULL)
        return MV_NOT_FOUND;

    /* Check validity */
    if( (speed == MV_ETH_SPEED_1000) && (duplex == MV_ETH_DUPLEX_HALF) )
        return MV_BAD_PARAM;

    portSerialCtrlReg = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(port));
    /* Set Speed */
    switch(speed)
    {
        case MV_ETH_SPEED_AN:
            portSerialCtrlReg &= ~ETH_DISABLE_SPEED_AUTO_NEG_MASK;
            break;

        case MV_ETH_SPEED_10:
            portSerialCtrlReg |= ETH_DISABLE_SPEED_AUTO_NEG_MASK;
            portSerialCtrlReg &= ~ETH_SET_GMII_SPEED_1000_MASK;
            portSerialCtrlReg &= ~ETH_SET_MII_SPEED_100_MASK;
            break;

        case MV_ETH_SPEED_100:
            portSerialCtrlReg |= ETH_DISABLE_SPEED_AUTO_NEG_MASK;
            portSerialCtrlReg &= ~ETH_SET_GMII_SPEED_1000_MASK;
            portSerialCtrlReg |= ETH_SET_MII_SPEED_100_MASK;
            break;

        case MV_ETH_SPEED_1000:
            portSerialCtrlReg |= ETH_DISABLE_SPEED_AUTO_NEG_MASK;
            portSerialCtrlReg |= ETH_SET_GMII_SPEED_1000_MASK;
            break;

        default:
            mvOsPrintf("ethDrv: Unexpected Speed value %d\n", speed);
            return MV_BAD_VALUE;
    }
    /* Set duplex */
    switch(duplex)
    {
        case MV_ETH_DUPLEX_AN:
            portSerialCtrlReg &= ~ETH_DISABLE_DUPLEX_AUTO_NEG_MASK;
            break;

        case MV_ETH_DUPLEX_HALF:
            portSerialCtrlReg |= ETH_DISABLE_DUPLEX_AUTO_NEG_MASK;
            portSerialCtrlReg &= ~ETH_SET_FULL_DUPLEX_MASK;
            break;

        case MV_ETH_DUPLEX_FULL:
            portSerialCtrlReg |= ETH_DISABLE_DUPLEX_AUTO_NEG_MASK;
            portSerialCtrlReg |= ETH_SET_FULL_DUPLEX_MASK;
            break;

        default:
            mvOsPrintf("ethDrv: Unexpected Duplex value %d\n", duplex);
            return MV_BAD_VALUE;
    }
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(port), portSerialCtrlReg);

    return MV_OK;
}

/*******************************************************************************
* mvEthFlowCtrlSet - Set Flow Control of the port.
*
* DESCRIPTION:
*       This function configure the port to work with desirable Duplex and 
*       Speed. Changing of these parameters are allowed only when port is 
*       disabled. This function disable the port if was enabled, change 
*       duplex and speed and, enable the port back if needed.
*
* INPUT:
*       void*           pPortHandle - Pointer to port specific handler;
*       MV_ETH_PORT_FC  flowControl - Flow control of the port.
*
* RETURN:   MV_STATUS
*       MV_OK           - Success
*       MV_OUT_OF_RANGE - Failed. Port is out of valid range
*       MV_NOT_FOUND    - Failed. Port is not initialized.
*       MV_BAD_VALUE    - Value flowControl parameters is not valid
*
*******************************************************************************/
MV_STATUS   mvEthFlowCtrlSet(void* pPortHandle, MV_ETH_PORT_FC flowControl)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;
    MV_U32      portSerialCtrlReg;
    
    if( (port < 0) || (port >= (int)mvCtrlEthMaxPortGet() ) )
        return MV_OUT_OF_RANGE;

    pPortCtrl = ethPortCtrl[port];
    if(pPortCtrl == NULL)
        return MV_NOT_FOUND;

    portSerialCtrlReg = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(port));
    switch(flowControl)
    {
        case MV_ETH_FC_AN_ADV_DIS:
            portSerialCtrlReg &= ~ETH_DISABLE_FC_AUTO_NEG_MASK;
            portSerialCtrlReg &= ~ETH_ADVERTISE_SYM_FC_MASK;
            break;

        case MV_ETH_FC_AN_ADV_SYM:
            portSerialCtrlReg &= ~ETH_DISABLE_FC_AUTO_NEG_MASK;
            portSerialCtrlReg |= ETH_ADVERTISE_SYM_FC_MASK;
            break;

        case MV_ETH_FC_DISABLE:
            portSerialCtrlReg |= ETH_DISABLE_FC_AUTO_NEG_MASK;
            portSerialCtrlReg &= ~ETH_SET_FLOW_CTRL_MASK;
            break;

        case MV_ETH_FC_ENABLE:
            portSerialCtrlReg |= ETH_DISABLE_FC_AUTO_NEG_MASK;
            portSerialCtrlReg |= ETH_SET_FLOW_CTRL_MASK;
            break;

        default:
            mvOsPrintf("ethDrv: Unexpected FlowControl value %d\n", flowControl);
            return MV_BAD_VALUE;
    }
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_REG(port), portSerialCtrlReg);

    return MV_OK;
}

/*******************************************************************************
* mvEthHeaderModeSet - Set port header mode.
*
* DESCRIPTION:
*       This function configures the port to work in Marvell-Header mode.
*
* INPUT:
*       void*           pPortHandle - Pointer to port specific handler;
*       MV_ETH_HEADER_MODE headerMode - The header mode to set the port in.
*
* RETURN:   MV_STATUS
*       MV_OK           - Success
*       MV_NOT_SUPPORTED- Feature not supported.
*       MV_OUT_OF_RANGE - Failed. Port is out of valid range
*       MV_NOT_FOUND    - Failed. Port is not initialized.
*       MV_BAD_VALUE    - Value of headerMode or numRxQueue parameter is not valid.
*
*******************************************************************************/
MV_STATUS mvEthHeaderModeSet(void* pPortHandle, MV_ETH_HEADER_MODE headerMode)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;
    MV_U32			mvHeaderReg;
    MV_U32          numRxQ = MV_ETH_RX_Q_NUM;
    
    if((port < 0) || (port >= mvCtrlEthMaxPortGet()))
        return MV_OUT_OF_RANGE;

    pPortCtrl = ethPortCtrl[port];
    if(pPortCtrl == NULL)
        return MV_NOT_FOUND;

    mvHeaderReg = MV_REG_READ(ETH_PORT_MARVELL_HEADER_REG(port));
    /* Disable header mode.             */
    mvHeaderReg &= ~ETH_MVHDR_EN_MASK;

    if(headerMode != MV_ETH_DISABLE_HEADER_MODE)
    {
        /* Enable Header mode.              */
        mvHeaderReg |= ETH_MVHDR_EN_MASK;

        /* Clear DA-Prefix  & MHMask fields.*/
        mvHeaderReg &= ~(ETH_MVHDR_DAPREFIX_MASK | ETH_MVHDR_MHMASK_MASK);

        if(numRxQ > 1)
        {
            switch (headerMode)
            {
                case(MV_ETH_ENABLE_HEADER_MODE_PRI_2_1):
                    mvHeaderReg |= ETH_MVHDR_DAPREFIX_PRI_1_2;
                    break;
                case(MV_ETH_ENABLE_HEADER_MODE_PRI_DBNUM):
                    mvHeaderReg |= ETH_MVHDR_DAPREFIX_DBNUM_PRI;
                    break;
                case(MV_ETH_ENABLE_HEADER_MODE_PRI_SPID):
                    mvHeaderReg |= ETH_MVHDR_DAPREFIX_SPID_PRI;
                    break;
                default:
                    break;
            }

            switch (numRxQ)
            {
                case (4):
                    mvHeaderReg |= ETH_MVHDR_MHMASK_4_QUEUE;
                    break;
                case (8):
                    mvHeaderReg |= ETH_MVHDR_MHMASK_8_QUEUE;
                    break;
                default:
                    break;
            }
        }
    }

    MV_REG_WRITE(ETH_PORT_MARVELL_HEADER_REG(port), mvHeaderReg);

    return MV_OK;
}

#if (MV_ETH_VERSION >= 4)
/*******************************************************************************
* mvEthEjpModeSet - Enable / Disable EJP policy for TX.
*
* DESCRIPTION:
*       This function 
*
* INPUT:
*       void*           pPortHandle - Pointer to port specific handler;
*       MV_BOOL         TRUE - enable EJP mode
*                       FALSE - disable EJP mode
*
* OUTPUT:   MV_STATUS
*       MV_OK           - Success
*       Other           - Failure
*
* RETURN:   None.
*
*******************************************************************************/
MV_STATUS    mvEthEjpModeSet(void* pPortHandle, int mode) 
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;
    
    if((port < 0) || (port >= mvCtrlEthMaxPortGet()))
        return MV_OUT_OF_RANGE;

    pPortCtrl = ethPortCtrl[port];
    if(pPortCtrl == NULL)
        return MV_NOT_FOUND;
    
    pPortCtrl->portConfig.ejpMode = mode;
    if(mode)
    {
        /* EJP enabled */
        MV_REG_WRITE(ETH_TXQ_CMD_1_REG(port), ETH_TX_EJP_ENABLE_MASK);
    }
    else
    {
        /* EJP disabled */
        MV_REG_WRITE(ETH_TXQ_CMD_1_REG(port), 0);
    }
    mvOsPrintf("eth_%d: EJP %s - ETH_TXQ_CMD_1_REG: 0x%x = 0x%08x\n", 
        port, mode ? "Enabled" : "Disabled", ETH_TXQ_CMD_1_REG(port), 
                    MV_REG_READ(ETH_TXQ_CMD_1_REG(port)));

    return MV_OK;
}
#endif /* MV_ETH_VERSION >= 4 */

/*******************************************************************************
* mvEthStatusGet - Get major properties of the port .
*
* DESCRIPTION:
*       This function get major properties of the port (link, speed, duplex, 
*       flowControl, etc) and return them using the single structure.
*
* INPUT:
*       void*           pPortHandle - Pointer to port specific handler;
*
* OUTPUT:
*       MV_ETH_PORT_STATUS* pStatus - Pointer to structure, were port status 
*                                   will be placed.
*
* RETURN:   None.
*
*******************************************************************************/
void    mvEthStatusGet(void* pPortHandle, MV_ETH_PORT_STATUS* pStatus)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;

    MV_U32  regValue;

    regValue = MV_REG_READ( ETH_PORT_STATUS_REG(port) );

    if(regValue & ETH_GMII_SPEED_1000_MASK)
        pStatus->speed = MV_ETH_SPEED_1000;
    else if(regValue & ETH_MII_SPEED_100_MASK)
        pStatus->speed = MV_ETH_SPEED_100;
    else
        pStatus->speed = MV_ETH_SPEED_10;

    if(regValue & ETH_LINK_UP_MASK) 
        pStatus->isLinkUp = MV_TRUE;
    else
        pStatus->isLinkUp = MV_FALSE;

    if(regValue & ETH_FULL_DUPLEX_MASK) 
        pStatus->duplex = MV_ETH_DUPLEX_FULL; 
    else
        pStatus->duplex = MV_ETH_DUPLEX_HALF; 


    if(regValue & ETH_ENABLE_RCV_FLOW_CTRL_MASK) 
        pStatus->flowControl = MV_ETH_FC_ENABLE;
    else
        pStatus->flowControl = MV_ETH_FC_DISABLE;
}


/******************************************************************************/
/*                         PHY Control Functions                              */
/******************************************************************************/


/*******************************************************************************
* mvEthPhyAddrSet - Set the ethernet port PHY address.
*
* DESCRIPTION:
*       This routine set the ethernet port PHY address according to given
*       parameter.
*
* INPUT:
*       void*   pPortHandle     - Pointer to port specific handler;
*       int     phyAddr         - PHY address       
*
* RETURN:
*       None.
*
*******************************************************************************/
void    mvEthPhyAddrSet(void* pPortHandle, int phyAddr)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;
    unsigned int    regData;

    regData = MV_REG_READ(ETH_PHY_ADDR_REG(port));

    regData &= ~ETH_PHY_ADDR_MASK;
    regData |=  phyAddr;

    MV_REG_WRITE(ETH_PHY_ADDR_REG(port), regData);

    return;
}

/*******************************************************************************
* mvEthPhyAddrGet - Get the ethernet port PHY address.
*
* DESCRIPTION:
*       This routine returns the given ethernet port PHY address.
*
* INPUT:
*       void*   pPortHandle - Pointer to port specific handler;
*
*
* RETURN: int - PHY address.
*
*******************************************************************************/
int     mvEthPhyAddrGet(void* pPortHandle)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHandle;
    int             port = pPortCtrl->portNo;
    unsigned int    regData;

    regData = MV_REG_READ(ETH_PHY_ADDR_REG(port));

    return ((regData >> (5 * port)) & 0x1f);
}

/******************************************************************************/
/*                Descriptor handling Functions                               */
/******************************************************************************/

/*******************************************************************************
* etherInitRxDescRing - Curve a Rx chain desc list and buffer in memory.
*
* DESCRIPTION:
*       This function prepares a Rx chained list of descriptors and packet
*       buffers in a form of a ring. The routine must be called after port
*       initialization routine and before port start routine.
*       The Ethernet SDMA engine uses CPU bus addresses to access the various
*       devices in the system (i.e. DRAM). This function uses the ethernet
*       struct 'virtual to physical' routine (set by the user) to set the ring
*       with physical addresses.
*
* INPUT:
*       ETH_QUEUE_CTRL  *pEthPortCtrl   Ethernet Port Control srtuct.
*       int             rxQueue         Number of Rx queue.
*       int             rxDescNum       Number of Rx descriptors
*       MV_U8*          rxDescBaseAddr  Rx descriptors memory area base addr.
*
* OUTPUT:
*       The routine updates the Ethernet port control struct with information
*       regarding the Rx descriptors and buffers.
*
* RETURN: None
*
*******************************************************************************/
static void ethInitRxDescRing(ETH_PORT_CTRL* pPortCtrl, int queue)
{
    ETH_RX_DESC     *pRxDescBase, *pRxDesc, *pRxPrevDesc; 
    int             ix, rxDescNum = pPortCtrl->rxQueueConfig[queue].descrNum;
    ETH_QUEUE_CTRL  *pQueueCtrl = &pPortCtrl->rxQueue[queue];

    /* Make sure descriptor address is cache line size aligned  */        
    pRxDescBase = (ETH_RX_DESC*)MV_ALIGN_UP((MV_ULONG)pQueueCtrl->descBuf.bufVirtPtr,
                                     CPU_D_CACHE_LINE_SIZE);

    pRxDesc      = (ETH_RX_DESC*)pRxDescBase;
    pRxPrevDesc  = pRxDesc;

    /* initialize the Rx descriptors ring */
    for (ix=0; ix<rxDescNum; ix++)
    {
        pRxDesc->bufSize     = 0x0;
        pRxDesc->byteCnt     = 0x0;
        pRxDesc->cmdSts      = ETH_BUFFER_OWNED_BY_HOST;
        pRxDesc->bufPtr      = 0x0;
        pRxDesc->returnInfo  = 0x0;
        pRxPrevDesc = pRxDesc;
        if(ix == (rxDescNum-1))
        {
            /* Closing Rx descriptors ring */
            pRxPrevDesc->nextDescPtr = (MV_U32)ethDescVirtToPhy(pQueueCtrl, (void*)pRxDescBase);
        }
        else
        {
            pRxDesc = (ETH_RX_DESC*)((MV_ULONG)pRxDesc + ETH_RX_DESC_ALIGNED_SIZE);
            pRxPrevDesc->nextDescPtr = (MV_U32)ethDescVirtToPhy(pQueueCtrl, (void*)pRxDesc);
        }
        ETH_DESCR_FLUSH_INV(pPortCtrl, pRxPrevDesc);
    }

    pQueueCtrl->pCurrentDescr = pRxDescBase;
    pQueueCtrl->pUsedDescr = pRxDescBase;
    
    pQueueCtrl->pFirstDescr = pRxDescBase;
    pQueueCtrl->pLastDescr = pRxDesc;
    pQueueCtrl->resource = 0;
}

void ethResetRxDescRing(void* pPortHndl, int queue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    ETH_QUEUE_CTRL* pQueueCtrl = &pPortCtrl->rxQueue[queue];
    ETH_RX_DESC*    pRxDesc = (ETH_RX_DESC*)pQueueCtrl->pFirstDescr;

    pQueueCtrl->resource = 0;
    if(pQueueCtrl->pFirstDescr != NULL)
    {
        while(MV_TRUE)
        {
            pRxDesc->bufSize     = 0x0;
            pRxDesc->byteCnt     = 0x0;
            pRxDesc->cmdSts      = ETH_BUFFER_OWNED_BY_HOST;
            pRxDesc->bufPtr      = 0x0;
            pRxDesc->returnInfo  = 0x0;
            ETH_DESCR_FLUSH_INV(pPortCtrl, pRxDesc);
            if( (void*)pRxDesc == pQueueCtrl->pLastDescr)
                    break;
            pRxDesc = RX_NEXT_DESC_PTR(pRxDesc, pQueueCtrl);
        }
        pQueueCtrl->pCurrentDescr = pQueueCtrl->pFirstDescr;
        pQueueCtrl->pUsedDescr = pQueueCtrl->pFirstDescr;

        /* Update RX Command register */
        pPortCtrl->portRxQueueCmdReg |= (1 << queue);

        /* update HW */
        MV_REG_WRITE( ETH_RX_CUR_DESC_PTR_REG(pPortCtrl->portNo, queue),
                 (MV_U32)ethDescVirtToPhy(pQueueCtrl, pQueueCtrl->pCurrentDescr) );
    }
    else
    {
        /* Update RX Command register */
        pPortCtrl->portRxQueueCmdReg &= ~(1 << queue);

        /* update HW */
        MV_REG_WRITE( ETH_RX_CUR_DESC_PTR_REG(pPortCtrl->portNo, queue), 0);
    }
}

/*******************************************************************************
* etherInitTxDescRing - Curve a Tx chain desc list and buffer in memory.
*
* DESCRIPTION:
*       This function prepares a Tx chained list of descriptors and packet
*       buffers in a form of a ring. The routine must be called after port
*       initialization routine and before port start routine.
*       The Ethernet SDMA engine uses CPU bus addresses to access the various
*       devices in the system (i.e. DRAM). This function uses the ethernet
*       struct 'virtual to physical' routine (set by the user) to set the ring
*       with physical addresses.
*
* INPUT:
*       ETH_PORT_CTRL   *pEthPortCtrl   Ethernet Port Control srtuct.
*       int             txQueue         Number of Tx queue.
*       int             txDescNum       Number of Tx descriptors
*       int             txBuffSize      Size of Tx buffer
*       MV_U8*          pTxDescBase     Tx descriptors memory area base addr.
*
* OUTPUT:
*       The routine updates the Ethernet port control struct with information
*       regarding the Tx descriptors and buffers.
*
* RETURN:   None.
*
*******************************************************************************/
static void ethInitTxDescRing(ETH_PORT_CTRL* pPortCtrl, int queue)
{
    ETH_TX_DESC     *pTxDescBase, *pTxDesc, *pTxPrevDesc; 
    int             ix, txDescNum = pPortCtrl->txQueueConfig[queue].descrNum;
    ETH_QUEUE_CTRL  *pQueueCtrl = &pPortCtrl->txQueue[queue];

    /* Make sure descriptor address is cache line size aligned  */
    pTxDescBase = (ETH_TX_DESC*)MV_ALIGN_UP((MV_ULONG)pQueueCtrl->descBuf.bufVirtPtr, 
                                     CPU_D_CACHE_LINE_SIZE);

    pTxDesc      = (ETH_TX_DESC*)pTxDescBase;
    pTxPrevDesc  = pTxDesc;

    /* initialize the Tx descriptors ring */
    for (ix=0; ix<txDescNum; ix++)
    {
        pTxDesc->byteCnt     = 0x0000;
        pTxDesc->L4iChk      = 0x0000;
        pTxDesc->cmdSts      = ETH_BUFFER_OWNED_BY_HOST;
        pTxDesc->bufPtr      = 0x0;
        pTxDesc->returnInfo  = 0x0;

        pTxPrevDesc = pTxDesc;

        if(ix == (txDescNum-1))
        {
            /* Closing Tx descriptors ring */
            pTxPrevDesc->nextDescPtr = (MV_U32)ethDescVirtToPhy(pQueueCtrl, (void*)pTxDescBase);
        }
        else
        {
            pTxDesc = (ETH_TX_DESC*)((MV_ULONG)pTxDesc + ETH_TX_DESC_ALIGNED_SIZE);
            pTxPrevDesc->nextDescPtr = (MV_U32)ethDescVirtToPhy(pQueueCtrl, (void*)pTxDesc);
        }
        ETH_DESCR_FLUSH_INV(pPortCtrl, pTxPrevDesc);
    }

    pQueueCtrl->pCurrentDescr = pTxDescBase;
    pQueueCtrl->pUsedDescr = pTxDescBase;
    
    pQueueCtrl->pFirstDescr = pTxDescBase;
    pQueueCtrl->pLastDescr = pTxDesc;
    /* Leave one TX descriptor out of use */
    pQueueCtrl->resource = txDescNum - 1;
}

void ethResetTxDescRing(void* pPortHndl, int queue)
{
    ETH_PORT_CTRL*  pPortCtrl = (ETH_PORT_CTRL*)pPortHndl;
    ETH_QUEUE_CTRL* pQueueCtrl = &pPortCtrl->txQueue[queue];
    ETH_TX_DESC*    pTxDesc = (ETH_TX_DESC*)pQueueCtrl->pFirstDescr;
    
    pQueueCtrl->resource = 0;
    if(pQueueCtrl->pFirstDescr != NULL)
    {
        while(MV_TRUE)
        {
            pTxDesc->byteCnt     = 0x0000;
            pTxDesc->L4iChk      = 0x0000;
            pTxDesc->cmdSts      = ETH_BUFFER_OWNED_BY_HOST;
            pTxDesc->bufPtr      = 0x0;
            pTxDesc->returnInfo  = 0x0;
            ETH_DESCR_FLUSH_INV(pPortCtrl, pTxDesc);
            pQueueCtrl->resource++;
            if( (void*)pTxDesc == pQueueCtrl->pLastDescr)
                    break;
            pTxDesc = TX_NEXT_DESC_PTR(pTxDesc, pQueueCtrl);
        }
        /* Leave one TX descriptor out of use */
        pQueueCtrl->resource--;
        pQueueCtrl->pCurrentDescr = pQueueCtrl->pFirstDescr;
        pQueueCtrl->pUsedDescr = pQueueCtrl->pFirstDescr;

        /* Update TX Command register */
        pPortCtrl->portTxQueueCmdReg |= MV_32BIT_LE_FAST(1 << queue);
        /* update HW */
        MV_REG_WRITE( ETH_TX_CUR_DESC_PTR_REG(pPortCtrl->portNo, queue),
        (MV_U32)ethDescVirtToPhy(pQueueCtrl, pQueueCtrl->pCurrentDescr) );
    }
    else
    {
        /* Update TX Command register */
        pPortCtrl->portTxQueueCmdReg &=  MV_32BIT_LE_FAST(~(1 << queue));
        /* update HW */
        MV_REG_WRITE( ETH_TX_CUR_DESC_PTR_REG(pPortCtrl->portNo, queue), 0 );    
    }
}

/*******************************************************************************
* ethAllocDescrMemory - Free memory allocated for RX and TX descriptors.
*
* DESCRIPTION:
*       This function allocates memory for RX and TX descriptors.
*       - If ETH_DESCR_IN_SRAM defined, allocate memory from SRAM.
*       - If ETH_DESCR_IN_SDRAM defined, allocate memory in SDRAM.
*
* INPUT:
*       int size - size of memory should be allocated.
*
* RETURN: None
*
*******************************************************************************/
static MV_U8*  ethAllocDescrMemory(ETH_PORT_CTRL* pPortCtrl, int descSize, 
                            MV_ULONG* pPhysAddr, MV_U32 *memHandle)
{
    MV_U8*  pVirt;

#if defined(ETH_DESCR_IN_SRAM)
    if(ethDescInSram == MV_TRUE)
        pVirt = (char*)mvSramMalloc(descSize, pPhysAddr);
    else
#endif /* ETH_DESCR_IN_SRAM */
    {
#ifdef ETH_DESCR_UNCACHED
        pVirt = (MV_U8*)mvOsIoUncachedMalloc(pPortCtrl->osHandle, descSize,
					    pPhysAddr,memHandle);
#else
        pVirt = (MV_U8*)mvOsIoCachedMalloc(pPortCtrl->osHandle, descSize,
					  pPhysAddr, memHandle);
#endif /* ETH_DESCR_UNCACHED */
    }
    memset(pVirt, 0, descSize);

    return pVirt;
}

/*******************************************************************************
* ethFreeDescrMemory - Free memory allocated for RX and TX descriptors.
*
* DESCRIPTION:
*       This function frees memory allocated for RX and TX descriptors.
*       - If ETH_DESCR_IN_SRAM defined, free memory using gtSramFree() function.
*       - If ETH_DESCR_IN_SDRAM defined, free memory using mvOsFree() function.
*
* INPUT:
*       void* pVirtAddr - virtual pointer to memory allocated for RX and TX
*                       desriptors.        
*
* RETURN: None
*
*******************************************************************************/
void    ethFreeDescrMemory(ETH_PORT_CTRL* pPortCtrl, MV_BUF_INFO* pDescBuf)
{
    if( (pDescBuf == NULL) || (pDescBuf->bufVirtPtr == NULL) )
        return;

#if defined(ETH_DESCR_IN_SRAM)
    if( ethDescInSram )
    {
        mvSramFree(pDescBuf->bufSize, pDescBuf->bufPhysAddr, pDescBuf->bufVirtPtr);
        return;
    }
#endif /* ETH_DESCR_IN_SRAM */

#ifdef ETH_DESCR_UNCACHED
    mvOsIoUncachedFree(pPortCtrl->osHandle, pDescBuf->bufSize, pDescBuf->bufPhysAddr, 
                     pDescBuf->bufVirtPtr,pDescBuf->memHandle);
#else
    mvOsIoCachedFree(pPortCtrl->osHandle, pDescBuf->bufSize, pDescBuf->bufPhysAddr, 
                     pDescBuf->bufVirtPtr,pDescBuf->memHandle);
#endif /* ETH_DESCR_UNCACHED */
}
                                                                                                                             
/******************************************************************************/
/*                Other Functions                                         */
/******************************************************************************/

void mvEthPortPowerUp(int port)
{
    MV_U32  regVal;

    /* MAC Cause register should be cleared */
    MV_REG_WRITE(ETH_UNIT_INTR_CAUSE_REG(port), 0);

	if (mvBoardIsPortInSgmii(port))
    mvEthPortSgmiiConfig(port);

    /* Cancel Port Reset */
    regVal = MV_REG_READ(ETH_PORT_SERIAL_CTRL_1_REG(port));
    regVal &= (~ETH_PORT_RESET_MASK);
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_1_REG(port), regVal);
    while( (MV_REG_READ(ETH_PORT_SERIAL_CTRL_1_REG(port)) & ETH_PORT_RESET_MASK) != 0);
}

void mvEthPortPowerDown(int port)
{
    MV_U32  regVal;

    /* Port must be DISABLED */
    regVal = MV_REG_READ(ETH_PORT_SERIAL_CTRL_REG(port));
    if( (regVal & ETH_PORT_ENABLE_MASK) != 0)
    {
        mvOsPrintf("ethPort #%d: PowerDown - port must be Disabled (PSC=0x%x)\n",
                    port, regVal);
        return;
    }

    /* Port Reset (Read after write the register as a precaution) */
    regVal = MV_REG_READ(ETH_PORT_SERIAL_CTRL_1_REG(port));
    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_1_REG(port), regVal | ETH_PORT_RESET_MASK);
    while((MV_REG_READ(ETH_PORT_SERIAL_CTRL_1_REG(port)) & ETH_PORT_RESET_MASK) == 0);
}

static void mvEthPortSgmiiConfig(int port)
{
    MV_U32  regVal;

    regVal = MV_REG_READ(ETH_PORT_SERIAL_CTRL_1_REG(port));

    regVal |= (ETH_SGMII_MODE_MASK /*| ETH_INBAND_AUTO_NEG_ENABLE_MASK */);
    regVal &= (~ETH_INBAND_AUTO_NEG_BYPASS_MASK);

    MV_REG_WRITE(ETH_PORT_SERIAL_CTRL_1_REG(port), regVal);
}