summaryrefslogtreecommitdiff
path: root/cleopatre/linux-2.6.25.10-spc300/drivers/net/arm/synop3504.c
blob: 2724a6519748b00b81600e21990e83b8cc19fbf8 (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
/* Cleopatre project {{{
 *
 * Copyright (C) 2008 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
 *
 * }}} */
/**
 * \file    driver/net/arm/synop3504.c
 * \brief   Driver for Synopsys 3504.
 * \ingroup cleopatre_net_driver.
 *
 * Linux level part of the Ethernet 3504 Synopsys IP.
 */

#define DRV_NAME	"Synop3504"
#define DRV_VERSION	"2.0"
#define DRV_RELDATE	"jun 17, 2009"

//#define TRACE_FRAME     1
//#define TRACE(...)      printk(DRV_NAME": " __VA_ARGS__)
#define TRACE(...)


#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>

#include "synop3504_hw.h"

MODULE_AUTHOR ("SPiDCOM Technologies");
MODULE_DESCRIPTION ("Synopsys 3504 ethernet driver");
MODULE_LICENSE ("GPL");
MODULE_VERSION ("1.0");

/** Max size of a eth frame (size for rx buffer) must be align on 4 bytes */
#define PKT_BUF_SZ              1524

/** Polling link timeout */
#define TIMEOUT                 (2 * HZ)
/** Step time for AutoNegotiation */
#define AUTONEG_STEP            (100)
/** Timeout for AutoNegotiation */
#define AUTONEG_TIMEOUT         (10)
/** Wait delay */
#define MSEC_PER_JIFFY          (1000 / HZ) //10ms
/** Watchdog timeout for Tx frames */
#define TX_TIMEOUT             (4 * HZ)
/** Size of the DMA ring for tx */
#define TX_RING_SIZE            32
/** Size of the DMA ring for rx */
#define RX_RING_SIZE            32
/** Phy Address for the MII */ //TODO:put MII_PHY_ADDR in menuconfig
#define MII_PHY_ADDR            0x4
/** Supported Phy reference (IC+ 175C) */
#define OUI_ICPLUS              0x90C3
#define ICPLUS_MODEL_IP175C     0x18

/** TX management structure */
struct dma_tx
{
    SynopsysDmaTx *ring;
    struct sk_buff *skbs[TX_RING_SIZE];
    uint32_t phy_addr;
    uint32_t head_ptr;
    uint32_t tail_ptr;
};

/** RX management structure */
struct dma_rx
{
    SynopsysDmaRx *ring;
    struct sk_buff *skbs[RX_RING_SIZE];
    uint32_t phy_addr;
    uint32_t head_ptr;
    uint32_t tail_ptr;
};

/** Private structure for our net device */
struct net_priv
{
    uint32_t phy_id;
    uint32_t phy_oui;
    uint32_t phy_model;
    uint32_t phy_rev;
    Synopsys synop;
    struct mii_if_info mii_if;
    struct net_device_stats stats;
    struct dma_tx tx;
    struct dma_rx rx;
    struct timer_list timer;
    struct napi_struct napi;
};

#ifdef TRACE_FRAME
static const unsigned char __hexdigits[] = "0123456789ABCDEF";
static void sprintf_hex(unsigned char * str, const unsigned char * ptr, int len, unsigned char delim)
{
    int i, j=0;
    for(i=0; i<len; i++){
        if(i)str[j++]=delim;
        str[j++]=__hexdigits[ptr[i]>>4];
        str[j++]=__hexdigits[ptr[i]&0x0F];
    }
    str[j] = 0;
}
static void print_packet(const char * prefix, int descr, int len, struct sk_buff * skb)
{
    struct ethhdr * h;
    unsigned char src[20], dst[20], body[50];
    int l;

    h = (struct ethhdr *)skb->data;
    l =  len - 14 > 16 ? 16 : len - 14;
    sprintf_hex(src,  &h->h_source[0], 6, ':');
    sprintf_hex(dst,  &h->h_dest[0],   6, ':');
    sprintf_hex(body, ((unsigned char *)skb->data)+14,   l, ' ');

    printk("%08ld %s: d=%-3d len=%-4d proto=0x%04X src=%s dst=%s\n"
           "             body=%s\n",
           jiffies, prefix, descr, len, be16_to_cpu(h->h_proto),
           src, dst, body);
}
#else
#define print_packet(a,b,c,d)
#endif

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

/** Our global net device */
static struct net_device *synop3504_device;

/**
 * Read a MII register.
 * \param  dev  device structure.
 * \param  phy  phy addr (not used for us).
 * \param  reg  register to read.
 * \return  value read.
 */
static int mdio_read(struct net_device *dev, int phy, int reg)
{
    struct net_priv *priv = (struct net_priv*)dev->priv;
    Synopsys *synop = &priv->synop;

    if(synop)
        return SynopsysMiiRead(synop, reg);
    else
        return 0;
}// mdio_read

/**
 * Write a MII register.
 * \param  dev  device structure.
 * \param  phy  phy addr (not used for us).
 * \param  reg  register to write.
 * \param  value  value to write.
 */
static void mdio_write(struct net_device *dev, int phy, int reg, int value)
{
    struct net_priv *priv = (struct net_priv*)dev->priv;
    Synopsys *synop = &priv->synop;

    if(synop)
        SynopsysMiiWrite(synop, reg, value);
}// mdio_write


/**
 * Initialise TX frames descriptors.
 * \param  dev  device structure.
 * \return  error code.
 */
static int synop3504_txdesc_init(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    int i;
    //Alloc TX DMA descriptors area
    if((priv->tx.ring = (SynopsysDmaTx *)dma_alloc_coherent(NULL, sizeof(SynopsysDmaTx)*TX_RING_SIZE, &priv->tx.phy_addr, GFP_KERNEL|GFP_DMA)) == NULL)
    {
        printk(KERN_ERR DRV_NAME": Error allocating TX descriptor buffers for %s\n", dev->name);
        return -ENOMEM;
    }

    //Flush DMA descriptors area
    memset(priv->tx.ring, 0, sizeof(SynopsysDmaTx)*TX_RING_SIZE);

    //Prepare descriptors
    for(i=0; i<TX_RING_SIZE; i++)
    {
        priv->tx.ring[i].addr2 = priv->tx.phy_addr + ((i+1)%TX_RING_SIZE)*sizeof(SynopsysDmaTx);
        priv->tx.ring[i].ctrl.bf.addr2en = 1;
        priv->tx.skbs[i] = NULL;
    }

    //Set head and tail pointers to the first descriptor
    priv->tx.head_ptr = 0;
    priv->tx.tail_ptr = 0;

    return 0;
}// synop3504_txdesc_init

/**
 * Initialise RX frames descriptors.
 * \param  dev  device structure.
 * \return  error code.
 */
static int synop3504_rxdesc_init(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    struct sk_buff *skb;
    int i;
    //Alloc RX DMA descriptors area
    if((priv->rx.ring = (SynopsysDmaRx *)dma_alloc_coherent(NULL, sizeof(SynopsysDmaRx)*RX_RING_SIZE, &priv->rx.phy_addr, GFP_KERNEL|GFP_DMA)) == NULL)
    {
        printk(KERN_ERR DRV_NAME": Error allocating RX descriptor buffers for %s\n", dev->name);
        return -ENOMEM;
    }

    //Flush DMA descriptors area
    memset(priv->rx.ring, 0, sizeof(SynopsysDmaRx)*RX_RING_SIZE);

    //Prepare descriptors
    for(i=0; i<RX_RING_SIZE; i++)
    {
        //Config descriptor
        priv->rx.ring[i].addr2 = priv->rx.phy_addr + ((i+1)%RX_RING_SIZE)*sizeof(SynopsysDmaRx);
        priv->rx.ring[i].ctrl.bf.addr2en = 1;

        //Prepare data
        skb = alloc_skb(PKT_BUF_SZ, GFP_KERNEL | GFP_DMA);
        if(!skb)
        {
            printk(KERN_ERR DRV_NAME": Error allocating RX buffers for %s\n",dev->name);
            return -ENOMEM;
        }

        priv->rx.ring[i].addr1 = (uint32_t)dma_map_single(NULL, skb->data, PKT_BUF_SZ, DMA_FROM_DEVICE);
        priv->rx.skbs[i] = skb;

        //Set data length
        priv->rx.ring[i].ctrl.bf.length1 = PKT_BUF_SZ;
        //Set own bit
        priv->rx.ring[i].status.bf.dma_own = 1;
    }

    //Set head and tail pointers to the first descriptor
    priv->rx.head_ptr = 0;
    priv->rx.tail_ptr = 0;

    return 0;
}// synop3504_rxdesc_init

/**
 * Uninitialise TX frames descriptors.
 * \param  dev  device structure.
 */
static void synop3504_txdesc_uninit(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    int i;

    for(i=0 ; i<TX_RING_SIZE ; i++)
    {
        if(priv->tx.skbs[i])
        {
            dev_kfree_skb_any(priv->tx.skbs[i]);
        }
    }
    dma_free_coherent(NULL, sizeof(SynopsysDmaTx)*TX_RING_SIZE, priv->tx.ring, priv->tx.phy_addr);
}// synop3504_txdesc_uninit

/**
 * Uninitialise RX frames descriptors.
 * \param  dev  device structure.
 */
static void synop3504_rxdesc_uninit(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    int i;

    for(i=0 ; i<RX_RING_SIZE ; i++)
    {
        if(priv->rx.skbs[i])
        {
            dev_kfree_skb_any(priv->rx.skbs[i]);
        }
    }
    dma_free_coherent(NULL, sizeof(SynopsysDmaTx)*TX_RING_SIZE, priv->rx.ring, priv->rx.phy_addr);
}// synop3504_rxdesc_uninit

/**
 * Reset TX frames descriptors.
 * \param  dev  device structure.
 */
static void synop3504_txdesc_reset(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    int i;

    //Flush DMA descriptors area
    memset(priv->tx.ring, 0, sizeof(SynopsysDmaTx)*TX_RING_SIZE);

    //Prepare descriptors
    for(i=0; i<TX_RING_SIZE; i++)
    {
        priv->tx.ring[i].addr2 = priv->tx.phy_addr + ((i+1)%TX_RING_SIZE)*sizeof(SynopsysDmaTx);
        priv->tx.ring[i].ctrl.bf.addr2en = 1;
        if(priv->tx.skbs[i])
        {
            dev_kfree_skb_any(priv->tx.skbs[i]);
        }
        priv->tx.skbs[i] = NULL;
    }

}// synop3504_txdesc_reset

/**
 * Reset RX frames descriptors.
 * \param  dev  device structure.
 */
static void synop3504_rxdesc_reset(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    int i;

    //Only set own bit is enough
    for(i=0 ; i<RX_RING_SIZE ; i++)
    {
        priv->rx.ring[i].status.val = 0;
        priv->rx.ring[i].status.bf.dma_own = 1;
    }

}// synop3504_rxdesc_reset


/**
 * What to do when a link up appears.
 * \param  dev  device structure.
 */
static int synop3504_on_link_up(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    Synopsys *synop = &priv->synop;
    int speed_100;
    int fdx;

    //Find ETH speed and duplex for DMA config
    speed_100 = (mdio_read(dev, 0, MII_BMCR) & BMCR_SPEED100) != 0;
    fdx = (mdio_read(dev, 0, MII_BMCR) & BMCR_FULLDPLX) != 0;
    SynopsysSetSpeedDuplex(synop, speed_100, fdx);

    //Start TX and RX DMA
    SynopsysStartTx(synop);
    SynopsysStartRx(synop);

    //Enable Interrupts
    SynopsysEnableInt(synop);

    return 0;
}// synop3504_on_link_up

/**
 * What to do when a link down appears.
 * \param  dev  device structure.
 */
static void synop3504_on_link_down(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv *)dev->priv;
    Synopsys *synop = &priv->synop;

    //Disable Interrupts
    SynopsysDisableInt(synop);

    //Stop RX and TX
    SynopsysStopRx(synop);
    SynopsysStopTx(synop);

    //Reset descriptors
    synop3504_txdesc_reset(dev);
    synop3504_rxdesc_reset(dev);

}// synop3504_on_link_down

