summaryrefslogtreecommitdiff
path: root/polux/linux-2.6.10/drivers/net/synop3504/synop3504.c
blob: b5ac4519daf1af8d166bda2c77f173744ba3f50d (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
/**
 *
 * (C) Copyright 2007 SPiDCOM Technologies
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#undef CONFIG_PROC_FS

#define DRV_NAME    "synop3504"
#define DRV_VERSION    "1.0"
#define DRV_RELDATE    "February 23, 2012"


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <asm/arch/hardware.h>
#include <asm/io.h>
#include <linux/proc_fs.h>
#include <asm/cacheflush.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/dma-mapping.h>
#include <linux/kthread.h>

MODULE_AUTHOR ("SPiDCOM Technologies");
MODULE_DESCRIPTION ("Synopsys" DRV_NAME "ethernet driver");
MODULE_LICENSE ("GPL");
MODULE_VERSION (DRV_VERSION);

//These identify the driver base version and may not be removed
static char version[] __devinitdata =
DRV_NAME " 10/100 Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";

static int debug = 0;

/** Synopsys device definitions and functions */
#include "synop3504_reg.h"
#include "synop3504_regdef.h"
#include "synop3504_hw.h"
#include "synop3504_dma.h"
#include "synop3504_mii.h"

#define TXDESCS 32
#define RXDESCS 128

#define MAC_OFFSET 0x0000
#define DMA_OFFSET 0x1000

/* aligned on HZ, which corresponds to 10ms */
#define MSEC_PER_JIFFY_HZ (1000/HZ)
#define AUTONEG_STEP_MS (100)
#define RESET_STEP_MS (100)
#define AUTONEG_TIMEOUT_S (10)
#define TIMEOUT (2*HZ)

//IOCTL are triplicated in linux/synop3504.c, linux-tools/safe-return.c and app/mib-interface.h
#define SIOCGLINKSTATUS            (SIOCDEVPRIVATE+0)
#define SIOCGSETLINKSPEED          (SIOCDEVPRIVATE+1)
#define SIOCGGETLINKSETUPSPEED     (SIOCDEVPRIVATE+2)
#define SIOCGGETLINKCURRENTSPEED   (SIOCDEVPRIVATE+3)
#define SIOCGSETLINKMODE           (SIOCDEVPRIVATE+4)
#define SIOCGGETLINKSETUPMODE      (SIOCDEVPRIVATE+5)
#define SIOCGGETLINKCURRENTMODE    (SIOCDEVPRIVATE+6)

#define ETH_MODE_AUTO   0
#define ETH_MODE_FD     1
#define ETH_MODE_HD     2

/** IP175 C/D is a 5 port PHY switch. The 5 real ports use addresses
 * from 0 to 4. There is a 6th "virtual" port at address 5 that is used
 * as a switch to control the real ports. */
#define IP175_VIRTUAL_PHY_ADDR 5

struct SynopsysMacReg
{
    uint32_t GmacConfig;
    uint32_t GmacFrameFilter;
    uint32_t GmacFlowControl;
    uint32_t GmacGmiiAddr;
};

/** Driver private data */
typedef struct PrivateStruct
{
    Synopsys synopsys;
    struct syeth_dma_rb *rx;
    struct syeth_dma_rb *tx;
    int minor;
    struct net_device_stats stats;
    struct timer_list timer;
    struct mii_if_info mii_if;
    enum phy_chipset_id OUI_model;
    uint32_t phy_id;
    uint32_t phy_oui;
    uint32_t phy_model;
    uint32_t phy_rev;
    struct SynopsysMacReg macreg;
} Private;

typedef enum synop3504_speed
{
    SYNOP3504_SPEED_10,
    SYNOP3504_SPEED_100,
} synop3504_speed;

typedef enum synop3504_duplex
{
    SYNOP3504_DUPLEX_HALF,
    SYNOP3504_DUPLEX_FULL,
} synop3504_duplex;

static uint32_t phy_modes = ADVERTISE_10HALF | ADVERTISE_10FULL
| ADVERTISE_100HALF | ADVERTISE_100FULL;
static int eth_duplex_mode = 0;
static int eth_speed = 0;
static int init_finish = 0;

/**
 * Function    : mdly
 * Purpose      : delay for thread.
 * Parameters   : msec->value of the delay in ms
 * Return value : void.
 */
static inline void
mdly (unsigned int msec)
{
    set_current_state (TASK_INTERRUPTIBLE);
    schedule_timeout ((msec + MSEC_PER_JIFFY_HZ - 1) / MSEC_PER_JIFFY_HZ);
}

/**
 * Function    : synop3504_receive
 * Purpose      : Receives the packet and give to the upper layers.
 * Parameters   : dev->network device
 *                budget->maximum packets count
 * Return value : void.
 */
static void
synop3504_receive (struct net_device *dev, int *budget)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;
    struct sk_buff *skb = NULL;
    int count = 0;

    pr = (Private *) dev->priv;
    tc = &pr->synopsys;

    if (*budget == 0)
        return;

    do
    {
        skb = syeth_dma_rx_rb_get (pr->rx);
        if (skb == NULL)
            break;
        skb->dev = dev;
        skb->protocol = eth_type_trans (skb, dev);
        dev->last_rx = jiffies;
        // with NAPI buffer have to be processed immediatelly
        netif_receive_skb (skb);
        (*budget)--;
        count++;
    }
    while (skb && *budget);
}

/**
 * Function    : synop3504_poll
 * Purpose      : This will call by netif framework for poll the device.
 * Parameters   : dev->network device structure
 *                budget->maximum count of packets to receive
 * Return value : error code.
 */
static int
synop3504_poll (struct net_device *dev, int *budget)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;
    u32 status;
    int tbudget, ibudget;

    pr = (Private *) dev->priv;
    tc = &pr->synopsys;

    tbudget = *budget;
    /* we have to receive no more packet then the current budget and the
     * device quota */
    if (tbudget > dev->quota)
    {
        tbudget = dev->quota;
    }
    ibudget = tbudget;

    do
    {
        //Get and cleanup IRQ status as soon as possible
        status = SynopsysReadDmaReg (tc, DmaStatus);
        SynopsysWriteDmaReg (tc, DmaStatus, status);

        status &= DmaIntMask;

        if (status == 0)
            break;

        if (status & DmaIntBusError)
        {
            //DMA error occured
            printk (DRV_NAME ": %s: poll: DMA error occured - fatal\n",
                    dev->name);
        }

        if (status & DmaIntTxNoBuffer)
        {
            syeth_dma_tx_rb_complete (pr->tx);
        }

        if (status & (DmaIntTxCompleted | DmaIntTxNoBuffer))
        {
            if (netif_queue_stopped (dev))
            {
                netif_wake_queue (dev);
                debugp (DRV_NAME ": %s: TX normal: Wake Up send queue\n",
                        dev->name);
            }
        }

        if (status & DmaIntTxUnderflow)
        {
            //Abnormal transmitter interrupt
            printk (DRV_NAME ": %s: interupt: Abnormal Tx Interrupt Seen\n",
                    dev->name);
            //Remove old buffers from TX descriptors
            syeth_dma_tx_rb_complete (pr->tx);
            netif_start_queue (dev);
        }

        if (status & (DmaIntRxCompleted | DmaIntRxNoBuffer))
        {
            synop3504_receive (dev, &tbudget);
        }

        if (status & DmaIntRxNoBuffer)
        {
            //Abnormal receiver interrupt
            debugp (DRV_NAME ": %s: interupt: Abnormal Rx Interrupt Seen\n",
                    dev->name);
            pr->stats.rx_over_errors++;
            SynopsysWriteDmaReg (tc, DmaRxPollDemand, 0);
        }

        if (status & DmaIntRxStopped)
        {
            //Receiver went to stopped state
            printk (DRV_NAME ": %s: interupt: Rx Stopped Interrupt Seen\n",
                    dev->name);
        }

        if (status & DmaIntTxStopped)
        {
            //Transmitter went to stopped state
            printk (DRV_NAME ": %s: interupt: Tx Stopped Interrupt Seen\n",
                    dev->name);
            netif_stop_queue (dev);
            syeth_dma_tx_rb_reset (pr->tx);
        }

    }
    while (tbudget > 0);

    *budget -= ibudget - tbudget;
    dev->quota -= ibudget - tbudget;
    if (status == 0)
    {
        netif_rx_complete (dev);
        //Enable Interrupts
        SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntEnable);
        return 0;
    }
    return 1;
}

/**
 * Function    : int_handler
 * Purpose      : This will call the particular handler according to
 *                the interrupt.
 * Parameters   : int_num->interrupt number
 *                dev_id->device address
 * Return value : error code.
 */
static irqreturn_t
int_handler (int int_num, void *dev_id, struct pt_regs *regs)
{
    struct net_device *dev = (struct net_device *) dev_id;
    Private *pr = NULL;
    Synopsys *tc = NULL;

    pr = (Private *) dev->priv;
    if (pr == NULL)
        return IRQ_NONE;

    tc = &pr->synopsys;
    if (tc == NULL)
        return IRQ_NONE;

    //Clear interrupts
    SynopsysWriteDmaReg (tc, DmaInterrupt, 0);
    if (netif_rx_schedule_prep (dev))
    {
        __netif_rx_schedule (dev);
    }
    else
    {
        printk (DRV_NAME
                ": %s: interrupt: driver bug! interrupt while in poll\n",
                dev->name);
    }
    return IRQ_HANDLED;
}

