summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/socionext/sni_ave.c
blob: f7e927ad67fac36ec5c02eaeeb0972a39b351a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
// SPDX-License-Identifier: GPL-2.0
/**
 * sni_ave.c - Socionext UniPhier AVE ethernet driver
 * Copyright 2014 Panasonic Corporation
 * Copyright 2015-2017 Socionext Inc.
 */

#include <linux/bitops.h>
#include <linux/clk.h>
#include <linux/etherdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/mfd/syscon.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/of_net.h>
#include <linux/of_mdio.h>
#include <linux/of_platform.h>
#include <linux/phy.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <linux/types.h>
#include <linux/u64_stats_sync.h>

/* General Register Group */
#define AVE_IDR			0x000	/* ID */
#define AVE_VR			0x004	/* Version */
#define AVE_GRR			0x008	/* Global Reset */
#define AVE_CFGR		0x00c	/* Configuration */

/* Interrupt Register Group */
#define AVE_GIMR		0x100	/* Global Interrupt Mask */
#define AVE_GISR		0x104	/* Global Interrupt Status */

/* MAC Register Group */
#define AVE_TXCR		0x200	/* TX Setup */
#define AVE_RXCR		0x204	/* RX Setup */
#define AVE_RXMAC1R		0x208	/* MAC address (lower) */
#define AVE_RXMAC2R		0x20c	/* MAC address (upper) */
#define AVE_MDIOCTR		0x214	/* MDIO Control */
#define AVE_MDIOAR		0x218	/* MDIO Address */
#define AVE_MDIOWDR		0x21c	/* MDIO Data */
#define AVE_MDIOSR		0x220	/* MDIO Status */
#define AVE_MDIORDR		0x224	/* MDIO Rd Data */

/* Descriptor Control Register Group */
#define AVE_DESCC		0x300	/* Descriptor Control */
#define AVE_TXDC		0x304	/* TX Descriptor Configuration */
#define AVE_RXDC0		0x308	/* RX Descriptor Ring0 Configuration */
#define AVE_IIRQC		0x34c	/* Interval IRQ Control */

/* Packet Filter Register Group */
#define AVE_PKTF_BASE		0x800	/* PF Base Address */
#define AVE_PFMBYTE_BASE	0xd00	/* PF Mask Byte Base Address */
#define AVE_PFMBIT_BASE		0xe00	/* PF Mask Bit Base Address */
#define AVE_PFSEL_BASE		0xf00	/* PF Selector Base Address */
#define AVE_PFEN		0xffc	/* Packet Filter Enable */
#define AVE_PKTF(ent)		(AVE_PKTF_BASE + (ent) * 0x40)
#define AVE_PFMBYTE(ent)	(AVE_PFMBYTE_BASE + (ent) * 8)
#define AVE_PFMBIT(ent)		(AVE_PFMBIT_BASE + (ent) * 4)
#define AVE_PFSEL(ent)		(AVE_PFSEL_BASE + (ent) * 4)

/* 64bit descriptor memory */
#define AVE_DESC_SIZE_64	12	/* Descriptor Size */

#define AVE_TXDM_64		0x1000	/* Tx Descriptor Memory */
#define AVE_RXDM_64		0x1c00	/* Rx Descriptor Memory */

#define AVE_TXDM_SIZE_64	0x0ba0	/* Tx Descriptor Memory Size 3KB */
#define AVE_RXDM_SIZE_64	0x6000	/* Rx Descriptor Memory Size 24KB */

/* 32bit descriptor memory */
#define AVE_DESC_SIZE_32	8	/* Descriptor Size */

#define AVE_TXDM_32		0x1000	/* Tx Descriptor Memory */
#define AVE_RXDM_32		0x1800	/* Rx Descriptor Memory */

#define AVE_TXDM_SIZE_32	0x07c0	/* Tx Descriptor Memory Size 2KB */
#define AVE_RXDM_SIZE_32	0x4000	/* Rx Descriptor Memory Size 16KB */

/* RMII Bridge Register Group */
#define AVE_RSTCTRL		0x8028	/* Reset control */
#define AVE_RSTCTRL_RMIIRST	BIT(16)
#define AVE_LINKSEL		0x8034	/* Link speed setting */
#define AVE_LINKSEL_100M	BIT(0)

/* AVE_GRR */
#define AVE_GRR_RXFFR		BIT(5)	/* Reset RxFIFO */
#define AVE_GRR_PHYRST		BIT(4)	/* Reset external PHY */
#define AVE_GRR_GRST		BIT(0)	/* Reset all MAC */

/* AVE_CFGR */
#define AVE_CFGR_FLE		BIT(31)	/* Filter Function */
#define AVE_CFGR_CHE		BIT(30)	/* Checksum Function */
#define AVE_CFGR_MII		BIT(27)	/* Func mode (1:MII/RMII, 0:RGMII) */
#define AVE_CFGR_IPFCEN		BIT(24)	/* IP fragment sum Enable */

/* AVE_GISR (common with GIMR) */
#define AVE_GI_PHY		BIT(24)	/* PHY interrupt */
#define AVE_GI_TX		BIT(16)	/* Tx complete */
#define AVE_GI_RXERR		BIT(8)	/* Receive frame more than max size */
#define AVE_GI_RXOVF		BIT(7)	/* Overflow at the RxFIFO */
#define AVE_GI_RXDROP		BIT(6)	/* Drop packet */
#define AVE_GI_RXIINT		BIT(5)	/* Interval interrupt */

/* AVE_TXCR */
#define AVE_TXCR_FLOCTR		BIT(18)	/* Flow control */
#define AVE_TXCR_TXSPD_1G	BIT(17)
#define AVE_TXCR_TXSPD_100	BIT(16)

/* AVE_RXCR */
#define AVE_RXCR_RXEN		BIT(30)	/* Rx enable */
#define AVE_RXCR_FDUPEN		BIT(22)	/* Interface mode */
#define AVE_RXCR_FLOCTR		BIT(21)	/* Flow control */
#define AVE_RXCR_AFEN		BIT(19)	/* MAC address filter */
#define AVE_RXCR_DRPEN		BIT(18)	/* Drop pause frame */
#define AVE_RXCR_MPSIZ_MASK	GENMASK(10, 0)

/* AVE_MDIOCTR */
#define AVE_MDIOCTR_RREQ	BIT(3)	/* Read request */
#define AVE_MDIOCTR_WREQ	BIT(2)	/* Write request */

/* AVE_MDIOSR */
#define AVE_MDIOSR_STS		BIT(0)	/* access status */

/* AVE_DESCC */
#define AVE_DESCC_STATUS_MASK	GENMASK(31, 16)
#define AVE_DESCC_RD0		BIT(8)	/* Enable Rx descriptor Ring0 */
#define AVE_DESCC_RDSTP		BIT(4)	/* Pause Rx descriptor */
#define AVE_DESCC_TD		BIT(0)	/* Enable Tx descriptor */

/* AVE_TXDC */
#define AVE_TXDC_SIZE		GENMASK(27, 16)	/* Size of Tx descriptor */
#define AVE_TXDC_ADDR		GENMASK(11, 0)	/* Start address */
#define AVE_TXDC_ADDR_START	0

/* AVE_RXDC0 */
#define AVE_RXDC0_SIZE		GENMASK(30, 16)	/* Size of Rx descriptor */
#define AVE_RXDC0_ADDR		GENMASK(14, 0)	/* Start address */
#define AVE_RXDC0_ADDR_START	0

/* AVE_IIRQC */
#define AVE_IIRQC_EN0		BIT(27)	/* Enable interval interrupt Ring0 */
#define AVE_IIRQC_BSCK		GENMASK(15, 0)	/* Interval count unit */

/* Command status for descriptor */
#define AVE_STS_OWN		BIT(31)	/* Descriptor ownership */
#define AVE_STS_INTR		BIT(29)	/* Request for interrupt */
#define AVE_STS_OK		BIT(27)	/* Normal transmit */
/* TX */
#define AVE_STS_NOCSUM		BIT(28)	/* No use HW checksum */
#define AVE_STS_1ST		BIT(26)	/* Head of buffer chain */
#define AVE_STS_LAST		BIT(25)	/* Tail of buffer chain */
#define AVE_STS_OWC		BIT(21)	/* Out of window,Late Collision */
#define AVE_STS_EC		BIT(20)	/* Excess collision occurred */
#define AVE_STS_PKTLEN_TX_MASK	GENMASK(15, 0)
/* RX */
#define AVE_STS_CSSV		BIT(21)	/* Checksum check performed */
#define AVE_STS_CSER		BIT(20)	/* Checksum error detected */
#define AVE_STS_PKTLEN_RX_MASK	GENMASK(10, 0)

