aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/bmips/files/drivers/net/ethernet/broadcom/bcm6348-enet.c
blob: 63841129a990153581a894b4228410a6cace97de (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * BCM6348 Ethernet Controller Driver
 *
 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
 * Copyright (C) 2015 Jonas Gorski <jonas.gorski@gmail.com>
 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/mtd/mtd.h>
#include <linux/of_address.h>
#include <linux/of_clk.h>
#include <linux/of_irq.h>
#include <linux/of_mdio.h>
#include <linux/of_net.h>
#include <linux/of_platform.h>
#include <linux/phy.h>
#include <linux/platform_device.h>
#include <linux/reset.h>

/* DMA channels */
#define DMA_CHAN_WIDTH			0x10

/* Controller Configuration Register */
#define DMA_CFG_REG			0x0
#define DMA_CFG_EN_SHIFT		0
#define DMA_CFG_EN_MASK			(1 << DMA_CFG_EN_SHIFT)
#define DMA_CFG_FLOWCH_MASK(x)		(1 << ((x >> 1) + 1))

/* Flow Control Descriptor Low Threshold register */
#define DMA_FLOWCL_REG(x)		(0x4 + (x) * 6)

/* Flow Control Descriptor High Threshold register */
#define DMA_FLOWCH_REG(x)		(0x8 + (x) * 6)

/* Flow Control Descriptor Buffer Alloca Threshold register */
#define DMA_BUFALLOC_REG(x)		(0xc + (x) * 6)
#define DMA_BUFALLOC_FORCE_SHIFT	31
#define DMA_BUFALLOC_FORCE_MASK		(1 << DMA_BUFALLOC_FORCE_SHIFT)

/* Channel Configuration register */
#define DMAC_CHANCFG_REG		0x0
#define DMAC_CHANCFG_EN_SHIFT		0
#define DMAC_CHANCFG_EN_MASK		(1 << DMAC_CHANCFG_EN_SHIFT)
#define DMAC_CHANCFG_PKTHALT_SHIFT	1
#define DMAC_CHANCFG_PKTHALT_MASK	(1 << DMAC_CHANCFG_PKTHALT_SHIFT)
#define DMAC_CHANCFG_BUFHALT_SHIFT	2
#define DMAC_CHANCFG_BUFHALT_MASK	(1 << DMAC_CHANCFG_BUFHALT_SHIFT)
#define DMAC_CHANCFG_CHAINING_SHIFT	2
#define DMAC_CHANCFG_CHAINING_MASK	(1 << DMAC_CHANCFG_CHAINING_SHIFT)
#define DMAC_CHANCFG_WRAP_EN_SHIFT	3
#define DMAC_CHANCFG_WRAP_EN_MASK	(1 << DMAC_CHANCFG_WRAP_EN_SHIFT)
#define DMAC_CHANCFG_FLOWC_EN_SHIFT	4
#define DMAC_CHANCFG_FLOWC_EN_MASK	(1 << DMAC_CHANCFG_FLOWC_EN_SHIFT)

/* Interrupt Control/Status register */
#define DMAC_IR_REG			0x4
#define DMAC_IR_BUFDONE_MASK		(1 << 0)
#define DMAC_IR_PKTDONE_MASK		(1 << 1)
#define DMAC_IR_NOTOWNER_MASK		(1 << 2)

/* Interrupt Mask register */
#define DMAC_IRMASK_REG			0x8

/* Maximum Burst Length */
#define DMAC_MAXBURST_REG		0xc

/* Ring Start Address register */
#define DMAS_RSTART_REG			0x0

/* State Ram Word 2 */
#define DMAS_SRAM2_REG			0x4

/* State Ram Word 3 */
#define DMAS_SRAM3_REG			0x8

/* State Ram Word 4 */
#define DMAS_SRAM4_REG			0xc

struct bcm6348_iudma_desc {
	u32 len_stat;
	u32 address;
};

/* control */
#define DMADESC_LENGTH_SHIFT		16
#define DMADESC_LENGTH_MASK		(0xfff << DMADESC_LENGTH_SHIFT)
#define DMADESC_OWNER_MASK		(1 << 15)
#define DMADESC_EOP_MASK		(1 << 14)
#define DMADESC_SOP_MASK		(1 << 13)
#define DMADESC_ESOP_MASK		(DMADESC_EOP_MASK | DMADESC_SOP_MASK)
#define DMADESC_WRAP_MASK		(1 << 12)

/* status */
#define DMADESC_UNDER_MASK		(1 << 9)
#define DMADESC_APPEND_CRC		(1 << 8)
#define DMADESC_OVSIZE_MASK		(1 << 4)
#define DMADESC_RXER_MASK		(1 << 2)
#define DMADESC_CRC_MASK		(1 << 1)
#define DMADESC_OV_MASK			(1 << 0)
#define DMADESC_ERR_MASK		(DMADESC_UNDER_MASK | \
					 DMADESC_OVSIZE_MASK | \
					 DMADESC_RXER_MASK | \
					 DMADESC_CRC_MASK | \
					 DMADESC_OV_MASK)

struct bcm6348_iudma {
	void __iomem *dma_base;
	void __iomem *dma_chan;
	void __iomem *dma_sram;

	spinlock_t dma_base_lock;

	struct clk **clock;
	unsigned int num_clocks;

	struct reset_control **reset;
	unsigned int num_resets;

	unsigned int dma_channels;
};

int bcm6348_iudma_drivers_register(struct platform_device *pdev);

static inline u32 dma_readl(struct bcm6348_iudma *iudma, u32 off)
{
	u32 val;

	spin_lock(&iudma->dma_base_lock);
	val = __raw_readl(iudma->dma_base + off);
	spin_unlock(&iudma->dma_base_lock);

	return val;
}

static inline void dma_writel(struct bcm6348_iudma *iudma, u32 val, u32 off)
{
	spin_lock(&iudma->dma_base_lock);
	__raw_writel(val, iudma->dma_base + off);
	spin_unlock(&iudma->dma_base_lock);
}

static inline u32 dmac_readl(struct bcm6348_iudma *iudma, u32 off, int chan)
{
	return __raw_readl(iudma->dma_chan + chan * DMA_CHAN_WIDTH + off);
}

static inline void dmac_writel(struct bcm6348_iudma *iudma, u32 val, u32 off,
			       int chan)
{
	__raw_writel(val, iudma->dma_chan + chan * DMA_CHAN_WIDTH + off);
}

static inline void dmas_writel(struct bcm6348_iudma *iudma, u32 val, u32 off,
			       int chan)
{
	__raw_writel(val, iudma->dma_sram + chan * DMA_CHAN_WIDTH + off);
}

static void bcm6348_iudma_chan_stop(struct bcm6348_iudma *iudma, int chan)
{
	int limit = 1000;

	dmac_writel(iudma, 0, DMAC_CHANCFG_REG, chan);

	do {
		u32 val;

		val = dmac_readl(iudma, DMAC_CHANCFG_REG, chan);
		if (!(val & DMAC_CHANCFG_EN_MASK))
			break;

		udelay(1);
	} while (limit--);
}