/**
 * Function    : synop3504_hw_config_update
 * Purpose      : Set default options of synopsys MAC IP.
 * Parameters   : dev->network device structure
 * Return value : error code.
 */
static void
synop3504_hw_config_update (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    unsigned int bmcr;
    synop3504_duplex mode = SYNOP3504_DUPLEX_FULL;
    synop3504_speed speed = SYNOP3504_SPEED_100;

    bmcr = SynopsysMiiRead (tc, MII_BMCR,
                            AUTOMATIC_PHY_RESOLUTION);
    /* Is autoneg enabled. */
    if (bmcr & BMCR_ANENABLE)
    {
        int adv;
        int lpa;
        /* Get the advertise register. */
        adv = SynopsysMiiRead (tc, MII_ADVERTISE,
                               AUTOMATIC_PHY_RESOLUTION);
        /* Get the link partner advertise. */
        lpa =  SynopsysMiiRead (tc, MII_LPA,
                                AUTOMATIC_PHY_RESOLUTION);
        lpa &= adv;
        /* We can get the speed and mode.
         * Speed and mode is on bits 5 to 9 */
        if (lpa & LPA_100FULL)
        {
            speed = SYNOP3504_SPEED_100;
            mode = SYNOP3504_DUPLEX_FULL;
        }
        else if (lpa & LPA_100HALF)
        {
            speed = SYNOP3504_SPEED_100;
            mode = SYNOP3504_DUPLEX_HALF;
        }
        else if (lpa & LPA_10FULL)
        {
            speed = SYNOP3504_SPEED_10;
            mode = SYNOP3504_DUPLEX_FULL;
        }
        else if (lpa & LPA_10HALF)
        {
            speed = SYNOP3504_SPEED_10;
            mode = SYNOP3504_DUPLEX_HALF;
        }
    }
    /* Auto neg is not enabled in control register. */
    else
    {
        /* Mode. */
        if (bmcr & BMCR_FULLDPLX)
        {
            mode = SYNOP3504_DUPLEX_FULL;
            speed = (bmcr & BMCR_SPEED100) ? SYNOP3504_SPEED_100
                : SYNOP3504_SPEED_10;
        }
        else
        {
            mode = SYNOP3504_DUPLEX_HALF;
            speed = (bmcr & BMCR_SPEED100) ? SYNOP3504_SPEED_100
                : SYNOP3504_SPEED_10;
        }
    }

    if (mode == SYNOP3504_DUPLEX_FULL)
    {
        SynopsysWriteMacReg (tc, GmacConfig, GmacConfigInitFdx110);
        SynopsysWriteMacReg (tc, GmacFrameFilter, GmacFrameFilterInitFdx);
        SynopsysWriteMacReg (tc, GmacFlowControl, GmacFlowControlInitFdx);
        SynopsysWriteMacReg (tc, GmacGmiiAddr, GmacGmiiAddrInitFdx);
    }
    else
    {
        SynopsysWriteMacReg (tc, GmacConfig, GmacConfigInitHdx110);
        SynopsysWriteMacReg (tc, GmacFrameFilter, GmacFrameFilterInitHdx);
        SynopsysWriteMacReg (tc, GmacFlowControl, GmacFlowControlInitHdx);
        SynopsysWriteMacReg (tc, GmacGmiiAddr, GmacGmiiAddrInitHdx);
    }
    /* Set hardware clock. */
    synop3504_hw_set_speed (pr->minor, ((speed | mode) & 0x0008));
}

/**
 * Function    : synop3504_save_mac_register
 * Purpose      : Save options of the synopsys MAC IP.
 * Parameters   : dev->network device structure
 * Return value : error code.
 */
static int
synop3504_save_mac_register (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    pr->macreg.GmacConfig = SynopsysReadMacReg (tc, GmacConfig);
    pr->macreg.GmacFrameFilter = SynopsysReadMacReg (tc, GmacFrameFilter);
    pr->macreg.GmacFlowControl = SynopsysReadMacReg (tc, GmacFlowControl);
    pr->macreg.GmacGmiiAddr = SynopsysReadMacReg (tc, GmacGmiiAddr);
}

/**
 * Function    : synop3504_restore_mac_register
 * Purpose      : Restore options of the synopsys MAC IP.
 * Parameters   : dev->network device structure
 * Return value : error code.
 */
static int
synop3504_restore_mac_register (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    SynopsysWriteMacReg (tc, GmacConfig, pr->macreg.GmacConfig);
    SynopsysWriteMacReg (tc, GmacFrameFilter, pr->macreg.GmacFrameFilter);
    SynopsysWriteMacReg (tc, GmacFlowControl, pr->macreg.GmacFlowControl);
    SynopsysWriteMacReg (tc, GmacGmiiAddr, pr->macreg.GmacGmiiAddr);
}

/**
 * Function    : synop3504_an_complete
 * Purpose      : Set the correct duplex mode to the hardware after AN.
 * Parameters   : dev->network device structure
 * Return value : error code.
 */
static void
synop3504_an_complete (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    uint32_t data;
    uint32_t control;
    int s100_used = 0;
    int fd_used = 0;

    //Check the duplex mode (result of the Autonegotiation)
    switch (pr->OUI_model)
    {
    case ICS_1893BF:
    case ICS_1893CF:
        data = SynopsysMiiRead (tc, MII_QPDSR, AUTOMATIC_PHY_RESOLUTION);
        s100_used = data & QPDSR_SPEED100;
        fd_used = data & QPDSR_FULLDPLX;
        break;

    case MICREL_KS8721:
        //using PHYCTRL instead of BMCR
        data = SynopsysMiiRead (tc, MII_PHYCTRL, AUTOMATIC_PHY_RESOLUTION);
        s100_used = (data & PHYCTRL_OP_MODE) == PHYCTRL_OM_100HALF
            || (data & PHYCTRL_OP_MODE) == PHYCTRL_OM_100FULL;
        fd_used = (data & PHYCTRL_OP_MODE) == PHYCTRL_OM_10FULL
            || (data & PHYCTRL_OP_MODE) == PHYCTRL_OM_100FULL;
        break;

    case VITESSE_VSC8601:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
    default:
        /* BMCR duplex status after autonegotiation is not standard : works on
         * RTL8201CP */
        data = SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION);
        s100_used = data & BMCR_SPEED100;
        fd_used = data & BMCR_FULLDPLX;
        break;
    }

    //Store the duplex mode in hardware
    synop3504_hw_set_speed (pr->minor, ((s100_used | fd_used) & 0x0008));

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
    default:
        // Nothing to do.
        break;

    case MICREL_KS8721:
    case ICS_1893BF:
    case ICS_1893CF:
        // Set the correct duplex mode to the hardware after AN.
        control = SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION);
        if (s100_used)
            control |= BMCR_SPEED100;
        else
            control &= ~BMCR_SPEED100;
        if (fd_used)
            control |= BMCR_FULLDPLX;
        else
            control &= ~BMCR_FULLDPLX;
        SynopsysMiiWrite (tc, MII_BMCR, control, AUTOMATIC_PHY_RESOLUTION);
        debugp (DRV_NAME
                ": %s: Store Speed and Duplex given by AutoNegotiation (10%sMbit/s %s-Duplex)\n",
                dev->name, s100_used ? "0" : "", fd_used ? "Full" : "Half");
        break;
    }
}

static void
synop3504_reset_phy (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
    case ICS_1893BF:
    case ICS_1893CF:
    case MICREL_KS8721:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
    default:
        SynopsysMiiWrite (tc, MII_BMCR, BMCR_RESET, AUTOMATIC_PHY_RESOLUTION);
        while (SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION)
                & BMCR_RESET)
            ;
        break;

    case ICPLUS_IP175C:
        SynopsysMiiWrite (tc, 0, 0x175C, 30);
        mdly (RESET_STEP_MS);
        break;

    case ICPLUS_IP175D:
        // Reset the Switch
        SynopsysMiiWrite (tc, 2, 0x175D, 20);
        mdly (RESET_STEP_MS);

        // Reset Vlan Table. PHY22 MII0, MSB.
        SynopsysMiiWrite (tc, 0, 0x8000, 22);

        // TPID Vlan. PHY22 MII3, MSB.
        SynopsysMiiWrite (tc, 3, 0x8100, 22);
        break;
    }
}

__u32
get_regdump_len (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    int size = 0;

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
    case ICS_1893BF:
    case ICS_1893CF:
    case MICREL_KS8721:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
    default:
        size = (2 * REGDUMP_OUI_Size);
        break;

    case ICPLUS_IP175C:
        size = (2 * REGDUMP_ICPLUS_IP175C_SIZE);
        break;

    case ICPLUS_IP175D:
        size = (2 * REGDUMP_ICPLUS_IP175D_SIZE);
        break;
    }

    return size;
}