/**
 * Enable Auto-Negotiation for PHY.
 * \param  dev  device structure.
 */
static void synop3504_enable_autonegotiation(struct net_device *dev)
{
    volatile int data;

#ifdef CONFIG_MACH_ARIZONA
    //Force hardware to be 10M FULL just for first tests
    //Disable Auto-Negotiation
    data = mdio_read(dev, 0, MII_BMCR);
    data &= ~BMCR_ANENABLE;
    mdio_write(dev, 0, MII_BMCR, data);

    //Force 10M Full Duplex
    data = mdio_read(dev, 0, MII_BMCR);
    data &= ~BMCR_SPEED100;
    data |= BMCR_FULLDPLX;
    mdio_write(dev, 0, MII_BMCR, data);
#else
    //Ensure that PHY is Auto-Negotiation capable
    if (mdio_read(dev, 0, MII_BMSR) & BMSR_ANEGCAPABLE)
    {
        //Disable Auto-Negotiation
        data = mdio_read(dev, 0, MII_BMCR);
        data &= ~BMCR_ANENABLE;
        mdio_write(dev, 0, MII_BMCR, data);

        //Set Auto-Negotiation advertisement register - all techs
        data = mdio_read(dev, 0, MII_ADVERTISE);
        data |= ADVERTISE_10HALF | ADVERTISE_10FULL | ADVERTISE_100HALF | ADVERTISE_100FULL;
        mdio_write(dev, 0, MII_ADVERTISE, data);

        //Kick Auto-Negotiation
        data = mdio_read(dev, 0, MII_BMCR);
        data |= BMCR_ANENABLE;
        mdio_write(dev, 0, MII_BMCR, data);
    }
#endif
}// synop3504_enable_autonegotiation

