-
Notifications
You must be signed in to change notification settings - Fork 1
/
sc92031.c
1614 lines (1282 loc) · 39.8 KB
/
sc92031.c
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
/* Silan SC92031 PCI Fast Ethernet Adapter driver
*
* Based on vendor drivers:
* Silan Fast Ethernet Netcard Driver:
* MODULE_AUTHOR ("gaoyonghong");
* MODULE_DESCRIPTION ("SILAN Fast Ethernet driver");
* MODULE_LICENSE("GPL");
* 8139D Fast Ethernet driver:
* (C) 2002 by gaoyonghong
* MODULE_AUTHOR ("gaoyonghong");
* MODULE_DESCRIPTION ("Rsltek 8139D PCI Fast Ethernet Adapter driver");
* MODULE_LICENSE("GPL");
* Both are almost identical and seem to be based on pci-skeleton.c
*
* Rewritten for 2.6 by Cesar Eduardo Barros
*
* A datasheet for this chip can be found at
* http://www.silan.com.cn/english/product/pdf/SC92031AY.pdf
*/
/* Note about set_mac_address: I don't know how to change the hardware
* matching, so you need to enable IFF_PROMISC when using it.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/crc32.h>
#include <asm/irq.h>
#define SC92031_NAME "sc92031"
/* BAR 0 is MMIO, BAR 1 is PIO */
#ifndef SC92031_USE_BAR
#define SC92031_USE_BAR 0
#endif
/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). */
static int multicast_filter_limit = 64;
module_param(multicast_filter_limit, int, 0);
MODULE_PARM_DESC(multicast_filter_limit,
"Maximum number of filtered multicast addresses");
static int media;
module_param(media, int, 0);
MODULE_PARM_DESC(media, "Media type (0x00 = autodetect,"
" 0x01 = 10M half, 0x02 = 10M full,"
" 0x04 = 100M half, 0x08 = 100M full)");
/* Size of the in-memory receive ring. */
#define RX_BUF_LEN_IDX 3 /* 0==8K, 1==16K, 2==32K, 3==64K ,4==128K*/
#define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX)
/* Number of Tx descriptor registers. */
#define NUM_TX_DESC 4
/* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/
#define MAX_ETH_FRAME_SIZE 1536
/* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
#define TX_BUF_SIZE MAX_ETH_FRAME_SIZE
#define TX_BUF_TOT_LEN (TX_BUF_SIZE * NUM_TX_DESC)
/* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */
#define RX_FIFO_THRESH 7 /* Rx buffer level before first PCI xfer. */
/* Time in jiffies before concluding the transmitter is hung. */
#define TX_TIMEOUT (4*HZ)
#define SILAN_STATS_NUM 2 /* number of ETHTOOL_GSTATS */
/* media options */
#define AUTOSELECT 0x00
#define M10_HALF 0x01
#define M10_FULL 0x02
#define M100_HALF 0x04
#define M100_FULL 0x08
/* Symbolic offsets to registers. */
enum silan_registers {
Config0 = 0x00, // Config0
Config1 = 0x04, // Config1
RxBufWPtr = 0x08, // Rx buffer writer poiter
IntrStatus = 0x0C, // Interrupt status
IntrMask = 0x10, // Interrupt mask
RxbufAddr = 0x14, // Rx buffer start address
RxBufRPtr = 0x18, // Rx buffer read pointer
Txstatusall = 0x1C, // Transmit status of all descriptors
TxStatus0 = 0x20, // Transmit status (Four 32bit registers).
TxAddr0 = 0x30, // Tx descriptors (also four 32bit).
RxConfig = 0x40, // Rx configuration
MAC0 = 0x44, // Ethernet hardware address.
MAR0 = 0x4C, // Multicast filter.
RxStatus0 = 0x54, // Rx status
TxConfig = 0x5C, // Tx configuration
PhyCtrl = 0x60, // physical control
FlowCtrlConfig = 0x64, // flow control
Miicmd0 = 0x68, // Mii command0 register
Miicmd1 = 0x6C, // Mii command1 register
Miistatus = 0x70, // Mii status register
Timercnt = 0x74, // Timer counter register
TimerIntr = 0x78, // Timer interrupt register
PMConfig = 0x7C, // Power Manager configuration
CRC0 = 0x80, // Power Manager CRC ( Two 32bit regisers)
Wakeup0 = 0x88, // power Manager wakeup( Eight 64bit regiser)
LSBCRC0 = 0xC8, // power Manager LSBCRC(Two 32bit regiser)
TestD0 = 0xD0,
TestD4 = 0xD4,
TestD8 = 0xD8,
};
#define MII_BMCR 0 // Basic mode control register
#define MII_BMSR 1 // Basic mode status register
#define MII_JAB 16
#define MII_OutputStatus 24
#define BMCR_FULLDPLX 0x0100 // Full duplex
#define BMCR_ANRESTART 0x0200 // Auto negotiation restart
#define BMCR_ANENABLE 0x1000 // Enable auto negotiation
#define BMCR_SPEED100 0x2000 // Select 100Mbps
#define BMSR_LSTATUS 0x0004 // Link status
#define PHY_16_JAB_ENB 0x1000
#define PHY_16_PORT_ENB 0x1
enum IntrStatusBits {
LinkFail = 0x80000000,
LinkOK = 0x40000000,
TimeOut = 0x20000000,
RxOverflow = 0x0040,
RxOK = 0x0020,
TxOK = 0x0001,
IntrBits = LinkFail|LinkOK|TimeOut|RxOverflow|RxOK|TxOK,
};
enum TxStatusBits {
TxCarrierLost = 0x20000000,
TxAborted = 0x10000000,
TxOutOfWindow = 0x08000000,
TxNccShift = 22,
EarlyTxThresShift = 16,
TxStatOK = 0x8000,
TxUnderrun = 0x4000,
TxOwn = 0x2000,
};
enum RxStatusBits {
RxStatesOK = 0x80000,
RxBadAlign = 0x40000,
RxHugeFrame = 0x20000,
RxSmallFrame = 0x10000,
RxCRCOK = 0x8000,
RxCrlFrame = 0x4000,
Rx_Broadcast = 0x2000,
Rx_Multicast = 0x1000,
RxAddrMatch = 0x0800,
MiiErr = 0x0400,
};
enum RxConfigBits {
RxFullDx = 0x80000000,
RxEnb = 0x40000000,
RxSmall = 0x20000000,
RxHuge = 0x10000000,
RxErr = 0x08000000,
RxAllphys = 0x04000000,
RxMulticast = 0x02000000,
RxBroadcast = 0x01000000,
RxLoopBack = (1 << 23) | (1 << 22),
LowThresholdShift = 12,
HighThresholdShift = 2,
};
enum TxConfigBits {
TxFullDx = 0x80000000,
TxEnb = 0x40000000,
TxEnbPad = 0x20000000,
TxEnbHuge = 0x10000000,
TxEnbFCS = 0x08000000,
TxNoBackOff = 0x04000000,
TxEnbPrem = 0x02000000,
TxCareLostCrs = 0x1000000,
TxExdCollNum = 0xf00000,
TxDataRate = 0x80000,
};
enum PhyCtrlconfigbits {
PhyCtrlAne = 0x80000000,
PhyCtrlSpd100 = 0x40000000,
PhyCtrlSpd10 = 0x20000000,
PhyCtrlPhyBaseAddr = 0x1f000000,
PhyCtrlDux = 0x800000,
PhyCtrlReset = 0x400000,
};
enum FlowCtrlConfigBits {
FlowCtrlFullDX = 0x80000000,
FlowCtrlEnb = 0x40000000,
};
enum Config0Bits {
Cfg0_Reset = 0x80000000,
Cfg0_Anaoff = 0x40000000,
Cfg0_LDPS = 0x20000000,
};
enum Config1Bits {
Cfg1_EarlyRx = 1 << 31,
Cfg1_EarlyTx = 1 << 30,
//rx buffer size
Cfg1_Rcv8K = 0x0,
Cfg1_Rcv16K = 0x1,
Cfg1_Rcv32K = 0x3,
Cfg1_Rcv64K = 0x7,
Cfg1_Rcv128K = 0xf,
};
enum MiiCmd0Bits {
Mii_Divider = 0x20000000,
Mii_WRITE = 0x400000,
Mii_READ = 0x200000,
Mii_SCAN = 0x100000,
Mii_Tamod = 0x80000,
Mii_Drvmod = 0x40000,
Mii_mdc = 0x20000,
Mii_mdoen = 0x10000,
Mii_mdo = 0x8000,
Mii_mdi = 0x4000,
};
enum MiiStatusBits {
Mii_StatusBusy = 0x80000000,
};
enum PMConfigBits {
PM_Enable = 1 << 31,
PM_LongWF = 1 << 30,
PM_Magic = 1 << 29,
PM_LANWake = 1 << 28,
PM_LWPTN = (1 << 27 | 1<< 26),
PM_LinkUp = 1 << 25,
PM_WakeUp = 1 << 24,
};
/* Locking rules:
* priv->lock protects most of the fields of priv and most of the
* hardware registers. It does not have to protect against softirqs
* between sc92031_disable_interrupts and sc92031_enable_interrupts;
* it also does not need to be used in ->open and ->stop while the
* device interrupts are off.
* Not having to protect against softirqs is very useful due to heavy
* use of mdelay() at _sc92031_reset.
* Functions prefixed with _sc92031_ must be called with the lock held;
* functions prefixed with sc92031_ must be called without the lock held.
* Use mmiowb() before unlocking if the hardware was written to.
*/
/* Locking rules for the interrupt:
* - the interrupt and the tasklet never run at the same time
* - neither run between sc92031_disable_interrupts and
* sc92031_enable_interrupt
*/
struct sc92031_priv {
spinlock_t lock;
/* iomap.h cookie */
void __iomem *port_base;
/* pci device structure */
struct pci_dev *pdev;
/* tasklet */
struct tasklet_struct tasklet;
/* CPU address of rx ring */
void *rx_ring;
/* PCI address of rx ring */
dma_addr_t rx_ring_dma_addr;
/* PCI address of rx ring read pointer */
dma_addr_t rx_ring_tail;
/* tx ring write index */
unsigned tx_head;
/* tx ring read index */
unsigned tx_tail;
/* CPU address of tx bounce buffer */
void *tx_bufs;
/* PCI address of tx bounce buffer */
dma_addr_t tx_bufs_dma_addr;
/* copies of some hardware registers */
u32 intr_status;
atomic_t intr_mask;
u32 rx_config;
u32 tx_config;
u32 pm_config;
/* copy of some flags from dev->flags */
unsigned int mc_flags;
/* for ETHTOOL_GSTATS */
u64 tx_timeouts;
u64 rx_loss;
/* for dev->get_stats */
long rx_value;
};
/* I don't know which registers can be safely read; however, I can guess
* MAC0 is one of them. */
static inline void _sc92031_dummy_read(void __iomem *port_base)
{
ioread32(port_base + MAC0);
}
static u32 _sc92031_mii_wait(void __iomem *port_base)
{
u32 mii_status;
do {
udelay(10);
mii_status = ioread32(port_base + Miistatus);
} while (mii_status & Mii_StatusBusy);
return mii_status;
}
static u32 _sc92031_mii_cmd(void __iomem *port_base, u32 cmd0, u32 cmd1)
{
iowrite32(Mii_Divider, port_base + Miicmd0);
_sc92031_mii_wait(port_base);
iowrite32(cmd1, port_base + Miicmd1);
iowrite32(Mii_Divider | cmd0, port_base + Miicmd0);
return _sc92031_mii_wait(port_base);
}
static void _sc92031_mii_scan(void __iomem *port_base)
{
_sc92031_mii_cmd(port_base, Mii_SCAN, 0x1 << 6);
}
static u16 _sc92031_mii_read(void __iomem *port_base, unsigned reg)
{
return _sc92031_mii_cmd(port_base, Mii_READ, reg << 6) >> 13;
}
static void _sc92031_mii_write(void __iomem *port_base, unsigned reg, u16 val)
{
_sc92031_mii_cmd(port_base, Mii_WRITE, (reg << 6) | ((u32)val << 11));
}
static void sc92031_disable_interrupts(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
/* tell the tasklet/interrupt not to enable interrupts */
atomic_set(&priv->intr_mask, 0);
wmb();
/* stop interrupts */
iowrite32(0, port_base + IntrMask);
_sc92031_dummy_read(port_base);
mmiowb();
/* wait for any concurrent interrupt/tasklet to finish */
synchronize_irq(dev->irq);
tasklet_disable(&priv->tasklet);
}
static void sc92031_enable_interrupts(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
tasklet_enable(&priv->tasklet);
atomic_set(&priv->intr_mask, IntrBits);
wmb();
iowrite32(IntrBits, port_base + IntrMask);
mmiowb();
}
static void _sc92031_disable_tx_rx(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
priv->rx_config &= ~RxEnb;
priv->tx_config &= ~TxEnb;
iowrite32(priv->rx_config, port_base + RxConfig);
iowrite32(priv->tx_config, port_base + TxConfig);
}
static void _sc92031_enable_tx_rx(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
priv->rx_config |= RxEnb;
priv->tx_config |= TxEnb;
iowrite32(priv->rx_config, port_base + RxConfig);
iowrite32(priv->tx_config, port_base + TxConfig);
}
static void _sc92031_tx_clear(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
while (priv->tx_head - priv->tx_tail > 0) {
priv->tx_tail++;
dev->stats.tx_dropped++;
}
priv->tx_head = priv->tx_tail = 0;
}
static void _sc92031_set_mar(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
u32 mar0 = 0, mar1 = 0;
if ((dev->flags & IFF_PROMISC) ||
netdev_mc_count(dev) > multicast_filter_limit ||
(dev->flags & IFF_ALLMULTI))
mar0 = mar1 = 0xffffffff;
else if (dev->flags & IFF_MULTICAST) {
struct netdev_hw_addr *ha;
netdev_for_each_mc_addr(ha, dev) {
u32 crc;
unsigned bit = 0;
crc = ~ether_crc(ETH_ALEN, ha->addr);
crc >>= 24;
if (crc & 0x01) bit |= 0x02;
if (crc & 0x02) bit |= 0x01;
if (crc & 0x10) bit |= 0x20;
if (crc & 0x20) bit |= 0x10;
if (crc & 0x40) bit |= 0x08;
if (crc & 0x80) bit |= 0x04;
if (bit > 31)
mar0 |= 0x1 << (bit - 32);
else
mar1 |= 0x1 << bit;
}
}
iowrite32(mar0, port_base + MAR0);
iowrite32(mar1, port_base + MAR0 + 4);
}
static void _sc92031_set_rx_config(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
unsigned int old_mc_flags;
u32 rx_config_bits = 0;
old_mc_flags = priv->mc_flags;
if (dev->flags & IFF_PROMISC)
rx_config_bits |= RxSmall | RxHuge | RxErr | RxBroadcast
| RxMulticast | RxAllphys;
if (dev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
rx_config_bits |= RxMulticast;
if (dev->flags & IFF_BROADCAST)
rx_config_bits |= RxBroadcast;
priv->rx_config &= ~(RxSmall | RxHuge | RxErr | RxBroadcast
| RxMulticast | RxAllphys);
priv->rx_config |= rx_config_bits;
priv->mc_flags = dev->flags & (IFF_PROMISC | IFF_ALLMULTI
| IFF_MULTICAST | IFF_BROADCAST);
if (netif_carrier_ok(dev) && priv->mc_flags != old_mc_flags)
iowrite32(priv->rx_config, port_base + RxConfig);
}
static bool _sc92031_check_media(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
u16 bmsr;
bmsr = _sc92031_mii_read(port_base, MII_BMSR);
rmb();
if (bmsr & BMSR_LSTATUS) {
bool speed_100, duplex_full;
u32 flow_ctrl_config = 0;
u16 output_status = _sc92031_mii_read(port_base,
MII_OutputStatus);
_sc92031_mii_scan(port_base);
speed_100 = output_status & 0x2;
duplex_full = output_status & 0x4;
/* Initial Tx/Rx configuration */
priv->rx_config = (0x40 << LowThresholdShift) | (0x1c0 << HighThresholdShift);
priv->tx_config = 0x48800000;
/* NOTE: vendor driver had dead code here to enable tx padding */
if (!speed_100)
priv->tx_config |= 0x80000;
// configure rx mode
_sc92031_set_rx_config(dev);
if (duplex_full) {
priv->rx_config |= RxFullDx;
priv->tx_config |= TxFullDx;
flow_ctrl_config = FlowCtrlFullDX | FlowCtrlEnb;
} else {
priv->rx_config &= ~RxFullDx;
priv->tx_config &= ~TxFullDx;
}
_sc92031_set_mar(dev);
_sc92031_set_rx_config(dev);
_sc92031_enable_tx_rx(dev);
iowrite32(flow_ctrl_config, port_base + FlowCtrlConfig);
netif_carrier_on(dev);
if (printk_ratelimit())
printk(KERN_INFO "%s: link up, %sMbps, %s-duplex\n",
dev->name,
speed_100 ? "100" : "10",
duplex_full ? "full" : "half");
return true;
} else {
_sc92031_mii_scan(port_base);
netif_carrier_off(dev);
_sc92031_disable_tx_rx(dev);
if (printk_ratelimit())
printk(KERN_INFO "%s: link down\n", dev->name);
return false;
}
}
static void _sc92031_phy_reset(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
u32 phy_ctrl;
phy_ctrl = ioread32(port_base + PhyCtrl);
phy_ctrl &= ~(PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10);
phy_ctrl |= PhyCtrlAne | PhyCtrlReset;
switch (media) {
default:
case AUTOSELECT:
phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10;
break;
case M10_HALF:
phy_ctrl |= PhyCtrlSpd10;
break;
case M10_FULL:
phy_ctrl |= PhyCtrlDux | PhyCtrlSpd10;
break;
case M100_HALF:
phy_ctrl |= PhyCtrlSpd100;
break;
case M100_FULL:
phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100;
break;
}
iowrite32(phy_ctrl, port_base + PhyCtrl);
mdelay(10);
phy_ctrl &= ~PhyCtrlReset;
iowrite32(phy_ctrl, port_base + PhyCtrl);
mdelay(1);
_sc92031_mii_write(port_base, MII_JAB,
PHY_16_JAB_ENB | PHY_16_PORT_ENB);
_sc92031_mii_scan(port_base);
netif_carrier_off(dev);
netif_stop_queue(dev);
}
static void _sc92031_reset(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
/* disable PM */
iowrite32(0, port_base + PMConfig);
/* soft reset the chip */
iowrite32(Cfg0_Reset, port_base + Config0);
mdelay(200);
iowrite32(0, port_base + Config0);
mdelay(10);
/* disable interrupts */
iowrite32(0, port_base + IntrMask);
/* clear multicast address */
iowrite32(0, port_base + MAR0);
iowrite32(0, port_base + MAR0 + 4);
/* init rx ring */
iowrite32(priv->rx_ring_dma_addr, port_base + RxbufAddr);
priv->rx_ring_tail = priv->rx_ring_dma_addr;
/* init tx ring */
_sc92031_tx_clear(dev);
/* clear old register values */
priv->intr_status = 0;
atomic_set(&priv->intr_mask, 0);
priv->rx_config = 0;
priv->tx_config = 0;
priv->mc_flags = 0;
/* configure rx buffer size */
/* NOTE: vendor driver had dead code here to enable early tx/rx */
iowrite32(Cfg1_Rcv64K, port_base + Config1);
_sc92031_phy_reset(dev);
_sc92031_check_media(dev);
/* calculate rx fifo overflow */
priv->rx_value = 0;
/* enable PM */
iowrite32(priv->pm_config, port_base + PMConfig);
/* clear intr register */
ioread32(port_base + IntrStatus);
}
static void _sc92031_tx_tasklet(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
unsigned old_tx_tail;
unsigned entry;
u32 tx_status;
old_tx_tail = priv->tx_tail;
while (priv->tx_head - priv->tx_tail > 0) {
entry = priv->tx_tail % NUM_TX_DESC;
tx_status = ioread32(port_base + TxStatus0 + entry * 4);
if (!(tx_status & (TxStatOK | TxUnderrun | TxAborted)))
break;
priv->tx_tail++;
if (tx_status & TxStatOK) {
dev->stats.tx_bytes += tx_status & 0x1fff;
dev->stats.tx_packets++;
/* Note: TxCarrierLost is always asserted at 100mbps. */
dev->stats.collisions += (tx_status >> 22) & 0xf;
}
if (tx_status & (TxOutOfWindow | TxAborted)) {
dev->stats.tx_errors++;
if (tx_status & TxAborted)
dev->stats.tx_aborted_errors++;
if (tx_status & TxCarrierLost)
dev->stats.tx_carrier_errors++;
if (tx_status & TxOutOfWindow)
dev->stats.tx_window_errors++;
}
if (tx_status & TxUnderrun)
dev->stats.tx_fifo_errors++;
}
if (priv->tx_tail != old_tx_tail)
if (netif_queue_stopped(dev))
netif_wake_queue(dev);
}
static void _sc92031_rx_tasklet_error(struct net_device *dev,
u32 rx_status, unsigned rx_size)
{
if(rx_size > (MAX_ETH_FRAME_SIZE + 4) || rx_size < 16) {
dev->stats.rx_errors++;
dev->stats.rx_length_errors++;
}
if (!(rx_status & RxStatesOK)) {
dev->stats.rx_errors++;
if (rx_status & (RxHugeFrame | RxSmallFrame))
dev->stats.rx_length_errors++;
if (rx_status & RxBadAlign)
dev->stats.rx_frame_errors++;
if (!(rx_status & RxCRCOK))
dev->stats.rx_crc_errors++;
} else {
struct sc92031_priv *priv = netdev_priv(dev);
priv->rx_loss++;
}
}
static void _sc92031_rx_tasklet(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
dma_addr_t rx_ring_head;
unsigned rx_len;
unsigned rx_ring_offset;
void *rx_ring = priv->rx_ring;
rx_ring_head = ioread32(port_base + RxBufWPtr);
rmb();
/* rx_ring_head is only 17 bits in the RxBufWPtr register.
* we need to change it to 32 bits physical address
*/
rx_ring_head &= (dma_addr_t)(RX_BUF_LEN - 1);
rx_ring_head |= priv->rx_ring_dma_addr & ~(dma_addr_t)(RX_BUF_LEN - 1);
if (rx_ring_head < priv->rx_ring_dma_addr)
rx_ring_head += RX_BUF_LEN;
if (rx_ring_head >= priv->rx_ring_tail)
rx_len = rx_ring_head - priv->rx_ring_tail;
else
rx_len = RX_BUF_LEN - (priv->rx_ring_tail - rx_ring_head);
if (!rx_len)
return;
if (unlikely(rx_len > RX_BUF_LEN)) {
if (printk_ratelimit())
printk(KERN_ERR "%s: rx packets length > rx buffer\n",
dev->name);
return;
}
rx_ring_offset = (priv->rx_ring_tail - priv->rx_ring_dma_addr) % RX_BUF_LEN;
while (rx_len) {
u32 rx_status;
unsigned rx_size, rx_size_align, pkt_size;
struct sk_buff *skb;
rx_status = le32_to_cpup((__le32 *)(rx_ring + rx_ring_offset));
rmb();
rx_size = rx_status >> 20;
rx_size_align = (rx_size + 3) & ~3; // for 4 bytes aligned
pkt_size = rx_size - 4; // Omit the four octet CRC from the length.
rx_ring_offset = (rx_ring_offset + 4) % RX_BUF_LEN;
if (unlikely(rx_status == 0 ||
rx_size > (MAX_ETH_FRAME_SIZE + 4) ||
rx_size < 16 ||
!(rx_status & RxStatesOK))) {
_sc92031_rx_tasklet_error(dev, rx_status, rx_size);
break;
}
if (unlikely(rx_size_align + 4 > rx_len)) {
if (printk_ratelimit())
printk(KERN_ERR "%s: rx_len is too small\n", dev->name);
break;
}
rx_len -= rx_size_align + 4;
skb = netdev_alloc_skb_ip_align(dev, pkt_size);
if (unlikely(!skb)) {
if (printk_ratelimit())
printk(KERN_ERR "%s: Couldn't allocate a skb_buff for a packet of size %u\n",
dev->name, pkt_size);
goto next;
}
if ((rx_ring_offset + pkt_size) > RX_BUF_LEN) {
memcpy(skb_put(skb, RX_BUF_LEN - rx_ring_offset),
rx_ring + rx_ring_offset, RX_BUF_LEN - rx_ring_offset);
memcpy(skb_put(skb, pkt_size - (RX_BUF_LEN - rx_ring_offset)),
rx_ring, pkt_size - (RX_BUF_LEN - rx_ring_offset));
} else {
memcpy(skb_put(skb, pkt_size), rx_ring + rx_ring_offset, pkt_size);
}
skb->protocol = eth_type_trans(skb, dev);
netif_rx(skb);
dev->stats.rx_bytes += pkt_size;
dev->stats.rx_packets++;
if (rx_status & Rx_Multicast)
dev->stats.multicast++;
next:
rx_ring_offset = (rx_ring_offset + rx_size_align) % RX_BUF_LEN;
}
mb();
priv->rx_ring_tail = rx_ring_head;
iowrite32(priv->rx_ring_tail, port_base + RxBufRPtr);
}
static void _sc92031_link_tasklet(struct net_device *dev)
{
if (_sc92031_check_media(dev))
netif_wake_queue(dev);
else {
netif_stop_queue(dev);
dev->stats.tx_carrier_errors++;
}
}
static void sc92031_tasklet(unsigned long data)
{
struct net_device *dev = (struct net_device *)data;
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
u32 intr_status, intr_mask;
intr_status = priv->intr_status;
spin_lock(&priv->lock);
if (unlikely(!netif_running(dev)))
goto out;
if (intr_status & TxOK)
_sc92031_tx_tasklet(dev);
if (intr_status & RxOK)
_sc92031_rx_tasklet(dev);
if (intr_status & RxOverflow)
dev->stats.rx_errors++;
if (intr_status & TimeOut) {
dev->stats.rx_errors++;
dev->stats.rx_length_errors++;
}
if (intr_status & (LinkFail | LinkOK))
_sc92031_link_tasklet(dev);
out:
intr_mask = atomic_read(&priv->intr_mask);
rmb();
iowrite32(intr_mask, port_base + IntrMask);
mmiowb();
spin_unlock(&priv->lock);
}
static irqreturn_t sc92031_interrupt(int irq, void *dev_id)
{
struct net_device *dev = dev_id;
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
u32 intr_status, intr_mask;
/* mask interrupts before clearing IntrStatus */
iowrite32(0, port_base + IntrMask);
_sc92031_dummy_read(port_base);
intr_status = ioread32(port_base + IntrStatus);
if (unlikely(intr_status == 0xffffffff))
return IRQ_NONE; // hardware has gone missing
intr_status &= IntrBits;
if (!intr_status)
goto out_none;
priv->intr_status = intr_status;
tasklet_schedule(&priv->tasklet);
return IRQ_HANDLED;
out_none:
intr_mask = atomic_read(&priv->intr_mask);
rmb();
iowrite32(intr_mask, port_base + IntrMask);
mmiowb();
return IRQ_NONE;
}
static struct net_device_stats *sc92031_get_stats(struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
// FIXME I do not understand what is this trying to do.
if (netif_running(dev)) {
int temp;
spin_lock_bh(&priv->lock);
/* Update the error count. */
temp = (ioread32(port_base + RxStatus0) >> 16) & 0xffff;
if (temp == 0xffff) {
priv->rx_value += temp;
dev->stats.rx_fifo_errors = priv->rx_value;
} else
dev->stats.rx_fifo_errors = temp + priv->rx_value;
spin_unlock_bh(&priv->lock);
}
return &dev->stats;
}
static netdev_tx_t sc92031_start_xmit(struct sk_buff *skb,
struct net_device *dev)
{
struct sc92031_priv *priv = netdev_priv(dev);
void __iomem *port_base = priv->port_base;
unsigned len;
unsigned entry;
u32 tx_status;
if (unlikely(skb->len > TX_BUF_SIZE)) {
dev->stats.tx_dropped++;
goto out;
}
spin_lock(&priv->lock);
if (unlikely(!netif_carrier_ok(dev))) {
dev->stats.tx_dropped++;
goto out_unlock;
}
BUG_ON(priv->tx_head - priv->tx_tail >= NUM_TX_DESC);
entry = priv->tx_head++ % NUM_TX_DESC;
skb_copy_and_csum_dev(skb, priv->tx_bufs + entry * TX_BUF_SIZE);
len = skb->len;
if (len < ETH_ZLEN) {
memset(priv->tx_bufs + entry * TX_BUF_SIZE + len,
0, ETH_ZLEN - len);
len = ETH_ZLEN;
}
wmb();
if (len < 100)
tx_status = len;
else if (len < 300)
tx_status = 0x30000 | len;
else
tx_status = 0x50000 | len;
iowrite32(priv->tx_bufs_dma_addr + entry * TX_BUF_SIZE,
port_base + TxAddr0 + entry * 4);
iowrite32(tx_status, port_base + TxStatus0 + entry * 4);
mmiowb();
if (priv->tx_head - priv->tx_tail >= NUM_TX_DESC)
netif_stop_queue(dev);
out_unlock:
spin_unlock(&priv->lock);
out:
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}