int
fill_regdump (struct net_device *dev, struct ethtool_regs *reg)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    u16 *temp = (u16 *) reg->data;
    int mii;

    *temp++ = pr->OUI_model >> 16;    // 0
    *temp++ = pr->OUI_model & 0xffff;    // 1

    switch (pr->OUI_model)
    {
    default:
        break;

    case ICPLUS_IP175C:
        // index from 2 to 6
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 0);    // 2
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 1);    // 3
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 2);    // 4
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 3);    // 5
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 4);    // 6

        for (mii = REGDUMP_ICPLUS_IP175C_PHY29_Begin;
             mii <= REGDUMP_ICPLUS_IP175C_PHY29_End; mii++)
        {
            *temp++ = SynopsysMiiRead (tc, mii, 29);
        }

        for (mii = REGDUMP_ICPLUS_IP175C_PHY30_Begin;
             mii <= REGDUMP_ICPLUS_IP175C_PHY30_End; mii++)
        {
            *temp++ = SynopsysMiiRead (tc, mii, 30);
        }

        for (mii = REGDUMP_ICPLUS_IP175C_PHY31_Begin;
             mii <= REGDUMP_ICPLUS_IP175C_PHY31_End; mii++)
        {
            *temp++ = SynopsysMiiRead (tc, mii, 31);
        }
        break;

    case ICPLUS_IP175D:
        // index from 2 to 6
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 0);    // 2
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 1);    // 3
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 2);    // 4
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 3);    // 5
        *temp++ = SynopsysMiiRead (tc, MII_BMSR, 4);    // 6

        for (mii = REGDUMP_ICPLUS_IP175D_PHY20_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY20_End; mii++)
        {
            if (mii == 1 || mii == 23)
                continue;

            // index from 7 to 29
            *temp++ = SynopsysMiiRead (tc, mii, 20);
        }

        for (mii = REGDUMP_ICPLUS_IP175D_PHY21_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY21_End; mii++)
        {
            if (mii == 11 || mii == 13)
                continue;

            // index from 30 to 53
            *temp++ = SynopsysMiiRead (tc, mii, 21);
        }

        for (mii = REGDUMP_ICPLUS_IP175D_PHY22_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY22_End; mii++)
        {
            // index from 54 to 83
            *temp++ = SynopsysMiiRead (tc, mii, 22);
        }

        for (mii = REGDUMP_ICPLUS_IP175D_PHY23_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY23_End; mii++)
        {
            // index from 84 to 115
            *temp++ = SynopsysMiiRead (tc, mii, 23);
        }

        for (mii = REGDUMP_ICPLUS_IP175D_PHY24_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY24_End; mii++)
        {
            // index from 116 to 119
            *temp++ = SynopsysMiiRead (tc, mii, 24);
        }

        for (mii = REGDUMP_ICPLUS_IP175D_PHY25_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY25_End; mii++)
        {
            // index from 120 to 143
            *temp++ = SynopsysMiiRead (tc, mii, 25);
        }

        for (mii = REGDUMP_ICPLUS_IP175D_PHY26_Begin;
             mii <= REGDUMP_ICPLUS_IP175D_PHY26_End; mii++)
        {
            // index from 144 to 167
            *temp++ = SynopsysMiiRead (tc, mii, 26);
        }
        break;
    }

    return 0;
}

int
synop3504_read_vlan_table (struct net_device *dev,
                           struct ethtool_vlanparam *ecmd)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    u16 reg;
    int port;

    if (pr->OUI_model == ICPLUS_IP175D)
    {
        // Read VLAN_MODE[5:0] field in PHY22, MII0.
        reg = SynopsysMiiRead (tc, 0, 22);

        for (port = 0; port < MAX_VLAN_SUPPORTED; port++)
        {
            if (reg & (1 << port))
            {
                ecmd->vlan_table[port].state = VLAN_ON;
            }
            else
            {
                ecmd->vlan_table[port].state = VLAN_OFF;
            }

            ecmd->vlan_table[port].vlan = SynopsysMiiRead (tc, port + 4, 22);
        }
    }
    else
    {
        // Feature not supported
        return -EOPNOTSUPP;
    }

    return 0;
}

int
synop3504_write_vlan_table (struct net_device *dev,
                            struct ethtool_vlanparam *ecmd)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    if (pr->OUI_model == ICPLUS_IP175D)
    {
        // ***************************************************
        //  All the fixed number are the registers.
        //  Because that cannot be changed, no define have been created.
        //  See Datasheet IP175DLF-DS-R02 : February 22, 2008
        // ***************************************************
        u8 add_tag_reg[IP175D_VLAN_TABLE_SIZE];
        u8 remove_tag_reg[IP175D_VLAN_TABLE_SIZE];
        u8 vlan_member_reg[IP175D_VLAN_TABLE_SIZE];
        u16 vlan_valid = 0;
        int i, j;

        memset (add_tag_reg, 0, sizeof (add_tag_reg));
        memset (remove_tag_reg, 0, sizeof (remove_tag_reg));
        memset (vlan_member_reg, 0, sizeof (vlan_member_reg));

        // entry from 0 to 4 are reserved for port-based or tag-based.
        for (i = 0; i < MAX_VLAN_SUPPORTED; i++)
        {
            // Write VLAN_INFO_x
            SynopsysMiiWrite (tc, 4 + i, ecmd->vlan_table[i].vlan, 22);

            // Write the VID_x
            SynopsysMiiWrite (tc, 14 + i, ecmd->vlan_table[i].vlan, 22);

            vlan_valid |=
                (ecmd->vlan_table[i].state != VLAN_OFF) ? (1 << i) : 0;
        }

        // entry 5, is connected to spc200c. special entry. always in tag mode.
        vlan_valid |= (1 << IP175D_VLAN_TABLE_COMMON_PHY);
        // Maybe this line is not usefull.
        vlan_member_reg[IP175D_VLAN_TABLE_COMMON_PHY] = 0x3F;
        // PVID_5 = 0
        SynopsysMiiWrite (tc, 9, 0, 22);

        // entry 6. // JLe : is it usefull ?????
        vlan_valid |= (1 << IP175D_VLAN_TABLE_NO_TAG_LINE);
        vlan_member_reg[IP175D_VLAN_TABLE_NO_TAG_LINE] |=
            (1 << IP175D_VLAN_TABLE_COMMON_PHY);
        SynopsysMiiWrite (tc, 20, 0, 22);    // Write the VID_6 (= PVID_5)

        for (i = 0; i < MAX_VLAN_SUPPORTED; i++)
        {
            if (ecmd->vlan_table[i].state != VLAN_OFF)
            {
                add_tag_reg[i] |= (1 << IP175D_VLAN_TABLE_COMMON_PHY);    // Add tag from outside to spc200c.
                remove_tag_reg[i] = 0x1F;    // Remove tag from spc200c to outside and from other port also.
                vlan_member_reg[i] |=
                    (1 << IP175D_VLAN_TABLE_COMMON_PHY) | (1 << i);

                for (j = i + 1; j < MAX_VLAN_SUPPORTED; j++)
                {
                    // The same VID can forward the packet between them.
                    if ((ecmd->vlan_table[i].vlan == ecmd->vlan_table[j].vlan)
                        && (ecmd->vlan_table[j].state != VLAN_OFF))
                    {
                        vlan_member_reg[i] |= (1 << j);
                        vlan_member_reg[j] |= (1 << i);
                    }
                }
            }
            else
            {
                // The no-VID can forward the packet between them.
                vlan_member_reg[IP175D_VLAN_TABLE_NO_TAG_LINE] |= (1 << i);
            }
        }

        for (i = 0; i < MAX_VLAN_SUPPORTED; i++)
        {
            if (ecmd->vlan_table[i].state == VLAN_OFF)
            {
                // The no-VID have the same forward rules.
                vlan_member_reg[i] =
                    vlan_member_reg[IP175D_VLAN_TABLE_NO_TAG_LINE];
            }
        }

        for (i = 0; i < IP175D_VLAN_TABLE_SIZE; i += 2)
        {
            u16 reg;

            reg =
                ((vlan_member_reg[i + 1] << 8) & 0xFF00) +
                (vlan_member_reg[i + 0] & 0x00FF);
            SynopsysMiiWrite (tc, 0 + (i / 2), reg, 23);    // VLAN_MEMBER

            reg =
                ((add_tag_reg[i + 1] << 8) & 0xFF00) +
                (add_tag_reg[i + 0] & 0x00FF);
            SynopsysMiiWrite (tc, 8 + (i / 2), reg, 23);    // ADD_TAG

            reg =
                ((remove_tag_reg[i + 1] << 8) & 0xFF00) +
                (remove_tag_reg[i + 0] & 0x00FF);
            SynopsysMiiWrite (tc, 16 + (i / 2), reg, 23);    // REMOVE_TAG
        }

        // Global register
        // Write VLAN_MODE + VLAN_CLS
        SynopsysMiiWrite (tc, 0, 0x1000 | (vlan_valid & 0x3F), 22);
        // Write the VLAN_VALID
        SynopsysMiiWrite (tc, 10, vlan_valid, 22);
    }
    else
        // Feature not supported
        return -EOPNOTSUPP;

    return 0;
}

/**
 * Function    : synop3504_enable_autonegotiation
 * Purpose      : activate the autonegotiation.
 * Parameters   : dev->network device structure
 * Return value : void.
 */