/**
 * Delay for thread.
 * \param  msec  value of the delay in ms.
 */
static inline void mdly (unsigned int msec)
{
    set_current_state(TASK_INTERRUPTIBLE);
    schedule_timeout((msec + MSEC_PER_JIFFY - 1) / MSEC_PER_JIFFY);
}//mdly

/**
 * Start Auto-Negotiation procedure.
 * \param  dev  device structure.
 * \param  wait  wait Auto-Negotiation ending or not.
 * \return  error code.
 */
static int synop3504_autonegotiate(struct net_device *dev, int wait)
{
    struct net_priv *priv = NULL;

    //Check pointer
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

#ifdef CONFIG_MACH_ARIZONA
    //Only start link up because the link is forced to 10M FULL
    synop3504_on_link_up(dev);
    netif_carrier_on(dev);
    netif_start_queue(dev);
#else
    //Start the Auto-Negotiation
    if(mdio_read(dev, 0, MII_BMSR) & BMSR_ANEGCAPABLE)
    {
        TRACE("starting AutoNegotiation for %s\n", dev->name);
        //Kick Auto Negotiation
        mii_nway_restart(&priv->mii_if);

        //Wait Auto-Negotiation ending
        if(wait)
        {
            int timeout = AUTONEG_TIMEOUT * 1000 / AUTONEG_STEP;
            volatile uint32_t data;
            do
            {
                mdly(AUTONEG_STEP);
                data = mdio_read(dev, 0, MII_BMSR);
            } while(!(data & BMSR_ANEGCOMPLETE) && timeout--);

            if(timeout <= 0)
            {
                printk(KERN_WARNING DRV_NAME ": AutoNegotiation timed out after %d s for %s\n", AUTONEG_TIMEOUT, dev->name);
                return -1;
            }
            else
            {
                TRACE("AutoNegotiation complete after %d ms for %s\n", AUTONEG_TIMEOUT * 1000 - timeout * AUTONEG_STEP, dev->name);
            }
            synop3504_on_link_up(dev);
            netif_carrier_on(dev);
            netif_start_queue(dev);
        }
    }
#endif

    return 0;
}// synop3504_autonegotiate