/* Packet filter */
#define AVE_PFMBYTE_MASK0	(GENMASK(31, 8) | GENMASK(5, 0))
#define AVE_PFMBYTE_MASK1	GENMASK(25, 0)
#define AVE_PFMBIT_MASK		GENMASK(15, 0)

#define AVE_PF_SIZE		17	/* Number of all packet filter */
#define AVE_PF_MULTICAST_SIZE	7	/* Number of multicast filter */

#define AVE_PFNUM_FILTER	0	/* No.0 */
#define AVE_PFNUM_UNICAST	1	/* No.1 */
#define AVE_PFNUM_BROADCAST	2	/* No.2 */
#define AVE_PFNUM_MULTICAST	11	/* No.11-17 */

/* NETIF Message control */
#define AVE_DEFAULT_MSG_ENABLE	(NETIF_MSG_DRV    |	\
				 NETIF_MSG_PROBE  |	\
				 NETIF_MSG_LINK   |	\
				 NETIF_MSG_TIMER  |	\
				 NETIF_MSG_IFDOWN |	\
				 NETIF_MSG_IFUP   |	\
				 NETIF_MSG_RX_ERR |	\
				 NETIF_MSG_TX_ERR)

/* Parameter for descriptor */
#define AVE_NR_TXDESC		64	/* Tx descriptor */
#define AVE_NR_RXDESC		256	/* Rx descriptor */

#define AVE_DESC_OFS_CMDSTS	0
#define AVE_DESC_OFS_ADDRL	4
#define AVE_DESC_OFS_ADDRU	8

/* Parameter for ethernet frame */
#define AVE_MAX_ETHFRAME	1518
#define AVE_FRAME_HEADROOM	2

/* Parameter for interrupt */
#define AVE_INTM_COUNT		20
#define AVE_FORCE_TXINTCNT	1

/* SG */
#define SG_ETPINMODE		0x540
#define SG_ETPINMODE_EXTPHY	BIT(1)	/* for LD11 */
#define SG_ETPINMODE_RMII(ins)	BIT(ins)

#define IS_DESC_64BIT(p)	((p)->data->is_desc_64bit)

#define AVE_MAX_CLKS		4
#define AVE_MAX_RSTS		2

enum desc_id {
	AVE_DESCID_RX,
	AVE_DESCID_TX,
};

enum desc_state {
	AVE_DESC_RX_PERMIT,
	AVE_DESC_RX_SUSPEND,
	AVE_DESC_START,
	AVE_DESC_STOP,
};

struct ave_desc {
	struct sk_buff	*skbs;
	dma_addr_t	skbs_dma;
	size_t		skbs_dmalen;
};

struct ave_desc_info {
	u32	ndesc;		/* number of descriptor */
	u32	daddr;		/* start address of descriptor */
	u32	proc_idx;	/* index of processing packet */
	u32	done_idx;	/* index of processed packet */
	struct ave_desc *desc;	/* skb info related descriptor */
};

struct ave_stats {
	struct	u64_stats_sync	syncp;
	u64	packets;
	u64	bytes;
	u64	errors;
	u64	dropped;
	u64	collisions;
	u64	fifo_errors;
};

struct ave_private {
	void __iomem            *base;
	int                     irq;
	int			phy_id;
	unsigned int		desc_size;
	u32			msg_enable;
	int			nclks;
	struct clk		*clk[AVE_MAX_CLKS];
	int			nrsts;
	struct reset_control	*rst[AVE_MAX_RSTS];
	phy_interface_t		phy_mode;
	struct phy_device	*phydev;
	struct mii_bus		*mdio;
	struct regmap		*regmap;
	unsigned int		pinmode_mask;
	unsigned int		pinmode_val;
	u32			wolopts;

	/* stats */
	struct ave_stats	stats_rx;
	struct ave_stats	stats_tx;

	/* NAPI support */
	struct net_device	*ndev;
	struct napi_struct	napi_rx;
	struct napi_struct	napi_tx;

	/* descriptor */
	struct ave_desc_info	rx;
	struct ave_desc_info	tx;

	/* flow control */
	int pause_auto;
	int pause_rx;
	int pause_tx;

	const struct ave_soc_data *data;
};

struct ave_soc_data {
	bool	is_desc_64bit;
	const char	*clock_names[AVE_MAX_CLKS];
	const char	*reset_names[AVE_MAX_RSTS];
	int	(*get_pinmode)(struct ave_private *priv,
			       phy_interface_t phy_mode, u32 arg);
};

static u32 ave_desc_read(struct net_device *ndev, enum desc_id id, int entry,
			 int offset)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 addr;

	addr = ((id == AVE_DESCID_TX) ? priv->tx.daddr : priv->rx.daddr)
		+ entry * priv->desc_size + offset;

	return readl(priv->base + addr);
}

static u32 ave_desc_read_cmdsts(struct net_device *ndev, enum desc_id id,
				int entry)
{
	return ave_desc_read(ndev, id, entry, AVE_DESC_OFS_CMDSTS);
}

static void ave_desc_write(struct net_device *ndev, enum desc_id id,
			   int entry, int offset, u32 val)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 addr;

	addr = ((id == AVE_DESCID_TX) ? priv->tx.daddr : priv->rx.daddr)
		+ entry * priv->desc_size + offset;

	writel(val, priv->base + addr);
}

static void ave_desc_write_cmdsts(struct net_device *ndev, enum desc_id id,
				  int entry, u32 val)
{
	ave_desc_write(ndev, id, entry, AVE_DESC_OFS_CMDSTS, val);
}

static void ave_desc_write_addr(struct net_device *ndev, enum desc_id id,
				int entry, dma_addr_t paddr)
{
	struct ave_private *priv = netdev_priv(ndev);

	ave_desc_write(ndev, id, entry, AVE_DESC_OFS_ADDRL,
		       lower_32_bits(paddr));
	if (IS_DESC_64BIT(priv))
		ave_desc_write(ndev, id,
			       entry, AVE_DESC_OFS_ADDRU,
			       upper_32_bits(paddr));
}

static u32 ave_irq_disable_all(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 ret;

	ret = readl(priv->base + AVE_GIMR);
	writel(0, priv->base + AVE_GIMR);

	return ret;
}

static void ave_irq_restore(struct net_device *ndev, u32 val)
{
	struct ave_private *priv = netdev_priv(ndev);

	writel(val, priv->base + AVE_GIMR);
}

static void ave_irq_enable(struct net_device *ndev, u32 bitflag)
{
	struct ave_private *priv = netdev_priv(ndev);

	writel(readl(priv->base + AVE_GIMR) | bitflag, priv->base + AVE_GIMR);
	writel(bitflag, priv->base + AVE_GISR);
}

static void ave_hw_write_macaddr(struct net_device *ndev,
				 const unsigned char *mac_addr,
				 int reg1, int reg2)
{
	struct ave_private *priv = netdev_priv(ndev);

	writel(mac_addr[0] | mac_addr[1] << 8 |
	       mac_addr[2] << 16 | mac_addr[3] << 24, priv->base + reg1);
	writel(mac_addr[4] | mac_addr[5] << 8, priv->base + reg2);
}

static void ave_hw_read_version(struct net_device *ndev, char *buf, int len)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 major, minor, vr;

	vr = readl(priv->base + AVE_VR);
	major = (vr & GENMASK(15, 8)) >> 8;
	minor = (vr & GENMASK(7, 0));
	snprintf(buf, len, "v%u.%u", major, minor);
}

static void ave_ethtool_get_drvinfo(struct net_device *ndev,
				    struct ethtool_drvinfo *info)
{
	struct device *dev = ndev->dev.parent;

	strlcpy(info->driver, dev->driver->name, sizeof(info->driver));
	strlcpy(info->bus_info, dev_name(dev), sizeof(info->bus_info));
	ave_hw_read_version(ndev, info->fw_version, sizeof(info->fw_version));
}

static u32 ave_ethtool_get_msglevel(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);

	return priv->msg_enable;
}

static void ave_ethtool_set_msglevel(struct net_device *ndev, u32 val)
{
	struct ave_private *priv = netdev_priv(ndev);

	priv->msg_enable = val;
}