static int bcm6348_iudma_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *node = dev->of_node;
	struct bcm6348_iudma *iudma;
	unsigned i;
	int num_resets;
	int ret;

	iudma = devm_kzalloc(dev, sizeof(*iudma), GFP_KERNEL);
	if (!iudma)
		return -ENOMEM;

	if (of_property_read_u32(node, "dma-channels", &iudma->dma_channels))
		return -ENODEV;

	iudma->dma_base = devm_platform_ioremap_resource_byname(pdev, "dma");
	if (IS_ERR_OR_NULL(iudma->dma_base))
		return PTR_ERR(iudma->dma_base);

	iudma->dma_chan = devm_platform_ioremap_resource_byname(pdev,
								"dma-channels");
	if (IS_ERR_OR_NULL(iudma->dma_chan))
		return PTR_ERR(iudma->dma_chan);

	iudma->dma_sram = devm_platform_ioremap_resource_byname(pdev,
								"dma-sram");
	if (IS_ERR_OR_NULL(iudma->dma_sram))
		return PTR_ERR(iudma->dma_sram);

	iudma->num_clocks = of_clk_get_parent_count(node);
	if (iudma->num_clocks) {
		iudma->clock = devm_kcalloc(dev, iudma->num_clocks,
					    sizeof(struct clk *), GFP_KERNEL);
		if (IS_ERR_OR_NULL(iudma->clock))
			return PTR_ERR(iudma->clock);
	}
	for (i = 0; i < iudma->num_clocks; i++) {
		iudma->clock[i] = of_clk_get(node, i);
		if (IS_ERR_OR_NULL(iudma->clock[i])) {
			dev_err(dev, "error getting iudma clock %d\n", i);
			return PTR_ERR(iudma->clock[i]);
		}

		ret = clk_prepare_enable(iudma->clock[i]);
		if (ret) {
			dev_err(dev, "error enabling iudma clock %d\n", i);
			return ret;
		}
	}

	num_resets = of_count_phandle_with_args(node, "resets",
						"#reset-cells");
	if (num_resets > 0)
		iudma->num_resets = num_resets;
	else
		iudma->num_resets = 0;
	if (iudma->num_resets) {
		iudma->reset = devm_kcalloc(dev, iudma->num_resets,
					    sizeof(struct reset_control *),
					    GFP_KERNEL);
		if (IS_ERR_OR_NULL(iudma->reset))
			return PTR_ERR(iudma->reset);
	}
	for (i = 0; i < iudma->num_resets; i++) {
		iudma->reset[i] = devm_reset_control_get_by_index(dev, i);
		if (IS_ERR_OR_NULL(iudma->reset[i])) {
			dev_err(dev, "error getting iudma reset %d\n", i);
			return PTR_ERR(iudma->reset[i]);
		}

		ret = reset_control_reset(iudma->reset[i]);
		if (ret) {
			dev_err(dev, "error performing iudma reset %d\n", i);
			return ret;
		}
	}

	dma_writel(iudma, 0, DMA_CFG_REG);
	for (i = 0; i < iudma->dma_channels; i++)
		bcm6348_iudma_chan_stop(iudma, i);
	dma_writel(iudma, DMA_CFG_EN_MASK, DMA_CFG_REG);

	spin_lock_init(&iudma->dma_base_lock);

	dev_info(dev, "bcm6348-iudma @ 0x%px\n", iudma->dma_base);

	platform_set_drvdata(pdev, iudma);

	return bcm6348_iudma_drivers_register(pdev);
}

static const struct of_device_id bcm6348_iudma_of_match[] = {
	{ .compatible = "brcm,bcm6338-iudma", },
	{ .compatible = "brcm,bcm6348-iudma", },
	{ .compatible = "brcm,bcm6358-iudma", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, bcm6348_emac_of_match);

static struct platform_driver bcm6348_iudma_driver = {
	.driver = {
		.name = "bcm6348-iudma",
		.of_match_table = of_match_ptr(bcm6348_iudma_of_match),
	},
	.probe	= bcm6348_iudma_probe,
};
module_platform_driver(bcm6348_iudma_driver);

/*
 * BCM6348 Eternet MACs
 */

/* MTU */
#define ENET_MAX_MTU			2046

#define ENET_TAG_SIZE			6
#define ENET_MTU_OVERHEAD		(VLAN_ETH_HLEN + VLAN_HLEN + \
					 ENET_TAG_SIZE)

/* Default number of descriptor */
#define ENET_DEF_RX_DESC		64
#define ENET_DEF_TX_DESC		32
#define ENET_DEF_CPY_BREAK		128

/* Maximum burst len for dma (4 bytes unit) */
#define ENET_DMA_MAXBURST		8

/* Receiver Configuration register */
#define ENET_RXCFG_REG			0x0
#define ENET_RXCFG_ALLMCAST_SHIFT	1
#define ENET_RXCFG_ALLMCAST_MASK	(1 << ENET_RXCFG_ALLMCAST_SHIFT)
#define ENET_RXCFG_PROMISC_SHIFT	3
#define ENET_RXCFG_PROMISC_MASK		(1 << ENET_RXCFG_PROMISC_SHIFT)
#define ENET_RXCFG_LOOPBACK_SHIFT	4
#define ENET_RXCFG_LOOPBACK_MASK	(1 << ENET_RXCFG_LOOPBACK_SHIFT)
#define ENET_RXCFG_ENFLOW_SHIFT		5
#define ENET_RXCFG_ENFLOW_MASK		(1 << ENET_RXCFG_ENFLOW_SHIFT)

/* Receive Maximum Length register */
#define ENET_RXMAXLEN_REG		0x4
#define ENET_RXMAXLEN_SHIFT		0
#define ENET_RXMAXLEN_MASK		(0x7ff << ENET_RXMAXLEN_SHIFT)

/* Transmit Maximum Length register */
#define ENET_TXMAXLEN_REG		0x8
#define ENET_TXMAXLEN_SHIFT		0
#define ENET_TXMAXLEN_MASK		(0x7ff << ENET_TXMAXLEN_SHIFT)

/* MII Status/Control register */
#define ENET_MIISC_REG			0x10
#define ENET_MIISC_MDCFREQDIV_SHIFT	0
#define ENET_MIISC_MDCFREQDIV_MASK	(0x7f << ENET_MIISC_MDCFREQDIV_SHIFT)
#define ENET_MIISC_PREAMBLEEN_SHIFT	7
#define ENET_MIISC_PREAMBLEEN_MASK	(1 << ENET_MIISC_PREAMBLEEN_SHIFT)

/* MII Data register */
#define ENET_MIID_REG			0x14
#define ENET_MIID_DATA_SHIFT		0
#define ENET_MIID_DATA_MASK		(0xffff << ENET_MIID_DATA_SHIFT)
#define ENET_MIID_TA_SHIFT		16
#define ENET_MIID_TA_MASK		(0x3 << ENET_MIID_TA_SHIFT)
#define ENET_MIID_REG_SHIFT		18
#define ENET_MIID_REG_MASK		(0x1f << ENET_MIID_REG_SHIFT)
#define ENET_MIID_PHY_SHIFT		23
#define ENET_MIID_PHY_MASK		(0x1f << ENET_MIID_PHY_SHIFT)
#define ENET_MIID_OP_SHIFT		28
#define ENET_MIID_OP_WRITE		(0x5 << ENET_MIID_OP_SHIFT)
#define ENET_MIID_OP_READ		(0x6 << ENET_MIID_OP_SHIFT)

/* Ethernet Interrupt Mask register */
#define ENET_IRMASK_REG			0x18

/* Ethernet Interrupt register */
#define ENET_IR_REG			0x1c
#define ENET_IR_MII			BIT(0)
#define ENET_IR_MIB			BIT(1)
#define ENET_IR_FLOWC			BIT(2)

