-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patham_net8218.c
More file actions
executable file
·2844 lines (2666 loc) · 79.7 KB
/
am_net8218.c
File metadata and controls
executable file
·2844 lines (2666 loc) · 79.7 KB
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
/*
* Amlogic Ethernet Driver
* h
* Copyright (C) 2012 Amlogic, Inc.
*
* 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 named License,
* or 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
*
* Author: Platform-BJ@amlogic.com
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/io.h>
#include <linux/mii.h>
#include <linux/sched.h>
#include <linux/crc32.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <plat/eth.h>
#include <plat/regops.h>
#include <mach/am_regs.h>
#include <mach/pinmux.h>
#include <mach/gpio.h>
#include <asm/delay.h>
#include <linux/delay.h>
#include <linux/pinctrl/consumer.h>
#include <linux/amlogic/aml_gpio_consumer.h>
#include <linux/of_platform.h>
#include <linux/kthread.h>
#include "am_net8218.h"
#include <mach/mod_gate.h>
#define MODULE_NAME "ethernet"
#define DRIVER_NAME "ethernet"
#define DRV_NAME DRIVER_NAME
#define DRV_VERSION "v2.0.2"
#undef CONFIG_HAS_EARLYSUSPEND
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
static struct early_suspend early_suspend;
#endif
MODULE_DESCRIPTION("Amlogic Ethernet Driver");
MODULE_AUTHOR("Platform-BJ@amlogic.com>");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);
// >0 basic init and remove info;
// >1 further setup info;
// >2 rx data dump
// >3 tx data dump
#undef M_DEBUG_ON // use this to turn on debug/printk when needed
#ifdef CONFIG_AM_ETHERNET_DEBUG_LEVEL
static int g_debug = CONFIG_AM_ETHERNET_DEBUG_LEVEL;
#else
static int g_debug = 1;
#endif
// These two now control how many packets per tasklet are sent/received
static int g_mdcclk = 2;
static int new_maclogic = 0;
static unsigned int ethbaseaddr = ETHBASE;
static unsigned int savepowermode = 0;
static int interruptnum = ETH_INTERRUPT;
static int phy_interface = 1;
static int reset_delay = 0;
static int reset_pin_num = 0;
static int reset_pin_enable = 0;
static const char *reset_pin;
static unsigned int MDCCLK = ETH_MAC_4_GMII_Addr_CR_100_150;
module_param_named(amlog_level, g_debug, int, 0664);
MODULE_PARM_DESC(amlog_level, "ethernet debug level\n");
#include "am_mdio.c"
//#define LOOP_BACK_TEST
//#define MAC_LOOPBACK_TEST
//#define PHY_LOOPBACK_TEST
static int running = 0;
static struct net_device *my_ndev = NULL;
static struct aml_eth_platdata *eth_pdata = NULL;
static unsigned int g_ethernet_registered = 0;
static unsigned int g_mac_addr_setup = 0;
static unsigned int g_mac_pmt_enable = 0;
static char DEFMAC[] = "\x00\x01\x23\xcd\xee\xaf";
#define PERIPHS_SET_BITS(reg, mask) \
aml_set_reg32_mask(reg, mask)
#define PERIPHS_CLEAR_BITS(reg, mask) \
aml_clr_reg32_mask(reg, mask)
void start_test(struct net_device *dev);
static void write_mac_addr(struct net_device *dev, char *macaddr);
static int ethernet_reset(struct net_device *dev);
static int reset_mac(struct net_device *dev);
static void am_net_dump_macreg(void);
static void read_macreg(void);
void hardware_reset_phy(void);
extern char *aml_efuse_get_item(unsigned char* key_name);
/* --------------------------------------------------------------------------*/
/**
* @brief data_dump
*
* @param p
* @param len
*/
/* --------------------------------------------------------------------------*/
static void data_dump(unsigned char *p, int len)
{
int i, j;
char s[20];
for (i = 0; i < len; i += 16) {
printk("%08x:", (unsigned int)p);
for (j = 0; j < 16 && j < len - 0 * 16; j++) {
s[j] = (p[j] > 15 && p[j] < 128) ? p[j] : '.';
printk(" %02x", p[j]);
}
s[j] = 0;
printk(" |%s|\n", s);
p = p + 16;
}
}
/* --------------------------------------------------------------------------*/
/**
* @brief tx_data_dump
*
* @param p
* @param len
*/
/* --------------------------------------------------------------------------*/
#ifdef M_DEBUG_ON
static void tx_data_dump(unsigned char *p, int len)
{
if ((g_debug == 3) || (g_debug == 5)) {
printk("---------->\n");
data_dump(p, len);
}
return;
}
/* --------------------------------------------------------------------------*/
/**
* @brief rx_data_dump
*
* @param p
* @param len
*/
/* --------------------------------------------------------------------------*/
static void rx_data_dump(unsigned char *p, int len)
{
if ((g_debug == 4) || (g_debug == 5)) {
printk("<----------\n");
data_dump(p, len);
}
return;
}
#endif
/* --------------------------------------------------------------------------*/
/**
* @brief netdev_ioctl
*
* @param dev
* @param rq
* @param cmd
*
* @return
*/
/* --------------------------------------------------------------------------*/
static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
struct am_net_private *priv = netdev_priv(dev);
int ret;
if (!netif_running(dev)) return -EINVAL;
if (!priv->phydev) return -EINVAL;
spin_lock(&priv->lock);
ret = phy_mii_ioctl(priv->phydev, rq, cmd);
spin_unlock(&priv->lock);
return ret;
}
/* --------------------------------------------------------------------------*/
/**
* @brief init_rxtx_rings
*
* @param dev
*
* @return
*/
/* --------------------------------------------------------------------------*/
int init_rxtx_rings(struct net_device *dev)
{
struct am_net_private *np = netdev_priv(dev);
int i;
/* Fill in the Rx buffers. Handle allocation failure gracefully. */
for (i = 0; i < RX_RING_SIZE; i++) {
struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz);
np->rx_ring[i].skb = skb;
if (skb == NULL) {
break;
}
skb_reserve(skb, 2); /* 16 byte alignd for ip */
skb->dev = dev; /* Mark as being used by this device. */
np->rx_ring[i].buf = (unsigned long)skb->data;
np->rx_ring[i].buf_dma = dma_map_single(&dev->dev, (void *)np->rx_ring[i].buf, np->rx_buf_sz, DMA_FROM_DEVICE);
np->rx_ring[i].count = (DescChain) | (np->rx_buf_sz & DescSize1Mask);
np->rx_ring[i].status = (DescOwnByDma);
np->rx_ring[i].next_dma = &np->rx_ring_dma[i + 1];
np->rx_ring[i].next = &np->rx_ring[i + 1];
}
np->rx_ring[RX_RING_SIZE - 1].next_dma = &np->rx_ring_dma[0];
np->rx_ring[RX_RING_SIZE - 1].next = &np->rx_ring[0];
/* Initialize the Tx descriptors */
for (i = 0; i < TX_RING_SIZE; i++) {
np->tx_ring[i].buf = 0;
np->tx_ring[i].status = 0;
np->tx_ring[i].count =
(DescChain) | (np->rx_buf_sz & DescSize1Mask);
np->tx_ring[i].next_dma = &np->tx_ring_dma[i + 1];
np->tx_ring[i].next = &np->tx_ring[i + 1];
np->tx_ring[i].skb = NULL;
}
np->tx_ring[TX_RING_SIZE - 1].next_dma = &np->tx_ring_dma[0];
np->tx_ring[TX_RING_SIZE - 1].next = &np->tx_ring[0];
np->start_tx = &np->tx_ring[0];
np->last_tx = NULL;
np->last_rx = &np->rx_ring[RX_RING_SIZE - 1];
CACHE_WSYNC(np->tx_ring, sizeof(struct _tx_desc)*TX_RING_SIZE);
CACHE_WSYNC(np->rx_ring, sizeof(struct _rx_desc)*RX_RING_SIZE);
return 0;
}
/* --------------------------------------------------------------------------*/
/**
* @brief alloc_ringdesc
*
* @param dev
*
* @return
*/
/* --------------------------------------------------------------------------*/
static int alloc_ringdesc(struct net_device *dev)
{
struct am_net_private *np = netdev_priv(dev);
np->rx_buf_sz = (dev->mtu <= 1500 ? PKT_BUF_SZ : dev->mtu + 32);
np->rx_ring = kmalloc(sizeof(struct _rx_desc) * RX_RING_SIZE, GFP_KERNEL | GFP_DMA);
np->rx_ring_dma = (void*)virt_to_phys(np->rx_ring);
if (!np->rx_ring) {
return -ENOMEM;
}
if (!IS_CACHE_ALIGNED(np->rx_ring)) {
printk("Error the alloc mem is not cache aligned(%p)\n", np->rx_ring);
}
printk("NET MDA descpter start addr=%p\n", np->rx_ring);
np->tx_ring = kmalloc(sizeof(struct _tx_desc) * TX_RING_SIZE, GFP_KERNEL | GFP_DMA);
np->tx_ring_dma = (void*)virt_to_phys(np->tx_ring);
if (init_rxtx_rings(dev)) {
printk("init rx tx ring failed!!\n");
return -1;
}
return 0;
}
/* --------------------------------------------------------------------------*/
/**
* @brief free_ringdesc
*
* @param dev
*
* @return
*/
/* --------------------------------------------------------------------------*/
static int free_ringdesc(struct net_device *dev)
{
struct am_net_private *np = netdev_priv(dev);
int i;
for (i = 0; i < RX_RING_SIZE; i++) {
if (np->rx_ring[i].skb) {
if (np->rx_ring[i].buf_dma != 0) {
dma_unmap_single(&dev->dev, np->rx_ring[i].buf_dma, np->rx_buf_sz, DMA_FROM_DEVICE);
}
dev_kfree_skb_any(np->rx_ring[i].skb);
}
np->rx_ring[i].skb = NULL;
}
for (i = 0; i < TX_RING_SIZE; i++) {
if (np->tx_ring[i].skb != NULL) {
if (np->tx_ring[i].buf_dma != 0) {
dma_unmap_single(&dev->dev, np->tx_ring[i].buf_dma, np->rx_buf_sz, DMA_TO_DEVICE);
}
dev_kfree_skb_any(np->tx_ring[i].skb);
}
np->tx_ring[i].skb = NULL;
}
if (np->rx_ring) {
kfree(np->rx_ring);
}
np->rx_ring = NULL;
if (np->tx_ring) {
kfree(np->tx_ring);
}
np->tx_ring = NULL;
return 0;
}
/* --------------------------------------------------------------------------*/
/**
* @brief update_status
*
* @param dev
* @param status
* @param mask
*
* @return
*/
/* --------------------------------------------------------------------------*/
// rt_tasklet
__attribute__((flatten)) void net_rt_update_status(unsigned long dev_instance)
{
struct net_device *dev = (struct net_device *)dev_instance;
struct am_net_private *np = netdev_priv(dev);
if (likely(np->status & NOR_INTR_EN)) { //Normal Interrupts Process
if (likely(np->status & RX_INTR_EN)) { //Receive Interrupt Process
writel((1 << 6 | 1 << 16), (void*)(np->base_addr + ETH_DMA_5_Status));
tasklet_schedule(&np->rx_tasklet);
}
if (likely(np->status & TX_INTR_EN)) { //Transmit Interrupt Process
writel(1,(void*)(np->base_addr + ETH_DMA_1_Tr_Poll_Demand));
netif_wake_queue(dev);
writel((1 << 0 | 1 << 16),(void*)(np->base_addr + ETH_DMA_5_Status));
tasklet_schedule(&np->tx_tasklet);
}
}
tasklet_schedule(&np->st_tasklet); // net_update_stats()
}
// This tasklet does all the error checks
__attribute__((flatten)) void net_update_status(unsigned long dev_instance)
{
unsigned long status;
struct net_device *dev = (struct net_device *)dev_instance;
struct am_net_private *np = netdev_priv(dev);
status = np->status;
if (likely(status & NOR_INTR_EN)) { //Normal Interrupts Process
if (unlikely(status & EARLY_RX_INTR_EN)) {
writel((EARLY_RX_INTR_EN | NOR_INTR_EN),(void*) (np->base_addr + ETH_DMA_5_Status));
}
if (unlikely(status & TX_BUF_UN_EN)) {
writel((1 << 2 | 1 << 16), (void*)(np->base_addr + ETH_DMA_5_Status));
tasklet_schedule(&np->tx_tasklet);
//this error will cleard in start tx...
}
} else if (unlikely(status & ANOR_INTR_EN)) { //Abnormal Interrupts Process
if (status & RX_BUF_UN) {
writel((RX_BUF_UN | ANOR_INTR_EN),(void*) (np->base_addr + ETH_DMA_5_Status));
np->stats.rx_over_errors++;
writel(1, (void*)(np->base_addr + ETH_DMA_2_Re_Poll_Demand));
tasklet_schedule(&np->rx_tasklet);
}
if (status & RX_STOP_EN) {
writel((RX_STOP_EN | ANOR_INTR_EN),
(void*)(np->base_addr + ETH_DMA_5_Status));
writel(1, (void*)(np->base_addr + ETH_DMA_2_Re_Poll_Demand));
tasklet_schedule(&np->rx_tasklet);
}
if (status & RX_WATCH_TIMEOUT) {
writel((RX_WATCH_TIMEOUT | ANOR_INTR_EN),
(void*)( np->base_addr + ETH_DMA_5_Status));
writel(1, (void*)(np->base_addr + ETH_DMA_2_Re_Poll_Demand));
}
if (status & FATAL_BUS_ERROR) {
writel((FATAL_BUS_ERROR | ANOR_INTR_EN),
(void*)(np->base_addr + ETH_DMA_5_Status));
printk(KERN_WARNING DRV_NAME "system reset\n");
free_ringdesc(dev);
writel(0, (void*)(np->base_addr + ETH_DMA_6_Operation_Mode));
writel(0, (void*)(np->base_addr + ETH_DMA_7_Interrupt_Enable));
reset_mac(dev);
printk(KERN_WARNING "[" DRV_NAME "]" "fatal bus error\n");
}
if (status & EARLY_TX_INTR_EN) {
writel((EARLY_TX_INTR_EN | ANOR_INTR_EN),
(void*) (np->base_addr + ETH_DMA_5_Status));
writel(1,(void*)(np->base_addr + ETH_DMA_1_Tr_Poll_Demand));
netif_wake_queue(dev);
}
if (status & TX_STOP_EN) {
writel((TX_STOP_EN | ANOR_INTR_EN),
(void*)(np->base_addr + ETH_DMA_5_Status));
writel(1,(void*)(np->base_addr + ETH_DMA_1_Tr_Poll_Demand));
netif_wake_queue(dev);
tasklet_schedule(&np->tx_tasklet);
}
if (status & TX_JABBER_TIMEOUT) {
writel((TX_JABBER_TIMEOUT | ANOR_INTR_EN),
(void*) (np->base_addr + ETH_DMA_5_Status));
printk(KERN_WARNING "[" DRV_NAME "]" "tx jabber timeout\n");
writel(1,(void*)(np->base_addr + ETH_DMA_1_Tr_Poll_Demand));
netif_wake_queue(dev);
np->first_tx = 1;
}
if (status & RX_FIFO_OVER) {
writel((RX_FIFO_OVER | ANOR_INTR_EN),
(void*)( np->base_addr + ETH_DMA_5_Status));
np->stats.rx_fifo_errors++;
writel(1, (void*)(np->base_addr + ETH_DMA_2_Re_Poll_Demand));
tasklet_schedule(&np->rx_tasklet);
printk(KERN_WARNING "[" DRV_NAME "]" "Rx fifo over\n");
}
if (status & TX_UNDERFLOW) {
writel((TX_UNDERFLOW | ANOR_INTR_EN),
(void*)(np->base_addr + ETH_DMA_5_Status));
printk(KERN_WARNING "[" DRV_NAME "]" "Tx underflow\n");
writel(1,(void*)(np->base_addr + ETH_DMA_1_Tr_Poll_Demand));
netif_wake_queue(dev);
np->first_tx = 1;
tasklet_schedule(&np->tx_tasklet);
}
}
}
/* --------------------------------------------------------------------------*/
/**
* @brief print_rx_error_log
*
* @param status
*/
/* --------------------------------------------------------------------------*/
static void inline print_rx_error_log(unsigned long status)
{
if (status & DescRxTruncated) {
printk(KERN_WARNING "Descriptor Error desc-mask[%d]\n",
DescRxTruncated);
}
if (status & DescSAFilterFail) {
printk(KERN_WARNING
"Source Address Filter Fail rx desc-mask[%d]\n",
DescSAFilterFail);
}
if (status & DescRxLengthError) {
printk(KERN_WARNING "Length Error rx desc-mask[%d]\n",
DescRxLengthError);
}
if (status & DescRxIPChecksumErr) {
printk(KERN_WARNING "TCP checksum Error rx desc-mask[%d]\n",
DescRxLengthError);
}
if (status & DescRxTCPChecksumErr) {
printk(KERN_WARNING "TCP checksum Error rx desc-mask[%d]\n",
DescRxLengthError);
}
if (status & DescRxDamaged) {
printk(KERN_WARNING "Overflow Error rx desc-mask[%d]\n",
DescRxDamaged);
}
if (status & DescRxMiiError) {
printk(KERN_WARNING "Receive Error rx desc-mask[%d]\n",
DescRxMiiError);
}
if (status & DescRxDribbling) {
printk(KERN_WARNING "Dribble Bit Error rx desc-mask[%d]\n",
DescRxDribbling);
}
if (status & DescRxCrc) {
printk(KERN_WARNING "CE: CRC Errorrx desc-mask[%d]\n",
DescRxCrc);
}
}
/* --------------------------------------------------------------------------*/
/**
* @brief net_tasklet
*
* @param dev_instance
*/
/* --------------------------------------------------------------------------*/
// todo: need to figure out how to either get multiple RX, TX queues on diff processors
// or quit hammering CPU0 so much. Perhaps split the priv->phy info into TX, RX versions
// note: problem with high CPU0 irq is due to usb1, uart, and vsync IRQ
__attribute__((flatten)) void net_tasklettx(unsigned long dev_instance)
{
struct net_device *dev = (struct net_device *)dev_instance;
struct am_net_private *np = netdev_priv(dev);
unsigned long flags;
struct _tx_desc *c_tx, *tx = NULL;
/* handle normal tx */
c_tx = (void *)readl((void*)(np->base_addr + ETH_DMA_18_Curr_Host_Tr_Descriptor));
c_tx = np->tx_ring + (c_tx - np->tx_ring_dma);
tx = np->start_tx;
CACHE_RSYNC(tx, sizeof(struct _tx_desc));
while (likely(tx != NULL && tx != c_tx && !(tx->status & DescOwnByDma))) {
if(unlikely(!spin_trylock_irqsave(&np->lock,flags)))
{
break;
}
if (likely(tx->skb != NULL)) {
//clear to next send;
if (likely(np->tx_full)) {
netif_wake_queue(dev);
np->tx_full = 0;
}
if (tx->buf_dma != 0) {
dma_unmap_single(&dev->dev, tx->buf_dma, np->rx_buf_sz, DMA_TO_DEVICE);
}
dev_kfree_skb_any(tx->skb);
tx->skb = NULL;
tx->buf = 0;
tx->buf_dma = 0;
tx->status = 0;
} else {
spin_unlock_irqrestore(&np->lock, flags);
break;
}
spin_unlock_irqrestore(&np->lock, flags);
tx = tx->next;
CACHE_RSYNC(tx, sizeof(struct _tx_desc));
}
np->start_tx = tx;
writel(np->irq_mask, (void*)(np->base_addr + ETH_DMA_7_Interrupt_Enable));
}
// Handle RX packets in this tasklet
__attribute__((flatten)) void net_taskletrx(unsigned long dev_instance)
{
struct net_device *dev = (struct net_device *)dev_instance;
struct am_net_private *np = netdev_priv(dev);
int len;
struct _rx_desc *c_rx, *rx = NULL;
/* handle normal rx */
c_rx = (void *)readl((void*)(np->base_addr + ETH_DMA_19_Curr_Host_Re_Descriptor));
c_rx = np->rx_ring + (c_rx - np->rx_ring_dma);
rx = np->last_rx->next;
while (likely(rx != NULL)) {
CACHE_RSYNC(rx, sizeof(struct _rx_desc));
if (likely(!(rx->status & (DescOwnByDma)))) {
int ip_summed = CHECKSUM_UNNECESSARY;
len = (rx->status & DescFrameLengthMask) >> DescFrameLengthShift;
if (unlikely(len < 18 || len > np->rx_buf_sz)) { //here is fatal error we drop it ;
np->stats.rx_dropped++;
np->stats.rx_errors++;
goto to_next;
}
if (unlikely(rx->status & (DescError))) { //here is not often occur
print_rx_error_log(rx->status);
if ((rx->status & DescRxIPChecksumErr) || (rx->status & DescRxTCPChecksumErr)) { //maybe checksum engine's problem;
//we set the NONE for ip/tcp need check it again
ip_summed = CHECKSUM_NONE;
} else {
np->stats.rx_dropped++;
np->stats.rx_errors++;
goto to_next;
}
}
len = len - 4; //clear the crc
if (unlikely(rx->skb == NULL)) {
printk("NET skb pointer error!!!\n");
break;
}
if (likely(rx->buf_dma != 0)) {
dma_unmap_single(&dev->dev, rx->buf_dma,/* np->rx_buf_sz*/len, DMA_FROM_DEVICE);
}
if (rx->skb->len > 0) {
printk("skb have data before,skb=%p,len=%d\n", rx->skb, rx->skb->len);
rx->skb = NULL;
goto to_next;
}
skb_put(rx->skb, len);
rx->skb->dev = dev;
rx->skb->protocol = eth_type_trans(rx->skb, dev);
/*we have checked in hardware; we do not need to check again */
rx->skb->ip_summed = ip_summed;
rx->buf_dma = 0;
netif_rx(rx->skb);
rx->skb = NULL;
dev->last_rx = jiffies;
np->stats.rx_packets++;
np->stats.rx_bytes += len;
#ifdef M_DEBUG_ON
rx_data_dump((unsigned char *)rx->buf,len);
#endif
to_next:
if (rx->skb) {
dev_kfree_skb_any(rx->skb);
}
rx->skb = dev_alloc_skb(np->rx_buf_sz );
if (unlikely(rx->skb == NULL)) {
printk(KERN_ERR "error to alloc the skb\n");
rx->buf = 0;
rx->buf_dma = 0;
rx->status = 0;
rx->count = 0;
np->last_rx = rx;
CACHE_WSYNC(rx, sizeof(struct _rx_desc));
break;
}
skb_reserve(rx->skb, 2);
rx->buf = (unsigned long)rx->skb->data;
rx->buf_dma = dma_map_single(&dev->dev, (void *)rx->buf, (unsigned long)np->rx_buf_sz, DMA_FROM_DEVICE); //invalidate for next dma in;
rx->count = (DescChain) | (np->rx_buf_sz & DescSize1Mask);
rx->status = DescOwnByDma;
CACHE_WSYNC(rx, sizeof(struct _rx_desc));
np->last_rx = rx;
rx = rx->next;
} else {
break;
}
}
writel(np->irq_mask, (void*)(np->base_addr + ETH_DMA_7_Interrupt_Enable));
}
/* --------------------------------------------------------------------------*/
/**
* @brief intr_handler
*
* @param irq
* @param dev_instance
*
* @return
*/
/* --------------------------------------------------------------------------*/
// This routine has all un-necessary if or variables removed to make it fast
static __attribute__((flatten)) irqreturn_t intr_handler(int irq, void *dev_instance)
{
struct net_device *dev = (struct net_device *)dev_instance;
struct am_net_private *np = netdev_priv(dev);
writel(0, (void*)(np->base_addr + ETH_DMA_7_Interrupt_Enable));//disable irq
np->status = readl((void*)(np->base_addr + ETH_DMA_5_Status));
tasklet_hi_schedule(&np->rt_tasklet);
return IRQ_HANDLED;
}
// PMT not used in this driver, could remove
static int mac_pmt_enable(unsigned int enable)
{
struct am_net_private *np = netdev_priv(my_ndev);
unsigned long val;
int i;
if (enable >= 1) {
switch (enable) {
case 1:
/* setup pmt mode */
val = 0 << 0 //Power Down
| 1 << 1 //Magic Packet Enable
| 0;
writel(val, (void*)(np->base_addr + ETH_MAC_PMT_Control_and_Status));
break;
case 2:
val = 0 << 0 //Power Down
| 1 << 2 //Wake-Up Frame Enable
| 1 << 31 //Wake-Up Frame Filter Register Pointer Reset
| 0;
writel(val, (void*)(np->base_addr + ETH_MAC_PMT_Control_and_Status));
/* setup Wake-Up Frame Filter */
/* Filter 0 */
val = 0x7f;
writel(val, (void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
val = 0;
/* Filter 1,2,3 */
for (i = 0; i < 3; i++) {
writel(val, (void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
}
val = 1 << 0 //Enable Filter 0
| 1 << 3 //Multicast
| 0;
writel(val, (void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
val = 42;
writel(val, (void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
val = 0x5b3e;
writel(val, (void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
val = 0;
writel(val, (void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
for (i = 0; i < 8; i++) {
val = readl((void*)(np->base_addr + ETH_MAC_Remote_Wake_Up_Frame_Filter));
printk("ETH_MAC_Remote_Wake_Up_Frame_Filter=%d : 0x%lx\n", i, val);
}
break;
case 3:
val = 0 << 0 //Power Down
| 1 << 2 //Wake-Up Frame Enable
| 1 << 9 //Global Unicast
| 0;
writel(val,(void*)(np->base_addr + ETH_MAC_PMT_Control_and_Status));
break;
default:
break;
}
} else {
/* setup pmt mode */
val = 0;
writel(val, (void*)(np->base_addr + ETH_MAC_PMT_Control_and_Status));
/* setup Wake-Up Frame Filter */
}
return 0;
}
/* --------------------------------------------------------------------------*/
/**
* @brief phy_reset
*
* @param ndev
*
* @return
*/
/* --------------------------------------------------------------------------*/
static int aml_mac_init(struct net_device *ndev)
{
struct am_net_private *np = netdev_priv(ndev);
unsigned long val;
int k;
// based on an old am_net8218.c below should be mac reset
writel(1, (void*)(np->base_addr + ETH_DMA_0_Bus_Mode));
// below waits for mac to reset
for (k=0;(readl((void*)(np->base_addr + ETH_DMA_6_Operation_Mode)) & 1) && k<1000;k++) udelay(1);
writel(0x00100800,(void*)(np->base_addr + ETH_DMA_0_Bus_Mode));
printk("--1--write mac add to:");
data_dump(ndev->dev_addr, 6);
printk("--2--write mac add to:");
data_dump(ndev->dev_addr, 6);
write_mac_addr(ndev, ndev->dev_addr);
val = 0xc80c | //8<<8 | 8<<17; //tx and rx all 8bit mode;
1 << 10 | 1 << 24; //checksum offload enabled
#ifdef MAC_LOOPBACK_TEST
val |= 1 << 12; //mac loop back
#endif
writel(val, (void*)(np->base_addr + ETH_MAC_0_Configuration));
val = 1 << 4;/*receive all muticast*/
//| 1 << 31; //receive all the data
writel(val,(void*)( np->base_addr + ETH_MAC_1_Frame_Filter));
writel((unsigned long)&np->rx_ring_dma[0], (void*)(np->base_addr + ETH_DMA_3_Re_Descriptor_List_Addr));
writel((unsigned long)&np->tx_ring_dma[0], (void*)(np->base_addr + ETH_DMA_4_Tr_Descriptor_List_Addr));
writel(np->irq_mask, (void*)(np->base_addr + ETH_DMA_7_Interrupt_Enable));
writel((0), (void*)(np->base_addr + ETH_MAC_Interrupt_Mask));
val = 7 << 14 //TTC
| 1 << 8 //EFC
| 1 << 21 //TSF
| 1 << 25 //RSF
| 1 << 26;//DT
/*don't start receive here */
printk("Current DMA mode=%x, set mode=%lx\n", readl((void*)(np->base_addr + ETH_DMA_6_Operation_Mode)), val);
writel(val, (void*)(np->base_addr + ETH_DMA_6_Operation_Mode));
return 0;
}
/*--------------------------*/
// https://www.kernel.org/doc/Documentation/networking/phy.txt
static void aml_adjust_link(struct net_device *dev)
{
struct am_net_private *priv = netdev_priv(dev);
struct phy_device *phydev = priv->phydev;
unsigned long flags;
int new_state = 0;
int val;
if (phydev == NULL)
return;
//#define P_PREG_ETHERNET_ADDR0 CBUS_REG_ADDR(PREG_ETHERNET_ADDR0)
//#define PREG_ETHERNET_ADDR0 0x2042 ///../ucode/register.h:450
spin_lock_irqsave(&priv->lock, flags);
if(phydev->phy_id == INTERNALPHY_ID) {
val = (8<<27)|(7 << 24)|(1<<16)|(1<<15)|(1 << 13)|(1 << 12)|(4 << 4)|(0 << 1);
PERIPHS_SET_BITS(P_PREG_ETHERNET_ADDR0, val);
}
if (phydev->link) {
//#define ETH_MAC_0_Configuration (0x0000)
u32 ctrl = readl((void*)(priv->base_addr + ETH_MAC_0_Configuration));
/* Now we make sure that we can be in full duplex mode.
* If not, we operate in half-duplex mode. */
if (phydev->duplex != priv->oldduplex) {
new_state = 1;
if (!(phydev->duplex)) {
printk("[adjust link] -> eth: half-duplex\n");
ctrl &= ~((1 << 11)|(7<< 17)|(3<<5));
if(new_maclogic != 0)
ctrl |= (4 << 17);
ctrl |= (3 << 5);
}
else {
printk("[adjust link] -> eth: full-duplex\n");
ctrl &= ~((7 << 17)|(3 << 5));
ctrl |= (1 << 11);
if(new_maclogic != 0)
ctrl |= (2 << 17);
}
priv->oldduplex = phydev->duplex;
}
if (phydev->speed != priv->speed) {
printk("[adjust link] -> eth: phy_speed <> priv_speed)\n");
new_state = 1;
if(new_maclogic != 0) PERIPHS_CLEAR_BITS(P_PREG_ETHERNET_ADDR0, 1);
switch (phydev->speed) {
case 1000:
ctrl &= ~((1 << 14)|(1 << 15));//1000m
ctrl |= (1 << 13);//1000m
break;
case 100:
ctrl |= (1 << 14)|(1 << 15);
printk("[adjust link] -> eth: switching to RGMII 100\n");
if(new_maclogic !=0) PERIPHS_SET_BITS(P_PREG_ETHERNET_ADDR0, (1 << 1));
break;
case 10:
ctrl &= ~((1 << 14)|(3 << 5));//10m half backoff = 00
printk("[adjust link] -> eth: switching to RGMII 10\n");
if(new_maclogic !=0) PERIPHS_CLEAR_BITS(P_PREG_ETHERNET_ADDR0, (1 << 1));
if(phydev->phy_id == INTERNALPHY_ID) {
val =0x4100b040;
WRITE_CBUS_REG(P_PREG_ETHERNET_ADDR0, val);
}
break;
default:
printk("%s: Speed (%d) is not 10"
" or 100!\n", dev->name, phydev->speed);
break;
}
if(new_maclogic !=0) PERIPHS_SET_BITS(P_PREG_ETHERNET_ADDR0, 1);
priv->speed = phydev->speed;
}
writel(ctrl, (void*)(priv->base_addr + ETH_MAC_0_Configuration));
if (!priv->oldlink) {
new_state = 1;
priv->oldlink = 1;
}
} else if (priv->oldlink) {
new_state = 1;
priv->oldlink = 0;
priv->speed = 0;
priv->oldduplex = -1;
}
if (new_state){
if(new_maclogic == 1) read_macreg();
printk("[adjust link -> eth: am_adjust_link state change (new_state=true)\n");
phy_print_status(phydev);
}
spin_unlock_irqrestore(&priv->lock, flags);
}
// Init phy, detect it and attach
static int aml_phy_init(struct net_device *dev)
{
struct am_net_private *priv = netdev_priv(dev);
struct phy_device *phydev;
char phy_id[MII_BUS_ID_SIZE + 3];
char bus_id[MII_BUS_ID_SIZE];
priv->oldlink = 0;
priv->speed = 0;
priv->oldduplex = -1;
printk("phy_interface = %d\n",phy_interface);
if(phy_interface == 1)
priv->phy_interface = PHY_INTERFACE_MODE_RMII;
else
priv->phy_interface = PHY_INTERFACE_MODE_RGMII;
if (priv->phy_addr == -1) {
/* We don't have a PHY, so do nothing */
pr_err("%s: have no attached PHY\n", dev->name);
return -1;
}
snprintf(bus_id, MII_BUS_ID_SIZE, "%x", 0);
snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
priv->phy_addr);
printk("aml_phy_init: trying to attach to %s\n", phy_id);
if(priv->phydev && savepowermode)
priv->phydev->drv->resume(priv->phydev);
phydev = phy_connect(dev, phy_id, &aml_adjust_link, priv->phy_interface);
if (IS_ERR(phydev)) {
pr_err("%s: Could not attach to PHY\n", dev->name);
return PTR_ERR(phydev);
}
/*
* Broken HW is sometimes missing the pull-up resistor on the
* MDIO line, which results in reads to non-existent devices returning
* 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
* device as well.
* Note: phydev->phy_id is the result of reading the UID PHY registers.
*/
if (phydev->phy_id == 0) {
phy_disconnect(phydev);
return -ENODEV;
}
pr_debug("aml_phy_init: %s: attached to PHY (UID 0x%x)"
" Link = %d\n", dev->name, phydev->phy_id, phydev->link);
priv->phydev = phydev;
if (priv->phydev) phy_start(priv->phydev);
return 0;
}
static void read_macreg(void)
{
int reg = 0;
int val = 0;
struct am_net_private *np = netdev_priv(my_ndev);
if ((np == NULL) || (np->dev == NULL))
return;
for (reg = ETH_MAC_0_Configuration; reg <= ETH_MAC_54_SGMII_RGMII_Status; reg += 0x4) {
val = readl((void*)(np->base_addr + reg));
}
for (reg = ETH_DMA_0_Bus_Mode; reg <= ETH_DMA_21_Curr_Host_Re_Buffer_Addr; reg += 0x4) {
val = readl((void*)(np->base_addr + reg));
}
}
static int reset_mac(struct net_device *dev)
{
struct am_net_private *np = netdev_priv(dev);
int res;
unsigned long flags;
int tmp;
spin_lock_irqsave(&np->lock, flags);
res = alloc_ringdesc(dev);
spin_unlock_irqrestore(&np->lock, flags);
if (res != 0) {
printk(KERN_INFO "can't alloc ring desc!err=%d\n", res);
goto out_err;
}
aml_mac_init(dev);
np->first_tx = 1;
tmp = readl((void*)(np->base_addr + ETH_DMA_6_Operation_Mode));//tx enable
tmp |= (7 << 14) | (1 << 13);
writel(tmp, (void*)(np->base_addr + ETH_DMA_6_Operation_Mode));
tmp = readl((void*)(np->base_addr + ETH_MAC_6_Flow_Control));
tmp |= (1 << 1) | (1 << 0);
writel(tmp, (void*)(np->base_addr + ETH_MAC_6_Flow_Control));
tmp = readl((void*)(np->base_addr + ETH_DMA_6_Operation_Mode));
tmp |= (1 << 1); /*start receive*/
writel(tmp, (void*)(np->base_addr + ETH_DMA_6_Operation_Mode));
out_err:
return res;
}
/* --------------------------------------------------------------------------*/
/**
* @brief ethernet_reset
*
* @param dev
*
* @return
*/
/* --------------------------------------------------------------------------*/
static int ethernet_reset(struct net_device *dev)
{
struct am_net_private *np = netdev_priv(dev);
int res;
unsigned long flags;
int tmp;
printk(KERN_INFO "Ethernet reset\n");
spin_lock_irqsave(&np->lock, flags);
res = alloc_ringdesc(dev);
spin_unlock_irqrestore(&np->lock, flags);
if (res != 0) {
printk(KERN_INFO "can't alloc ring desc!err=%d\n", res);
goto out_err;
}
res = aml_phy_init(dev);
if (res != 0) {
printk(KERN_INFO "init phy failed! err=%d\n", res);
goto out_err;
}
aml_mac_init(dev);
np->first_tx = 1;
tmp = readl((void*)(np->base_addr + ETH_DMA_6_Operation_Mode));//tx enable
tmp |= (7 << 14) | (1 << 13);
writel(tmp, (void*)(np->base_addr + ETH_DMA_6_Operation_Mode));
tmp = readl((void*)(np->base_addr + ETH_MAC_6_Flow_Control));
tmp |= (1 << 1) | (1 << 0);
writel(tmp, (void*)(np->base_addr + ETH_MAC_6_Flow_Control));