static void
synop3504_enable_autonegotiation (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    volatile int data;

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
        /* Deactivate Gigabit support. */
        SynopsysMiiWrite (tc, MII_CTRL1000, 0, AUTOMATIC_PHY_RESOLUTION);
    case ICS_1893BF:
    case ICS_1893CF:
    case MICREL_KS8721:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
        //Ensure that phy is autonegotiation capable
        if (SynopsysMiiRead (tc, MII_BMSR, AUTOMATIC_PHY_RESOLUTION)
                & BMSR_ANEGCAPABLE)
        {
            //Disable auto negotiation
            data = SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION);
            data &= ~BMCR_ANENABLE;
            SynopsysMiiWrite (tc, MII_BMCR, data, AUTOMATIC_PHY_RESOLUTION);

            //Set auto-neogtiation advertisement register - all techs
            data = SynopsysMiiRead (tc, MII_ADVERTISE,
                                    AUTOMATIC_PHY_RESOLUTION);
            data |= phy_modes;
            SynopsysMiiWrite (tc, MII_ADVERTISE, data,
                              AUTOMATIC_PHY_RESOLUTION);

            //Kick auto negotiation
            data = SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION);
            data |= BMCR_ANENABLE;
            SynopsysMiiWrite (tc, MII_BMCR, data, AUTOMATIC_PHY_RESOLUTION);
        }
        break;

    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
        /* Nothing to do. */
        break;

    default:
        break;
    }

}

/**
 * Function    : synop3504_autonegotiate
 * Purpose      : Start the autonegotiation procedure.
 * Parameters   : dev->network device structure
 * Return value : error code.
 */
static int
synop3504_autonegotiate (struct net_device *dev, int wait)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    int result = 0;

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
    case ICS_1893BF:
    case ICS_1893CF:
    case MICREL_KS8721:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
        //Ensure that phy is autonegotiation capable
        if (SynopsysMiiRead (tc, MII_BMSR, AUTOMATIC_PHY_RESOLUTION) &
            BMSR_ANEGCAPABLE)
        {
            debugp (DRV_NAME ": starting autonegotiation for %s\n", dev->name);

            //Kick auto negotiation
            result = mii_nway_restart (&pr->mii_if);
            if (wait)
            {
                int timeout = AUTONEG_TIMEOUT_S * 1000 / AUTONEG_STEP_MS;
                volatile uint32_t data;

                switch (pr->OUI_model)
                {
                case ICS_1893BF:
                case ICS_1893CF:
                    do
                    {
                        mdly (AUTONEG_STEP_MS);
                        data =
                            SynopsysMiiRead (tc, MII_QPDSR,
                                             AUTOMATIC_PHY_RESOLUTION);
                    }
                    while ((!(data & QPDSR_ANEGCOMPLETE)
                            || !(data & QPDSR_LSTATUS)) && timeout--);
                    break;

                default:
                    do
                    {
                        mdly (AUTONEG_STEP_MS);
                        data =
                            SynopsysMiiRead (tc, MII_BMSR,
                                             AUTOMATIC_PHY_RESOLUTION);
                    }
                    while (!(data & BMSR_ANEGCOMPLETE) && timeout--);
                    break;
                }

                if (timeout <= 0)
                {
                    printk (DRV_NAME
                            ": autonegotiation timed out after %d s for %s\n",
                            AUTONEG_TIMEOUT_S, dev->name);
                    return -1;
                }
                else
                {
                    debugp (DRV_NAME
                            ": autonegotiation complete after %d ms for %s\n",
                            AUTONEG_TIMEOUT_S * 1000 - timeout * AUTONEG_STEP_MS,
                            dev->name);
                }
            }
            //Set duplex mode anyway
            synop3504_an_complete (dev);
        }
        else
        {
            result = -ENOSYS;
        }
        break;

    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
        // TODO
        result = -ENOSYS;
        break;

    default:
        result = -ENOSYS;
        break;
    }

    return result;
}

/**
 * Function    : synop3504_setup_ethernet_address
 * Purpose      : Change the MAC address.
 * Parameters   : dev->network device structure
 * Return value : void.
 */
static void
synop3504_setup_ethernet_address (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    uint32_t data;
    uint32_t data2;

    //Setup MAC address
    data = (dev->dev_addr[5] << 8) | dev->dev_addr[4];
    SynopsysWriteMacReg (tc, GmacAddr0High, data);
    data = (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16)
        | (dev-> dev_addr[1] << 8) | dev->dev_addr[0];
    SynopsysWriteMacReg (tc, GmacAddr0Low, data);


    data = SynopsysReadMacReg (tc, GmacAddr0High);
    data2 = SynopsysReadMacReg (tc, GmacAddr0Low);
    debugp (DRV_NAME
            ": hardware ethernet addr =%02X:%02X:%02X:%02X:%02X:%02X\n",
            (data2) & 0xFF, (data2 >> 8) & 0xFF, (data2 >> 16) & 0xFF,
            (data2 >> 24) & 0xFF, (data) & 0xFF, (data >> 8) & 0xFF);
}

/**
 * Function    : synop3504_on_link_up
 * Purpose      : Called when driver found link up event.
 * Parameters   : dev->network device structure
 * Return value : error code.
 */
static int
synop3504_on_link_up (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    int speed_100 = 0;
    uint32_t data;

    //Set init register values
    SynopsysWriteDmaReg (tc, DmaBusMode, DmaBusModeInit);
    SynopsysWriteDmaReg (tc, DmaRxBaseAddr, pr->rx->phys);
    SynopsysWriteDmaReg (tc, DmaTxBaseAddr, pr->tx->phys);

    //Select DMA speed
    switch (pr->OUI_model)
    {
    case ICS_1893BF:
    case ICS_1893CF:
        data = SynopsysMiiRead (tc, MII_QPDSR, AUTOMATIC_PHY_RESOLUTION);
        speed_100 = data & QPDSR_SPEED100;
        break;

    case MICREL_KS8721:
        //using PHYCTRL instead of BMCR
        data = SynopsysMiiRead (tc, MII_PHYCTRL, AUTOMATIC_PHY_RESOLUTION);
        speed_100 = (data & PHYCTRL_OP_MODE) == PHYCTRL_OM_100HALF
            || (data & PHYCTRL_OP_MODE) == PHYCTRL_OM_100FULL;
        break;

    case VITESSE_VSC8601:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
        //BMCR duplex status after autonegotiation is not standard : works on RTL8201CP
        data = SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION);
        speed_100 = data & BMCR_SPEED100;
        break;

    default:
        break;
    }

    if (speed_100)
        SynopsysWriteDmaReg (tc, DmaControl, DmaControlInit100);
    else
        SynopsysWriteDmaReg (tc, DmaControl, DmaControlInit10);

    //Disable interrupts
    SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntDisable);

    debugp (DRV_NAME ": %s: autoned - DMA Tuned\n", dev->name);

    //Write MAC Address
    synop3504_setup_ethernet_address (dev);

    //Restore Mac configuration
    synop3504_restore_mac_register (dev);

    //Start DMAs
    SynopsysWriteDmaReg (tc, DmaStatus, 0xFFFFFFFF);
    SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntEnable);
    SynopsysSetDmaReg (tc, DmaControl, DmaRxStart);

    return 0;
}

/**
 * Function    : synop3504_on_link_down
 * Purpose      : Called when driver found link down event.
 * Parameters   : dev->network device structure
 * Return value : void.
 */
static void
synop3504_on_link_down (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    //Save Mac configuration
    synop3504_save_mac_register (dev);

    //Stop RX and TX
    SynopsysClearDmaReg (tc, DmaControl, DmaTxStart);
    SynopsysClearDmaReg (tc, DmaControl, DmaRxStart);

    //Reset DMA engine
    while (SynopsysReadDmaReg (tc, DmaBusMode) & DmaResetOn);
    SynopsysWriteDmaReg (tc, DmaBusMode, DmaResetOn);
    while (SynopsysReadDmaReg (tc, DmaBusMode) & DmaResetOn);
    SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntDisable);

    //Reset descriptors
    syeth_dma_tx_rb_reset (pr->tx);
    syeth_dma_rx_rb_reset (pr->rx);
}

/**
 * Function    : synop3504_scan_link
 * Purpose      : check the link status.
 * parameters   : dev->device address
 * return value : value of the link status.
 */
static int
synop3504_scan_link (struct net_device *dev)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    SynopsysMiiRead (tc, MII_BMSR, AUTOMATIC_PHY_RESOLUTION);    //TODO:Dummy Read

    switch (pr->OUI_model)
    {
    case ICS_1893BF:
    case ICS_1893CF:
        return SynopsysMiiRead (tc, MII_QPDSR,
                                AUTOMATIC_PHY_RESOLUTION) & QPDSR_LSTATUS;

    case VITESSE_VSC8601:
    case MICREL_KS8721:
    case REALTEK_RTL8201CP:
    case ICPLUS_IP101A:
    case JVA_1893BF:
        return SynopsysMiiRead (tc, MII_BMSR,
                                AUTOMATIC_PHY_RESOLUTION) & BMSR_LSTATUS;

    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
        return 1;

    default:
        // No links up
        return 0;
    }

    return 0;
}

/**
 * Function    : synop3504_timer
 * Purpose      : Poll the link status to check connexion/unconnexion
 *                and restart autonegociation.
 * parameters   : data->device address
 * return value : void.
 */