/* Ethernet Control register */
#define ENET_CTL_REG			0x2c
#define ENET_CTL_ENABLE_SHIFT		0
#define ENET_CTL_ENABLE_MASK		(1 << ENET_CTL_ENABLE_SHIFT)
#define ENET_CTL_DISABLE_SHIFT		1
#define ENET_CTL_DISABLE_MASK		(1 << ENET_CTL_DISABLE_SHIFT)
#define ENET_CTL_SRESET_SHIFT		2
#define ENET_CTL_SRESET_MASK		(1 << ENET_CTL_SRESET_SHIFT)
#define ENET_CTL_EPHYSEL_SHIFT		3
#define ENET_CTL_EPHYSEL_MASK		(1 << ENET_CTL_EPHYSEL_SHIFT)

/* Transmit Control register */
#define ENET_TXCTL_REG			0x30
#define ENET_TXCTL_FD_SHIFT		0
#define ENET_TXCTL_FD_MASK		(1 << ENET_TXCTL_FD_SHIFT)

/* Transmit Watermask register */
#define ENET_TXWMARK_REG		0x34
#define ENET_TXWMARK_WM_SHIFT		0
#define ENET_TXWMARK_WM_MASK		(0x3f << ENET_TXWMARK_WM_SHIFT)

/* MIB Control register */
#define ENET_MIBCTL_REG			0x38
#define ENET_MIBCTL_RDCLEAR_SHIFT	0
#define ENET_MIBCTL_RDCLEAR_MASK	(1 << ENET_MIBCTL_RDCLEAR_SHIFT)

/* Perfect Match Data Low register */
#define ENET_PML_REG(x)			(0x58 + (x) * 8)
#define ENET_PMH_REG(x)			(0x5c + (x) * 8)
#define ENET_PMH_DATAVALID_SHIFT	16
#define ENET_PMH_DATAVALID_MASK		(1 << ENET_PMH_DATAVALID_SHIFT)

/* MIB register */
#define ENET_MIB_REG(x)			(0x200 + (x) * 4)
#define ENET_MIB_REG_COUNT		55

/*
 * TX transmit threshold (4 bytes unit), FIFO is 256 bytes, the value
 * must be low enough so that a DMA transfer of above burst length can
 * not overflow the fifo
 */
#define ENET_TX_FIFO_TRESH		32

struct bcm6348_emac {
	struct bcm6348_iudma *iudma;
	void __iomem *base;

	struct clk **clock;
	unsigned int num_clocks;

	struct reset_control **reset;
	unsigned int num_resets;

	int copybreak;

	int irq_rx;
	int irq_tx;

	/* hw view of rx & tx dma ring */
	dma_addr_t rx_desc_dma;
	dma_addr_t tx_desc_dma;

	/* allocated size (in bytes) for rx & tx dma ring */
	unsigned int rx_desc_alloc_size;
	unsigned int tx_desc_alloc_size;

	struct napi_struct napi;

	/* dma channel id for rx */
	int rx_chan;

	/* number of dma desc in rx ring */
	int rx_ring_size;

	/* cpu view of rx dma ring */
	struct bcm6348_iudma_desc *rx_desc_cpu;

	/* current number of armed descriptor given to hardware for rx */
	int rx_desc_count;

	/* next rx descriptor to fetch from hardware */
	int rx_curr_desc;

	/* next dirty rx descriptor to refill */
	int rx_dirty_desc;

	/* size of allocated rx skbs */
	unsigned int rx_skb_size;

	/* list of skb given to hw for rx */
	struct sk_buff **rx_skb;

	/* used when rx skb allocation failed, so we defer rx queue
	 * refill */
	struct timer_list rx_timeout;

	/* lock rx_timeout against rx normal operation */
	spinlock_t rx_lock;

	/* dma channel id for tx */
	int tx_chan;

	/* number of dma desc in tx ring */
	int tx_ring_size;

	/* cpu view of tx dma ring */
	struct bcm6348_iudma_desc *tx_desc_cpu;

	/* number of available descriptor for tx */
	int tx_desc_count;

	/* next tx descriptor avaiable */
	int tx_curr_desc;

	/* next dirty tx descriptor to reclaim */
	int tx_dirty_desc;

	/* list of skb given to hw for tx */
	struct sk_buff **tx_skb;

	/* lock used by tx reclaim and xmit */
	spinlock_t tx_lock;

	/* network device reference */
	struct net_device *net_dev;

	/* platform device reference */
	struct platform_device *pdev;

	/* external mii bus */
	bool ext_mii;

	/* phy */
	int old_link;
	int old_duplex;
	int old_pause;
};

static inline void emac_writel(struct bcm6348_emac *emac, u32 val, u32 off)
{
	__raw_writel(val, emac->base + off);
}

static inline u32 emac_readl(struct bcm6348_emac *emac, u32 off)
{
	return __raw_readl(emac->base + off);
}

/*
 * refill rx queue
 */
static int bcm6348_emac_refill_rx(struct net_device *ndev)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct bcm6348_iudma *iudma = emac->iudma;
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;

	while (emac->rx_desc_count < emac->rx_ring_size) {
		struct bcm6348_iudma_desc *desc;
		struct sk_buff *skb;
		dma_addr_t p;
		int desc_idx;
		u32 len_stat;

		desc_idx = emac->rx_dirty_desc;
		desc = &emac->rx_desc_cpu[desc_idx];

		if (!emac->rx_skb[desc_idx]) {
			skb = netdev_alloc_skb(ndev, emac->rx_skb_size);
			if (!skb)
				break;
			emac->rx_skb[desc_idx] = skb;
			p = dma_map_single(dev, skb->data, emac->rx_skb_size,
					   DMA_FROM_DEVICE);
			desc->address = p;
		}

		len_stat = emac->rx_skb_size << DMADESC_LENGTH_SHIFT;
		len_stat |= DMADESC_OWNER_MASK;
		if (emac->rx_dirty_desc == emac->rx_ring_size - 1) {
			len_stat |= DMADESC_WRAP_MASK;
			emac->rx_dirty_desc = 0;
		} else {
			emac->rx_dirty_desc++;
		}
		wmb();
		desc->len_stat = len_stat;

		emac->rx_desc_count++;

		/* tell dma engine we allocated one buffer */
		dma_writel(iudma, 1, DMA_BUFALLOC_REG(emac->rx_chan));
	}

	/* If rx ring is still empty, set a timer to try allocating
	 * again at a later time. */
	if (emac->rx_desc_count == 0 && netif_running(ndev)) {
		dev_warn(dev, "unable to refill rx ring\n");
		emac->rx_timeout.expires = jiffies + HZ;
		add_timer(&emac->rx_timeout);
	}

	return 0;
}

/*
 * timer callback to defer refill rx queue in case we're OOM
 */
static void bcm6348_emac_refill_rx_timer(struct timer_list *t)
{
	struct bcm6348_emac *emac = from_timer(emac, t, rx_timeout);
	struct net_device *ndev = emac->net_dev;

	spin_lock(&emac->rx_lock);
	bcm6348_emac_refill_rx(ndev);
	spin_unlock(&emac->rx_lock);
}

/*
 * extract packet from rx queue
 */
