-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivers-net-ethernet-allwinner-sunxi_emac.c
1858 lines (1520 loc) · 48.3 KB
/
drivers-net-ethernet-allwinner-sunxi_emac.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
/*
* Allwinner sunxi ethernet mac driver for Linux.
* Copyright (C) 1997 Sten Wang
*
* 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.
*
* (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
*
* Additional updates, Copyright:
* Ben Dooks <ben@simtec.co.uk>
* Sascha Hauer <s.hauer@pengutronix.de>
*/
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/crc32.h>
#include <linux/mii.h>
#include <linux/ethtool.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/clk.h>
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/workqueue.h>
#include <asm/cacheflush.h>
#include <asm/irq.h>
#include <plat/dma_compat.h>
#include <plat/platform.h>
#include <plat/sys_config.h>
#include <plat/system.h>
#include <mach/clock.h>
#include "sunxi_emac.h"
/* Board/System/Debug information/definition ---------------- */
#define SUNXI_EMAC_PHY 0x100 /* PHY address 0x01 */
#define CARDNAME "sunxi_emac"
#define DRV_VERSION "1.01"
#define DMA_CPU_TRRESHOLD 2000
/* bit_flags */
#define SUNXI_EMAC_TX_TIMEOUT_PENDING 0
/*
* Transmit timeout, default 5 seconds.
*/
static int emac_used;
static int watchdog = 5000;
static unsigned char mac_addr[6] = {0x00};
static char *mac_addr_param = ":";
module_param(watchdog, int, 0400);
MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
/* SUNXI_EMAC register address locking.
*
* The SUNXI_EMAC uses an address register to control where data written
* to the data register goes. This means that the address register
* must be preserved over interrupts or similar calls.
*
* During interrupt and other critical calls, a spinlock is used to
* protect the system, but the calls themselves save the address
* in the address register in case they are interrupting another
* access to the device.
*
* For general accesses a lock is provided so that calls which are
* allowed to sleep are serialised so that the address register does
* not need to be saved. This lock also serves to serialise access
* to the EEPROM and PHY access registers which are shared between
* these two devices.
*/
/* The driver supports the original SUNXI_EMACE, and now the two newer
* devices, SUNXI_EMACA and SUNXI_EMACB.
*/
/* Structure/enum declaration ------------------------------- */
typedef struct sunxi_emac_board_info {
void __iomem *emac_vbase; /* mac I/O base address */
void __iomem *sram_vbase; /* sram control I/O base address */
void __iomem *gpio_vbase; /* gpio I/O base address */
u16 irq; /* IRQ */
u16 tx_fifo_stat;
int debug_level;
unsigned long bit_flags;
void (*inblk)(void __iomem *port, void *data, int length);
void (*outblk)(void __iomem *port, void *data, int length);
void (*dumpblk)(void __iomem *port, int length);
struct device *dev; /* parent device */
struct resource *emac_base_res; /* resources found */
struct resource *sram_base_res; /* resources found */
struct resource *gpio_base_res; /* resources found */
struct resource *emac_base_req; /* resources found */
struct resource *sram_base_req; /* resources found */
struct resource *gpio_base_req; /* resources found */
struct resource *irq_res;
struct mutex addr_lock; /* phy and eeprom access lock */
struct delayed_work phy_poll;
struct work_struct tx_timeout_work;
struct net_device *ndev;
spinlock_t lock;
struct mii_if_info mii;
u32 msg_enable;
user_gpio_set_t *mos_gpio;
u32 mos_pin_handler;
} sunxi_emac_board_info_t;
/* debug code */
#define CONFIG_SUNXI_EMAC_DEBUGLEVEL 00
#define sunxi_emac_dbg(db, lev, msg...) do { \
if ((lev) < CONFIG_SUNXI_EMAC_DEBUGLEVEL && \
(lev) < db->debug_level) { \
dev_dbg(db->dev, msg); \
} \
} while (0)
static inline sunxi_emac_board_info_t *to_sunxi_emac_board(struct net_device *dev)
{
return netdev_priv(dev);
}
static int sunxi_emac_phy_read(struct net_device *dev, int phyaddr_unused, int reg);
static void sunxi_emac_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value);
static void sunxi_emac_rx(struct net_device *dev);
static void read_random_macaddr(unsigned char *mac, struct net_device *ndev);
static struct sunxi_dma_params emacrx_dma = {
.client.name = "EMACRX_DMA",
#if defined CONFIG_ARCH_SUN4I || defined CONFIG_ARCH_SUN5I
.channel = DMACH_DEMACR,
#endif
.dma_addr = 0x01C0B04C,
};
static int emacrx_completed_flag = 1;
void emacrx_dma_buffdone(struct sunxi_dma_params *dma, void *arg)
{
struct net_device *dev = arg;
sunxi_emac_rx(dev);
}
int emacrx_dma_inblk(dma_addr_t buff_addr, __u32 len)
{
int ret;
#if defined CONFIG_ARCH_SUN4I || defined CONFIG_ARCH_SUN5I
struct dma_hw_conf emac_hwconf = {
.xfer_type = DMAXFER_D_SWORD_S_SWORD,
.hf_irq = SW_DMA_IRQ_FULL,
.cmbk = 0x03030303,
.dir = SW_DMA_RDEV,
.from = emacrx_dma.dma_addr,
.address_type = DMAADDRT_D_LN_S_IO,
.drqsrc_type = DRQ_TYPE_EMAC
};
ret = sw_dma_setflags(emacrx_dma.channel, SW_DMAF_AUTOSTART);
if (ret != 0)
return ret;
#else
dma_config_t emac_hwconf = {
.xfer_type = {
.src_data_width = DATA_WIDTH_32BIT,
.src_bst_len = DATA_BRST_4,
.dst_data_width = DATA_WIDTH_32BIT,
.dst_bst_len = DATA_BRST_4
},
.address_type = {
.src_addr_mode = DDMA_ADDR_IO,
.dst_addr_mode = DDMA_ADDR_LINEAR
},
.bconti_mode = false,
.src_drq_type = D_SRC_EMAC_RX,
.dst_drq_type = D_DST_SRAM,
.irq_spt = CHAN_IRQ_FD
};
#endif
ret = sunxi_dma_config(&emacrx_dma, &emac_hwconf, 0x03030303);
if (ret != 0)
return ret;
ret = sunxi_dma_enqueue(&emacrx_dma, buff_addr, len, 1);
if (ret != 0)
return ret;
ret = sunxi_dma_start(&emacrx_dma);
if (ret != 0)
return ret;
return 0;
}
/* SUNXI_EMAC network board routine ---------------------------- */
static void
sunxi_emac_reset(sunxi_emac_board_info_t *db)
{
dev_dbg(db->dev, "resetting device\n");
/* RESET device */
writel(0, db->emac_vbase + SUNXI_EMAC_CTL_REG);
udelay(200);
writel(1, db->emac_vbase + SUNXI_EMAC_CTL_REG);
udelay(200);
}
static void sunxi_emac_outblk_32bit(void __iomem *reg, void *data, int count)
{
writesl(reg, data, (count+3) >> 2);
}
static void sunxi_emac_inblk_32bit(void __iomem *reg, void *data, int count)
{
readsl(reg, data, (count+3) >> 2);
}
static void sunxi_emac_dumpblk_32bit(void __iomem *reg, int count)
{
int i;
int tmp;
count = (count + 3) >> 2;
for (i = 0; i < count; i++)
tmp = readl(reg);
}
static void sunxi_emac_schedule_poll(sunxi_emac_board_info_t *db)
{
schedule_delayed_work(&db->phy_poll, HZ * 2);
}
static int sunxi_emac_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
if (!netif_running(dev))
return -EINVAL;
return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
}
/* ethtool ops */
static void sunxi_emac_get_drvinfo(struct net_device *dev,
struct ethtool_drvinfo *info)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
strcpy(info->driver, CARDNAME);
strcpy(info->version, DRV_VERSION);
strcpy(info->bus_info, to_platform_device(dm->dev)->name);
}
static u32 sunxi_emac_get_msglevel(struct net_device *dev)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
return dm->msg_enable;
}
static void sunxi_emac_set_msglevel(struct net_device *dev, u32 value)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
dm->msg_enable = value;
}
static int sunxi_emac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
mii_ethtool_gset(&dm->mii, cmd);
return 0;
}
static int sunxi_emac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
return mii_ethtool_sset(&dm->mii, cmd);
}
static int sunxi_emac_nway_reset(struct net_device *dev)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
return mii_nway_restart(&dm->mii);
}
static u32 sunxi_emac_get_link(struct net_device *dev)
{
sunxi_emac_board_info_t *dm = to_sunxi_emac_board(dev);
return mii_link_ok(&dm->mii);
}
static const struct ethtool_ops sunxi_emac_ethtool_ops = {
.get_drvinfo = sunxi_emac_get_drvinfo,
.get_settings = sunxi_emac_get_settings,
.set_settings = sunxi_emac_set_settings,
.get_msglevel = sunxi_emac_get_msglevel,
.set_msglevel = sunxi_emac_set_msglevel,
.nway_reset = sunxi_emac_nway_reset,
.get_link = sunxi_emac_get_link,
};
static void emac_gpio_pin_function(sunxi_emac_board_info_t *db, int cfg0, int pin,
int func, int set)
{
unsigned int orig, val, mask;
orig = val = readl(db->gpio_vbase + cfg0 + (pin / 8) * 4);
mask = 7 << ((pin & 7) * 4);
func = func << ((pin & 7) * 4);
if (set) {
val &= ~mask;
val |= func;
} else {
/* Only clear if the current function is emac */
if ((val & mask) == func)
val &= ~mask; /* Set as input gpio pin */
}
if (val != orig)
writel(val, db->gpio_vbase + cfg0 + (pin / 8) * 4);
}
void emac_sys_setup(sunxi_emac_board_info_t *db)
{
struct clk *tmpClk;
unsigned int reg_val;
int i;
/* map SRAM to EMAC */
reg_val = readl(db->sram_vbase + SRAMC_CFG_REG);
reg_val |= 0x5<<2;
writel(reg_val, db->sram_vbase + SRAMC_CFG_REG);
/* Set function for PA, if emac_used == 1 configure PA for emac, else
make sure it is *not* set for emac */
for (i = 0; i <= 17; i++)
emac_gpio_pin_function(db, PA_CFG0_REG, i, 2, emac_used == 1);
/* On sun5i PD can be used instead, in this case emac_used == 2 */
if (sunxi_is_sun5i()) {
emac_gpio_pin_function(db, PD_CFG0_REG, 6, 3, emac_used == 2);
emac_gpio_pin_function(db, PD_CFG0_REG, 7, 3, emac_used == 2);
for (i = 10; i <= 15; i++)
emac_gpio_pin_function(db, PD_CFG0_REG, i, 3,
emac_used == 2);
for (i = 18; i <= 27; i++)
emac_gpio_pin_function(db, PD_CFG0_REG, i, 3,
emac_used == 2);
}
/* set up clock gating */
tmpClk = clk_get(NULL, "ahb_emac");
clk_enable(tmpClk);
}
unsigned int emac_setup(struct net_device *ndev)
{
unsigned int reg_val;
unsigned int phy_val;
unsigned int duplex_flag;
sunxi_emac_board_info_t *db = netdev_priv(ndev);
sunxi_emac_dbg(db, 3, "EMAC seting ==>\n"
"PHY_AUTO_NEGOTIOATION %x 0: Normal 1: Auto\n"
"PHY_SPEED %x 0: 10M 1: 100M\n"
"SUNXI_EMAC_MAC_FULL %x 0: Half duplex 1: Full duplex\n"
"SUNXI_EMAC_TX_TM %x 0: CPU 1: DMA\n"
"SUNXI_EMAC_TX_AB_M %x 0: Disable 1: Aborted frame enable\n"
"SUNXI_EMAC_RX_TM %x 0: CPU 1: DMA\n"
"SUNXI_EMAC_RX_DRQ_MODE %x 0: DRQ asserted 1: DRQ automatically\n",
PHY_AUTO_NEGOTIOATION,
PHY_SPEED,
SUNXI_EMAC_MAC_FULL,
SUNXI_EMAC_TX_TM,
SUNXI_EMAC_TX_AB_M,
SUNXI_EMAC_RX_TM,
SUNXI_EMAC_RX_DRQ_MODE);
/* set up TX */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_TX_MODE_REG);
if (SUNXI_EMAC_TX_AB_M)
reg_val |= 0x1;
else
reg_val &= (~0x1);
if (SUNXI_EMAC_TX_TM)
reg_val |= (0x1<<1);
else
reg_val &= (~(0x1<<1));
writel(reg_val, db->emac_vbase + SUNXI_EMAC_TX_MODE_REG);
/* set up RX */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_RX_CTL_REG);
if (SUNXI_EMAC_RX_DRQ_MODE)
reg_val |= (0x1<<1);
else
reg_val &= (~(0x1<<1));
if (SUNXI_EMAC_RX_TM)
reg_val |= (0x1<<2);
else
reg_val &= (~(0x1<<2));
if (SUNXI_EMAC_RX_PA)
reg_val |= (0x1<<4);
else
reg_val &= (~(0x1<<4));
if (SUNXI_EMAC_RX_PCF)
reg_val |= (0x1<<5);
else
reg_val &= (~(0x1<<5));
if (SUNXI_EMAC_RX_PCRCE)
reg_val |= (0x1<<6);
else
reg_val &= (~(0x1<<6));
if (SUNXI_EMAC_RX_PLE)
reg_val |= (0x1<<7);
else
reg_val &= (~(0x1<<7));
if (SUNXI_EMAC_RX_POR)
reg_val |= (0x1<<8);
else
reg_val &= (~(0x1<<8));
if (SUNXI_EMAC_RX_UCAD)
reg_val |= (0x1<<16);
else
reg_val &= (~(0x1<<16));
if (SUNXI_EMAC_RX_DAF)
reg_val |= (0x1<<17);
else
reg_val &= (~(0x1<<17));
if (SUNXI_EMAC_RX_MCO)
reg_val |= (0x1<<20);
else
reg_val &= (~(0x1<<20));
if (SUNXI_EMAC_RX_MHF)
reg_val |= (0x1<<21);
else
reg_val &= (~(0x1<<21));
if (SUNXI_EMAC_RX_BCO)
reg_val |= (0x1<<22);
else
reg_val &= (~(0x1<<22));
if (SUNXI_EMAC_RX_SAF)
reg_val |= (0x1<<24);
else
reg_val &= (~(0x1<<24));
if (SUNXI_EMAC_RX_SAIF)
reg_val |= (0x1<<25);
else
reg_val &= (~(0x1<<25));
writel(reg_val, db->emac_vbase + SUNXI_EMAC_RX_CTL_REG);
/* set MAC */
/* set MAC CTL0 */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_MAC_CTL0_REG);
if (SUNXI_EMAC_MAC_TFC)
reg_val |= (0x1<<3);
else
reg_val &= (~(0x1<<3));
if (SUNXI_EMAC_MAC_RFC)
reg_val |= (0x1<<2);
else
reg_val &= (~(0x1<<2));
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_CTL0_REG);
/* set MAC CTL1 */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_MAC_CTL1_REG);
/* phy setup */
if (!PHY_AUTO_NEGOTIOATION) {
phy_val = sunxi_emac_phy_read(ndev, 0, 0);
dev_dbg(db->dev, "PHY reg 0 value: %x\n", phy_val);
phy_val = (PHY_SPEED<<13)|(SUNXI_EMAC_MAC_FULL<<8) ;
dev_dbg(db->dev, "PHY SETUP, write reg 0 with value: %x\n", phy_val);
sunxi_emac_phy_write(ndev, 0, 0, phy_val);
/* soft reset phy */
phy_val = sunxi_emac_phy_read(ndev, 0, 0);
phy_val |= 0x1<<15;
sunxi_emac_phy_write(ndev, 0, 0, phy_val);
phy_val = sunxi_emac_phy_read(ndev, 0, 0);
dev_dbg(db->dev, "PHY reg 0 value: %x\n", phy_val);
mdelay(10);
phy_val = (PHY_SPEED<<13)|(SUNXI_EMAC_MAC_FULL<<8) ;
dev_dbg(db->dev, "PHY SETUP, write reg 0 with value: %x\n", phy_val);
sunxi_emac_phy_write(ndev, 0, 0, phy_val);
}
mdelay(10);
phy_val = sunxi_emac_phy_read(ndev, 0, 0);
dev_dbg(db->dev, "PHY SETUP, reg 0 value: %x\n", phy_val);
duplex_flag = !!(phy_val & (1<<8));
if (PHY_AUTO_NEGOTIOATION) {
if (duplex_flag)
reg_val |= (0x1<<0);
else
reg_val &= (~(0x1<<0));
} else {
if (SUNXI_EMAC_MAC_FULL)
reg_val |= (0x1<<0);
else
reg_val &= (~(0x1<<0));
}
if (SUNXI_EMAC_MAC_FLC)
reg_val |= (0x1<<1);
else
reg_val &= (~(0x1<<1));
if (SUNXI_EMAC_MAC_HF)
reg_val |= (0x1<<2);
else
reg_val &= (~(0x1<<2));
if (SUNXI_EMAC_MAC_DCRC)
reg_val |= (0x1<<3);
else
reg_val &= (~(0x1<<3));
if (SUNXI_EMAC_MAC_CRC)
reg_val |= (0x1<<4);
else
reg_val &= (~(0x1<<4));
if (SUNXI_EMAC_MAC_PC)
reg_val |= (0x1<<5);
else
reg_val &= (~(0x1<<5));
if (SUNXI_EMAC_MAC_VC)
reg_val |= (0x1<<6);
else
reg_val &= (~(0x1<<6));
if (SUNXI_EMAC_MAC_ADP)
reg_val |= (0x1<<7);
else
reg_val &= (~(0x1<<7));
if (SUNXI_EMAC_MAC_PRE)
reg_val |= (0x1<<8);
else
reg_val &= (~(0x1<<8));
if (SUNXI_EMAC_MAC_LPE)
reg_val |= (0x1<<9);
else
reg_val &= (~(0x1<<9));
if (SUNXI_EMAC_MAC_NB)
reg_val |= (0x1<<12);
else
reg_val &= (~(0x1<<12));
if (SUNXI_EMAC_MAC_BNB)
reg_val |= (0x1<<13);
else
reg_val &= (~(0x1<<13));
if (SUNXI_EMAC_MAC_ED)
reg_val |= (0x1<<14);
else
reg_val &= (~(0x1<<14));
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_CTL1_REG);
/* set up IPGT */
reg_val = SUNXI_EMAC_MAC_IPGT;
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_IPGT_REG);
/* set up IPGR */
reg_val = SUNXI_EMAC_MAC_NBTB_IPG2;
reg_val |= (SUNXI_EMAC_MAC_NBTB_IPG1<<8);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_IPGR_REG);
/* set up Collison window */
reg_val = SUNXI_EMAC_MAC_RM;
reg_val |= (SUNXI_EMAC_MAC_CW<<8);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_CLRT_REG);
/* set up Max Frame Length */
reg_val = SUNXI_EMAC_MAC_MFL;
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_MAXF_REG);
return 1;
}
static void sunxi_emac_set_mac_addr(sunxi_emac_board_info_t *db, unsigned char *buf)
{
writel(buf[0] << 16 | buf[1] << 8 | buf[2],
db->emac_vbase + SUNXI_EMAC_MAC_A1_REG);
writel(buf[3] << 16 | buf[4] << 8 | buf[5],
db->emac_vbase + SUNXI_EMAC_MAC_A0_REG);
}
unsigned int sunxi_emac_powerup(struct net_device *ndev)
{
sunxi_emac_board_info_t *db = netdev_priv(ndev);
char emac_mac[13] = {'\0'};
int i, got_mac = 0;
unsigned int reg_val;
/* initial EMAC */
/* flush RX FIFO */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_RX_CTL_REG); /* RX FIFO */
reg_val |= 0x8;
writel(reg_val, db->emac_vbase + SUNXI_EMAC_RX_CTL_REG);
udelay(1);
/* initial MAC */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_MAC_CTL0_REG); /* soft reset MAC */
reg_val &= (~(0x1<<15));
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_CTL0_REG);
reg_val = readl(db->emac_vbase + SUNXI_EMAC_MAC_MCFG_REG); /* set MII clock */
reg_val &= (~(0xf<<2));
reg_val |= (0xD<<2);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_MCFG_REG);
/* clear RX counter */
writel(0x0, db->emac_vbase + SUNXI_EMAC_RX_FBC_REG);
/* disable all interrupt and clear interrupt status */
writel(0, db->emac_vbase + SUNXI_EMAC_INT_CTL_REG);
reg_val = readl(db->emac_vbase + SUNXI_EMAC_INT_STA_REG);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_INT_STA_REG);
udelay(1);
/* set up EMAC */
emac_setup(ndev);
/* set mac_address to chip */
if (strlen(mac_addr_param) == 17) {
int i;
char *p = mac_addr_param;
printk(KERN_INFO "MAC address: %s\n", mac_addr_param);
for (i = 0; i < 6; i++, p++)
mac_addr[i] = simple_strtoul(p, &p, 16);
pr_info("%s Using MAC from kernel cmdline: "
"%02x:%02x:%02x:%02x:%02x:%02x", CARDNAME,
mac_addr[0], mac_addr[1], mac_addr[2],
mac_addr[3], mac_addr[4], mac_addr[5]);
got_mac = 1;
}
i = script_parser_fetch("dynamic", "MAC", (int *)emac_mac, 3);
emac_mac[12] = '\0';
if (!got_mac && i == SCRIPT_PARSER_OK &&
strcmp(emac_mac, "000000000000") != 0) {
for (i = 0; i < 6; i++) {
char emac_tmp[3] = ":::";
memcpy(emac_tmp, (char *)(emac_mac+i*2), 2);
emac_tmp[2] = ':';
mac_addr[i] = simple_strtoul(emac_tmp, NULL, 16);
}
pr_info("%s Using MAC from FEX: %02x:%02x:%02x:%02x:%02x:%02x",
CARDNAME, mac_addr[0], mac_addr[1], mac_addr[2],
mac_addr[3], mac_addr[4], mac_addr[5]);
got_mac = 1;
}
reg_val = readl(SW_VA_SID_IO_BASE);
if (!got_mac && reg_val != 0) {
mac_addr[0] = 0x02; /* Non OUI / registered MAC address */
mac_addr[1] = (reg_val >> 0) & 0xff;
reg_val = readl(SW_VA_SID_IO_BASE + 0x0c);
mac_addr[2] = (reg_val >> 24) & 0xff;
mac_addr[3] = (reg_val >> 16) & 0xff;
mac_addr[4] = (reg_val >> 8) & 0xff;
mac_addr[5] = (reg_val >> 0) & 0xff;
pr_info("%s Using MAC from SID: 02:%02x:%02x:%02x:%02x:%02x",
CARDNAME, mac_addr[1], mac_addr[2], mac_addr[3],
mac_addr[4], mac_addr[5]);
got_mac = 1;
}
sunxi_emac_set_mac_addr(db, mac_addr);
mdelay(1);
return 1;
}
static void
sunxi_emac_poll_work(struct work_struct *w)
{
struct delayed_work *dw = container_of(w, struct delayed_work, work);
sunxi_emac_board_info_t *db = container_of(dw, sunxi_emac_board_info_t, phy_poll);
struct net_device *ndev = db->ndev;
mii_check_media(&db->mii, netif_msg_link(db), 0);
if (netif_running(ndev))
sunxi_emac_schedule_poll(db);
}
/* sunxi_emac_release_board
*
* release a board, and any mapped resources
*/
static void
sunxi_emac_release_board(struct platform_device *pdev, struct sunxi_emac_board_info *db)
{
/* unmap our resources */
iounmap(db->emac_vbase);
iounmap(db->sram_vbase);
iounmap(db->gpio_vbase);
/* release the resources */
release_resource(db->emac_base_req);
kfree(db->emac_base_req);
release_resource(db->sram_base_req);
kfree(db->sram_base_req);
release_resource(db->gpio_base_req);
kfree(db->gpio_base_req);
}
/*
* Set SUNXI_EMAC multicast address
*/
static void
sunxi_emac_hash_table(struct net_device *dev)
{
#if 0
sunxi_emac_board_info_t *db = netdev_priv(dev);
struct dev_mc_list *mcptr = dev->mc_list;
int mc_cnt = dev->mc_count;
int i, oft;
u32 hash_val;
u16 hash_table[4];
u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
unsigned long flags;
sunxi_emac_dbg(db, 1, "entering %s\n", __func__);
spin_lock_irqsave(&db->lock, flags);
for (i = 0, oft = SUNXI_EMAC_PAR; i < 6; i++, oft++)
iow(db, oft, dev->dev_addr[i]);
/* Clear Hash Table */
for (i = 0; i < 4; i++)
hash_table[i] = 0x0;
/* broadcast address */
hash_table[3] = 0x8000;
if (dev->flags & IFF_PROMISC)
rcr |= RCR_PRMSC;
if (dev->flags & IFF_ALLMULTI)
rcr |= RCR_ALL;
/* the multicast address in Hash Table : 64 bits */
for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
}
/* Write the hash table to MAC MD table */
for (i = 0, oft = SUNXI_EMAC_MAR; i < 4; i++) {
iow(db, oft++, hash_table[i]);
iow(db, oft++, hash_table[i] >> 8);
}
iow(db, SUNXI_EMAC_RCR, rcr);
spin_unlock_irqrestore(&db->lock, flags);
#endif
}
static void read_random_macaddr(unsigned char *mac, struct net_device *ndev)
{
unsigned char *buf = mac;
sunxi_emac_board_info_t *db = netdev_priv(ndev);
get_random_bytes(buf, 6);
buf[0] &= 0xfe; /* the 48bit must set 0 */
buf[0] |= 0x02; /* the 47bit must set 1 */
/* we write the random number into chip */
sunxi_emac_set_mac_addr(db, buf);
}
static int sunxi_emac_set_mac_address(struct net_device *dev, void *p)
{
struct sockaddr *addr = p;
sunxi_emac_board_info_t *db = netdev_priv(dev);
if (netif_running(dev))
return -EBUSY;
if (!is_valid_ether_addr(addr->sa_data)) {
dev_err(&dev->dev, "not setting invalid mac address %pM\n",
addr->sa_data);
return -EADDRNOTAVAIL;
}
memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
sunxi_emac_set_mac_addr(db, dev->dev_addr);
return 0;
}
/*
* ****************************************************************************
* void phy_link_check()
* Description:
*
*
* Return Value: 1: Link valid 0: Link not valid
* ****************************************************************************
*/
unsigned int phy_link_check(struct net_device *dev)
{
unsigned int reg_val;
reg_val = sunxi_emac_phy_read(dev, 0, 1);
if (reg_val & 0x4) {
/* PHY Linked */
return 1;
} else {
/* PHY Link waiting */
return 0;
}
}
/*
* Initilize sunxi_emac board
*/
static void
sunxi_emac_init_sunxi_emac(struct net_device *dev)
{
int link_wait_limit_ms = 4500; /* wait up to 4.5 seconds for a link */
sunxi_emac_board_info_t *db = netdev_priv(dev);
unsigned int phy_reg;
unsigned int reg_val;
if (db->mos_pin_handler) {
db->mos_gpio->data = 1;
gpio_set_one_pin_status(db->mos_pin_handler, db->mos_gpio, "emac_power", 1);
/* Give the phy some time to "boot", this is necessary to avoid
ending up with 10 Mbit half-duplex on 100 Mbit networks */
msleep(20);
}
/* PHY POWER UP */
phy_reg = sunxi_emac_phy_read(dev, 0, 0);
sunxi_emac_phy_write(dev, 0, 0, phy_reg & (~(1<<11)));
mdelay(1);
while (!phy_link_check(dev) && link_wait_limit_ms > 0) {
msleep(500);
link_wait_limit_ms -= 500;
}
phy_reg = sunxi_emac_phy_read(dev, 0, 0);
/* set EMAC SPEED, depend on PHY */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_MAC_SUPP_REG);
reg_val &= (~(0x1<<8));
/*reg_val |= ((phy_reg & (1<<13)) << 8);*/
reg_val |= (((phy_reg & (1<<13))>>13) << 8);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_SUPP_REG);
/* set duplex depend on phy*/
reg_val = readl(db->emac_vbase + SUNXI_EMAC_MAC_CTL1_REG);
reg_val &= (~(0x1<<0));
/*reg_val |= ((phy_reg & (1<<8)) << 0);*/
reg_val |= (((phy_reg & (1<<8))>>8) << 0);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_MAC_CTL1_REG);
/* Set address filter table */
sunxi_emac_hash_table(dev);
/* enable RX/TX */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_CTL_REG);
reg_val |= 0x7;
writel(reg_val, db->emac_vbase + SUNXI_EMAC_CTL_REG);
/* enable RX/TX0/RX Hlevel interrup */
reg_val = readl(db->emac_vbase + SUNXI_EMAC_INT_CTL_REG);
/*reg_val |= (0x1<<0) | (0x01<<8)| (0x1<<17);*/
reg_val |= (0xf<<0) | (0x01<<8);
writel(reg_val, db->emac_vbase + SUNXI_EMAC_INT_CTL_REG);
/* Init Driver variable */
db->tx_fifo_stat = 0;
dev->trans_start = 0;
}
/* Our watchdog timed out. Called by the networking layer */
static void sunxi_emac_timeout(struct net_device *dev)
{
sunxi_emac_board_info_t *db = netdev_priv(dev);
/* init_sunxi_emac uses phy_r/w which can sleep, so use a work_queue */
if (!test_and_set_bit(SUNXI_EMAC_TX_TIMEOUT_PENDING, &db->bit_flags)) {
netif_stop_queue(dev);
schedule_work(&db->tx_timeout_work);
}
}
/* The real tx timeout handle work is done here, where we can sleep */
static void sunxi_emac_timeout_work(struct work_struct *work)
{
sunxi_emac_board_info_t *db =
container_of(work, sunxi_emac_board_info_t, tx_timeout_work);
if (netif_msg_timer(db))
dev_err(db->dev, "tx time out, resetting emac\n");
sunxi_emac_reset(db);
sunxi_emac_init_sunxi_emac(db->ndev);
/* We can accept TX packets again */
db->ndev->trans_start = jiffies;
netif_wake_queue(db->ndev);
clear_bit(SUNXI_EMAC_TX_TIMEOUT_PENDING, &db->bit_flags);
}
#define PINGPANG_BUF 1
/*
* Hardware start transmission.
* Send a packet to media from the upper layer.
*/
static int
sunxi_emac_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
unsigned long channal;
unsigned long flags;
sunxi_emac_board_info_t *db = netdev_priv(dev);
#if PINGPANG_BUF
if ((channal = (db->tx_fifo_stat & 3)) == 3)
return 1;
channal = (channal == 1 ? 1 : 0);
#else