/**
 * Poll the link status to check connexion/unconnexion.
 * \param  data  device structure.
 */
static void synop3504_timer(unsigned long data)
{
    struct net_device *dev = (struct net_device *)data;
    struct net_priv *priv = NULL;

    //Check pointer
    if(dev == NULL)
    {
        printk(KERN_ERR DRV_NAME": Error with timer parameter\n");
        return;
    }
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
    {
        printk(KERN_ERR DRV_NAME": Error with timer parameter\n");
        return;
    }

    if(!(mdio_read(dev, 0, MII_BMSR) & BMSR_LSTATUS))
    {
        if(netif_carrier_ok(dev))
        {
            //OK LINK DOWN.
            printk(DRV_NAME ": %s: " "Link DOWN\n", dev->name);
            netif_stop_queue(dev);
            netif_carrier_off(dev);
            synop3504_on_link_down(dev);
        }
        else
        {
//            TRACE("%s: Link still down\n", dev->name);
        }
    }
    else
    {
        if(!netif_carrier_ok(dev))
        {
            //OK LINK UP.
            printk(DRV_NAME ": %s: Link UP\n", dev->name);
            synop3504_on_link_up(dev);
            netif_carrier_on(dev);
            netif_wake_queue(dev);
        }
        else
        {
//            TRACE("%s: Link still up\n", dev->name);
        }
    }
    mod_timer(&priv->timer, jiffies + TIMEOUT);
}//synop3504_timer


/**
 * Read packet status from the device.
 * \param  dev  device structure.
 * \return  the device stats.
 */
static struct net_device_stats *synop3504_stats(struct net_device *dev)
{
    struct net_priv *priv = NULL;

    //Check pointers
    if(dev == NULL)
        return NULL;

    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return NULL;

    return &priv->stats;
}// synop3504_stats

/**
 * Change the MTU.
 * \param  dev  device structure.
 * \param  new_mtu  the new mtu value.
 * \return  error code.
 */
static int synop3504_change_mtu(struct net_device *dev, int new_mtu)
{
    //Check pointers
    if(dev == NULL)
        return -1;

    //Check arguments
    if(new_mtu < 64 || new_mtu > 1508)
        return -EINVAL;
    else
    {
        dev->mtu = new_mtu;
        return 0;
    }
}// synop3504_change_mtu

/**
 * Change the MAC address.
 * \param  dev  device structure.
 * \param  p  mac addr source.
 * \return  error code.
 */
static int synop3504_set_mac_address(struct net_device *dev, void *p)
{
    struct sockaddr *addr = p;
    struct net_priv *priv = NULL;
    Synopsys *synop = NULL;

    //Check pointers
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;
    synop = &priv->synop;
    if(synop == NULL)
        return -1;
    if(p == NULL)
        return -1;

    //Store the new address for Linux
    memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

    //Store the new address for Synopsys hardware
    SynopsysSetupEthernetAddress(synop, dev->dev_addr);

    return 0;
}// synop3504_set_mac_address

/**
 * This will control promiscuous mode
 * and multicast mode.
 * \param  dev  net device.
 * \return  error code.
 */
static void synop3504_set_multicast (struct net_device *dev)
{
    struct net_priv *priv = NULL;
    Synopsys *synop = NULL;

    //Check pointers
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return;
    synop = &priv->synop;
    if(synop == NULL)
        return;

    //Set promiscuous mode if it's asked for.
    if(dev->flags & IFF_PROMISC)
    {
        SynopsysSetPromiscuousMode(synop, 1);
        return;
    }
    else
    {
        SynopsysSetPromiscuousMode(synop, 0);
        return;
    }

    //Hardware cannot filter multicast addresses
    //Just block or unblock all addresses
    if(dev->flags & IFF_ALLMULTI || dev->mc_count > 0)
        SynopsysSetMulticastFilter(synop,1);
    else
        SynopsysSetMulticastFilter(synop,0);
}//synop3504_set_multicast