static void
synop3504_timer (unsigned long data)
{
    struct net_device *dev = (struct net_device *) data;
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    if (!synop3504_scan_link (dev))
    {
        if (netif_carrier_ok (dev))
        {
            //OK. LINK DOWN.
            //            debugp(DRV_NAME ": %s: " "Link DOWN\n", dev->name);
            printk (DRV_NAME ": %s: " "Link DOWN\n", dev->name);
            netif_stop_queue (dev);
            netif_carrier_off (dev);
            synop3504_on_link_down (dev);
        }
        else
        {
#ifdef SYNOP_ETH_DEBUG_LINK
            debugp (DRV_NAME ": %s: Link still down\n", dev->name);
#endif
        }
    }
    else
    {
        if (!netif_carrier_ok (dev))
        {
            //OK. link UP.
            //            debugp(DRV_NAME ": %s: Link UP\n", dev->name);
            printk (DRV_NAME ": %s: Link UP\n", dev->name);
            //Check if it is the first time the link goes up
            if (!init_finish)
            {
                //save the current value for promiscuous mode multicast....
                pr->macreg.GmacFrameFilter =
                    SynopsysReadMacReg (tc, GmacFrameFilter);
                synop3504_an_complete (dev);
                synop3504_on_link_up (dev);
                netif_carrier_on (dev);
                netif_wake_queue (dev);
                //set default config into hardware
                synop3504_mac_config (dev);
                //store the config saved before for promiscuous mode multicast....
                SynopsysWriteMacReg (tc, GmacFrameFilter,
                                     pr->macreg.GmacFrameFilter);
                init_finish = 1;
            }
            else
            {
                synop3504_an_complete (dev);
                synop3504_on_link_up (dev);
                netif_carrier_on (dev);
                netif_wake_queue (dev);
            }
        }
        else
        {
#ifdef SYNOP_ETH_DEBUG_LINK
            debugp (DRV_NAME ": %s: Link still up\n", dev->name);
#endif
            //save the current value for promiscuous mode multicast....
            pr->macreg.GmacFrameFilter =
                SynopsysReadMacReg (tc, GmacFrameFilter);
        }
    }
    mod_timer (&pr->timer, jiffies + TIMEOUT);
}

/**
 * Function    : synop3504_stop
 * Purpose      : Stop the device.
 * Parameters   : device address
 * Return value : error code.
 */
static int
synop3504_stop (struct net_device *dev)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;

    if (dev == NULL)
        return -1;
    pr = (Private *) dev->priv;
    tc = &pr->synopsys;

    debugp (DRV_NAME ": %s: stop\n", dev->name);

    //Stop timer thread
    del_timer_sync (&pr->timer);

    //Disable transmitter
    netif_stop_queue (dev);
    netif_carrier_off (dev);

    //Device stopped
    SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntDisable);
    SynopsysClearDmaReg (tc, DmaControl, DmaRxStart);
    SynopsysClearDmaReg (tc, DmaControl, DmaTxStart);

    //Reset DMA engine
    while (SynopsysReadDmaReg (tc, DmaBusMode) & DmaResetOn);
    SynopsysWriteDmaReg (tc, DmaBusMode, DmaResetOn);
    while (SynopsysReadDmaReg (tc, DmaBusMode) & DmaResetOn);
    SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntDisable);

    //Disconnect from IRQ
    free_irq (dev->irq, dev);

    //Delete DMA descriptors
    syeth_dma_rx_rb_free (pr->rx);
    pr->rx = NULL;
    syeth_dma_tx_rb_free (pr->tx);
    pr->tx = NULL;
    return 0;
}

/**
 * Function    : synop3504_hard_start_xmit
 * Purpose      : Transmit the packet to the destination.
 * Parameters   : skb->includes all the headers and data
 *          dev->device address
 * Return value : error code.
 */
static int
synop3504_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;
    int r = 0;

    //Check pointers
    if (dev == NULL)
        return -EBUSY;
    pr = (Private *) dev->priv;
    if (pr == NULL)
        return -EBUSY;
    tc = &pr->synopsys;
    if (tc == NULL)
        return -EBUSY;
    if (skb == NULL)
        return 1;

    debugp (DRV_NAME ": hard_start_xmit(%s)\n", dev->name);

    /* Transmit the packet to hardware. */
    r = syeth_dma_tx_rb_put (pr->tx, skb);
    if (r < 0)
    {
        /* should never occur because descriptor expiration have
         * to be detected during the previous pass */
        netif_stop_queue (dev);
        printk (DRV_NAME ": %s: No more free tx descriptors\n", dev->name);
        return 1;
    }
    if (r > 0)
    {
        /* no more space in TX desc buffer, transmission will be respawned
         * when interrupt or poll will came on. */
        netif_stop_queue (dev);
        debugp (DRV_NAME ": %s: No more space on device om d=%d. Stop queue\n",
                dev->name, r - 1);
    }

    SynopsysSetDmaReg (tc, DmaControl, DmaTxStart);
    SynopsysWriteDmaReg (tc, DmaTxPollDemand, 0);

    dev->trans_start = jiffies;

    return 0;
}

/**
 * Function    : synop3504_set_multicast
 * Purpose      : This will control promiscuous mode.
 * Parameters   : dev->device address
 * Return value : void.
 */
static void
synop3504_set_multicast (struct net_device *dev)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;
    uint32_t value;

    pr = (Private *) dev->priv;
    if (pr == NULL)
        return;

    tc = &pr->synopsys;
    if (tc == NULL)
        return;

    value = SynopsysReadMacReg (tc, GmacFrameFilter);

    //Set promiscuous mode if it's asked for.
    if (dev->flags & IFF_PROMISC)
    {
        if (!(value & GmacPromiscuousModeOn))
        {
            value |= GmacPromiscuousModeOn;
            SynopsysWriteMacReg (tc, GmacFrameFilter, value);
            return;
        }
    }
    else
    {
        if (value & GmacPromiscuousModeOn)
        {
            value &= ~GmacPromiscuousModeOn;
            SynopsysWriteMacReg (tc, GmacFrameFilter, value);
            return;
        }
    }

    //Hardware cannot filter multicast addresses
    //Just block or unblock all addresses
    if (dev->flags & IFF_ALLMULTI || dev->mc_count > 0)
        value |= GmacMulticastFilterOff;
    else
        value &= ~GmacMulticastFilterOff;

    SynopsysWriteMacReg (tc, GmacFrameFilter, value);
}

/**
 * Function    : synop3504_ethtool_ioctl
 * Purpose      : This will control the device for ethtool.
 * Parameters   : dev->device address
 *          useraddr->user data address
 * Return value : error code.
 */
#ifndef MODULE
static int
synop3504_ethtool_ioctl (struct net_device *dev, void *useraddr)
{
    uint32_t ethcmd;
    Private *pr = (Private *) dev->priv;

    //dev_ioctl() in ../../net/core/dev.c has already checked
    //capable(CAP_NET_ADMIN), so don't bother with that here.

    if (get_user (ethcmd, (uint32_t *) useraddr))
        return -EFAULT;

    debugp (DRV_NAME ": %s: ethtool(cmd=%08x)\n", dev->name, ethcmd);
    switch (ethcmd)
    {
    case ETHTOOL_GDRVINFO:
        {
            struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
            strcpy (info.driver, DRV_NAME);
            strcpy (info.version, DRV_VERSION);
            info.regdump_len = get_regdump_len (dev);
            if (copy_to_user (useraddr, &info, sizeof (info)))
                return -EFAULT;
            return 0;
        }

        //Get settings
    case ETHTOOL_GSET:
        {
            struct ethtool_cmd ecmd = { ETHTOOL_GSET };
            mii_ethtool_gset (&pr->mii_if, &ecmd);
            if (copy_to_user (useraddr, &ecmd, sizeof (ecmd)))
                return -EFAULT;
            return 0;
        }

        //Set settings
    case ETHTOOL_SSET:
        {
            int r;
            struct ethtool_cmd ecmd;

            if (copy_from_user (&ecmd, useraddr, sizeof (ecmd)))
                return -EFAULT;
            r = mii_ethtool_sset (&pr->mii_if, &ecmd);
            return r;
        }

        //Restart autonegotiation
    case ETHTOOL_NWAY_RST:
        {
            return synop3504_autonegotiate (dev, 1);
        }
        //Get link status
    case ETHTOOL_GLINK:
        {
            struct ethtool_value edata = { ETHTOOL_GLINK };
            edata.data = mii_link_ok (&pr->mii_if);
            if (copy_to_user (useraddr, &edata, sizeof (edata)))
                return -EFAULT;
            return 0;
        }

    case ETHTOOL_GREGS:
        {
            char buffer[512];
            int len = get_regdump_len (dev);

            if (copy_from_user (buffer, useraddr, sizeof (u32) * 3))
                return -EFAULT;

            fill_regdump (dev, (struct ethtool_regs *) buffer);

            if (copy_to_user
                (useraddr, &buffer, sizeof (struct ethtool_regs) + len))
                return -EFAULT;

            return 0;
        }
    case ETHTOOL_RVLAN_TABLE:
        {
            struct ethtool_vlanparam ecmd;

            if (synop3504_read_vlan_table (dev, &ecmd))
                return -EFAULT;

            if (copy_to_user (useraddr, &ecmd, sizeof (ecmd)))
                return -EFAULT;

            return 0;
        }
    case ETHTOOL_SVLAN_TABLE:
        {
            struct ethtool_vlanparam ecmd;

            if (copy_from_user (&ecmd, useraddr, sizeof (ecmd)))
                return -EFAULT;

            if (synop3504_write_vlan_table (dev, &ecmd))
                return -EFAULT;

            return 0;
        }

    default:
        break;
    }
    return -EOPNOTSUPP;
}
#endif