static void ave_ethtool_get_wol(struct net_device *ndev,
				struct ethtool_wolinfo *wol)
{
	wol->supported = 0;
	wol->wolopts   = 0;

	if (ndev->phydev)
		phy_ethtool_get_wol(ndev->phydev, wol);
}

static int ave_ethtool_set_wol(struct net_device *ndev,
			       struct ethtool_wolinfo *wol)
{
	int ret;

	if (!ndev->phydev ||
	    (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE)))
		return -EOPNOTSUPP;

	ret = phy_ethtool_set_wol(ndev->phydev, wol);
	if (!ret)
		device_set_wakeup_enable(&ndev->dev, !!wol->wolopts);

	return ret;
}

static void ave_ethtool_get_pauseparam(struct net_device *ndev,
				       struct ethtool_pauseparam *pause)
{
	struct ave_private *priv = netdev_priv(ndev);

	pause->autoneg  = priv->pause_auto;
	pause->rx_pause = priv->pause_rx;
	pause->tx_pause = priv->pause_tx;
}

static int ave_ethtool_set_pauseparam(struct net_device *ndev,
				      struct ethtool_pauseparam *pause)
{
	struct ave_private *priv = netdev_priv(ndev);
	struct phy_device *phydev = ndev->phydev;

	if (!phydev)
		return -EINVAL;

	priv->pause_auto = pause->autoneg;
	priv->pause_rx   = pause->rx_pause;
	priv->pause_tx   = pause->tx_pause;

	phy_set_asym_pause(phydev, pause->rx_pause, pause->tx_pause);

	return 0;
}

static const struct ethtool_ops ave_ethtool_ops = {
	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
	.get_drvinfo		= ave_ethtool_get_drvinfo,
	.nway_reset		= phy_ethtool_nway_reset,
	.get_link		= ethtool_op_get_link,
	.get_msglevel		= ave_ethtool_get_msglevel,
	.set_msglevel		= ave_ethtool_set_msglevel,
	.get_wol		= ave_ethtool_get_wol,
	.set_wol		= ave_ethtool_set_wol,
	.get_pauseparam         = ave_ethtool_get_pauseparam,
	.set_pauseparam         = ave_ethtool_set_pauseparam,
};

static int ave_mdiobus_read(struct mii_bus *bus, int phyid, int regnum)
{
	struct net_device *ndev = bus->priv;
	struct ave_private *priv;
	u32 mdioctl, mdiosr;
	int ret;

	priv = netdev_priv(ndev);

	/* write address */
	writel((phyid << 8) | regnum, priv->base + AVE_MDIOAR);

	/* read request */
	mdioctl = readl(priv->base + AVE_MDIOCTR);
	writel((mdioctl | AVE_MDIOCTR_RREQ) & ~AVE_MDIOCTR_WREQ,
	       priv->base + AVE_MDIOCTR);

	ret = readl_poll_timeout(priv->base + AVE_MDIOSR, mdiosr,
				 !(mdiosr & AVE_MDIOSR_STS), 20, 2000);
	if (ret) {
		netdev_err(ndev, "failed to read (phy:%d reg:%x)\n",
			   phyid, regnum);
		return ret;
	}

	return readl(priv->base + AVE_MDIORDR) & GENMASK(15, 0);
}

static int ave_mdiobus_write(struct mii_bus *bus, int phyid, int regnum,
			     u16 val)
{
	struct net_device *ndev = bus->priv;
	struct ave_private *priv;
	u32 mdioctl, mdiosr;
	int ret;

	priv = netdev_priv(ndev);

	/* write address */
	writel((phyid << 8) | regnum, priv->base + AVE_MDIOAR);

	/* write data */
	writel(val, priv->base + AVE_MDIOWDR);

	/* write request */
	mdioctl = readl(priv->base + AVE_MDIOCTR);
	writel((mdioctl | AVE_MDIOCTR_WREQ) & ~AVE_MDIOCTR_RREQ,
	       priv->base + AVE_MDIOCTR);

	ret = readl_poll_timeout(priv->base + AVE_MDIOSR, mdiosr,
				 !(mdiosr & AVE_MDIOSR_STS), 20, 2000);
	if (ret)
		netdev_err(ndev, "failed to write (phy:%d reg:%x)\n",
			   phyid, regnum);

	return ret;
}

static int ave_dma_map(struct net_device *ndev, struct ave_desc *desc,
		       void *ptr, size_t len, enum dma_data_direction dir,
		       dma_addr_t *paddr)
{
	dma_addr_t map_addr;

	map_addr = dma_map_single(ndev->dev.parent, ptr, len, dir);
	if (unlikely(dma_mapping_error(ndev->dev.parent, map_addr)))
		return -ENOMEM;

	desc->skbs_dma = map_addr;
	desc->skbs_dmalen = len;
	*paddr = map_addr;

	return 0;
}

static void ave_dma_unmap(struct net_device *ndev, struct ave_desc *desc,
			  enum dma_data_direction dir)
{
	if (!desc->skbs_dma)
		return;

	dma_unmap_single(ndev->dev.parent,
			 desc->skbs_dma, desc->skbs_dmalen, dir);
	desc->skbs_dma = 0;
}

/* Prepare Rx descriptor and memory */
static int ave_rxdesc_prepare(struct net_device *ndev, int entry)
{
	struct ave_private *priv = netdev_priv(ndev);
	struct sk_buff *skb;
	dma_addr_t paddr;
	int ret;

	skb = priv->rx.desc[entry].skbs;
	if (!skb) {
		skb = netdev_alloc_skb(ndev, AVE_MAX_ETHFRAME);
		if (!skb) {
			netdev_err(ndev, "can't allocate skb for Rx\n");
			return -ENOMEM;
		}
		skb->data += AVE_FRAME_HEADROOM;
		skb->tail += AVE_FRAME_HEADROOM;
	}

	/* set disable to cmdsts */
	ave_desc_write_cmdsts(ndev, AVE_DESCID_RX, entry,
			      AVE_STS_INTR | AVE_STS_OWN);

	/* map Rx buffer
	 * Rx buffer set to the Rx descriptor has two restrictions:
	 * - Rx buffer address is 4 byte aligned.
	 * - Rx buffer begins with 2 byte headroom, and data will be put from
	 *   (buffer + 2).
	 * To satisfy this, specify the address to put back the buffer
	 * pointer advanced by AVE_FRAME_HEADROOM, and expand the map size
	 * by AVE_FRAME_HEADROOM.
	 */
	ret = ave_dma_map(ndev, &priv->rx.desc[entry],
			  skb->data - AVE_FRAME_HEADROOM,
			  AVE_MAX_ETHFRAME + AVE_FRAME_HEADROOM,
			  DMA_FROM_DEVICE, &paddr);
	if (ret) {
		netdev_err(ndev, "can't map skb for Rx\n");
		dev_kfree_skb_any(skb);
		return ret;
	}
	priv->rx.desc[entry].skbs = skb;

	/* set buffer pointer */
	ave_desc_write_addr(ndev, AVE_DESCID_RX, entry, paddr);

	/* set enable to cmdsts */
	ave_desc_write_cmdsts(ndev, AVE_DESCID_RX, entry,
			      AVE_STS_INTR | AVE_MAX_ETHFRAME);

	return ret;
}