/**
 * Ethtool device interface.
 * \param  dev  device structure.
 * \param  useraddr  user data address.
 * \return  error code.
 */
static int synop3504_ethtool_ioctl(struct net_device *dev, void *useraddr)
{
#ifndef MODULE
    uint32_t ethcmd;
    struct net_priv *priv = (struct net_priv *)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;

    TRACE("%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);
            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(&priv->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(&priv->mii_if, &ecmd);
            return r;
        }
        //Restart Auto-Negotiation
    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(&priv->mii_if);
            if(copy_to_user(useraddr, &edata, sizeof(edata)))
                return -EFAULT;
            return 0;
        }
    default:
        break;
    }
#endif
    return -EOPNOTSUPP;
}// synop3504_ethtool_ioctl

/**
 * User control device interface.
 * \param  dev  device structure.
 * \param  ifr  user exchange structure.
 * \param  cmd  command to execute.
 * \return  error code.
 */
static int synop3504_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
    //Check pointers
    if(ifr == NULL)
        return -1;
    if(dev == NULL)
        return -1;

    //Find the command
    switch(cmd)
    {
        //EthTool Interface
    case SIOCETHTOOL:
        {
            synop3504_ethtool_ioctl(dev, (void *)ifr->ifr_data);
            break;
        }

    default:
            return -EOPNOTSUPP;
    }

    return 0;
}// synop3504_ioctl

/**
 * transmit frame procedure.
 * \param  skb  frame structure.
 * \param  dev  device structure.
 * \return  error code.
 */
static int synop3504_tx(struct sk_buff *skb, struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv*)dev->priv;
    Synopsys *synop = &priv->synop;
    struct dma_tx *tx = (struct dma_tx*)&priv->tx;

    uint32_t n;

    uint32_t status_mdio;

    TRACE("%s: Transmit\n", dev->name);

    n = tx->head_ptr;

    //Check if the link is down
    status_mdio = mdio_read(dev, 0, MII_BMSR);
    if((status_mdio & BMSR_LSTATUS) == 0)
    {
        printk(KERN_WARNING DRV_NAME "%s: TX error : Link still down\n", dev->name);
        dev_kfree_skb_any(skb);
        return 0;
    }

    //Check a free descriptor
    if((tx->skbs[n] != NULL) || (tx->ring[n].status.bf.dma_own))
    {
        netif_stop_queue(dev);
        printk(KERN_WARNING DRV_NAME "%s: TX dropped DMA Queue full\n", dev->name);
        priv->stats.tx_dropped++;
        dev_kfree_skb_any(skb);
        return 0;
    }

    tx->skbs[n] = skb;

    //Set up the buffer descriptors, use only one buffer
    tx->ring[n].status.val = 0;
    tx->ring[n].addr1 = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
    tx->ring[n].ctrl.bf.first = 1;
    tx->ring[n].ctrl.bf.last = 1;
    tx->ring[n].ctrl.bf.int_oncomp = 1;
    tx->ring[n].ctrl.bf.length1 = skb->len;

    print_packet("TX", n, skb->len, skb);

    tx->ring[n].status.val = 0;
    tx->ring[n].status.bf.dma_own = 1;

    //Increase head pointer and check end of ring
    if(++(tx->head_ptr) >= TX_RING_SIZE)
        tx->head_ptr = 0;

    //Invalidate cached areas
    dma_cache_maint(skb->data, skb->len, DMA_BIDIRECTIONAL);

    //Starting DMA transfert
    SynopsysRestartTx(synop);

    //Handle transmit
    dev->trans_start = jiffies;

    return 0;
}// synop3504_tx

/**
 * receive frame procedure.
 * \param  dev  device structure.
 * \param  budget  allocated budget.
 */