/**
 * Function    : synop3504_do_ioctl
 * Purpose      : This will control the device.
 * Parameters   : dev->device address
 *          cmd->command to execute
 * Return value : error code.
 */
static int
synop3504_do_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;
    uint16_t reg;

    if (ifr == NULL)
        return -1;
    if (dev == NULL)
        return -1;
    pr = (Private *) dev->priv;
    if (pr == NULL)
        return -1;
    tc = &pr->synopsys;
    if (tc == NULL)
        return -1;

    debugp (DRV_NAME ": %s: do_ioctl(cmd=%08x)\n", dev->name, cmd);

    switch (cmd)
    {
    case SIOCGLINKSTATUS:
        ifr->ifr_data = (void *) synop3504_scan_link (dev);
        break;

    case SIOCGSETLINKSPEED:
        eth_speed = (int) ifr->ifr_data;
        switch (eth_duplex_mode)
        {
        case 0:
            if (!eth_speed)
                phy_modes = ADVERTISE_10HALF | ADVERTISE_10FULL |
                    ADVERTISE_100HALF | ADVERTISE_100FULL;
            else if (eth_speed == 10)
                phy_modes = ADVERTISE_10HALF | ADVERTISE_10FULL;
            else
                phy_modes = ADVERTISE_100HALF | ADVERTISE_100FULL;
            break;
        case 1:
            if (!eth_speed)
                phy_modes = ADVERTISE_10FULL | ADVERTISE_100FULL;
            else if (eth_speed == 10)
                phy_modes = ADVERTISE_10FULL;
            else
                phy_modes = ADVERTISE_100FULL;
            break;
        case 2:
            if (!eth_speed)
                phy_modes = ADVERTISE_10HALF | ADVERTISE_100HALF;
            else if  (eth_speed == 10)
                phy_modes = ADVERTISE_10HALF;
            else
                phy_modes = ADVERTISE_100HALF;
            break;
        }
        synop3504_enable_autonegotiation (dev);
        synop3504_autonegotiate (dev, 1);
        break;

    case SIOCGGETLINKSETUPSPEED:
        ifr->ifr_data = (void *) eth_speed;
        break;
    case SIOCGGETLINKCURRENTSPEED:
        reg = SynopsysMiiRead (tc, MII_QPDSR, AUTOMATIC_PHY_RESOLUTION);
        if (reg & QPDSR_SPEED100)
            ifr->ifr_data = (void *) 100;
        else
            ifr->ifr_data = (void *) 10;
        break;
    case SIOCGSETLINKMODE:
        eth_duplex_mode = (int) ifr->ifr_data;
        switch (eth_duplex_mode)
        {
        case 0:
            if (!eth_speed)
                phy_modes = ADVERTISE_10HALF | ADVERTISE_10FULL
                    | ADVERTISE_100HALF | ADVERTISE_100FULL;
            else if (eth_speed == 10)
                phy_modes =  ADVERTISE_10HALF | ADVERTISE_10FULL;
            else
               phy_modes = ADVERTISE_100HALF | ADVERTISE_100FULL;
            break;
        case 1:
            if (!eth_speed)
                phy_modes = ADVERTISE_10FULL | ADVERTISE_100FULL;
            else if (eth_speed == 10)
                phy_modes = ADVERTISE_10FULL;
            else
                phy_modes = ADVERTISE_100FULL;
            break;
        case 2:
            if (!eth_speed)
                phy_modes = ADVERTISE_10HALF | ADVERTISE_100HALF;
            else if (eth_speed == 10)
                phy_modes = ADVERTISE_10HALF;
            else
                phy_modes = ADVERTISE_100HALF;
        }
        synop3504_enable_autonegotiation (dev);
        synop3504_autonegotiate (dev, 1);
        break;

    case SIOCGGETLINKSETUPMODE:
        ifr->ifr_data = (void *) eth_duplex_mode;
        break;

    case SIOCGGETLINKCURRENTMODE:
        reg = SynopsysMiiRead (tc, MII_QPDSR, AUTOMATIC_PHY_RESOLUTION);
        if (reg & QPDSR_FULLDPLX)
            ifr->ifr_data = (void *) ETH_MODE_FD;
        else
            ifr->ifr_data = (void *) ETH_MODE_HD;
        break;

#ifndef MODULE
    case SIOCETHTOOL:        //EthTool Interface
        return synop3504_ethtool_ioctl (dev, (void *) ifr->ifr_data);
#endif

    default:
        return -EOPNOTSUPP;
    }

    return 0;
}

/**
 * Function    : synop3504_get_stats
 * Purpose      : Read the packet status from the device.
 * Parameters   : dev->device address
 * Return value : return the device stats.
 */
static struct net_device_stats *
synop3504_get_stats (struct net_device *dev)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;

    if (dev == NULL)
        return NULL;

    debugp (DRV_NAME ": Synopsys::get_stats(%s)\n", dev->name);

    pr = (Private *) dev->priv;
    if (pr == NULL)
        return NULL;

    tc = &pr->synopsys;
    if (tc == NULL)
        return NULL;

    return &pr->stats;
}

/**
 * Function    : synop3504_change_mtu
 * Purpose      : Change the MTU.
 * Parameters   : dev->device address
 *          new_mtu->the new mtu value
 * Return value : error code.
 */
static int
synop3504_change_mtu (struct net_device *dev, int new_mtu)
{
    if (new_mtu < 64 || new_mtu > 1508)
        return -EINVAL;
    else
    {
        //should reset the port first, no?
        dev->mtu = new_mtu;
        return 0;
    }
}

/**
 * Function    : synop3504_set_mac_address
 * Purpose      : Change the MAC address.
 * Parameters   : dev->device address
 *                p->mac addr source
 * Return value : error code.
 */
static int
synop3504_set_mac_address (struct net_device *dev, void *p)
{
    struct sockaddr *addr = p;

    memcpy (dev->dev_addr, addr->sa_data, dev->addr_len);
    synop3504_setup_ethernet_address (dev);

    return 0;
}

/**
 * Function    : mdio_read
 * Purpose      : read a MII register.
 * Parameters   : dev->device address
 *                phy->phy addr (not used for us)
 *                reg->register to read
 * Return value : value read.
 */
static int
mdio_read (struct net_device *dev, int phy, int reg)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    return SynopsysMiiRead (tc, reg, (s16) phy);
}

/**
 * Function    : mdio_write
 * Purpose      : write a MII register.
 * Parameters   : dev->device address
 *                phy->phy addr (not used for us)
 *                reg->register to write
 *                value->value to write
 * Return value : void.
 */
static void
mdio_write (struct net_device *dev, int phy, int reg, int value)
{
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;

    SynopsysMiiWrite (tc, reg, value, (s16) phy);
}

/**
 * Function : synop3504_phy_init
 * Purpose      : This function get the OUI from PHY and check that it is alive.
 * parameters   : priv->private context
 * return value : error code.
 */
static int
synop3504_phy_init (Private * priv)
{
    int id2;
    if (priv == NULL)
        return -1;
    /* Get PHY ID. */
    priv->phy_id = SynopsysMiiRead (&priv->synopsys, MII_PHYSID1,
                                    AUTOMATIC_PHY_RESOLUTION) << 16
        | SynopsysMiiRead (&priv->synopsys, MII_PHYSID2,
                           AUTOMATIC_PHY_RESOLUTION);

    if ((priv->phy_id == 0x0 || priv->phy_id == 0xFFFFFFFF)
        && priv->synopsys.phyAddr == IP175_VIRTUAL_PHY_ADDR)
    {
        /* It may be an IP175 C/D. This component does not give its ID on
         * "virtual" phy 5, we have to interrogate one of the real phys
         * (numbered from 0 to 4). */
        priv->phy_id = SynopsysMiiRead (&priv->synopsys, MII_PHYSID1, 0) << 16
            | SynopsysMiiRead (&priv->synopsys, MII_PHYSID2, 0);
    }
    if (priv->phy_id == 0x0 || priv->phy_id == 0xFFFFFFFF)
        return -1;
    /* Build OUI model from PHY ID. */
    priv->phy_oui = (priv->phy_id >> 10) & 0x00ffffff;
    priv->phy_model = (priv->phy_id >> 4) & 0x0000003f;
    priv->phy_rev = (priv->phy_id >> 0) & 0x0000000f;
    priv->OUI_model = (priv->phy_oui << OFFSET_OUI)
        + (priv->phy_model << OFFSET_MODEL);
    if (priv->OUI_model == ICPLUS_IP175C)
    {
        if (priv->synopsys.phyAddr != IP175_VIRTUAL_PHY_ADDR)
        {
            /* Forbid use of IP175 as a simple PHY. */
            printk (KERN_WARNING "IP175 configured with PHY addr %d, "
                    "should be %d.  NVRAM value will be overwtitten.\n",
                    priv->synopsys.phyAddr, IP175_VIRTUAL_PHY_ADDR);
            priv->synopsys.phyAddr = IP175_VIRTUAL_PHY_ADDR;
        }
        /* Try to read the Second ID number: available only in the IP175D. */
        id2 = SynopsysMiiRead (&priv->synopsys, 0, 20);
        if ((id2 == 0x175D) || (id2 == 0x0c40))
           priv->OUI_model = ICPLUS_IP175D;
    }
    return 0;
}

/**
 * Function    : synop3504_open
 * Purpose      : This will initialize the device.
 * parameters   : device address
 * return value : error code.
 */