static int bcm6348_emac_receive_queue(struct net_device *ndev, int budget)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct bcm6348_iudma *iudma = emac->iudma;
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	int processed = 0;

	/* don't scan ring further than number of refilled
	 * descriptor */
	if (budget > emac->rx_desc_count)
		budget = emac->rx_desc_count;

	do {
		struct bcm6348_iudma_desc *desc;
		struct sk_buff *skb;
		int desc_idx;
		u32 len_stat;
		unsigned int len;

		desc_idx = emac->rx_curr_desc;
		desc = &emac->rx_desc_cpu[desc_idx];

		/* make sure we actually read the descriptor status at
		 * each loop */
		rmb();

		len_stat = desc->len_stat;

		/* break if dma ownership belongs to hw */
		if (len_stat & DMADESC_OWNER_MASK)
			break;

		processed++;
		emac->rx_curr_desc++;
		if (emac->rx_curr_desc == emac->rx_ring_size)
			emac->rx_curr_desc = 0;
		emac->rx_desc_count--;

		/* if the packet does not have start of packet _and_
		 * end of packet flag set, then just recycle it */
		if ((len_stat & DMADESC_ESOP_MASK) != DMADESC_ESOP_MASK) {
			ndev->stats.rx_dropped++;
			continue;
		}

		/* valid packet */
		skb = emac->rx_skb[desc_idx];
		len = (len_stat & DMADESC_LENGTH_MASK)
		      >> DMADESC_LENGTH_SHIFT;
		/* don't include FCS */
		len -= 4;

		if (len < emac->copybreak) {
			struct sk_buff *nskb;

			nskb = napi_alloc_skb(&emac->napi, len);
			if (!nskb) {
				/* forget packet, just rearm desc */
				ndev->stats.rx_dropped++;
				continue;
			}

			dma_sync_single_for_cpu(dev, desc->address,
						len, DMA_FROM_DEVICE);
			memcpy(nskb->data, skb->data, len);
			dma_sync_single_for_device(dev, desc->address,
						   len, DMA_FROM_DEVICE);
			skb = nskb;
		} else {
			dma_unmap_single(dev, desc->address,
					 emac->rx_skb_size, DMA_FROM_DEVICE);
			emac->rx_skb[desc_idx] = NULL;
		}

		skb_put(skb, len);
		skb->protocol = eth_type_trans(skb, ndev);
		ndev->stats.rx_packets++;
		ndev->stats.rx_bytes += len;
		netif_receive_skb(skb);
	} while (--budget > 0);

	if (processed || !emac->rx_desc_count) {
		bcm6348_emac_refill_rx(ndev);

		/* kick rx dma */
		dmac_writel(iudma, DMAC_CHANCFG_EN_MASK, DMAC_CHANCFG_REG,
			    emac->rx_chan);
	}

	return processed;
}

/*
 * try to or force reclaim of transmitted buffers
 */
static int bcm6348_emac_tx_reclaim(struct net_device *ndev, int force)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	int released = 0;

	while (emac->tx_desc_count < emac->tx_ring_size) {
		struct bcm6348_iudma_desc *desc;
		struct sk_buff *skb;

		/* We run in a bh and fight against start_xmit, which
		 * is called with bh disabled */
		spin_lock(&emac->tx_lock);

		desc = &emac->tx_desc_cpu[emac->tx_dirty_desc];

		if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
			spin_unlock(&emac->tx_lock);
			break;
		}

		/* ensure other field of the descriptor were not read
		 * before we checked ownership */
		rmb();

		skb = emac->tx_skb[emac->tx_dirty_desc];
		emac->tx_skb[emac->tx_dirty_desc] = NULL;
		dma_unmap_single(dev, desc->address, skb->len, DMA_TO_DEVICE);

		emac->tx_dirty_desc++;
		if (emac->tx_dirty_desc == emac->tx_ring_size)
			emac->tx_dirty_desc = 0;
		emac->tx_desc_count++;

		spin_unlock(&emac->tx_lock);

		if (desc->len_stat & DMADESC_UNDER_MASK)
			ndev->stats.tx_errors++;

		dev_kfree_skb(skb);
		released++;
	}

	if (netif_queue_stopped(ndev) && released)
		netif_wake_queue(ndev);

	return released;
}

static int bcm6348_emac_poll(struct napi_struct *napi, int budget)
{
	struct bcm6348_emac *emac = container_of(napi, struct bcm6348_emac,
						 napi);
	struct bcm6348_iudma *iudma = emac->iudma;
	struct net_device *ndev = emac->net_dev;
	int rx_work_done;

	/* ack interrupts */
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK, DMAC_IR_REG,
		    emac->rx_chan);
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK, DMAC_IR_REG,
		    emac->tx_chan);

	/* reclaim sent skb */
	bcm6348_emac_tx_reclaim(ndev, 0);

	spin_lock(&emac->rx_lock);
	rx_work_done = bcm6348_emac_receive_queue(ndev, budget);
	spin_unlock(&emac->rx_lock);

	if (rx_work_done >= budget) {
		/* rx queue is not yet empty/clean */
		return rx_work_done;
	}

	/* no more packet in rx/tx queue, remove device from poll
	 * queue */
	napi_complete_done(napi, rx_work_done);

	/* restore rx/tx interrupt */
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK, DMAC_IRMASK_REG,
		    emac->rx_chan);
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK, DMAC_IRMASK_REG,
		    emac->tx_chan);

	return rx_work_done;
}

/*
 * emac interrupt handler
 */
static irqreturn_t bcm6348_emac_isr_mac(int irq, void *dev_id)
{
	struct net_device *ndev = dev_id;
	struct bcm6348_emac *emac = netdev_priv(ndev);
	u32 stat;

	stat = emac_readl(emac, ENET_IR_REG);
	if (!(stat & ENET_IR_MIB))
		return IRQ_NONE;

	/* clear & mask interrupt */
	emac_writel(emac, ENET_IR_MIB, ENET_IR_REG);
	emac_writel(emac, 0, ENET_IRMASK_REG);

	return IRQ_HANDLED;
}

/*
 * rx/tx dma interrupt handler
 */
static irqreturn_t bcm6348_emac_isr_dma(int irq, void *dev_id)
{
	struct net_device *ndev = dev_id;
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct bcm6348_iudma *iudma = emac->iudma;

	/* mask rx/tx interrupts */
	dmac_writel(iudma, 0, DMAC_IRMASK_REG, emac->rx_chan);
	dmac_writel(iudma, 0, DMAC_IRMASK_REG, emac->tx_chan);

	napi_schedule(&emac->napi);

	return IRQ_HANDLED;
}

/*
 * tx request callback
 */
static netdev_tx_t bcm6348_emac_start_xmit(struct sk_buff *skb,
					   struct net_device *ndev)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct bcm6348_iudma *iudma = emac->iudma;
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	struct bcm6348_iudma_desc *desc;
	u32 len_stat;
	netdev_tx_t ret;

	/* lock against tx reclaim */
	spin_lock(&emac->tx_lock);

	/* make sure the tx hw queue is not full, should not happen
	 * since we stop queue before it's the case */
	if (unlikely(!emac->tx_desc_count)) {
		netif_stop_queue(ndev);
		dev_err(dev, "xmit called with no tx desc available?\n");
		ret = NETDEV_TX_BUSY;
		goto out_unlock;
	}

	/* point to the next available desc */
	desc = &emac->tx_desc_cpu[emac->tx_curr_desc];
	emac->tx_skb[emac->tx_curr_desc] = skb;

	/* fill descriptor */
	desc->address = dma_map_single(dev, skb->data, skb->len,
				       DMA_TO_DEVICE);

	len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
	len_stat |= DMADESC_ESOP_MASK | DMADESC_APPEND_CRC |
		    DMADESC_OWNER_MASK;

	emac->tx_curr_desc++;
	if (emac->tx_curr_desc == emac->tx_ring_size) {
		emac->tx_curr_desc = 0;
		len_stat |= DMADESC_WRAP_MASK;
	}
	emac->tx_desc_count--;

	/* dma might be already polling, make sure we update desc
	 * fields in correct order */
	wmb();
	desc->len_stat = len_stat;
	wmb();

	/* kick tx dma */
	dmac_writel(iudma, DMAC_CHANCFG_EN_MASK, DMAC_CHANCFG_REG,
		    emac->tx_chan);

	/* stop queue if no more desc available */
	if (!emac->tx_desc_count)
		netif_stop_queue(ndev);

	ndev->stats.tx_bytes += skb->len;
	ndev->stats.tx_packets++;
	ret = NETDEV_TX_OK;