static void synop3504_rx(struct net_device *dev, int *budget)
{
    struct sk_buff *new_skb;
    struct sk_buff *skb;
    uint32_t rxsize;
    struct net_priv *priv = (struct net_priv*)dev->priv;
    struct dma_rx *rx = (struct dma_rx*)&priv->rx;
    uint32_t n;

    if(*budget == 0)
        return;

    TRACE("%s: Receive Done\n", dev->name);

    while((!rx->ring[rx->tail_ptr].status.bf.dma_own) && *budget)
    {
        n = rx->tail_ptr;

        //Check errors for the current RX frame
        if((rx->ring[n].status.bf.err) || (rx->ring[n].status.bf.err_drib))
        {
            //Overflow error
            if(rx->ring[n].status.bf.err_over)
            {
                priv->stats.rx_fifo_errors++;
                priv->stats.rx_errors++;
            }
            //CRC error
            if(rx->ring[n].status.bf.err_crc)
            {
                priv->stats.rx_crc_errors++;
                priv->stats.rx_errors++;
            }
            //Collision error
            if(rx->ring[n].status.bf.err_coll)
            {
                priv->stats.collisions++;
            }
            //Dribble bit error
            if(rx->ring[n].status.bf.err_drib)
            {
                priv->stats.rx_errors++;
                priv->stats.rx_frame_errors++;
            }
            goto reuse_buffer;
        }

	//Pre-allocate a new buffer
        if((new_skb = alloc_skb(PKT_BUF_SZ, GFP_KERNEL | GFP_DMA)) == NULL)
        {
            //Cannot allocate a new buffer re-use the same and drop the
            //current frame
            priv->stats.rx_dropped++;
            goto reuse_buffer;
	}

        //Check that an skbuff was associated to this RX frame
	if((skb = rx->skbs[n]) == NULL)
        {
            priv->stats.rx_errors++;
	}
        else
        {
            rxsize = rx->ring[n].status.bf.length - 4;

            //Invalidate cached area
            dma_cache_maint(skb->data, rxsize, DMA_FROM_DEVICE);

            print_packet("RX", n, rxsize, skb);

            //Update the skbuff
            skb_put(skb, rxsize);
            skb->dev = dev;
            skb->protocol = eth_type_trans(skb, dev);
            dev->last_rx = jiffies;

            //Send RX packet to Linux
            if(netif_receive_skb(skb) == NET_RX_DROP)
            {
                //Linux buffers full dropped by protocol
                priv->stats.rx_dropped++;
            }

            //Update stats
            priv->stats.rx_packets++;
            priv->stats.rx_bytes += rxsize;
	}

	//Update the current buffer descriptor with new buffer
        dma_unmap_single(NULL, rx->ring[n].addr1, rx->ring[n].ctrl.bf.length1, DMA_FROM_DEVICE);
        rx->ring[n].addr1 = (uint32_t)dma_map_single(NULL, new_skb->data, PKT_BUF_SZ, DMA_FROM_DEVICE);
        rx->skbs[n] = new_skb;

reuse_buffer:
        //Reset current descriptor control
        rx->ring[n].ctrl.val = 0;
        rx->ring[n].ctrl.bf.addr2en = 1;
        rx->ring[n].ctrl.bf.length1 = PKT_BUF_SZ;

        //Reset current descriptor status
        rx->ring[n].status.val = 0;
        rx->ring[n].status.bf.dma_own = 1;

        //Increase head pointer and check end of ring
        if(++(rx->tail_ptr) >= RX_RING_SIZE)
            rx->tail_ptr = 0;

        //Decrease budget
        (*budget)--;
    }
}// synop3504_rx

/**
 * finish the transmit frame procedure.
 * \param  dev  device structure.
 */
static void synop3504_tx_done(struct net_device *dev)
{
    struct net_priv *priv = (struct net_priv*)dev->priv;
    struct dma_tx *tx = (struct dma_tx*)&priv->tx;
    uint32_t n;

    TRACE("%s: Transmit Done (num=%d)\n", dev->name, tx->tail_ptr);

    while((tx->skbs[tx->tail_ptr] != NULL) && (!tx->ring[tx->tail_ptr].status.bf.dma_own))
    {
        n = tx->tail_ptr;
        TRACE("Found\n");

        //Check errors for the current TX frame
        if(tx->ring[n].status.bf.err)
        {
            //Underflow error
            if(tx->ring[n].status.bf.err_under)
            {
                priv->stats.tx_fifo_errors++;
                priv->stats.tx_errors++;
            }
            //Carrier error
            if(tx->ring[n].status.bf.err_clost || tx->ring[n].status.bf.err_nocar)
            {
                priv->stats.tx_carrier_errors++;
                priv->stats.tx_errors++;
            }
            //Collision error
            if(tx->ring[n].status.bf.err_ecoll || tx->ring[n].status.bf.err_lcoll)
            {
                priv->stats.collisions++;
            }
        }
        else
        {
            //Update TX normal stats
            priv->stats.tx_packets++;
            priv->stats.tx_bytes += tx->skbs[n]->len;
        }

        //Update the current buffer descriptor
        dma_unmap_single(NULL, tx->ring[n].addr1, tx->ring[n].ctrl.bf.length1, DMA_TO_DEVICE);
        tx->ring[n].status.val = 0;
        tx->ring[n].ctrl.val = 0;
        tx->ring[n].ctrl.bf.addr2en = 1;
        tx->ring[n].addr1 = 0;

        //Freeing skbuff
        dev_kfree_skb_any(tx->skbs[n]);
        tx->skbs[n] = NULL;

        //Increase tail pointer and check end of ring
        if(++(tx->tail_ptr) >= TX_RING_SIZE)
            tx->tail_ptr = 0;
    }
}// synop3504_tx_done

/**
 * Poll frame procedure.
 * \param  napi  NAPI structure.
 * \param  budget  max count of packets to receive.
 * \return  error code.
 */