static int
synop3504_open (struct net_device *dev)
{
    Private *pr = NULL;
    Synopsys *tc = NULL;

    //Check pointers
    if (dev == NULL)
        return -1;
    pr = (Private *) dev->priv;
    if (pr == NULL)
        return -1;
    tc = &pr->synopsys;
    if (tc == NULL)
        return -1;

    debugp (DRV_NAME ": %s: open\n", dev->name);

    //Call platform dependent hook
    synop3504_hw_prepare (pr->minor);

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
        printk (DRV_NAME ": PHY for %s is VITESSE VSC8601 rev %u\n", dev->name,
                pr->phy_rev);

#ifdef CONFIG_SYNOP3504_PHY_DINI
        /* Add a 2ns delay for TX and RX clock. */

        /* In register 23 "Extended Phy Control 1", enable "RGMII skew timing
         * compensation". */
        SynopsysMiiWrite (tc, 23, 0x0100, AUTOMATIC_PHY_RESOLUTION);

        /* To access the extended register 28E, put 1 in register 31
         * "Extended Page Access". */
        SynopsysMiiWrite (tc, 31, 0x0001, AUTOMATIC_PHY_RESOLUTION);

        /* In register 28E "RGMII Skew Control", set both TX and RX RGMII Skew
         * compensation to 2ns. */
        SynopsysMiiWrite (tc, 28, 0xF000, AUTOMATIC_PHY_RESOLUTION);

        /* Switch back to main register space, by putting 0 in register 31
         * "Extended Page Access". */
        SynopsysMiiWrite (tc, 31, 0x0000, AUTOMATIC_PHY_RESOLUTION);
#endif /* CONFIG_SYNOP3504_PHY_DINI */

        break;

    case ICS_1893BF:
        printk (DRV_NAME ": PHY for %s is ICS 1893BF rev %u\n", dev->name,
                pr->phy_rev);
        break;

    case ICS_1893CF:
        printk (DRV_NAME ": PHY for %s is ICS ICS1893CF rev %u\n", dev->name,
                pr->phy_rev);
        break;

    case MICREL_KS8721:
        printk (DRV_NAME ": PHY for %s is Micrel KS8721B/BT rev %u\n",
                dev->name, pr->phy_rev);
        break;

    case REALTEK_RTL8201CP:
        printk (DRV_NAME ": PHY for %s is Realtek RTL8201CP rev %u\n",
                dev->name, pr->phy_rev);
        break;

    case ICPLUS_IP101A:
        printk (DRV_NAME ": PHY for %s is IC+ IP101A rev %u\n", dev->name,
                pr->phy_rev);
        break;

    case ICPLUS_IP175C:
        printk (DRV_NAME ": PHY for %s is IC+ IP175C rev %u\n", dev->name,
                pr->phy_rev);
        break;

    case ICPLUS_IP175D:
        printk (DRV_NAME ": PHY for %s is IC+ IP175D rev %u\n", dev->name,
                pr->phy_rev);
        break;

    case JVA_1893BF:
        printk (DRV_NAME ": PHY for %s is ICS_NO rev %u\n", dev->name,
                pr->phy_rev);
        break;

    default:
        printk (DRV_NAME ": PHY ID for %s id is 0x%8.8x on phy N°%d\n",
                dev->name, pr->phy_id, tc->phyAddr);
    }

    debugp (DRV_NAME ": %s: open: MII status=0x%08X control=0x%08X\n",
            dev->name, SynopsysMiiRead (tc, MII_BMCR, AUTOMATIC_PHY_RESOLUTION),
            SynopsysMiiRead (tc, MII_BMSR, AUTOMATIC_PHY_RESOLUTION));

    //Set MAC address
    synop3504_setup_ethernet_address (dev);

    //Activate multicast management
    synop3504_set_multicast (dev);

    //Activate auto-negotiation
    synop3504_enable_autonegotiation (dev);

    /* Mask all GMAC interrupts. */
    SynopsysSetConfigReg (tc, GmacIntMask, 0xFF);

    //Initialize dma
    pr->rx = syeth_dma_rx_rb_static_alloc (&pr->stats);
    if (NULL == pr->rx)
    {
        debugp (DRV_NAME ": %s: open: cannot init RX DMA\n", dev->name);
        return -ENODEV;
    }
    debugp (DRV_NAME ": %s: open: RX DMA initialized: 0x%p=>0x%08X\n",
            dev->name, pr->rx->d.rx, pr->rx->phys);

    pr->tx = syeth_dma_tx_rb_static_alloc (&pr->stats);
    if (NULL == pr->tx)
    {
        debugp (DRV_NAME ": %s: open: cannot init TX DMA\n", dev->name);
        return -ENODEV;
    }
    debugp (DRV_NAME ": %s: open: TX DMA initialized: 0x%p=>0x%08X\n",
            dev->name, pr->tx->d.tx, pr->tx->phys);

    //Reset DMA engine
    while (SynopsysReadDmaReg (tc, DmaBusMode) & DmaResetOn);
    SynopsysWriteDmaReg (tc, DmaBusMode, DmaResetOn);
    while (SynopsysReadDmaReg (tc, DmaBusMode) & DmaResetOn);
    SynopsysWriteDmaReg (tc, DmaInterrupt, DmaIntDisable);

    //start autonegotiation
    synop3504_an_complete (dev);    //store default config in hardware
    netif_stop_queue (dev);    //considere link as off
    netif_carrier_off (dev);
    synop3504_on_link_down (dev);
    synop3504_autonegotiate (dev, 0);

    //Request irq
    debugp (DRV_NAME ": %s: open: request interrupt %d\n",
            dev->name, dev->irq);
    if (request_irq (dev->irq, int_handler, SA_INTERRUPT, dev->name, dev))
    {
        debugp (DRV_NAME ": Synopsys::open(%s) - interrupt %d request fail\n",
                dev->name, dev->irq);
        return -ENODEV;
    }

    //Autonegotiation polling will be started in timer thread immediately
    pr->timer.expires = jiffies;
    add_timer (&pr->timer);

    return 0;
}

/**
 * Function    : synop3504_init
 * Purpose      : Register the device with the network subsystem.
 * Parameters   : dev->device address
 * Return value : error code.
 */
static int
synop3504_init (struct net_device *dev)
{
    Private *pr = NULL;
    if (dev == NULL)
        return -ENODEV;
    pr = (Private *) dev->priv;
    // try Mem space access
    dev->base_addr = synop3504_hw_device.base_addr;
    dev->irq = synop3504_hw_device.irq;
    pr->minor = synop3504_hw_device.minor;
    debugp (DRV_NAME ": eth%d: addr=%08lx, irq=%d\n",
            pr->minor, dev->base_addr, dev->irq);
    // Init Synopsys internal data.
    synop3504_hw_prepare (pr->minor);
    SynopsysInit (&pr->synopsys, dev->base_addr,
                  dev->base_addr + MAC_OFFSET,
                  dev->base_addr + DMA_OFFSET, 0);
    if (synop3504_phy_init (pr))
    {
        printk (DRV_NAME ": eth%d: Could not find valid PHY at address %d\n",
                synop3504_hw_device.minor, 0);
        return -1;
    }
    //Reset PHY
    synop3504_reset_phy (dev);
    //Initialisation functions
    dev->open = &synop3504_open;
    dev->hard_start_xmit = &synop3504_hard_start_xmit;
    dev->stop = &synop3504_stop;
    dev->do_ioctl = &synop3504_do_ioctl;
    dev->set_multicast_list = &synop3504_set_multicast;
    dev->get_stats = &synop3504_get_stats;
    dev->set_mac_address = &synop3504_set_mac_address;
    dev->change_mtu = &synop3504_change_mtu;
    dev->weight = 16;
    dev->poll = &synop3504_poll;
    if (debug)
        dev->flags |= IFF_DEBUG;
    //Set MAC Address
    char mac_address[6] = { 0x00, 0x13, 0xD7, 0x10, 0x10, 0x01 };
    memcpy (dev->dev_addr, mac_address, 6);
    //Initialise timer thread
    init_timer (&pr->timer);
    pr->timer.data = (unsigned long) dev;
    pr->timer.function = synop3504_timer;
    //Init MII information sturcture
    pr->mii_if.dev = dev;
    pr->mii_if.mdio_read = mdio_read;
    pr->mii_if.mdio_write = mdio_write;
    pr->mii_if.phy_id = pr->synopsys.phyAddr;
    return 0;
}