out_unlock:
	spin_unlock(&emac->tx_lock);
	return ret;
}

/*
 * Change the interface's emac address.
 */
static int bcm6348_emac_set_mac_address(struct net_device *ndev, void *p)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct sockaddr *addr = p;
	u32 val;

	eth_hw_addr_set(ndev, addr->sa_data);

	/* use perfect match register 0 to store my emac address */
	val = (ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
		(ndev->dev_addr[4] << 8) | ndev->dev_addr[5];
	emac_writel(emac, val, ENET_PML_REG(0));

	val = (ndev->dev_addr[0] << 8 | ndev->dev_addr[1]);
	val |= ENET_PMH_DATAVALID_MASK;
	emac_writel(emac, val, ENET_PMH_REG(0));

	return 0;
}

/*
 * Change rx mode (promiscuous/allmulti) and update multicast list
 */
static void bcm6348_emac_set_multicast_list(struct net_device *ndev)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct netdev_hw_addr *ha;
	u32 val;
	unsigned int i;

	val = emac_readl(emac, ENET_RXCFG_REG);

	if (ndev->flags & IFF_PROMISC)
		val |= ENET_RXCFG_PROMISC_MASK;
	else
		val &= ~ENET_RXCFG_PROMISC_MASK;

	/* only 3 perfect match registers left, first one is used for
	 * own mac address */
	if ((ndev->flags & IFF_ALLMULTI) || netdev_mc_count(ndev) > 3)
		val |= ENET_RXCFG_ALLMCAST_MASK;
	else
		val &= ~ENET_RXCFG_ALLMCAST_MASK;

	/* no need to set perfect match registers if we catch all
	 * multicast */
	if (val & ENET_RXCFG_ALLMCAST_MASK) {
		emac_writel(emac, val, ENET_RXCFG_REG);
		return;
	}

	i = 0;
	netdev_for_each_mc_addr(ha, ndev) {
		u8 *dmi_addr;
		u32 tmp;

		if (i == 3)
			break;

		/* update perfect match registers */
		dmi_addr = ha->addr;
		tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
			(dmi_addr[4] << 8) | dmi_addr[5];
		emac_writel(emac, tmp, ENET_PML_REG(i + 1));

		tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
		tmp |= ENET_PMH_DATAVALID_MASK;
		emac_writel(emac, tmp, ENET_PMH_REG(i++ + 1));
	}

	for (; i < 3; i++) {
		emac_writel(emac, 0, ENET_PML_REG(i + 1));
		emac_writel(emac, 0, ENET_PMH_REG(i + 1));
	}

	emac_writel(emac, val, ENET_RXCFG_REG);
}

/*
 * disable emac
 */
static void bcm6348_emac_disable_mac(struct bcm6348_emac *emac)
{
	int limit;
	u32 val;

	val = emac_readl(emac, ENET_CTL_REG);
	val |= ENET_CTL_DISABLE_MASK;
	emac_writel(emac, val, ENET_CTL_REG);

	limit = 1000;
	do {
		val = emac_readl(emac, ENET_CTL_REG);
		if (!(val & ENET_CTL_DISABLE_MASK))
			break;
		udelay(1);
	} while (limit--);
}

/*
 * set emac duplex parameters
 */
static void bcm6348_emac_set_duplex(struct bcm6348_emac *emac, int fullduplex)
{
	u32 val;

	val = emac_readl(emac, ENET_TXCTL_REG);
	if (fullduplex)
		val |= ENET_TXCTL_FD_MASK;
	else
		val &= ~ENET_TXCTL_FD_MASK;
	emac_writel(emac, val, ENET_TXCTL_REG);
}

/*
 * set emac flow control parameters
 */
static void bcm6348_emac_set_flow(struct bcm6348_emac *emac, bool rx_en, bool tx_en)
{
	struct bcm6348_iudma *iudma = emac->iudma;
	u32 val;

	val = emac_readl(emac, ENET_RXCFG_REG);
	if (rx_en)
		val |= ENET_RXCFG_ENFLOW_MASK;
	else
		val &= ~ENET_RXCFG_ENFLOW_MASK;
	emac_writel(emac, val, ENET_RXCFG_REG);

	dmas_writel(iudma, emac->rx_desc_dma, DMAS_RSTART_REG, emac->rx_chan);
	dmas_writel(iudma, emac->tx_desc_dma, DMAS_RSTART_REG, emac->tx_chan);

	val = dma_readl(iudma, DMA_CFG_REG);
	if (tx_en)
		val |= DMA_CFG_FLOWCH_MASK(emac->rx_chan);
	else
		val &= ~DMA_CFG_FLOWCH_MASK(emac->rx_chan);
	dma_writel(iudma, val, DMA_CFG_REG);
}

/*
 * adjust emac phy
 */
static void bcm6348_emac_adjust_phy(struct net_device *ndev)
{
	struct phy_device *phydev = ndev->phydev;
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	bool status_changed = false;

	if (emac->old_link != phydev->link) {
		status_changed = true;
		emac->old_link = phydev->link;
	}

	if (phydev->link && phydev->duplex != emac->old_duplex) {
		bcm6348_emac_set_duplex(emac, phydev->duplex == DUPLEX_FULL);
		status_changed = true;
		emac->old_duplex = phydev->duplex;
	}

	if (phydev->link && phydev->pause != emac->old_pause) {
		bool rx_pause_en, tx_pause_en;

		if (phydev->pause) {
			rx_pause_en = true;
			tx_pause_en = true;
		} else {
			rx_pause_en = false;
			tx_pause_en = false;
		}

		bcm6348_emac_set_flow(emac, rx_pause_en, tx_pause_en);
		status_changed = true;
		emac->old_pause = phydev->pause;
	}

	if (status_changed)
		dev_info(dev, "%s: phy link %s %s/%s/%s/%s\n",
			 ndev->name,
			 phydev->link ? "UP" : "DOWN",
			 phy_modes(phydev->interface),
			 phy_speed_to_str(phydev->speed),
			 phy_duplex_to_str(phydev->duplex),
			 phydev->pause ? "rx/tx" : "off");
}