/* Switch state of descriptor */
static int ave_desc_switch(struct net_device *ndev, enum desc_state state)
{
	struct ave_private *priv = netdev_priv(ndev);
	int ret = 0;
	u32 val;

	switch (state) {
	case AVE_DESC_START:
		writel(AVE_DESCC_TD | AVE_DESCC_RD0, priv->base + AVE_DESCC);
		break;

	case AVE_DESC_STOP:
		writel(0, priv->base + AVE_DESCC);
		if (readl_poll_timeout(priv->base + AVE_DESCC, val, !val,
				       150, 15000)) {
			netdev_err(ndev, "can't stop descriptor\n");
			ret = -EBUSY;
		}
		break;

	case AVE_DESC_RX_SUSPEND:
		val = readl(priv->base + AVE_DESCC);
		val |= AVE_DESCC_RDSTP;
		val &= ~AVE_DESCC_STATUS_MASK;
		writel(val, priv->base + AVE_DESCC);
		if (readl_poll_timeout(priv->base + AVE_DESCC, val,
				       val & (AVE_DESCC_RDSTP << 16),
				       150, 150000)) {
			netdev_err(ndev, "can't suspend descriptor\n");
			ret = -EBUSY;
		}
		break;

	case AVE_DESC_RX_PERMIT:
		val = readl(priv->base + AVE_DESCC);
		val &= ~AVE_DESCC_RDSTP;
		val &= ~AVE_DESCC_STATUS_MASK;
		writel(val, priv->base + AVE_DESCC);
		break;

	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static int ave_tx_complete(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 proc_idx, done_idx, ndesc, cmdsts;
	unsigned int nr_freebuf = 0;
	unsigned int tx_packets = 0;
	unsigned int tx_bytes = 0;

	proc_idx = priv->tx.proc_idx;
	done_idx = priv->tx.done_idx;
	ndesc    = priv->tx.ndesc;

	/* free pre-stored skb from done_idx to proc_idx */
	while (proc_idx != done_idx) {
		cmdsts = ave_desc_read_cmdsts(ndev, AVE_DESCID_TX, done_idx);

		/* do nothing if owner is HW (==1 for Tx) */
		if (cmdsts & AVE_STS_OWN)
			break;

		/* check Tx status and updates statistics */
		if (cmdsts & AVE_STS_OK) {
			tx_bytes += cmdsts & AVE_STS_PKTLEN_TX_MASK;
			/* success */
			if (cmdsts & AVE_STS_LAST)
				tx_packets++;
		} else {
			/* error */
			if (cmdsts & AVE_STS_LAST) {
				priv->stats_tx.errors++;
				if (cmdsts & (AVE_STS_OWC | AVE_STS_EC))
					priv->stats_tx.collisions++;
			}
		}

		/* release skb */
		if (priv->tx.desc[done_idx].skbs) {
			ave_dma_unmap(ndev, &priv->tx.desc[done_idx],
				      DMA_TO_DEVICE);
			dev_consume_skb_any(priv->tx.desc[done_idx].skbs);
			priv->tx.desc[done_idx].skbs = NULL;
			nr_freebuf++;
		}
		done_idx = (done_idx + 1) % ndesc;
	}

	priv->tx.done_idx = done_idx;

	/* update stats */
	u64_stats_update_begin(&priv->stats_tx.syncp);
	priv->stats_tx.packets += tx_packets;
	priv->stats_tx.bytes   += tx_bytes;
	u64_stats_update_end(&priv->stats_tx.syncp);

	/* wake queue for freeing buffer */
	if (unlikely(netif_queue_stopped(ndev)) && nr_freebuf)
		netif_wake_queue(ndev);

	return nr_freebuf;
}

static int ave_rx_receive(struct net_device *ndev, int num)
{
	struct ave_private *priv = netdev_priv(ndev);
	unsigned int rx_packets = 0;
	unsigned int rx_bytes = 0;
	u32 proc_idx, done_idx;
	struct sk_buff *skb;
	unsigned int pktlen;
	int restpkt, npkts;
	u32 ndesc, cmdsts;

	proc_idx = priv->rx.proc_idx;
	done_idx = priv->rx.done_idx;
	ndesc    = priv->rx.ndesc;
	restpkt  = ((proc_idx + ndesc - 1) - done_idx) % ndesc;

	for (npkts = 0; npkts < num; npkts++) {
		/* we can't receive more packet, so fill desc quickly */
		if (--restpkt < 0)
			break;

		cmdsts = ave_desc_read_cmdsts(ndev, AVE_DESCID_RX, proc_idx);

		/* do nothing if owner is HW (==0 for Rx) */
		if (!(cmdsts & AVE_STS_OWN))
			break;

		if (!(cmdsts & AVE_STS_OK)) {
			priv->stats_rx.errors++;
			proc_idx = (proc_idx + 1) % ndesc;
			continue;
		}

		pktlen = cmdsts & AVE_STS_PKTLEN_RX_MASK;

		/* get skbuff for rx */
		skb = priv->rx.desc[proc_idx].skbs;
		priv->rx.desc[proc_idx].skbs = NULL;

		ave_dma_unmap(ndev, &priv->rx.desc[proc_idx], DMA_FROM_DEVICE);

		skb->dev = ndev;
		skb_put(skb, pktlen);
		skb->protocol = eth_type_trans(skb, ndev);

		if ((cmdsts & AVE_STS_CSSV) && (!(cmdsts & AVE_STS_CSER)))
			skb->ip_summed = CHECKSUM_UNNECESSARY;

		rx_packets++;
		rx_bytes += pktlen;

		netif_receive_skb(skb);

		proc_idx = (proc_idx + 1) % ndesc;
	}

	priv->rx.proc_idx = proc_idx;

	/* update stats */
	u64_stats_update_begin(&priv->stats_rx.syncp);
	priv->stats_rx.packets += rx_packets;
	priv->stats_rx.bytes   += rx_bytes;
	u64_stats_update_end(&priv->stats_rx.syncp);

	/* refill the Rx buffers */
	while (proc_idx != done_idx) {
		if (ave_rxdesc_prepare(ndev, done_idx))
			break;
		done_idx = (done_idx + 1) % ndesc;
	}

	priv->rx.done_idx = done_idx;

	return npkts;
}

static int ave_napi_poll_rx(struct napi_struct *napi, int budget)
{
	struct ave_private *priv;
	struct net_device *ndev;
	int num;

	priv = container_of(napi, struct ave_private, napi_rx);
	ndev = priv->ndev;

	num = ave_rx_receive(ndev, budget);
	if (num < budget) {
		napi_complete_done(napi, num);

		/* enable Rx interrupt when NAPI finishes */
		ave_irq_enable(ndev, AVE_GI_RXIINT);
	}

	return num;
}

static int ave_napi_poll_tx(struct napi_struct *napi, int budget)
{
	struct ave_private *priv;
	struct net_device *ndev;
	int num;

	priv = container_of(napi, struct ave_private, napi_tx);
	ndev = priv->ndev;

	num = ave_tx_complete(ndev);
	napi_complete(napi);

	/* enable Tx interrupt when NAPI finishes */
	ave_irq_enable(ndev, AVE_GI_TX);

	return num;
}

static void ave_global_reset(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 val;

	/* set config register */
	val = AVE_CFGR_FLE | AVE_CFGR_IPFCEN | AVE_CFGR_CHE;
	if (!phy_interface_mode_is_rgmii(priv->phy_mode))
		val |= AVE_CFGR_MII;
	writel(val, priv->base + AVE_CFGR);

	/* reset RMII register */
	val = readl(priv->base + AVE_RSTCTRL);
	val &= ~AVE_RSTCTRL_RMIIRST;
	writel(val, priv->base + AVE_RSTCTRL);

	/* assert reset */
	writel(AVE_GRR_GRST | AVE_GRR_PHYRST, priv->base + AVE_GRR);
	msleep(20);

	/* 1st, negate PHY reset only */
	writel(AVE_GRR_GRST, priv->base + AVE_GRR);
	msleep(40);

	/* negate reset */
	writel(0, priv->base + AVE_GRR);
	msleep(40);

	/* negate RMII register */
	val = readl(priv->base + AVE_RSTCTRL);
	val |= AVE_RSTCTRL_RMIIRST;
	writel(val, priv->base + AVE_RSTCTRL);

	ave_irq_disable_all(ndev);
}

static void ave_rxfifo_reset(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 rxcr_org;

	/* save and disable MAC receive op */
	rxcr_org = readl(priv->base + AVE_RXCR);
	writel(rxcr_org & (~AVE_RXCR_RXEN), priv->base + AVE_RXCR);

	/* suspend Rx descriptor */
	ave_desc_switch(ndev, AVE_DESC_RX_SUSPEND);

	/* receive all packets before descriptor starts */
	ave_rx_receive(ndev, priv->rx.ndesc);

	/* assert reset */
	writel(AVE_GRR_RXFFR, priv->base + AVE_GRR);
	udelay(50);

	/* negate reset */
	writel(0, priv->base + AVE_GRR);
	udelay(20);

	/* negate interrupt status */
	writel(AVE_GI_RXOVF, priv->base + AVE_GISR);

	/* permit descriptor */
	ave_desc_switch(ndev, AVE_DESC_RX_PERMIT);

	/* restore MAC reccieve op */
	writel(rxcr_org, priv->base + AVE_RXCR);
}

static irqreturn_t ave_irq_handler(int irq, void *netdev)
{
	struct net_device *ndev = (struct net_device *)netdev;
	struct ave_private *priv = netdev_priv(ndev);
	u32 gimr_val, gisr_val;

	gimr_val = ave_irq_disable_all(ndev);

	/* get interrupt status */
	gisr_val = readl(priv->base + AVE_GISR);

	/* PHY */
	if (gisr_val & AVE_GI_PHY)
		writel(AVE_GI_PHY, priv->base + AVE_GISR);

	/* check exceeding packet */
	if (gisr_val & AVE_GI_RXERR) {
		writel(AVE_GI_RXERR, priv->base + AVE_GISR);
		netdev_err(ndev, "receive a packet exceeding frame buffer\n");
	}

	gisr_val &= gimr_val;
	if (!gisr_val)
		goto exit_isr;

	/* RxFIFO overflow */
	if (gisr_val & AVE_GI_RXOVF) {
		priv->stats_rx.fifo_errors++;
		ave_rxfifo_reset(ndev);
		goto exit_isr;
	}

	/* Rx drop */
	if (gisr_val & AVE_GI_RXDROP) {
		priv->stats_rx.dropped++;
		writel(AVE_GI_RXDROP, priv->base + AVE_GISR);
	}

	/* Rx interval */
	if (gisr_val & AVE_GI_RXIINT) {
		napi_schedule(&priv->napi_rx);
		/* still force to disable Rx interrupt until NAPI finishes */
		gimr_val &= ~AVE_GI_RXIINT;
	}

	/* Tx completed */
	if (gisr_val & AVE_GI_TX) {
		napi_schedule(&priv->napi_tx);
		/* still force to disable Tx interrupt until NAPI finishes */
		gimr_val &= ~AVE_GI_TX;
	}

exit_isr:
	ave_irq_restore(ndev, gimr_val);

	return IRQ_HANDLED;
}

static int ave_pfsel_start(struct net_device *ndev, unsigned int entry)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 val;

	if (WARN_ON(entry > AVE_PF_SIZE))
		return -EINVAL;

	val = readl(priv->base + AVE_PFEN);
	writel(val | BIT(entry), priv->base + AVE_PFEN);

	return 0;
}

static int ave_pfsel_stop(struct net_device *ndev, unsigned int entry)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 val;

	if (WARN_ON(entry > AVE_PF_SIZE))
		return -EINVAL;

	val = readl(priv->base + AVE_PFEN);
	writel(val & ~BIT(entry), priv->base + AVE_PFEN);

	return 0;
}