static int synop3504_poll(struct napi_struct *napi, int budget)
{
    struct net_device *dev = (struct net_device*)synop3504_device;
    struct net_priv *priv = (struct net_priv*)dev->priv;
    Synopsys *synop = &priv->synop;
    SynopsysIntStatus status;

    do
    {
        //Get IRQ status
        SynopsysGetIntStatus(synop, &status);
        //Suppress rx, tx states and error bits (not needed in this function)
        status.bf.rxState = 0;
        status.bf.txState = 0;
        status.bf.errorBits = 0;

        TRACE("%s: IRQ (status=0x%x) ; budget=%d\n", dev->name, status.val, budget);

        if(status.val == 0)
            break;

        //Normal Interrupt
        if(status.bf.intNormal)
        {
            //TX completed
            if(status.bf.intTxCompleted)
            {
                synop3504_tx_done(dev);

                //Check the TX queue and re-enable it
                if(netif_queue_stopped(dev))
                {
                    netif_wake_queue(dev);
                    TRACE("%s: TX queue Waked Up\n", dev->name);
                }
            }

            //RX completed
            if(status.bf.intRxCompleted)
            {
                synop3504_rx(dev, &budget);
            }
        }

        //AbNormal interrupt
        if(status.bf.intAbnormal)
        {
            //TX enter in stopped state
            if(status.bf.intTxStopped)
            {
                netif_stop_queue(dev);
                synop3504_txdesc_reset(dev);
                TRACE("%s: TX queue Stopped\n", dev->name);
            }

            //TX underflow
            if(status.bf.intTxUnderflow)
            {
                //Remove old buffers from TX descriptors
                synop3504_tx_done(dev);
                netif_start_queue(dev);
                printk(KERN_ERR DRV_NAME ": %s: TX FIFO Error\n", dev->name);
            }

            //RX FIFO full
            if(status.bf.intRxOverflow)
            {
                synop3504_rx(dev, &budget);
                printk(KERN_ERR DRV_NAME ": %s: RX FIFO Error\n", dev->name);
            }

            //RX queue nearly full
            if(status.bf.intRxNoBuffer)
            {
                //Refresh the rx dma
                SynopsysRestartRx(synop);
                printk(KERN_WARNING DRV_NAME ": %s: RX queue nearly Full\n", dev->name);
            }

            //Bus error
            if(status.bf.intBusError)
            {
                printk(KERN_ERR DRV_NAME ": %s: Fatal BUS error (0x%x)\n", dev->name, status.bf.errorBits);
            }
        }
    } while(budget > 0);

    if(status.val == 0)
    {
        netif_rx_complete(dev, &priv->napi);
        //Enable interrupt
        SynopsysEnableInt(synop);
        return 0;
    }
    return 1;
}// synop3504_poll

/**
 * Receive frame procedure.
 * \param  irq  interrupt number.
 * \param  dev  device structure.
 * \param  regs  not used.
 * \return  error code.
 */
static irqreturn_t synop3504_interrupt(int irq, void * dev_id)
{
    struct net_device *dev = (struct net_device*)dev_id;
    struct net_priv *priv = NULL;
    Synopsys *synop;

    //Check pointer
    if(dev == NULL)
        return IRQ_NONE;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return IRQ_NONE;
    synop = &priv->synop;
    if(synop == NULL)
        return IRQ_NONE;

    //Disable interrupt
    SynopsysDisableInt(synop);

    //Prepare polling
    if(netif_rx_schedule_prep(dev, &priv->napi))
    {
        __netif_rx_schedule(dev, &priv->napi);
    }
    else
    {
        printk(KERN_ERR DRV_NAME ": %s: ERROR interrupt while in poll\n", dev->name);
    }

    return IRQ_HANDLED;
}// synop3504_interrupt

/**
 * Initialize the device.
 * \param  dev  device structure.
 * \return  error code.
 */
static int synop3504_open(struct net_device *dev)
{
    struct net_priv *priv = NULL;
    Synopsys *synop = NULL;

    //Check pointers
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;
    synop = &priv->synop;
    if(synop == NULL)
        return -1;

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

    //Reset synopsys hardware
    SynopsysReset(synop);

    //Reset PHY
    mdio_write(dev, 0, MII_BMCR, BMCR_RESET);
    while(mdio_read(dev, 0, MII_BMCR) & BMCR_RESET);

    //Check PHY ID
    priv->phy_id = mdio_read(dev, 0, MII_PHYSID1) << 16 | mdio_read(dev, 0, MII_PHYSID2);
    priv->phy_oui = (priv->phy_id >> 10) & 0x00ffffff;
    priv->phy_model = (priv->phy_id >> 4) & 0x0000003f;
    priv->phy_rev = (priv->phy_id >> 0) & 0x0000000f;
    if(priv->phy_oui == OUI_ICPLUS && priv->phy_model == ICPLUS_MODEL_IP175C)
        printk(DRV_NAME ": PHY for %s is IC+ IP175C rev %u\n", dev->name, priv->phy_rev);
    else
        printk(DRV_NAME ": PHY ID for %s id is 0x%8.8x\n", dev->name, priv->phy_id);

    //Initialise DMA descriptors
    synop3504_txdesc_init(dev);
    synop3504_rxdesc_init(dev);

    //Initialise synopsys hardware
    SynopsysInit(synop, priv->tx.phy_addr, priv->rx.phy_addr);

    //Set MAC address to synopsys hardware
    SynopsysSetupEthernetAddress(synop, dev->dev_addr);

    //Activate Auto-Negotiation
    synop3504_enable_autonegotiation(dev);

    //Prepare Linux as link down
    netif_stop_queue(dev);
    netif_carrier_off(dev);

    //Prepare hardware as link down
    synop3504_on_link_down(dev);

    //Start Auto-Negotiation
    synop3504_autonegotiate(dev, 0);

    //Request irq
    if(request_irq(dev->irq, synop3504_interrupt, 0, dev->name, dev) != 0)
    {
        printk(KERN_ERR DRV_NAME ": %s - interrupt %d request fail\n", dev->name, dev->irq);
        return -ENODEV;
    }

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

    return 0;
}// synop3504_open