static int bcm6348_emac_open(struct net_device *ndev)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct bcm6348_iudma *iudma = emac->iudma;
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	struct sockaddr addr;
	unsigned int i, size;
	int ret;
	void *p;
	u32 val;

	/* mask all interrupts and request them */
	emac_writel(emac, 0, ENET_IRMASK_REG);
	dmac_writel(iudma, 0, DMAC_IRMASK_REG, emac->rx_chan);
	dmac_writel(iudma, 0, DMAC_IRMASK_REG, emac->tx_chan);

	ret = request_irq(ndev->irq, bcm6348_emac_isr_mac, 0, ndev->name,
			  ndev);
	if (ret)
		return ret;

	ret = request_irq(emac->irq_rx, bcm6348_emac_isr_dma,
			  0, ndev->name, ndev);
	if (ret)
		goto out_freeirq;

	ret = request_irq(emac->irq_tx, bcm6348_emac_isr_dma,
			  0, ndev->name, ndev);
	if (ret)
		goto out_freeirq_rx;

	/* initialize perfect match registers */
	for (i = 0; i < 4; i++) {
		emac_writel(emac, 0, ENET_PML_REG(i));
		emac_writel(emac, 0, ENET_PMH_REG(i));
	}

	/* write device mac address */
	memcpy(addr.sa_data, ndev->dev_addr, ETH_ALEN);
	bcm6348_emac_set_mac_address(ndev, &addr);

	/* allocate rx dma ring */
	size = emac->rx_ring_size * sizeof(struct bcm6348_iudma_desc);
	p = dma_alloc_coherent(dev, size, &emac->rx_desc_dma, GFP_KERNEL);
	if (!p) {
		dev_err(dev, "cannot allocate rx ring %u\n", size);
		ret = -ENOMEM;
		goto out_freeirq_tx;
	}

	memset(p, 0, size);
	emac->rx_desc_alloc_size = size;
	emac->rx_desc_cpu = p;

	/* allocate tx dma ring */
	size = emac->tx_ring_size * sizeof(struct bcm6348_iudma_desc);
	p = dma_alloc_coherent(dev, size, &emac->tx_desc_dma, GFP_KERNEL);
	if (!p) {
		dev_err(dev, "cannot allocate tx ring\n");
		ret = -ENOMEM;
		goto out_free_rx_ring;
	}

	memset(p, 0, size);
	emac->tx_desc_alloc_size = size;
	emac->tx_desc_cpu = p;

	emac->tx_skb = kzalloc(sizeof(struct sk_buff *) * emac->tx_ring_size,
			       GFP_KERNEL);
	if (!emac->tx_skb) {
		dev_err(dev, "cannot allocate rx skb queue\n");
		ret = -ENOMEM;
		goto out_free_tx_ring;
	}

	emac->tx_desc_count = emac->tx_ring_size;
	emac->tx_dirty_desc = 0;
	emac->tx_curr_desc = 0;
	spin_lock_init(&emac->tx_lock);

	/* init & fill rx ring with skbs */
	emac->rx_skb = kzalloc(sizeof(struct sk_buff *) * emac->rx_ring_size,
			       GFP_KERNEL);
	if (!emac->rx_skb) {
		dev_err(dev, "cannot allocate rx skb queue\n");
		ret = -ENOMEM;
		goto out_free_tx_skb;
	}

	emac->rx_desc_count = 0;
	emac->rx_dirty_desc = 0;
	emac->rx_curr_desc = 0;

	/* initialize flow control buffer allocation */
	dma_writel(iudma, DMA_BUFALLOC_FORCE_MASK | 0,
		   DMA_BUFALLOC_REG(emac->rx_chan));

	if (bcm6348_emac_refill_rx(ndev)) {
		dev_err(dev, "cannot allocate rx skb queue\n");
		ret = -ENOMEM;
		goto out;
	}

	/* write rx & tx ring addresses */
	dmas_writel(iudma, emac->rx_desc_dma,
		    DMAS_RSTART_REG, emac->rx_chan);
	dmas_writel(iudma, emac->tx_desc_dma,
		    DMAS_RSTART_REG, emac->tx_chan);

	/* clear remaining state ram for rx & tx channel */
	dmas_writel(iudma, 0, DMAS_SRAM2_REG, emac->rx_chan);
	dmas_writel(iudma, 0, DMAS_SRAM2_REG, emac->tx_chan);
	dmas_writel(iudma, 0, DMAS_SRAM3_REG, emac->rx_chan);
	dmas_writel(iudma, 0, DMAS_SRAM3_REG, emac->tx_chan);
	dmas_writel(iudma, 0, DMAS_SRAM4_REG, emac->rx_chan);
	dmas_writel(iudma, 0, DMAS_SRAM4_REG, emac->tx_chan);

	/* set max rx/tx length */
	emac_writel(emac, ndev->mtu, ENET_RXMAXLEN_REG);
	emac_writel(emac, ndev->mtu, ENET_TXMAXLEN_REG);

	/* set dma maximum burst len */
	dmac_writel(iudma, ENET_DMA_MAXBURST,
		    DMAC_MAXBURST_REG, emac->rx_chan);
	dmac_writel(iudma, ENET_DMA_MAXBURST,
		    DMAC_MAXBURST_REG, emac->tx_chan);

	/* set correct transmit fifo watermark */
	emac_writel(emac, ENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);

	/* set flow control low/high threshold to 1/3 / 2/3 */
	val = emac->rx_ring_size / 3;
	dma_writel(iudma, val, DMA_FLOWCL_REG(emac->rx_chan));
	val = (emac->rx_ring_size * 2) / 3;
	dma_writel(iudma, val, DMA_FLOWCH_REG(emac->rx_chan));

	/* all set, enable emac and interrupts, start dma engine and
	 * kick rx dma channel
	 */
	wmb();
	val = emac_readl(emac, ENET_CTL_REG);
	val |= ENET_CTL_ENABLE_MASK;
	emac_writel(emac, val, ENET_CTL_REG);
	dmac_writel(iudma, DMAC_CHANCFG_EN_MASK,
		    DMAC_CHANCFG_REG, emac->rx_chan);

	/* watch "mib counters about to overflow" interrupt */
	emac_writel(emac, ENET_IR_MIB, ENET_IR_REG);
	emac_writel(emac, ENET_IR_MIB, ENET_IRMASK_REG);

	/* watch "packet transferred" interrupt in rx and tx */
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK,
		    DMAC_IR_REG, emac->rx_chan);
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK,
		    DMAC_IR_REG, emac->tx_chan);

	/* make sure we enable napi before rx interrupt  */
	napi_enable(&emac->napi);

	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK,
		    DMAC_IRMASK_REG, emac->rx_chan);
	dmac_writel(iudma, DMAC_IR_PKTDONE_MASK,
		    DMAC_IRMASK_REG, emac->tx_chan);

	if (ndev->phydev)
		phy_start(ndev->phydev);

	netif_carrier_on(ndev);
	netif_start_queue(ndev);

	return 0;

out:
	for (i = 0; i < emac->rx_ring_size; i++) {
		struct bcm6348_iudma_desc *desc;

		if (!emac->rx_skb[i])
			continue;

		desc = &emac->rx_desc_cpu[i];
		dma_unmap_single(dev, desc->address, emac->rx_skb_size,
				 DMA_FROM_DEVICE);
		kfree_skb(emac->rx_skb[i]);
	}
	kfree(emac->rx_skb);

out_free_tx_skb:
	kfree(emac->tx_skb);

out_free_tx_ring:
	dma_free_coherent(dev, emac->tx_desc_alloc_size,
			  emac->tx_desc_cpu, emac->tx_desc_dma);

out_free_rx_ring:
	dma_free_coherent(dev, emac->rx_desc_alloc_size,
			  emac->rx_desc_cpu, emac->rx_desc_dma);

out_freeirq_tx:
	if (emac->irq_tx != -1)
		free_irq(emac->irq_tx, ndev);

out_freeirq_rx:
	free_irq(emac->irq_rx, ndev);