static int ave_pfsel_set_macaddr(struct net_device *ndev,
				 unsigned int entry,
				 const unsigned char *mac_addr,
				 unsigned int set_size)
{
	struct ave_private *priv = netdev_priv(ndev);

	if (WARN_ON(entry > AVE_PF_SIZE))
		return -EINVAL;
	if (WARN_ON(set_size > 6))
		return -EINVAL;

	ave_pfsel_stop(ndev, entry);

	/* set MAC address for the filter */
	ave_hw_write_macaddr(ndev, mac_addr,
			     AVE_PKTF(entry), AVE_PKTF(entry) + 4);

	/* set byte mask */
	writel(GENMASK(31, set_size) & AVE_PFMBYTE_MASK0,
	       priv->base + AVE_PFMBYTE(entry));
	writel(AVE_PFMBYTE_MASK1, priv->base + AVE_PFMBYTE(entry) + 4);

	/* set bit mask filter */
	writel(AVE_PFMBIT_MASK, priv->base + AVE_PFMBIT(entry));

	/* set selector to ring 0 */
	writel(0, priv->base + AVE_PFSEL(entry));

	/* restart filter */
	ave_pfsel_start(ndev, entry);

	return 0;
}

static void ave_pfsel_set_promisc(struct net_device *ndev,
				  unsigned int entry, u32 rxring)
{
	struct ave_private *priv = netdev_priv(ndev);

	if (WARN_ON(entry > AVE_PF_SIZE))
		return;

	ave_pfsel_stop(ndev, entry);

	/* set byte mask */
	writel(AVE_PFMBYTE_MASK0, priv->base + AVE_PFMBYTE(entry));
	writel(AVE_PFMBYTE_MASK1, priv->base + AVE_PFMBYTE(entry) + 4);

	/* set bit mask filter */
	writel(AVE_PFMBIT_MASK, priv->base + AVE_PFMBIT(entry));

	/* set selector to rxring */
	writel(rxring, priv->base + AVE_PFSEL(entry));

	ave_pfsel_start(ndev, entry);
}

static void ave_pfsel_init(struct net_device *ndev)
{
	unsigned char bcast_mac[ETH_ALEN];
	int i;

	eth_broadcast_addr(bcast_mac);

	for (i = 0; i < AVE_PF_SIZE; i++)
		ave_pfsel_stop(ndev, i);

	/* promiscious entry, select ring 0 */
	ave_pfsel_set_promisc(ndev, AVE_PFNUM_FILTER, 0);

	/* unicast entry */
	ave_pfsel_set_macaddr(ndev, AVE_PFNUM_UNICAST, ndev->dev_addr, 6);

	/* broadcast entry */
	ave_pfsel_set_macaddr(ndev, AVE_PFNUM_BROADCAST, bcast_mac, 6);
}

static void ave_phy_adjust_link(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	struct phy_device *phydev = ndev->phydev;
	u32 val, txcr, rxcr, rxcr_org;
	u16 rmt_adv = 0, lcl_adv = 0;
	u8 cap;

	/* set RGMII speed */
	val = readl(priv->base + AVE_TXCR);
	val &= ~(AVE_TXCR_TXSPD_100 | AVE_TXCR_TXSPD_1G);

	if (phy_interface_is_rgmii(phydev) && phydev->speed == SPEED_1000)
		val |= AVE_TXCR_TXSPD_1G;
	else if (phydev->speed == SPEED_100)
		val |= AVE_TXCR_TXSPD_100;

	writel(val, priv->base + AVE_TXCR);

	/* set RMII speed (100M/10M only) */
	if (!phy_interface_is_rgmii(phydev)) {
		val = readl(priv->base + AVE_LINKSEL);
		if (phydev->speed == SPEED_10)
			val &= ~AVE_LINKSEL_100M;
		else
			val |= AVE_LINKSEL_100M;
		writel(val, priv->base + AVE_LINKSEL);
	}

	/* check current RXCR/TXCR */
	rxcr = readl(priv->base + AVE_RXCR);
	txcr = readl(priv->base + AVE_TXCR);
	rxcr_org = rxcr;

	if (phydev->duplex) {
		rxcr |= AVE_RXCR_FDUPEN;

		if (phydev->pause)
			rmt_adv |= LPA_PAUSE_CAP;
		if (phydev->asym_pause)
			rmt_adv |= LPA_PAUSE_ASYM;

		lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising);
		cap = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
		if (cap & FLOW_CTRL_TX)
			txcr |= AVE_TXCR_FLOCTR;
		else
			txcr &= ~AVE_TXCR_FLOCTR;
		if (cap & FLOW_CTRL_RX)
			rxcr |= AVE_RXCR_FLOCTR;
		else
			rxcr &= ~AVE_RXCR_FLOCTR;
	} else {
		rxcr &= ~AVE_RXCR_FDUPEN;
		rxcr &= ~AVE_RXCR_FLOCTR;
		txcr &= ~AVE_TXCR_FLOCTR;
	}

	if (rxcr_org != rxcr) {
		/* disable Rx mac */
		writel(rxcr & ~AVE_RXCR_RXEN, priv->base + AVE_RXCR);
		/* change and enable TX/Rx mac */
		writel(txcr, priv->base + AVE_TXCR);
		writel(rxcr, priv->base + AVE_RXCR);
	}

	phy_print_status(phydev);
}

static void ave_macaddr_init(struct net_device *ndev)
{
	ave_hw_write_macaddr(ndev, ndev->dev_addr, AVE_RXMAC1R, AVE_RXMAC2R);

	/* pfsel unicast entry */
	ave_pfsel_set_macaddr(ndev, AVE_PFNUM_UNICAST, ndev->dev_addr, 6);
}