#ifdef CONFIG_PROC_FS
static int
synop3504_readproc_stats (char *buf, char **start, off_t offset,
                          int count, int *eof, void *data)
{
    //    struct net_device *dev = (struct net_device *) data;
    //    uint32_t reg;
    char *p;

    p = buf;
    p += sprintf (p, "TBD\n");
    /*
     * DISPLAY_DRVSTAT("Events Dropped Counter (Rx Overruns)",0x058);
     * DISPLAY_DRVSTAT("FCS/Alignment Errors Counter",0x05C);
     * DISPLAY_DRVSTAT("Runt Packets Counter",0x060);
     * DISPLAY_DRVSTAT("Oversize Packets Counter",0x064);
     * DISPLAY_DRVSTAT("Fragment Packets Counter",0x068);
     * DISPLAY_DRVSTAT("Jabber Counter",0x06C);
     * DISPLAY_DRVSTAT("Transmit Underruns Counter",0x070);
     * DISPLAY_DRVSTAT("Late Collisions Counter",0x074);
     * DISPLAY_DRVSTAT("Carrier Loss Counter",0x078);
     * DISPLAY_DRVSTAT("Collisions Counter: 1  or More Back-to-Back",0x07C);
     * DISPLAY_DRVSTAT("Collisions Counter: 2  or More Back-to-Back",0x080);
     * DISPLAY_DRVSTAT("Collisions Counter: 3  or More Back-to-Back",0x084);
     * DISPLAY_DRVSTAT("Collisions Counter: 4  or More Back-to-Back",0x088);
     * DISPLAY_DRVSTAT("Collisions Counter: 5  or More Back-to-Back",0x08C);
     * DISPLAY_DRVSTAT("Collisions Counter: 6  or More Back-to-Back",0x090);
     * DISPLAY_DRVSTAT("Collisions Counter: 7  or More Back-to-Back",0x094);
     * DISPLAY_DRVSTAT("Collisions Counter: 8  or More Back-to-Back",0x098);
     * DISPLAY_DRVSTAT("Collisions Counter: 9  or More Back-to-Back",0x09C);
     * DISPLAY_DRVSTAT("Collisions Counter: 10 or More Back-to-Back",0x0A0);
     * DISPLAY_DRVSTAT("Collisions Counter: 11 or More Back-to-Back",0x0A4);
     * DISPLAY_DRVSTAT("Collisions Counter: 12 or More Back-to-Back",0x0A8);
     * DISPLAY_DRVSTAT("Collisions Counter: 13 or More Back-to-Back",0x0AC);
     * DISPLAY_DRVSTAT("Collisions Counter: 14 or More Back-to-Back",0x0B0);
     * DISPLAY_DRVSTAT("Collisions Counter: 15 or More Back-to-Back",0x0B4);
     */
    *eof = 1;
    return p - buf + 1;
}

static int
synop3504_readproc_internals (char *buf, char **start, off_t offset,
                              int count, int *eof, void *data)
{
    //    struct net_device *dev = (struct net_device *) data;
    //    struct net_priv *priv = dev->priv;
    char *p;

    p = buf;

    p += sprintf (p, "TBD\n");
    /*
     * DISPLAY_INTRN(dma_isr);
     * DISPLAY_INTRN(dma_poll_rx);
     * DISPLAY_INTRN(dma_poll_tx);
     * DISPLAY_INTRN(int_ahb_err);
     * DISPLAY_INTRN(mac_lcarr);
     * DISPLAY_INTRN(mac_rx_or);
     * DISPLAY_INTRN(rx_status_ok);
     * DISPLAY_INTRN(rx_status_err);
     * if (priv->drvstats.rx_status_err)
     * {
     * DISPLAY_INTRN(viol_4b_5b);
     * DISPLAY_INTRN(drib_nib);
     * DISPLAY_INTRN(crc_err);
     * DISPLAY_INTRN(bad_pre);
     * DISPLAY_INTRN(long_ev);
     * DISPLAY_INTRN(bad_packet);
     * DISPLAY_INTRN(late_carr_ev);
     * DISPLAY_INTRN(reserved);
     * }
     * DISPLAY_INTRN(rx_eof_err);
     * DISPLAY_INTRN(rx_alloc_err);
     * DISPLAY_INTRN(tx_status_err);
     * DISPLAY_INTRN(tx_qfull_err);
     * DISPLAY_INTRN(tx_skb_err);
     * DISPLAY_INTRN(tx_bmd_valid_err);
     * p += sprintf(p,"tx_queue_stopped = %d\n",priv->tx_queue_stopped);
     */
    *eof = 1;
    return p - buf + 1;
}

#define DISPLAY_PHYREG_DIRTY(tc, phy , mii) \
    SynopsysMiiRead(tc, mii, phy); \
    p += sprintf(p,"PHY%d MR%-2d 0x%4.4x\n",phy,mii,reg)

static int
synop3504_readproc_regs_mii (char *buf, char **start, off_t offset,
                             int count, int *eof, void *data)
{
    int phy_begin, phy_end, phy_addr;
    struct net_device *dev = (struct net_device *) data;
    Private *pr = (Private *) dev->priv;
    Synopsys *tc = &pr->synopsys;
    int reg;
    char *p;

    p = buf;

    switch (pr->OUI_model)
    {
    case ICPLUS_IP175C:
    case ICPLUS_IP175D:
        phy_begin = 0;
        phy_end = 5;
        break;

    default:
        phy_begin = AUTOMATIC_PHY_RESOLUTION;
        phy_end = AUTOMATIC_PHY_RESOLUTION;
        break;
    }

    for (phy_addr = phy_begin; (phy_addr <= phy_end); phy_addr++)
    {
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_BMCR);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_BMSR);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_PHYSID1);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_PHYSID2);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_ADVERTISE);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_LPA);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_EXPANSION);
        reg = DISPLAY_PHYREG_DIRTY (tc, phy_addr, MII_TPISTATUS);
        p += sprintf (p, "\n");
    }

    *eof = 1;
    return p - buf + 1;
}

static int
synop3504_writeproc_regs_mii (struct file *file, const char *buffer,
                              unsigned long count, void *data)
{
    struct net_device *dev = (struct net_device *) data;
    unsigned int reg;
    int value;
    int phy;

    if (sscanf (buffer, "PHY%d MII%u %i", &phy, &reg, &value) == 3)
    {
        mdio_write (dev, phy, reg, value);
        printk ("wrote PHY%d MII%d 0x%-4x\n", phy, reg, value);
    }

    return count;
}

static int
synop3504_readproc_phy_model (char *buf, char **start, off_t offset,
                              int count, int *eof, void *data)
{
    struct net_device *dev = (struct net_device *) data;
    Private *pr = (Private *) dev->priv;
    char *p;

    p = buf;

    switch (pr->OUI_model)
    {
    case VITESSE_VSC8601:
        p += sprintf (p, "VITESSE VSC8601\n");
        break;

    case ICPLUS_IP175C:
        p += sprintf (p, "ICPLUS IP175C\n");
        break;

    case ICPLUS_IP175D:
        p += sprintf (p, "ICPLUS IP175D\n");
        break;

    case ICS_1893BF:
        p += sprintf (p, "ICS 1893BF\n");
        break;

    case ICS_1893CF:
        p += sprintf (p, "ICS 1893CF\n");
        break;

    case MICREL_KS8721:
        p += sprintf (p, "MICREL KS8721\n");
        break;

    case REALTEK_RTL8201CP:
        p += sprintf (p, "REALTEK RTL8201CP\n");
        break;

    case ICPLUS_IP101A:
        p += sprintf (p, "ICPLUS IP101A\n");
        break;

    case JVA_1893BF:
        p += sprintf (p, "JVA 1893BF\n");
        break;

    default:
        p += sprintf (p, "Unknown Chipset %08x\n", pr->OUI_model);
        break;
    }

    *eof = 1;
    return p - buf + 1;
}

#endif

static struct net_device *_device;

/**
 * Function    : synop3504_eth_init
 * Purpose      : Initialize the module.
 * parameters   : void
 * return value : error code.
 */
static int
synop3504_eth_init (void)
{
    int result;
    Private *pr;
    struct net_device *dev;

    printk ("%s", version);

    dev = alloc_etherdev (sizeof (Private));
    if (NULL == dev)
    {
        result = -ENOMEM;
    }
    pr = (Private *) dev->priv;
    dev->init = synop3504_init;
    SET_MODULE_OWNER (dev);

    //Register net device
    result = register_netdev (dev);
    if (result < 0)
    {
        printk (KERN_ERR "synop3504: eth%d: Could not register device\n",
                pr->minor);
        kfree (dev->priv);
        free_netdev (dev);
    }
    else
    {
        _device = dev;
    }


#ifdef CONFIG_PROC_FS
    {
        struct proc_dir_entry *eth_dir;
        struct proc_dir_entry *regs_dir;
        struct proc_dir_entry *entry;
        struct proc_dir_entry *internals_dir;

        eth_dir = proc_mkdir ("eth", proc_net);
        create_proc_read_entry ("stats", 0, eth_dir, synop3504_readproc_stats,
                                dev);
        create_proc_read_entry ("phy_model", 0, eth_dir,
                                synop3504_readproc_phy_model, dev);
        internals_dir = proc_mkdir ("internals", eth_dir);
        create_proc_read_entry ("stats", 0, internals_dir,
                                synop3504_readproc_internals, dev);
        regs_dir = proc_mkdir ("regs", eth_dir);
        entry = create_proc_entry ("mii", 0, regs_dir);
        entry->read_proc = synop3504_readproc_regs_mii;
        entry->write_proc = synop3504_writeproc_regs_mii;
        entry->data = (int *) dev;
    }
#endif
    return result;
}

/**
 * Function    : synop3504_eth_exit
 * Purpose      : Unregister the device.
 * Parameters   : void
 * Return value : void.
 */
static void
synop3504_eth_exit (void)
{
    Private *pr = _device->priv;
    int minor = pr->minor;

    if (_device)
    {
        //Freeing private field of the net device struture
        if (pr)
            kfree (pr);

        //Freeing network device
        free_netdev (_device);

        //Unregister net device
        unregister_netdev (_device);
    }

    //cleanup Hardware part
    synop3504_hw_cleanup (minor);

}

module_init (synop3504_eth_init);
module_exit (synop3504_eth_exit);