out_freeirq:
	if (ndev->phydev)
		phy_disconnect(ndev->phydev);

	return ret;
}

static int bcm6348_emac_stop(struct net_device *ndev)
{
	struct bcm6348_emac *emac = netdev_priv(ndev);
	struct bcm6348_iudma *iudma = emac->iudma;
	struct device *dev = &emac->pdev->dev;
	unsigned int i;

	netif_stop_queue(ndev);
	napi_disable(&emac->napi);
	if (ndev->phydev)
		phy_stop(ndev->phydev);
	del_timer_sync(&emac->rx_timeout);

	/* mask all interrupts */
	emac_writel(emac, 0, ENET_IRMASK_REG);
	dmac_writel(iudma, 0, DMAC_IRMASK_REG, emac->rx_chan);
	dmac_writel(iudma, 0, DMAC_IRMASK_REG, emac->tx_chan);

	/* disable dma & emac */
	bcm6348_iudma_chan_stop(iudma, emac->tx_chan);
	bcm6348_iudma_chan_stop(iudma, emac->rx_chan);
	bcm6348_emac_disable_mac(emac);

	/* force reclaim of all tx buffers */
	bcm6348_emac_tx_reclaim(ndev, 1);

	/* free the rx skb ring */
	for (i = 0; i < emac->rx_ring_size; i++) {
		struct bcm6348_iudma_desc *desc;

		if (!emac->rx_skb[i])
			continue;

		desc = &emac->rx_desc_cpu[i];
		dma_unmap_single_attrs(dev, desc->address, emac->rx_skb_size,
				       DMA_FROM_DEVICE,
				       DMA_ATTR_SKIP_CPU_SYNC);
		kfree_skb(emac->rx_skb[i]);
	}

	/* free remaining allocated memory */
	kfree(emac->rx_skb);
	kfree(emac->tx_skb);
	dma_free_coherent(dev, emac->rx_desc_alloc_size, emac->rx_desc_cpu,
			  emac->rx_desc_dma);
	dma_free_coherent(dev, emac->tx_desc_alloc_size, emac->tx_desc_cpu,
			  emac->tx_desc_dma);
	free_irq(emac->irq_tx, ndev);
	free_irq(emac->irq_rx, ndev);
	free_irq(ndev->irq, ndev);

	netdev_reset_queue(ndev);

	return 0;
}

static const struct net_device_ops bcm6348_emac_ops = {
	.ndo_open = bcm6348_emac_open,
	.ndo_stop = bcm6348_emac_stop,
	.ndo_start_xmit = bcm6348_emac_start_xmit,
	.ndo_set_mac_address = bcm6348_emac_set_mac_address,
	.ndo_set_rx_mode = bcm6348_emac_set_multicast_list,
};

static int bcm6348_emac_mdio_op(struct bcm6348_emac *emac, uint32_t data)
{
	int limit;

	/* Make sure mii interrupt status is cleared */
	emac_writel(emac, ENET_IR_MII, ENET_IR_REG);

	/* Issue mii op */
	emac_writel(emac, data, ENET_MIID_REG);
	wmb();

	/* busy wait on mii interrupt bit, with timeout */
	limit = 1000;
	do {
		if (emac_readl(emac, ENET_IR_REG) & ENET_IR_MII)
			break;
		udelay(1);
	} while (limit-- > 0);

	return (limit < 0) ? 1 : 0;
}

static int bcm6348_emac_mdio_read(struct mii_bus *bus, int phy_id, int loc)
{
	struct bcm6348_emac *emac = bus->priv;
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	uint32_t reg;

	reg = 0x2 << ENET_MIID_TA_SHIFT;
	reg |= loc << ENET_MIID_REG_SHIFT;
	reg |= phy_id << ENET_MIID_PHY_SHIFT;
	reg |= ENET_MIID_OP_READ;

	if (bcm6348_emac_mdio_op(emac, reg)) {
		dev_err(dev, "mdio_read: phy=%d loc=%x timeout!\n",
			phy_id, loc);
		return -EINVAL;
	}

	reg = emac_readl(emac, ENET_MIID_REG);
	reg = (reg >> ENET_MIID_DATA_SHIFT) & ENET_MIID_DATA_MASK;

	return (int) reg;
}

static int bcm6348_emac_mdio_write(struct mii_bus *bus, int phy_id,
				   int loc, uint16_t val)
{
	struct bcm6348_emac *emac = bus->priv;
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	uint32_t reg;

	reg = (val << ENET_MIID_DATA_SHIFT) & ENET_MIID_DATA_MASK;
	reg |= 0x2 << ENET_MIID_TA_SHIFT;
	reg |= loc << ENET_MIID_REG_SHIFT;
	reg |= phy_id << ENET_MIID_PHY_SHIFT;
	reg |= ENET_MIID_OP_WRITE;

	if (bcm6348_emac_mdio_op(emac, reg)) {
		dev_err(dev, "mdio_write: phy=%d loc=%x timeout!\n",
			phy_id, loc);
		return -EINVAL;
	}

	bcm6348_emac_mdio_op(emac, reg);

	return 0;
}

static int bcm6348_emac_mdio_init(struct bcm6348_emac *emac,
				  struct device_node *np)
{
	struct platform_device *pdev = emac->pdev;
	struct device *dev = &pdev->dev;
	struct device_node *mnp;
	struct mii_bus *mii_bus;
	int ret;

	mnp = of_get_child_by_name(np, "mdio");
	if (!mnp)
		return -ENODEV;

	mii_bus = devm_mdiobus_alloc(dev);
	if (!mii_bus) {
		of_node_put(mnp);
		return -ENOMEM;
	}

	mii_bus->priv = emac;
	mii_bus->name = np->full_name;
	snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(dev));
	mii_bus->parent = dev;
	mii_bus->read = bcm6348_emac_mdio_read;
	mii_bus->write = bcm6348_emac_mdio_write;
	mii_bus->phy_mask = 0x3f;

	ret = devm_of_mdiobus_register(dev, mii_bus, mnp);
	of_node_put(mnp);
	if (ret) {
		dev_err(dev, "MDIO bus registration failed\n");
		return ret;
	}

	dev_info(dev, "MDIO bus init\n");

	return 0;
}

/*
 * preinit hardware to allow mii operation while device is down
 */
static void bcm6348_emac_hw_preinit(struct bcm6348_emac *emac)
{
	u32 val;
	int limit;

	/* make sure emac is disabled */
	bcm6348_emac_disable_mac(emac);

	/* soft reset emac */
	val = ENET_CTL_SRESET_MASK;
	emac_writel(emac, val, ENET_CTL_REG);
	wmb();

	limit = 1000;
	do {
		val = emac_readl(emac, ENET_CTL_REG);
		if (!(val & ENET_CTL_SRESET_MASK))
			break;
		udelay(1);
	} while (limit--);

	/* select correct mii interface */
	val = emac_readl(emac, ENET_CTL_REG);
	if (emac->ext_mii)
		val |= ENET_CTL_EPHYSEL_MASK;
	else
		val &= ~ENET_CTL_EPHYSEL_MASK;
	emac_writel(emac, val, ENET_CTL_REG);

	/* turn on mdc clock */
	emac_writel(emac, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
		    ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);

	/* set mib counters to self-clear when read */
	val = emac_readl(emac, ENET_MIBCTL_REG);
	val |= ENET_MIBCTL_RDCLEAR_MASK;
	emac_writel(emac, val, ENET_MIBCTL_REG);
}