static int ave_init(struct net_device *ndev)
{
	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
	struct ave_private *priv = netdev_priv(ndev);
	struct device *dev = ndev->dev.parent;
	struct device_node *np = dev->of_node;
	struct device_node *mdio_np;
	struct phy_device *phydev;
	int nc, nr, ret;

	/* enable clk because of hw access until ndo_open */
	for (nc = 0; nc < priv->nclks; nc++) {
		ret = clk_prepare_enable(priv->clk[nc]);
		if (ret) {
			dev_err(dev, "can't enable clock\n");
			goto out_clk_disable;
		}
	}

	for (nr = 0; nr < priv->nrsts; nr++) {
		ret = reset_control_deassert(priv->rst[nr]);
		if (ret) {
			dev_err(dev, "can't deassert reset\n");
			goto out_reset_assert;
		}
	}

	ret = regmap_update_bits(priv->regmap, SG_ETPINMODE,
				 priv->pinmode_mask, priv->pinmode_val);
	if (ret)
		return ret;

	ave_global_reset(ndev);

	mdio_np = of_get_child_by_name(np, "mdio");
	if (!mdio_np) {
		dev_err(dev, "mdio node not found\n");
		ret = -EINVAL;
		goto out_reset_assert;
	}
	ret = of_mdiobus_register(priv->mdio, mdio_np);
	of_node_put(mdio_np);
	if (ret) {
		dev_err(dev, "failed to register mdiobus\n");
		goto out_reset_assert;
	}

	phydev = of_phy_get_and_connect(ndev, np, ave_phy_adjust_link);
	if (!phydev) {
		dev_err(dev, "could not attach to PHY\n");
		ret = -ENODEV;
		goto out_mdio_unregister;
	}

	priv->phydev = phydev;

	ave_ethtool_get_wol(ndev, &wol);
	device_set_wakeup_capable(&ndev->dev, !!wol.supported);

	/* set wol initial state disabled */
	wol.wolopts = 0;
	ave_ethtool_set_wol(ndev, &wol);

	if (!phy_interface_is_rgmii(phydev))
		phy_set_max_speed(phydev, SPEED_100);

	phy_support_asym_pause(phydev);

	phy_attached_info(phydev);

	return 0;

out_mdio_unregister:
	mdiobus_unregister(priv->mdio);
out_reset_assert:
	while (--nr >= 0)
		reset_control_assert(priv->rst[nr]);
out_clk_disable:
	while (--nc >= 0)
		clk_disable_unprepare(priv->clk[nc]);

	return ret;
}

static void ave_uninit(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	int i;

	phy_disconnect(priv->phydev);
	mdiobus_unregister(priv->mdio);

	/* disable clk because of hw access after ndo_stop */
	for (i = 0; i < priv->nrsts; i++)
		reset_control_assert(priv->rst[i]);
	for (i = 0; i < priv->nclks; i++)
		clk_disable_unprepare(priv->clk[i]);
}

static int ave_open(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	int entry;
	int ret;
	u32 val;

	ret = request_irq(priv->irq, ave_irq_handler, IRQF_SHARED, ndev->name,
			  ndev);
	if (ret)
		return ret;

	priv->tx.desc = kcalloc(priv->tx.ndesc, sizeof(*priv->tx.desc),
				GFP_KERNEL);
	if (!priv->tx.desc) {
		ret = -ENOMEM;
		goto out_free_irq;
	}

	priv->rx.desc = kcalloc(priv->rx.ndesc, sizeof(*priv->rx.desc),
				GFP_KERNEL);
	if (!priv->rx.desc) {
		kfree(priv->tx.desc);
		ret = -ENOMEM;
		goto out_free_irq;
	}

	/* initialize Tx work and descriptor */
	priv->tx.proc_idx = 0;
	priv->tx.done_idx = 0;
	for (entry = 0; entry < priv->tx.ndesc; entry++) {
		ave_desc_write_cmdsts(ndev, AVE_DESCID_TX, entry, 0);
		ave_desc_write_addr(ndev, AVE_DESCID_TX, entry, 0);
	}
	writel(AVE_TXDC_ADDR_START |
	       (((priv->tx.ndesc * priv->desc_size) << 16) & AVE_TXDC_SIZE),
	       priv->base + AVE_TXDC);

	/* initialize Rx work and descriptor */
	priv->rx.proc_idx = 0;
	priv->rx.done_idx = 0;
	for (entry = 0; entry < priv->rx.ndesc; entry++) {
		if (ave_rxdesc_prepare(ndev, entry))
			break;
	}
	writel(AVE_RXDC0_ADDR_START |
	       (((priv->rx.ndesc * priv->desc_size) << 16) & AVE_RXDC0_SIZE),
	       priv->base + AVE_RXDC0);

	ave_desc_switch(ndev, AVE_DESC_START);

	ave_pfsel_init(ndev);
	ave_macaddr_init(ndev);

	/* set Rx configuration */
	/* full duplex, enable pause drop, enalbe flow control */
	val = AVE_RXCR_RXEN | AVE_RXCR_FDUPEN | AVE_RXCR_DRPEN |
		AVE_RXCR_FLOCTR | (AVE_MAX_ETHFRAME & AVE_RXCR_MPSIZ_MASK);
	writel(val, priv->base + AVE_RXCR);

	/* set Tx configuration */
	/* enable flow control, disable loopback */
	writel(AVE_TXCR_FLOCTR, priv->base + AVE_TXCR);

	/* enable timer, clear EN,INTM, and mask interval unit(BSCK) */
	val = readl(priv->base + AVE_IIRQC) & AVE_IIRQC_BSCK;
	val |= AVE_IIRQC_EN0 | (AVE_INTM_COUNT << 16);
	writel(val, priv->base + AVE_IIRQC);

	val = AVE_GI_RXIINT | AVE_GI_RXOVF | AVE_GI_TX | AVE_GI_RXDROP;
	ave_irq_restore(ndev, val);

	napi_enable(&priv->napi_rx);
	napi_enable(&priv->napi_tx);

	phy_start(ndev->phydev);
	phy_start_aneg(ndev->phydev);
	netif_start_queue(ndev);

	return 0;

out_free_irq:
	disable_irq(priv->irq);
	free_irq(priv->irq, ndev);

	return ret;
}

static int ave_stop(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	int entry;

	ave_irq_disable_all(ndev);
	disable_irq(priv->irq);
	free_irq(priv->irq, ndev);

	netif_tx_disable(ndev);
	phy_stop(ndev->phydev);
	napi_disable(&priv->napi_tx);
	napi_disable(&priv->napi_rx);

	ave_desc_switch(ndev, AVE_DESC_STOP);

	/* free Tx buffer */
	for (entry = 0; entry < priv->tx.ndesc; entry++) {
		if (!priv->tx.desc[entry].skbs)
			continue;

		ave_dma_unmap(ndev, &priv->tx.desc[entry], DMA_TO_DEVICE);
		dev_kfree_skb_any(priv->tx.desc[entry].skbs);
		priv->tx.desc[entry].skbs = NULL;
	}
	priv->tx.proc_idx = 0;
	priv->tx.done_idx = 0;

	/* free Rx buffer */
	for (entry = 0; entry < priv->rx.ndesc; entry++) {
		if (!priv->rx.desc[entry].skbs)
			continue;

		ave_dma_unmap(ndev, &priv->rx.desc[entry], DMA_FROM_DEVICE);
		dev_kfree_skb_any(priv->rx.desc[entry].skbs);
		priv->rx.desc[entry].skbs = NULL;
	}
	priv->rx.proc_idx = 0;
	priv->rx.done_idx = 0;

	kfree(priv->tx.desc);
	kfree(priv->rx.desc);

	return 0;
}

static int ave_start_xmit(struct sk_buff *skb, struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	u32 proc_idx, done_idx, ndesc, cmdsts;
	int ret, freepkt;
	dma_addr_t paddr;

	proc_idx = priv->tx.proc_idx;
	done_idx = priv->tx.done_idx;
	ndesc = priv->tx.ndesc;
	freepkt = ((done_idx + ndesc - 1) - proc_idx) % ndesc;

	/* stop queue when not enough entry */
	if (unlikely(freepkt < 1)) {
		netif_stop_queue(ndev);
		return NETDEV_TX_BUSY;
	}

	/* add padding for short packet */
	if (skb_put_padto(skb, ETH_ZLEN)) {
		priv->stats_tx.dropped++;
		return NETDEV_TX_OK;
	}

	/* map Tx buffer
	 * Tx buffer set to the Tx descriptor doesn't have any restriction.
	 */
	ret = ave_dma_map(ndev, &priv->tx.desc[proc_idx],
			  skb->data, skb->len, DMA_TO_DEVICE, &paddr);
	if (ret) {
		dev_kfree_skb_any(skb);
		priv->stats_tx.dropped++;
		return NETDEV_TX_OK;
	}

	priv->tx.desc[proc_idx].skbs = skb;

	ave_desc_write_addr(ndev, AVE_DESCID_TX, proc_idx, paddr);

	cmdsts = AVE_STS_OWN | AVE_STS_1ST | AVE_STS_LAST |
		(skb->len & AVE_STS_PKTLEN_TX_MASK);

	/* set interrupt per AVE_FORCE_TXINTCNT or when queue is stopped */
	if (!(proc_idx % AVE_FORCE_TXINTCNT) || netif_queue_stopped(ndev))
		cmdsts |= AVE_STS_INTR;

	/* disable checksum calculation when skb doesn't calurate checksum */
	if (skb->ip_summed == CHECKSUM_NONE ||
	    skb->ip_summed == CHECKSUM_UNNECESSARY)
		cmdsts |= AVE_STS_NOCSUM;

	ave_desc_write_cmdsts(ndev, AVE_DESCID_TX, proc_idx, cmdsts);

	priv->tx.proc_idx = (proc_idx + 1) % ndesc;

	return NETDEV_TX_OK;
}