/**
 * Uninitialize the device.
 * \param  dev  device structure.
 * \return  error code.
 */
static int synop3504_stop(struct net_device *dev)
{
    struct net_priv *priv = NULL;
    Synopsys *synop = NULL;

    //Check pointers
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;
    synop = &priv->synop;
    if(synop == NULL)
        return -1;

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

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

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

    //Disable Interrupts
    SynopsysDisableInt(synop);

    //Stop RX and TX DMAs
    SynopsysStopRx(synop);
    SynopsysStopTx(synop);

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

    //Freeing descriptors
    synop3504_txdesc_uninit(dev);
    synop3504_rxdesc_uninit(dev);

    return 0;
}// synop3504_stop

/**
 * Initialise the network device.
 * \param  dev  device structure.
 * \return  error code.
 */
static int synop3504_init(struct net_device *dev)
{
    struct net_priv *priv = NULL;

    //Check pointers
    if(dev == NULL)
        return -1;
    priv = (struct net_priv *)dev->priv;
    if(priv == NULL)
        return -1;

    //Set IP base addresses
    dev->base_addr = (unsigned int)IO_ADDRESS(ETHERNET_CTRL_BASE);

    //Set Interrupt number
    dev->irq = INT_ETH;

    //Attach hardware layer addresses
    SynopsysAttach(&priv->synop,
                   IO_ADDRESS(ETHERNET_CTRL_BASE) + (GMAC_BaseAddress - ETHERNET_CTRL_BASE),
                   IO_ADDRESS(ETHERNET_CTRL_BASE) + (DMA_BaseAddress - ETHERNET_CTRL_BASE),
                   MII_PHY_ADDR);

    //Initialise device functions
    ether_setup(dev);
    dev->open = synop3504_open;
    dev->stop = synop3504_stop;
    dev->do_ioctl = synop3504_ioctl;
    dev->set_mac_address = synop3504_set_mac_address;
    dev->hard_start_xmit = synop3504_tx;
    dev->get_stats = synop3504_stats;
    dev->set_multicast_list = synop3504_set_multicast;
    dev->change_mtu = synop3504_change_mtu;
    //TODO:
/*    dev->tx_timeout = synop3504_tx_timeout; */
/*    dev->watchdog_timeo = TX_TIMEOUT; */

    //Set NAPI mode
    netif_napi_add(dev, &priv->napi, &synop3504_poll, 64);
    priv->napi.state=0;

    //TODO:Setup MAC address for Linux (stored in NVRAM)
    dev->dev_addr[0] = 0x00;
    dev->dev_addr[1] = 0x11;
    dev->dev_addr[2] = 0x22;
    dev->dev_addr[3] = 0x33;
    dev->dev_addr[4] = 0x44;
    dev->dev_addr[5] = 0x55;

    //MII initialisation for mdio dialog by Linux APIs
    priv->mii_if.dev = dev;
    priv->mii_if.mdio_read = mdio_read;
    priv->mii_if.mdio_write = mdio_write;
    priv->mii_if.phy_id = MII_PHY_ADDR;

    //Initialise timer thread
    init_timer(&priv->timer);
    priv->timer.data = (unsigned long) dev;
    priv->timer.function = synop3504_timer;

    return 0;
}// synop3504_init

/**
 * Initialise the module.
 * \return  error code.
 */
static int __init synop3504_module_init(void)
{
    //TODO:Recode init process with platform device
    int result;
    struct net_device *dev;

    printk("%s", version);

    //Allocate device memory
    dev = alloc_etherdev(sizeof(struct net_priv));
    if(dev == NULL)
        result = -ENOMEM;

    //Proceed the init driver
    dev->init = synop3504_init;

    //Register net device
    result = register_netdev(dev);
    if(result < 0)
    {
        printk(KERN_ERR DRV_NAME": Error %i registering driver\n", result);
        kfree(dev->priv);
        free_netdev(dev);
    }
    else
    {
        synop3504_device = dev;
    }

    return result;
}// synop3504_module_init

/**
 * Uninitialise the module.
 */
static void __exit synop3504_module_exit(void)
{
    struct net_priv *priv;

    if(synop3504_device)
    {
        //Freeing private field of the net device struture
        priv = synop3504_device->priv;
        if(priv)
            kfree(priv);

        //Unregister net device
        unregister_netdev(synop3504_device);

        //Freeing network device
        free_netdev(synop3504_device);
    }

}// synop3504_module_exit

module_init(synop3504_module_init);
module_exit(synop3504_module_exit);