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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Driver for Analog Devices, Inc. ADIN1140 10BASE-T1S MAC-PHY
*
* Copyright 2026 Analog Devices Inc.
*/
#include <linux/cleanup.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/mdio.h>
#include <linux/module.h>
#include <linux/oa_tc6.h>
#include <linux/phy.h>
#define ADIN1140_MAC_CONFIG2_STAT_CLR_RD BIT(6)
#define ADIN1140_MAC_CONFIG2_FWD_UNK2HOST BIT(2)
#define ADIN1140_MAC_P1_LOOP_ADDR_REG 0xC4
#define ADIN1140_MAC_ADDR_FILT_UPR_REG 0x50
#define ADIN1140_MAC_ADDR_FILT_APPLY2PORT1 BIT(30)
#define ADIN1140_MAC_ADDR_FILT_TO_HOST BIT(16)
#define ADIN1140_MAC_ADDR_FILT_LWR_REG 0x51
#define ADIN1140_MAC_ADDR_MASK_UPR_REG 0x70
#define ADIN1140_MAC_ADDR_MASK_LWR_REG 0x71
#define ADIN1140_MAC_FILT_MC_SLOT 0U
#define ADIN1140_MAC_FILT_BC_SLOT 1U
#define ADIN1140_MAC_FILT_UC_SLOT 2U
#define ADIN1140_MAC_FILT_MAX_SLOT 16U
#define ADIN1140_MAC_FILT_MASK_LIMIT 2U
#define ADIN1140_MAC_RX_FRAME_CNT 0xA1
#define ADIN1140_MAC_RX_BC_FRAME_CNT 0xA2
#define ADIN1140_MAC_RX_MC_FRAME_CNT 0xA3
#define ADIN1140_MAC_RX_CRC_ERR_CNT 0xA5
#define ADIN1140_MAC_RX_ALIGN_ERR_CNT 0xA6
#define ADIN1140_MAC_RX_PREAMBLE_ERR_CNT 0xA7
#define ADIN1140_MAC_RX_SHORT_ERR_CNT 0xA8
#define ADIN1140_MAC_RX_LONG_ERR_CNT 0xA9
#define ADIN1140_MAC_RX_PHY_ERR_CNT 0xAA
#define ADIN1140_MAC_RX_DRP_FULL_CNT 0xAB
#define ADIN1140_MAC_RX_DRP_FILTER_CNT 0xAD
#define ADIN1140_MAC_RX_IFG_ERR_CNT 0xAE
#define ADIN1140_MAC_TX_FRAME_CNT 0xB1
#define ADIN1140_MAC_TX_BC_FRAME_CNT 0xB2
#define ADIN1140_MAC_TX_MC_FRAME_CNT 0xB3
#define ADIN1140_MAC_TX_SINGLE_COL_CNT 0xB5
#define ADIN1140_MAC_TX_MULTI_COL_CNT 0xB6
#define ADIN1140_MAC_TX_DEFERRED_CNT 0xB7
#define ADIN1140_MAC_TX_LATE_COL_CNT 0xB8
#define ADIN1140_MAC_TX_EXCESS_COL_CNT 0xB9
#define ADIN1140_MAC_TX_UNDERRUN_CNT 0xBA
/* ADIN1140_MAC_FILT_MAX_SLOT - 3 (multicast, broadcast and unicast
* reserved slots)
*/
#define ADIN1140_MAC_FILT_AVAIL 13U
#define ADIN1140_PHY_CTRL_DEFAULT 0x1000
#define ADIN1140_PHY_STATUS_DEFAULT 0x082D
#define ADIN1140_PHY_ID1 0x0283
#define ADIN1140_PHY_ID2 0xBE00
#define ADIN1140_STATS_CHECK_DELAY (3 * HZ)
enum adin1140_statistics_entry {
rx_frames,
rx_bc_frames,
rx_mc_frames,
rx_crc_errors,
rx_align_errors,
rx_preamble_errors,
rx_short_frame_errors,
rx_long_frame_errors,
rx_phy_errors,
rx_fifo_full_dropped,
rx_addr_filter_dropped,
rx_ifg_errors,
tx_frames,
tx_bc_frames,
tx_mc_frames,
tx_single_collision,
tx_multi_collision,
tx_deferred,
tx_late_collision,
tx_excess_collision,
tx_underrun,
ADIN1140_STATS_CNT,
};
struct adin1140_statistics_reg {
const char *name;
enum adin1140_statistics_entry idx;
};
struct adin1140_priv {
struct net_device *netdev;
struct oa_tc6 *tc6;
struct mii_bus *mdiobus;
struct phy_device *phydev;
struct delayed_work stats_work;
/* Protects stats[] from concurrent updates in adin1140_stats_work
* and reads in the get_stats functions
*/
spinlock_t stat_lock;
u64 stats[ADIN1140_STATS_CNT];
};
static const u32 adin1140_stat_regs[] = {
[rx_frames] = ADIN1140_MAC_RX_FRAME_CNT,
[rx_bc_frames] = ADIN1140_MAC_RX_BC_FRAME_CNT,
[rx_mc_frames] = ADIN1140_MAC_RX_MC_FRAME_CNT,
[rx_crc_errors] = ADIN1140_MAC_RX_CRC_ERR_CNT,
[rx_align_errors] = ADIN1140_MAC_RX_ALIGN_ERR_CNT,
[rx_preamble_errors] = ADIN1140_MAC_RX_PREAMBLE_ERR_CNT,
[rx_short_frame_errors] = ADIN1140_MAC_RX_SHORT_ERR_CNT,
[rx_long_frame_errors] = ADIN1140_MAC_RX_LONG_ERR_CNT,
[rx_phy_errors] = ADIN1140_MAC_RX_PHY_ERR_CNT,
[rx_fifo_full_dropped] = ADIN1140_MAC_RX_DRP_FULL_CNT,
[rx_addr_filter_dropped] = ADIN1140_MAC_RX_DRP_FILTER_CNT,
[rx_ifg_errors] = ADIN1140_MAC_RX_IFG_ERR_CNT,
[tx_frames] = ADIN1140_MAC_TX_FRAME_CNT,
[tx_bc_frames] = ADIN1140_MAC_TX_BC_FRAME_CNT,
[tx_mc_frames] = ADIN1140_MAC_TX_MC_FRAME_CNT,
[tx_single_collision] = ADIN1140_MAC_TX_SINGLE_COL_CNT,
[tx_multi_collision] = ADIN1140_MAC_TX_MULTI_COL_CNT,
[tx_deferred] = ADIN1140_MAC_TX_DEFERRED_CNT,
[tx_late_collision] = ADIN1140_MAC_TX_LATE_COL_CNT,
[tx_excess_collision] = ADIN1140_MAC_TX_EXCESS_COL_CNT,
[tx_underrun] = ADIN1140_MAC_TX_UNDERRUN_CNT,
};
static const struct adin1140_statistics_reg adin1140_stats[] = {
{.name = "rx_preamble_errors", .idx = rx_preamble_errors},
{.name = "rx_ifg_errors", .idx = rx_ifg_errors},
};
static int adin1140_mac_filter_set(struct adin1140_priv *priv,
const u8 *addr, const u8 *mask,
u8 slot)
{
u32 reg_address;
u32 val;
int ret;
if (slot >= ADIN1140_MAC_FILT_MAX_SLOT)
return -ENOSPC;
reg_address = ADIN1140_MAC_ADDR_FILT_UPR_REG + 2 * slot;
ret = oa_tc6_write_register_mms(priv->tc6, OA_TC6_MAC_MMS1,
reg_address,
get_unaligned_be16(&addr[0]) |
ADIN1140_MAC_ADDR_FILT_APPLY2PORT1 |
ADIN1140_MAC_ADDR_FILT_TO_HOST);
if (ret)
return ret;
reg_address = ADIN1140_MAC_ADDR_FILT_LWR_REG + 2 * slot;
ret = oa_tc6_write_register_mms(priv->tc6, OA_TC6_MAC_MMS1,
reg_address,
get_unaligned_be32(&addr[2]));
if (ret)
return ret;
/* Only the first 2 destination MAC filter slots support masking.
* For the other entries, the destination address in the received
* frame must match exactly.
*/
if (slot >= ADIN1140_MAC_FILT_MASK_LIMIT)
return 0;
val = get_unaligned_be16(&mask[0]);
reg_address = ADIN1140_MAC_ADDR_MASK_UPR_REG + (2 * slot);
ret = oa_tc6_write_register_mms(priv->tc6, OA_TC6_MAC_MMS1,
reg_address, val);
if (ret)
return ret;
val = get_unaligned_be32(&mask[2]);
reg_address = ADIN1140_MAC_ADDR_MASK_LWR_REG + (2 * slot);
return oa_tc6_write_register_mms(priv->tc6, OA_TC6_MAC_MMS1,
reg_address, val);
}
static int adin1140_mac_filter_clear(struct adin1140_priv *priv, u8 slot)
{
u8 mask[ETH_ALEN];
u8 addr[ETH_ALEN];
memset(mask, 0xFF, ETH_ALEN);
memset(addr, 0x0, ETH_ALEN);
return adin1140_mac_filter_set(priv, addr, mask, slot);
}
static int adin1140_filter_unicast(struct adin1140_priv *priv)
{
/* Only the first 2 filter slots support masking, so no unicast
* address will ever need a mask. The first slots are used for the
* all multicast and broadcast filter.
*/
return adin1140_mac_filter_set(priv, priv->netdev->dev_addr, NULL,
ADIN1140_MAC_FILT_UC_SLOT);
}
static int adin1140_filter_all_multicast(struct adin1140_priv *priv, bool en)
{
u8 multicast_addr[ETH_ALEN] = {1, 0, 0, 0, 0, 0};
if (en)
return adin1140_mac_filter_set(priv, multicast_addr,
multicast_addr,
ADIN1140_MAC_FILT_MC_SLOT);
return adin1140_mac_filter_clear(priv, ADIN1140_MAC_FILT_MC_SLOT);
}
static int adin1140_filter_broadcast(struct adin1140_priv *priv, bool enabled)
{
u8 mask[ETH_ALEN];
if (enabled) {
memset(mask, 0xFF, ETH_ALEN);
return adin1140_mac_filter_set(priv, mask, mask,
ADIN1140_MAC_FILT_BC_SLOT);
}
return adin1140_mac_filter_clear(priv, ADIN1140_MAC_FILT_BC_SLOT);
}
static int adin1140_default_filter_config(struct adin1140_priv *priv)
{
int ret;
ret = adin1140_filter_broadcast(priv, true);
if (ret)
return ret;
return adin1140_filter_unicast(priv);
}
static int adin1140_promiscuous_mode(struct adin1140_priv *priv, bool enabled)
{
int ret;
u32 val;
ret = oa_tc6_read_register(priv->tc6, OA_TC6_REG_CONFIG2, &val);
if (ret)
return ret;
if (enabled)
val |= ADIN1140_MAC_CONFIG2_FWD_UNK2HOST;
else
val &= ~ADIN1140_MAC_CONFIG2_FWD_UNK2HOST;
return oa_tc6_write_register(priv->tc6, OA_TC6_REG_CONFIG2, val);
}
static int adin1140_rx_mode(struct net_device *dev,
struct netdev_hw_addr_list *uc,
struct netdev_hw_addr_list *mc)
{
struct adin1140_priv *priv = netdev_priv(dev);
struct netdev_hw_addr *ha;
bool all_multi, promisc;
u32 mac_addrs;
u8 slot, i;
int ret;
/* The ADIN1140 has 16 dest MAC address filter slots:
* 0 - reserved for all multicast filter.
* 1 - reserved for broadcast filter.
* 2 - reserved for the device's own unicast MAC.
* 3 -> 15 - available for other unicast/multicast filters.
*/
mac_addrs = netdev_hw_addr_list_count(uc);
all_multi = false;
promisc = false;
if (priv->netdev->flags & IFF_PROMISC)
promisc = true;
else if (priv->netdev->flags & IFF_ALLMULTI)
all_multi = true;
else
mac_addrs += netdev_hw_addr_list_count(mc);
if (mac_addrs > ADIN1140_MAC_FILT_AVAIL) {
/* The filter table is full. Enable promisc mode. */
promisc = true;
}
ret = adin1140_promiscuous_mode(priv, promisc);
if (ret)
return ret;
ret = adin1140_filter_all_multicast(priv, all_multi);
if (ret)
return ret;
slot = ADIN1140_MAC_FILT_UC_SLOT + 1;
if (!promisc) {
netdev_hw_addr_list_for_each(ha, uc) {
ret = adin1140_mac_filter_set(priv, ha->addr, NULL,
slot);
if (ret)
return ret;
slot++;
}
if (!all_multi) {
netdev_hw_addr_list_for_each(ha, mc) {
ret = adin1140_mac_filter_set(priv, ha->addr,
NULL, slot);
if (ret)
return ret;
slot++;
}
}
}
for (i = slot; i < ADIN1140_MAC_FILT_MAX_SLOT; i++) {
ret = adin1140_mac_filter_clear(priv, i);
if (ret)
return ret;
}
return 0;
}
static void adin1140_stats_work(struct work_struct *work)
{
struct delayed_work *dwork = to_delayed_work(work);
struct adin1140_priv *priv;
u32 reg_val;
int ret;
u32 i;
priv = container_of(dwork, struct adin1140_priv, stats_work);
for (i = 0; i < ARRAY_SIZE(adin1140_stat_regs); i++) {
ret = oa_tc6_read_register_mms(priv->tc6, OA_TC6_MAC_MMS1,
adin1140_stat_regs[i],
®_val);
if (ret)
goto out;
scoped_guard(spinlock, &priv->stat_lock)
priv->stats[i] += reg_val;
}
out:
schedule_delayed_work(dwork, ADIN1140_STATS_CHECK_DELAY);
}
static int adin1140_configure(struct adin1140_priv *priv)
{
u32 reg_val;
int ret;
ret = oa_tc6_zero_align_receive_frame_enable(priv->tc6);
if (ret)
return ret;
/* Disable MAC loopback */
ret = oa_tc6_write_register_mms(priv->tc6, OA_TC6_MAC_MMS1,
ADIN1140_MAC_P1_LOOP_ADDR_REG, 0x0);
if (ret)
return ret;
ret = oa_tc6_read_register(priv->tc6, OA_TC6_REG_CONFIG2, ®_val);
if (ret)
return ret;
reg_val |= ADIN1140_MAC_CONFIG2_STAT_CLR_RD;
ret = oa_tc6_write_register(priv->tc6, OA_TC6_REG_CONFIG2, reg_val);
if (ret)
return ret;
return adin1140_default_filter_config(priv);
}
static int adin1140_open(struct net_device *netdev)
{
struct adin1140_priv *priv = netdev_priv(netdev);
schedule_delayed_work(&priv->stats_work, ADIN1140_STATS_CHECK_DELAY);
phy_start(netdev->phydev);
netif_start_queue(netdev);
return 0;
}
static int adin1140_close(struct net_device *netdev)
{
struct adin1140_priv *priv = netdev_priv(netdev);
cancel_delayed_work_sync(&priv->stats_work);
netif_stop_queue(netdev);
phy_stop(netdev->phydev);
return 0;
}
static netdev_tx_t adin1140_start_xmit(struct sk_buff *skb,
struct net_device *netdev)
{
struct adin1140_priv *priv = netdev_priv(netdev);
/* The MAC doesn't automatically pad the frame to a 60 byte minimum
* size in case the host sends a shorter skb, so we have to do it in
* the driver. The FCS will be added by the MAC.
*/
if (skb_put_padto(skb, ETH_ZLEN))
return NETDEV_TX_OK;
return oa_tc6_start_xmit(priv->tc6, skb);
}
static int adin1140_set_mac_address(struct net_device *netdev, void *addr)
{
struct adin1140_priv *priv = netdev_priv(netdev);
struct sockaddr *address = addr;
u8 mask[ETH_ALEN];
int ret;
ret = eth_prepare_mac_addr_change(netdev, addr);
if (ret < 0)
return ret;
if (ether_addr_equal(address->sa_data, netdev->dev_addr))
return 0;
memset(mask, 0xFF, ETH_ALEN);
ret = adin1140_mac_filter_set(priv, address->sa_data, mask,
ADIN1140_MAC_FILT_UC_SLOT);
if (ret)
return ret;
eth_commit_mac_addr_change(netdev, addr);
return 0;
}
static void __adin1140_ndo_get_stats64(struct adin1140_priv *priv,
struct rtnl_link_stats64 *storage)
{
storage->rx_errors = priv->stats[rx_crc_errors] +
priv->stats[rx_align_errors] +
priv->stats[rx_preamble_errors] +
priv->stats[rx_short_frame_errors] +
priv->stats[rx_long_frame_errors] +
priv->stats[rx_phy_errors] +
priv->stats[rx_ifg_errors];
storage->tx_errors = priv->stats[tx_excess_collision] +
priv->stats[tx_underrun];
storage->rx_dropped = priv->stats[rx_fifo_full_dropped] +
priv->stats[rx_addr_filter_dropped];
storage->multicast = priv->stats[rx_mc_frames];
storage->collisions = priv->stats[tx_single_collision] +
priv->stats[tx_multi_collision];
storage->rx_length_errors = priv->stats[rx_short_frame_errors] +
priv->stats[rx_long_frame_errors];
storage->rx_over_errors = priv->stats[rx_fifo_full_dropped];
storage->rx_crc_errors = priv->stats[rx_crc_errors];
storage->rx_frame_errors = priv->stats[rx_align_errors];
storage->rx_missed_errors = priv->stats[rx_fifo_full_dropped];
storage->tx_aborted_errors = priv->stats[tx_excess_collision];
storage->tx_fifo_errors = priv->stats[tx_underrun];
storage->tx_window_errors = priv->stats[tx_late_collision];
}
static void adin1140_ndo_get_stats64(struct net_device *dev,
struct rtnl_link_stats64 *storage)
{
struct adin1140_priv *priv = netdev_priv(dev);
storage->rx_packets = priv->netdev->stats.rx_packets;
storage->tx_packets = priv->netdev->stats.tx_packets;
storage->rx_bytes = priv->netdev->stats.rx_bytes;
storage->tx_bytes = priv->netdev->stats.tx_bytes;
scoped_guard(spinlock, &priv->stat_lock)
__adin1140_ndo_get_stats64(priv, storage);
}
static void adin1140_get_drvinfo(struct net_device *netdev,
struct ethtool_drvinfo *info)
{
strscpy(info->driver, "ADIN1140", sizeof(info->driver));
strscpy(info->bus_info, dev_name(netdev->dev.parent),
sizeof(info->bus_info));
}
static void adin1140_get_ethtool_stats(struct net_device *netdev,
struct ethtool_stats *stats, u64 *data)
{
struct adin1140_priv *priv = netdev_priv(netdev);
u32 i;
scoped_guard(spinlock, &priv->stat_lock) {
for (i = 0; i < ARRAY_SIZE(adin1140_stats); i++)
data[i] = priv->stats[adin1140_stats[i].idx];
}
}
static void adin1140_get_ethtool_strings(struct net_device *netdev, u32 sset,
u8 *p)
{
u32 i;
switch (sset) {
case ETH_SS_STATS:
for (i = 0; i < ARRAY_SIZE(adin1140_stats); i++)
ethtool_puts(&p, adin1140_stats[i].name);
break;
}
}
static int adin1140_get_sset_count(struct net_device *netdev, int sset)
{
switch (sset) {
case ETH_SS_STATS:
return ARRAY_SIZE(adin1140_stats);
default:
return -EOPNOTSUPP;
}
}
static void __adin1140_eth_mac_stats(struct adin1140_priv *priv,
struct ethtool_eth_mac_stats *mac_stats)
{
mac_stats->FramesReceivedOK = priv->stats[rx_frames];
mac_stats->BroadcastFramesReceivedOK = priv->stats[rx_bc_frames];
mac_stats->MulticastFramesReceivedOK = priv->stats[rx_mc_frames];
mac_stats->FrameCheckSequenceErrors = priv->stats[rx_crc_errors];
mac_stats->AlignmentErrors = priv->stats[rx_align_errors];
mac_stats->FrameTooLongErrors = priv->stats[rx_long_frame_errors];
mac_stats->FramesLostDueToIntMACRcvError =
priv->stats[rx_fifo_full_dropped];
mac_stats->FramesTransmittedOK = priv->stats[tx_frames];
mac_stats->BroadcastFramesXmittedOK = priv->stats[tx_bc_frames];
mac_stats->MulticastFramesXmittedOK = priv->stats[tx_mc_frames];
mac_stats->SingleCollisionFrames = priv->stats[tx_single_collision];
mac_stats->MultipleCollisionFrames = priv->stats[tx_multi_collision];
mac_stats->FramesWithDeferredXmissions = priv->stats[tx_deferred];
mac_stats->LateCollisions = priv->stats[tx_late_collision];
mac_stats->FramesAbortedDueToXSColls =
priv->stats[tx_excess_collision];
mac_stats->FramesLostDueToIntMACXmitError = priv->stats[tx_underrun];
}
static void adin1140_get_eth_mac_stats(struct net_device *netdev,
struct ethtool_eth_mac_stats *mac_stats)
{
struct adin1140_priv *priv = netdev_priv(netdev);
scoped_guard(spinlock, &priv->stat_lock)
__adin1140_eth_mac_stats(priv, mac_stats);
}
static int adin1140_mdiobus_read(struct mii_bus *bus, int addr, int regnum)
{
/* The ADIN1140's standard PHY C22 register map (OA TC6 0xFF00 -
* 0xFF1F), of which only 0xFF00 - 0xFF03 are implemented) cannot be
* accessed while frames are being received by the PHY. In case this
* happens the CONFIG0 and CONFIG2 register values will get corrupted,
* getting a random value. Both reads and writes cause the same
* behavior. This is a workaround that avoids MDIO accesses all
* together. Since this is a 10BASE-T1S PHY, only the loopback and
* reset (AN) bits in the control register (0x0) can be written.
* These functionalities have custom implementations in the PHY
* driver. C45 accesses do not cause this issue.
*/
switch (regnum) {
case MII_BMCR:
return ADIN1140_PHY_CTRL_DEFAULT;
case MII_BMSR:
return ADIN1140_PHY_STATUS_DEFAULT;
case MII_PHYSID1:
return ADIN1140_PHY_ID1;
case MII_PHYSID2:
return ADIN1140_PHY_ID2;
default:
return 0xFFFF;
}
}
static int adin1140_mdiobus_write(struct mii_bus *bus, int addr, int regnum,
u16 val)
{
return -EIO;
}
static int adin1140_mdio_register(struct adin1140_priv *priv,
struct spi_device *spidev)
{
priv->mdiobus = devm_mdiobus_alloc(&spidev->dev);
if (!priv->mdiobus)
return dev_err_probe(&spidev->dev, -ENOMEM,
"MDIO bus alloc failed\n");
priv->mdiobus->name = "adin1140-mdiobus";
priv->mdiobus->priv = priv->tc6;
priv->mdiobus->parent = &spidev->dev;
priv->mdiobus->phy_mask = GENMASK(31, 1);
priv->mdiobus->read = adin1140_mdiobus_read;
priv->mdiobus->write = adin1140_mdiobus_write;
priv->mdiobus->read_c45 = oa_tc6_mdiobus_read_c45;
priv->mdiobus->write_c45 = oa_tc6_mdiobus_write_c45;
snprintf(priv->mdiobus->id, MII_BUS_ID_SIZE, "adin1140-%s.%u",
dev_name(&spidev->dev), spi_get_chipselect(spidev, 0));
return devm_mdiobus_register(&spidev->dev, priv->mdiobus);
}
static void adin1140_handle_link_change(struct net_device *netdev)
{
phy_print_status(netdev->phydev);
}
static void adin1140_phy_remove(void *data)
{
phy_disconnect(data);
}
static int adin1140_phy_init(struct adin1140_priv *priv,
struct spi_device *spidev)
{
int ret;
ret = adin1140_mdio_register(priv, spidev);
if (ret)
return ret;
priv->phydev = phy_find_first(priv->mdiobus);
if (!priv->phydev)
return dev_err_probe(&spidev->dev, -ENODEV, "No PHY found\n");
priv->phydev->is_internal = true;
ret = phy_connect_direct(priv->netdev, priv->phydev,
&adin1140_handle_link_change,
PHY_INTERFACE_MODE_INTERNAL);
if (ret)
return dev_err_probe(&spidev->dev, ret,
"Can't attach PHY to %s\n",
priv->mdiobus->id);
ret = devm_add_action_or_reset(&spidev->dev, adin1140_phy_remove,
priv->phydev);
if (ret)
return ret;
phy_attached_info(priv->phydev);
return 0;
}
static const struct ethtool_ops adin1140_ethtool_ops = {
.get_drvinfo = adin1140_get_drvinfo,
.get_link = ethtool_op_get_link,
.get_ethtool_stats = adin1140_get_ethtool_stats,
.get_sset_count = adin1140_get_sset_count,
.get_strings = adin1140_get_ethtool_strings,
.get_link_ksettings = phy_ethtool_get_link_ksettings,
.set_link_ksettings = phy_ethtool_set_link_ksettings,
.get_eth_mac_stats = adin1140_get_eth_mac_stats,
};
static const struct net_device_ops adin1140_netdev_ops = {
.ndo_open = adin1140_open,
.ndo_stop = adin1140_close,
.ndo_start_xmit = adin1140_start_xmit,
.ndo_set_mac_address = adin1140_set_mac_address,
.ndo_validate_addr = eth_validate_addr,
.ndo_set_rx_mode_async = adin1140_rx_mode,
.ndo_eth_ioctl = phy_do_ioctl_running,
.ndo_get_stats64 = adin1140_ndo_get_stats64,
};
static void adin1140_oa_tc6_remove(void *data)
{
oa_tc6_exit(data);
}
static int adin1140_probe(struct spi_device *spi)
{
struct oa_tc6_quirks tc6_quirks = {};
struct net_device *netdev;
struct adin1140_priv *priv;
int ret;
netdev = devm_alloc_etherdev(&spi->dev, sizeof(struct adin1140_priv));
if (!netdev)
return -ENOMEM;
priv = netdev_priv(netdev);
priv->netdev = netdev;
spi_set_drvdata(spi, priv);
spin_lock_init(&priv->stat_lock);
tc6_quirks.quirk_flags = OA_TC6_BROKEN_PHY;
priv->tc6 = oa_tc6_init(spi, netdev, &tc6_quirks);
if (!priv->tc6)
return -ENODEV;
ret = devm_add_action_or_reset(&spi->dev, adin1140_oa_tc6_remove,
priv->tc6);
if (ret)
return ret;
ret = adin1140_phy_init(priv, spi);
if (ret)
return ret;
if (device_get_ethdev_address(&spi->dev, netdev))
eth_hw_addr_random(netdev);
ret = adin1140_configure(priv);
if (ret)
return ret;
INIT_DELAYED_WORK(&priv->stats_work, adin1140_stats_work);
netdev->if_port = IF_PORT_10BASET;
netdev->irq = spi->irq;
netdev->netdev_ops = &adin1140_netdev_ops;
netdev->ethtool_ops = &adin1140_ethtool_ops;
netdev->netns_immutable = true;
netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE |
IFF_UNICAST_FLT;
ret = devm_register_netdev(&spi->dev, netdev);
if (ret)
return dev_err_probe(&spi->dev, ret,
"Failed to register netdev");
return 0;
}
static const struct spi_device_id adin1140_spi_id[] = {
{ .name = "ad3306" },
{ .name = "adin1140" },
{},
};
MODULE_DEVICE_TABLE(spi, adin1140_spi_id);
static const struct of_device_id adin1140_match_table[] = {
{ .compatible = "adi,ad3306" },
{ .compatible = "adi,adin1140" },
{ }
};
MODULE_DEVICE_TABLE(of, adin1140_match_table);
static struct spi_driver adin1140_driver = {
.driver = {
.name = "adin1140",
.of_match_table = adin1140_match_table,
},
.probe = adin1140_probe,
.id_table = adin1140_spi_id,
};
module_spi_driver(adin1140_driver);
MODULE_DESCRIPTION("Analog Devices, Inc. ADIN1140 10BASE-T1S MAC-PHY");
MODULE_AUTHOR("Ciprian Regus <ciprian.regus@analog.com>");
MODULE_LICENSE("GPL");
|