static int ave_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
{
	return phy_mii_ioctl(ndev->phydev, ifr, cmd);
}

static const u8 v4multi_macadr[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const u8 v6multi_macadr[] = { 0x33, 0x00, 0x00, 0x00, 0x00, 0x00 };

static void ave_set_rx_mode(struct net_device *ndev)
{
	struct ave_private *priv = netdev_priv(ndev);
	struct netdev_hw_addr *hw_adr;
	int count, mc_cnt;
	u32 val;

	/* MAC addr filter enable for promiscious mode */
	mc_cnt = netdev_mc_count(ndev);
	val = readl(priv->base + AVE_RXCR);
	if (ndev->flags & IFF_PROMISC || !mc_cnt)
		val &= ~AVE_RXCR_AFEN;
	else
		val |= AVE_RXCR_AFEN;
	writel(val, priv->base + AVE_RXCR);

	/* set all multicast address */
	if ((ndev->flags & IFF_ALLMULTI) || mc_cnt > AVE_PF_MULTICAST_SIZE) {
		ave_pfsel_set_macaddr(ndev, AVE_PFNUM_MULTICAST,
				      v4multi_macadr, 1);
		ave_pfsel_set_macaddr(ndev, AVE_PFNUM_MULTICAST + 1,
				      v6multi_macadr, 1);
	} else {
		/* stop all multicast filter */
		for (count = 0; count < AVE_PF_MULTICAST_SIZE; count++)
			ave_pfsel_stop(ndev, AVE_PFNUM_MULTICAST + count);

		/* set multicast addresses */
		count = 0;
		netdev_for_each_mc_addr(hw_adr, ndev) {
			if (count == mc_cnt)
				break;
			ave_pfsel_set_macaddr(ndev, AVE_PFNUM_MULTICAST + count,
					      hw_adr->addr, 6);
			count++;
		}
	}
}

static void ave_get_stats64(struct net_device *ndev,
			    struct rtnl_link_stats64 *stats)
{
	struct ave_private *priv = netdev_priv(ndev);
	unsigned int start;

	do {
		start = u64_stats_fetch_begin_irq(&priv->stats_rx.syncp);
		stats->rx_packets = priv->stats_rx.packets;
		stats->rx_bytes	  = priv->stats_rx.bytes;
	} while (u64_stats_fetch_retry_irq(&priv->stats_rx.syncp, start));

	do {
		start = u64_stats_fetch_begin_irq(&priv->stats_tx.syncp);
		stats->tx_packets = priv->stats_tx.packets;
		stats->tx_bytes	  = priv->stats_tx.bytes;
	} while (u64_stats_fetch_retry_irq(&priv->stats_tx.syncp, start));

	stats->rx_errors      = priv->stats_rx.errors;
	stats->tx_errors      = priv->stats_tx.errors;
	stats->rx_dropped     = priv->stats_rx.dropped;
	stats->tx_dropped     = priv->stats_tx.dropped;
	stats->rx_fifo_errors = priv->stats_rx.fifo_errors;
	stats->collisions     = priv->stats_tx.collisions;
}

static int ave_set_mac_address(struct net_device *ndev, void *p)
{
	int ret = eth_mac_addr(ndev, p);

	if (ret)
		return ret;

	ave_macaddr_init(ndev);

	return 0;
}

static const struct net_device_ops ave_netdev_ops = {
	.ndo_init		= ave_init,
	.ndo_uninit		= ave_uninit,
	.ndo_open		= ave_open,
	.ndo_stop		= ave_stop,
	.ndo_start_xmit		= ave_start_xmit,
	.ndo_do_ioctl		= ave_ioctl,
	.ndo_set_rx_mode	= ave_set_rx_mode,
	.ndo_get_stats64	= ave_get_stats64,
	.ndo_set_mac_address	= ave_set_mac_address,
};

static int ave_probe(struct platform_device *pdev)
{
	const struct ave_soc_data *data;
	struct device *dev = &pdev->dev;
	char buf[ETHTOOL_FWVERS_LEN];
	struct of_phandle_args args;
	phy_interface_t phy_mode;
	struct ave_private *priv;
	struct net_device *ndev;
	struct device_node *np;
	const void *mac_addr;
	void __iomem *base;
	const char *name;
	int i, irq, ret;
	u64 dma_mask;
	u32 ave_id;

	data = of_device_get_match_data(dev);
	if (WARN_ON(!data))
		return -EINVAL;

	np = dev->of_node;
	ret = of_get_phy_mode(np, &phy_mode);
	if (ret) {
		dev_err(dev, "phy-mode not found\n");
		return ret;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return irq;

	base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(base))
		return PTR_ERR(base);

	ndev = alloc_etherdev(sizeof(struct ave_private));
	if (!ndev) {
		dev_err(dev, "can't allocate ethernet device\n");
		return -ENOMEM;
	}

	ndev->netdev_ops = &ave_netdev_ops;
	ndev->ethtool_ops = &ave_ethtool_ops;
	SET_NETDEV_DEV(ndev, dev);

	ndev->features    |= (NETIF_F_IP_CSUM | NETIF_F_RXCSUM);
	ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_RXCSUM);

	ndev->max_mtu = AVE_MAX_ETHFRAME - (ETH_HLEN + ETH_FCS_LEN);

	mac_addr = of_get_mac_address(np);
	if (!IS_ERR(mac_addr))
		ether_addr_copy(ndev->dev_addr, mac_addr);

	/* if the mac address is invalid, use random mac address */
	if (!is_valid_ether_addr(ndev->dev_addr)) {
		eth_hw_addr_random(ndev);
		dev_warn(dev, "Using random MAC address: %pM\n",
			 ndev->dev_addr);
	}

	priv = netdev_priv(ndev);
	priv->base = base;
	priv->irq = irq;
	priv->ndev = ndev;
	priv->msg_enable = netif_msg_init(-1, AVE_DEFAULT_MSG_ENABLE);
	priv->phy_mode = phy_mode;
	priv->data = data;

	if (IS_DESC_64BIT(priv)) {
		priv->desc_size = AVE_DESC_SIZE_64;
		priv->tx.daddr  = AVE_TXDM_64;
		priv->rx.daddr  = AVE_RXDM_64;
		dma_mask = DMA_BIT_MASK(64);
	} else {
		priv->desc_size = AVE_DESC_SIZE_32;
		priv->tx.daddr  = AVE_TXDM_32;
		priv->rx.daddr  = AVE_RXDM_32;
		dma_mask = DMA_BIT_MASK(32);
	}
	ret = dma_set_mask(dev, dma_mask);
	if (ret)
		goto out_free_netdev;

	priv->tx.ndesc = AVE_NR_TXDESC;
	priv->rx.ndesc = AVE_NR_RXDESC;

	u64_stats_init(&priv->stats_tx.syncp);
	u64_stats_init(&priv->stats_rx.syncp);

	for (i = 0; i < AVE_MAX_CLKS; i++) {
		name = priv->data->clock_names[i];
		if (!name)
			break;
		priv->clk[i] = devm_clk_get(dev, name);
		if (IS_ERR(priv->clk[i])) {
			ret = PTR_ERR(priv->clk[i]);
			goto out_free_netdev;
		}
		priv->nclks++;
	}

	for (i = 0; i < AVE_MAX_RSTS; i++) {
		name = priv->data->reset_names[i];
		if (!name)
			break;
		priv->rst[i] = devm_reset_control_get_shared(dev, name);
		if (IS_ERR(priv->rst[i])) {
			ret = PTR_ERR(priv->rst[i]);
			goto out_free_netdev;
		}
		priv->nrsts++;
	}

	ret = of_parse_phandle_with_fixed_args(np,
					       "socionext,syscon-phy-mode",
					       1, 0, &args);
	if (ret) {
		dev_err(dev, "can't get syscon-phy-mode property\n");
		goto out_free_netdev;
	}
	priv->regmap = syscon_node_to_regmap(args.np);
	of_node_put(args.np);
	if (IS_ERR(priv->regmap)) {
		dev_err(dev, "can't map syscon-phy-mode\n");
		ret = PTR_ERR(priv->regmap);
		goto out_free_netdev;
	}
	ret = priv->data->get_pinmode(priv, phy_mode, args.args[0]);
	if (ret) {
		dev_err(dev, "invalid phy-mode setting\n");
		goto out_free_netdev;
	}

	priv->mdio = devm_mdiobus_alloc(dev);
	if (!priv->mdio) {
		ret = -ENOMEM;
		goto out_free_netdev;
	}
	priv->mdio->priv = ndev;
	priv->mdio->parent = dev;
	priv->mdio->read = ave_mdiobus_read;
	priv->mdio->write = ave_mdiobus_write;
	priv->mdio->name = "uniphier-mdio";
	snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%x",
		 pdev->name, pdev->id);

	/* Register as a NAPI supported driver */
	netif_napi_add(ndev, &priv->napi_rx, ave_napi_poll_rx,
		       NAPI_POLL_WEIGHT);
	netif_tx_napi_add(ndev, &priv->napi_tx, ave_napi_poll_tx,
			  NAPI_POLL_WEIGHT);

	platform_set_drvdata(pdev, ndev);

	ret = register_netdev(ndev);
	if (ret) {
		dev_err(dev, "failed to register netdevice\n");
		goto out_del_napi;
	}

	/* get ID and version */
	ave_id = readl(priv->base + AVE_IDR);
	ave_hw_read_version(ndev, buf, sizeof(buf));

	dev_info(dev, "Socionext %c%c%c%c Ethernet IP %s (irq=%d, phy=%s)\n",
		 (ave_id >> 24) & 0xff, (ave_id >> 16) & 0xff,
		 (ave_id >> 8) & 0xff, (ave_id >> 0) & 0xff,
		 buf, priv->irq, phy_modes(phy_mode));

	return 0;