static int bcm6348_emac_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *node = dev->of_node;
	struct device_node *dma_node;
	struct platform_device *dma_pdev;
	struct bcm6348_emac *emac;
	struct bcm6348_iudma *iudma;
	struct net_device *ndev;
	unsigned i;
	int num_resets;
	int ret;

	dma_node = of_parse_phandle(node, "brcm,iudma", 0);
	if (!dma_node)
		return -EINVAL;

	dma_pdev = of_find_device_by_node(dma_node);
	of_node_put(dma_node);
	if (!dma_pdev)
		return -EINVAL;

	iudma = platform_get_drvdata(dma_pdev);
	if (!iudma)
		return -EPROBE_DEFER;

	ndev = devm_alloc_etherdev(dev, sizeof(*emac));
	if (!ndev)
		return -ENOMEM;

	platform_set_drvdata(pdev, ndev);
	SET_NETDEV_DEV(ndev, dev);

	emac = netdev_priv(ndev);
	emac->iudma = iudma;
	emac->pdev = pdev;
	emac->net_dev = ndev;

	emac->base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR_OR_NULL(emac->base))
		return PTR_ERR(emac->base);

	ndev->irq = of_irq_get_byname(node, "emac");
	if (!ndev->irq)
		return -ENODEV;

	emac->irq_rx = of_irq_get_byname(node, "rx");
	if (!emac->irq_rx)
		return -ENODEV;

	emac->irq_tx = of_irq_get_byname(node, "tx");
	if (!emac->irq_tx)
		return -ENODEV;

	if (of_property_read_u32(node, "dma-rx", &emac->rx_chan))
		return -ENODEV;

	if (of_property_read_u32(node, "dma-tx", &emac->tx_chan))
		return -ENODEV;

	emac->ext_mii = of_property_read_bool(node, "brcm,external-mii");

	emac->rx_ring_size = ENET_DEF_RX_DESC;
	emac->tx_ring_size = ENET_DEF_TX_DESC;
	emac->copybreak = ENET_DEF_CPY_BREAK;

	emac->old_link = 0;
	emac->old_duplex = -1;
	emac->old_pause = -1;

	of_get_mac_address(node, ndev->dev_addr);
	if (is_valid_ether_addr(ndev->dev_addr)) {
		dev_info(dev, "mtd mac %pM\n", ndev->dev_addr);
	} else {
		random_ether_addr(ndev->dev_addr);
		dev_info(dev, "random mac %pM\n", ndev->dev_addr);
	}

	emac->rx_skb_size = ALIGN(ndev->mtu + ENET_MTU_OVERHEAD,
				  ENET_DMA_MAXBURST * 4);

	emac->num_clocks = of_clk_get_parent_count(node);
	if (emac->num_clocks) {
		emac->clock = devm_kcalloc(dev, emac->num_clocks,
					   sizeof(struct clk *), GFP_KERNEL);
		if (IS_ERR_OR_NULL(emac->clock))
			return PTR_ERR(emac->clock);
	}
	for (i = 0; i < emac->num_clocks; i++) {
		emac->clock[i] = of_clk_get(node, i);
		if (IS_ERR_OR_NULL(emac->clock[i])) {
			dev_err(dev, "error getting emac clock %d\n", i);
			return PTR_ERR(emac->clock[i]);
		}

		ret = clk_prepare_enable(emac->clock[i]);
		if (ret) {
			dev_err(dev, "error enabling emac clock %d\n", i);
			return ret;
		}
	}

	num_resets = of_count_phandle_with_args(node, "resets",
						"#reset-cells");
	if (num_resets > 0)
		emac->num_resets = num_resets;
	else
		emac->num_resets = 0;
	if (emac->num_resets) {
		emac->reset = devm_kcalloc(dev, emac->num_resets,
					   sizeof(struct reset_control *),
					   GFP_KERNEL);
		if (IS_ERR_OR_NULL(emac->reset))
			return PTR_ERR(emac->reset);
		
	}
	for (i = 0; i < emac->num_resets; i++) {
		emac->reset[i] = devm_reset_control_get_by_index(dev, i);
		if (IS_ERR_OR_NULL(emac->reset[i])) {
			dev_err(dev, "error getting emac reset %d\n", i);
			return PTR_ERR(emac->reset[i]);
		}

		ret = reset_control_reset(emac->reset[i]);
		if (ret) {
			dev_err(dev, "error performing emac reset %d\n", i);
			return ret;
		}
	}

	/* do minimal hardware init to be able to probe mii bus */
	bcm6348_emac_hw_preinit(emac);

	ret = bcm6348_emac_mdio_init(emac, node);
	if (ret)
		return ret;

	spin_lock_init(&emac->rx_lock);

	timer_setup(&emac->rx_timeout, bcm6348_emac_refill_rx_timer, 0);

	/* zero mib counters */
	for (i = 0; i < ENET_MIB_REG_COUNT; i++)
		emac_writel(emac, 0, ENET_MIB_REG(i));

	/* register netdevice */
	ndev->netdev_ops = &bcm6348_emac_ops;
	ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
	ndev->mtu = ETH_DATA_LEN - VLAN_ETH_HLEN;
	ndev->max_mtu = ENET_MAX_MTU - VLAN_ETH_HLEN;
	netif_napi_add(ndev, &emac->napi, bcm6348_emac_poll, 16);
	SET_NETDEV_DEV(ndev, dev);

	ret = devm_register_netdev(dev, ndev);
	if (ret)
		goto out_disable_clk;

	netif_carrier_off(ndev);

	ndev->phydev = of_phy_get_and_connect(ndev, node,
					      bcm6348_emac_adjust_phy);
	if (IS_ERR_OR_NULL(ndev->phydev))
		dev_warn(dev, "PHY not found!\n");

	dev_info(dev, "%s at 0x%px, IRQ %d\n", ndev->name, emac->base,
		 ndev->irq);

	return 0;

out_disable_clk:
	for (i = 0; i < emac->num_resets; i++)
		reset_control_assert(emac->reset[i]);

	for (i = 0; i < emac->num_clocks; i++)
		clk_disable_unprepare(emac->clock[i]);

	return ret;
}

static int bcm6348_emac_remove(struct platform_device *pdev)
{
	struct net_device *ndev = platform_get_drvdata(pdev);
	struct bcm6348_emac *emac = netdev_priv(ndev);
	unsigned int i;

	emac_writel(emac, 0, ENET_MIISC_REG);

	for (i = 0; i < emac->num_resets; i++)
		reset_control_assert(emac->reset[i]);

	for (i = 0; i < emac->num_clocks; i++)
		clk_disable_unprepare(emac->clock[i]);

	return 0;
}

static const struct of_device_id bcm6348_emac_of_match[] = {
	{ .compatible = "brcm,bcm6338-emac", },
	{ .compatible = "brcm,bcm6348-emac", },
	{ .compatible = "brcm,bcm6358-emac", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, bcm6348_emac_of_match);

static struct platform_driver bcm6348_emac_driver = {
	.driver = {
		.name = "bcm6348-emac",
		.of_match_table = of_match_ptr(bcm6348_emac_of_match),
	},
	.probe	= bcm6348_emac_probe,
	.remove	= bcm6348_emac_remove,
};

int bcm6348_iudma_drivers_register(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	int ret;

	ret = platform_driver_register(&bcm6348_emac_driver);
	if (ret)
		dev_err(dev, "error registering emac driver!\n");

	return ret;
}

MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
MODULE_DESCRIPTION("BCM6348 Ethernet Controller Driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:bcm6348-enet");