out_del_napi:
	netif_napi_del(&priv->napi_rx);
	netif_napi_del(&priv->napi_tx);
out_free_netdev:
	free_netdev(ndev);

	return ret;
}

static int ave_remove(struct platform_device *pdev)
{
	struct net_device *ndev = platform_get_drvdata(pdev);
	struct ave_private *priv = netdev_priv(ndev);

	unregister_netdev(ndev);
	netif_napi_del(&priv->napi_rx);
	netif_napi_del(&priv->napi_tx);
	free_netdev(ndev);

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int ave_suspend(struct device *dev)
{
	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
	struct net_device *ndev = dev_get_drvdata(dev);
	struct ave_private *priv = netdev_priv(ndev);
	int ret = 0;

	if (netif_running(ndev)) {
		ret = ave_stop(ndev);
		netif_device_detach(ndev);
	}

	ave_ethtool_get_wol(ndev, &wol);
	priv->wolopts = wol.wolopts;

	return ret;
}

static int ave_resume(struct device *dev)
{
	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
	struct net_device *ndev = dev_get_drvdata(dev);
	struct ave_private *priv = netdev_priv(ndev);
	int ret = 0;

	ave_global_reset(ndev);

	ave_ethtool_get_wol(ndev, &wol);
	wol.wolopts = priv->wolopts;
	ave_ethtool_set_wol(ndev, &wol);

	if (ndev->phydev) {
		ret = phy_resume(ndev->phydev);
		if (ret)
			return ret;
	}

	if (netif_running(ndev)) {
		ret = ave_open(ndev);
		netif_device_attach(ndev);
	}

	return ret;
}

static SIMPLE_DEV_PM_OPS(ave_pm_ops, ave_suspend, ave_resume);
#define AVE_PM_OPS	(&ave_pm_ops)
#else
#define AVE_PM_OPS	NULL
#endif

static int ave_pro4_get_pinmode(struct ave_private *priv,
				phy_interface_t phy_mode, u32 arg)
{
	if (arg > 0)
		return -EINVAL;

	priv->pinmode_mask = SG_ETPINMODE_RMII(0);

	switch (phy_mode) {
	case PHY_INTERFACE_MODE_RMII:
		priv->pinmode_val = SG_ETPINMODE_RMII(0);
		break;
	case PHY_INTERFACE_MODE_MII:
	case PHY_INTERFACE_MODE_RGMII:
		priv->pinmode_val = 0;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int ave_ld11_get_pinmode(struct ave_private *priv,
				phy_interface_t phy_mode, u32 arg)
{
	if (arg > 0)
		return -EINVAL;

	priv->pinmode_mask = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);

	switch (phy_mode) {
	case PHY_INTERFACE_MODE_INTERNAL:
		priv->pinmode_val = 0;
		break;
	case PHY_INTERFACE_MODE_RMII:
		priv->pinmode_val = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int ave_ld20_get_pinmode(struct ave_private *priv,
				phy_interface_t phy_mode, u32 arg)
{
	if (arg > 0)
		return -EINVAL;

	priv->pinmode_mask = SG_ETPINMODE_RMII(0);

	switch (phy_mode) {
	case PHY_INTERFACE_MODE_RMII:
		priv->pinmode_val = SG_ETPINMODE_RMII(0);
		break;
	case PHY_INTERFACE_MODE_RGMII:
		priv->pinmode_val = 0;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int ave_pxs3_get_pinmode(struct ave_private *priv,
				phy_interface_t phy_mode, u32 arg)
{
	if (arg > 1)
		return -EINVAL;

	priv->pinmode_mask = SG_ETPINMODE_RMII(arg);

	switch (phy_mode) {
	case PHY_INTERFACE_MODE_RMII:
		priv->pinmode_val = SG_ETPINMODE_RMII(arg);
		break;
	case PHY_INTERFACE_MODE_RGMII:
		priv->pinmode_val = 0;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static const struct ave_soc_data ave_pro4_data = {
	.is_desc_64bit = false,
	.clock_names = {
		"gio", "ether", "ether-gb", "ether-phy",
	},
	.reset_names = {
		"gio", "ether",
	},
	.get_pinmode = ave_pro4_get_pinmode,
};

static const struct ave_soc_data ave_pxs2_data = {
	.is_desc_64bit = false,
	.clock_names = {
		"ether",
	},
	.reset_names = {
		"ether",
	},
	.get_pinmode = ave_pro4_get_pinmode,
};

static const struct ave_soc_data ave_ld11_data = {
	.is_desc_64bit = false,
	.clock_names = {
		"ether",
	},
	.reset_names = {
		"ether",
	},
	.get_pinmode = ave_ld11_get_pinmode,
};

static const struct ave_soc_data ave_ld20_data = {
	.is_desc_64bit = true,
	.clock_names = {
		"ether",
	},
	.reset_names = {
		"ether",
	},
	.get_pinmode = ave_ld20_get_pinmode,
};

static const struct ave_soc_data ave_pxs3_data = {
	.is_desc_64bit = false,
	.clock_names = {
		"ether",
	},
	.reset_names = {
		"ether",
	},
	.get_pinmode = ave_pxs3_get_pinmode,
};

static const struct of_device_id of_ave_match[] = {
	{
		.compatible = "socionext,uniphier-pro4-ave4",
		.data = &ave_pro4_data,
	},
	{
		.compatible = "socionext,uniphier-pxs2-ave4",
		.data = &ave_pxs2_data,
	},
	{
		.compatible = "socionext,uniphier-ld11-ave4",
		.data = &ave_ld11_data,
	},
	{
		.compatible = "socionext,uniphier-ld20-ave4",
		.data = &ave_ld20_data,
	},
	{
		.compatible = "socionext,uniphier-pxs3-ave4",
		.data = &ave_pxs3_data,
	},
	{ /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, of_ave_match);

static struct platform_driver ave_driver = {
	.probe  = ave_probe,
	.remove = ave_remove,
	.driver	= {
		.name = "ave",
		.pm   = AVE_PM_OPS,
		.of_match_table	= of_ave_match,
	},
};
module_platform_driver(ave_driver);

MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
MODULE_DESCRIPTION("Socionext UniPhier AVE ethernet driver");
MODULE_LICENSE("GPL v2");