forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_adin2111.c
1593 lines (1340 loc) · 41.7 KB
/
eth_adin2111.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023 PHOENIX CONTACT Electronics GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(eth_adin2111, CONFIG_ETHERNET_LOG_LEVEL);
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/phy.h>
#if CONFIG_ETH_ADIN2111_SPI_CFG0
#include <zephyr/sys/crc.h>
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
#include <string.h>
#include <errno.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/phy.h>
#include <zephyr/drivers/ethernet/eth_adin2111.h>
#include "phy/phy_adin2111_priv.h"
#include "eth_adin2111_priv.h"
#define DT_DRV_COMPAT adi_adin2111
/* SPI Communication check retry delay */
#define ADIN2111_DEV_AWAIT_DELAY_POLL_US 100U
/* Number of retries SPI Communication check */
#define ADIN2111_DEV_AWAIT_RETRY_COUNT 200U
/* ADIN RESETC check retry delay */
#define ADIN2111_RESETC_AWAIT_DELAY_POLL_US 100U
/* Number of retries for ADIN RESETC check */
#define ADIN2111_RESETC_AWAIT_RETRY_COUNT 200U
/* Boot delay for clocks stabilisation (maximum 90ms) */
#define ADIN2111_HW_BOOT_DELAY_MS 100
/* MAC Address Rule and DA Filter multicast slot/idx */
#define ADIN2111_MULTICAST_ADDR_SLOT 0U
/* MAC Address Rule and DA Filter broadcast slot/idx */
#define ADIN2111_BROADCAST_ADDR_SLOT 1U
/* MAC Address Rule and DA Filter Port 1 slot/idx */
#define ADIN2111_UNICAST_P1_ADDR_SLOT 2U
/* MAC Address Rule and DA Filter Port 2 slot/idx */
#define ADIN2111_UNICAST_P2_ADDR_SLOT 3U
/* Free slots for further filtering */
#define ADIN2111_FILTER_FIRST_SLOT 4U
#define ADIN2111_FILTER_SLOTS 16U
/* As per RM rev. A table 3, t3 >= 50ms, delay for SPI interface to be ready */
#define ADIN2111_SPI_ACTIVE_DELAY_MS 50U
/* As per RM rev. A page 20: approximately 10 ms (maximum) for internal logic to be ready. */
#define ADIN2111_SW_RESET_DELAY_MS 10U
int eth_adin2111_mac_reset(const struct device *dev)
{
uint32_t val;
int ret;
ret = eth_adin2111_reg_write(dev, ADIN2111_SOFT_RST_REG, ADIN2111_SWRESET_KEY1);
if (ret < 0) {
return ret;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_SOFT_RST_REG, ADIN2111_SWRESET_KEY2);
if (ret < 0) {
return ret;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_SOFT_RST_REG, ADIN2111_SWRELEASE_KEY1);
if (ret < 0) {
return ret;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_SOFT_RST_REG, ADIN2111_SWRELEASE_KEY2);
if (ret < 0) {
return ret;
}
ret = eth_adin2111_reg_read(dev, ADIN1110_MAC_RST_STATUS_REG, &val);
if (ret < 0) {
return ret;
}
if (val == 0) {
return -EBUSY;
}
return 0;
}
int eth_adin2111_reg_update(const struct device *dev, const uint16_t reg,
uint32_t mask, uint32_t data)
{
uint32_t val;
int ret;
ret = eth_adin2111_reg_read(dev, reg, &val);
if (ret < 0) {
return ret;
}
val &= ~mask;
val |= mask & data;
return eth_adin2111_reg_write(dev, reg, val);
}
struct net_if *eth_adin2111_get_iface(const struct device *dev, const uint16_t port_idx)
{
struct adin2111_data *ctx = dev->data;
return ((struct adin2111_port_data *)ctx->port[port_idx]->data)->iface;
}
int eth_adin2111_lock(const struct device *dev, k_timeout_t timeout)
{
struct adin2111_data *ctx = dev->data;
return k_mutex_lock(&ctx->lock, timeout);
}
int eth_adin2111_unlock(const struct device *dev)
{
struct adin2111_data *ctx = dev->data;
return k_mutex_unlock(&ctx->lock);
}
static inline bool eth_adin2111_oa_get_parity(const uint32_t x)
{
uint32_t y;
y = x ^ (x >> 1);
y = y ^ (y >> 2);
y = y ^ (y >> 4);
y = y ^ (y >> 8);
y = y ^ (y >> 16);
return !(y & 1);
}
int eth_adin2111_oa_spi_xfer(const struct device *dev, uint8_t *buf_rx, uint8_t *buf_tx, int len)
{
const struct adin2111_config *cfg = dev->config;
struct spi_buf tx_buf[1];
struct spi_buf rx_buf[1];
struct spi_buf_set tx;
struct spi_buf_set rx;
int ret;
tx_buf[0].buf = buf_tx;
tx_buf[0].len = len;
rx_buf[0].buf = buf_rx;
rx_buf[0].len = len;
rx.buffers = rx_buf;
rx.count = 1;
tx.buffers = tx_buf;
tx.count = 1;
ret = spi_transceive_dt(&cfg->spi, &tx, &rx);
if (ret < 0) {
LOG_ERR("ERRR dma!\n");
return ret;
}
return 0;
}
static int eth_adin2111_reg_read_oa(const struct device *dev, const uint16_t reg,
uint32_t *val)
{
struct adin2111_data *ctx = dev->data;
uint32_t pval;
uint32_t *hdr = (uint32_t *)ctx->oa_tx_buf;
int len;
int ret;
*hdr = reg << 8;
if (reg >= 0x30) {
*hdr |= ADIN2111_OA_CTL_MMS;
}
*hdr |= eth_adin2111_oa_get_parity(*hdr);
*hdr = sys_cpu_to_be32(*hdr);
len = (ctx->oa_prot) ? ADIN2111_OA_CTL_LEN_PROT : ADIN2111_OA_CTL_LEN;
ret = eth_adin2111_oa_spi_xfer(dev, ctx->oa_rx_buf, ctx->oa_tx_buf, len);
if (ret < 0) {
return ret;
}
*val = sys_be32_to_cpu(*(uint32_t *)&ctx->oa_rx_buf[8]);
/* In protected mode read data is followed by its compliment value */
if (ctx->oa_prot) {
pval = sys_be32_to_cpu(*(uint32_t *)&ctx->oa_rx_buf[12]);
if (*val != ~pval) {
LOG_ERR("OA protected mode rx error !");
return -1;
}
}
return 0;
}
static int eth_adin2111_reg_write_oa(const struct device *dev, const uint16_t reg,
uint32_t val)
{
struct adin2111_data *ctx = dev->data;
uint32_t pval;
uint32_t *hdr = (uint32_t *)ctx->oa_tx_buf;
int len;
int ret;
*hdr = reg << 8 | ADIN2111_OA_CTL_WNR;
if (reg >= 0x30) {
*hdr |= ADIN2111_OA_CTL_MMS;
}
*hdr |= eth_adin2111_oa_get_parity(*hdr);
*hdr = sys_cpu_to_be32(*hdr);
len = (ctx->oa_prot) ? ADIN2111_OA_CTL_LEN_PROT : ADIN2111_OA_CTL_LEN;
*(uint32_t *)&ctx->oa_tx_buf[4] = sys_cpu_to_be32(val);
if (ctx->oa_prot) {
*(uint32_t *)&ctx->oa_tx_buf[8] = sys_cpu_to_be32(~val);
}
ret = eth_adin2111_oa_spi_xfer(dev, ctx->oa_rx_buf, ctx->oa_tx_buf, len);
if (ret < 0) {
return ret;
}
if (ctx->oa_prot) {
pval = sys_be32_to_cpu(*(uint32_t *)&ctx->oa_rx_buf[12]);
if (val != ~pval) {
LOG_ERR("OA protected mode tx error !");
return -1;
}
}
return 0;
}
int eth_adin2111_oa_data_read(const struct device *dev, const uint16_t port_idx)
{
struct adin2111_data *ctx = dev->data;
struct net_if *iface = ((struct adin2111_port_data *)ctx->port[port_idx]->data)->iface;
struct net_pkt *pkt;
uint32_t hdr, ftr;
int i, len, rx_pos, ret, rca, swo;
ret = eth_adin2111_reg_read(dev, ADIN2111_BUFSTS, &rca);
if (ret < 0) {
LOG_ERR("can't read BUFSTS");
return -EIO;
}
rca &= ADIN2111_BUFSTS_RCA_MASK;
/* Preare all tx headers */
for (i = 0, len = 0; i < rca; ++i) {
hdr = ADIN2111_OA_DATA_HDR_DNC;
hdr |= eth_adin2111_oa_get_parity(hdr);
*(uint32_t *)&ctx->oa_tx_buf[len] = sys_cpu_to_be32(hdr);
len += sizeof(uint32_t) + ctx->oa_cps;
}
ret = eth_adin2111_oa_spi_xfer(dev, ctx->oa_rx_buf, ctx->oa_tx_buf, len);
if (ret < 0) {
LOG_ERR("SPI xfer failed");
return ret;
}
for (i = 0, rx_pos = 0; i < rca; ++i) {
ftr = sys_be32_to_cpu(*(uint32_t *)&ctx->oa_rx_buf[rx_pos + ctx->oa_cps]);
if (eth_adin2111_oa_get_parity(ftr)) {
LOG_ERR("OA RX: Footer parity error !");
return -EIO;
}
if (!(ftr & ADIN2111_OA_DATA_FTR_SYNC)) {
LOG_ERR("OA RX: Configuration not in sync !");
return -EIO;
}
if (!(ftr & ADIN2111_OA_DATA_FTR_DV)) {
LOG_DBG("OA RX: Data chunk not valid, skip !");
goto update_pos;
}
if (ftr & ADIN2111_OA_DATA_FTR_SV) {
swo = (ftr & ADIN2111_OA_DATA_FTR_SWO_MSK) >> ADIN2111_OA_DATA_FTR_SWO;
if (swo != 0) {
LOG_ERR("OA RX: Misalignbed start of frame !");
return -EIO;
}
/* Reset store cursor */
ctx->scur = 0;
}
len = (ftr & ADIN2111_OA_DATA_FTR_EV) ?
((ftr & ADIN2111_OA_DATA_FTR_EBO_MSK) >> ADIN2111_OA_DATA_FTR_EBO) + 1 :
ctx->oa_cps;
memcpy(&ctx->buf[ctx->scur], &ctx->oa_rx_buf[rx_pos], len);
ctx->scur += len;
if (ftr & ADIN2111_OA_DATA_FTR_EV) {
pkt = net_pkt_rx_alloc_with_buffer(iface, CONFIG_ETH_ADIN2111_BUFFER_SIZE,
AF_UNSPEC, 0,
K_MSEC(CONFIG_ETH_ADIN2111_TIMEOUT));
if (!pkt) {
LOG_ERR("OA RX: cannot allcate packet space, skipping.");
return -EIO;
}
/* Skipping CRC32 */
ret = net_pkt_write(pkt, ctx->buf, ctx->scur - sizeof(uint32_t));
if (ret < 0) {
net_pkt_unref(pkt);
LOG_ERR("Failed to write pkt, scur %d, err %d", ctx->scur, ret);
return ret;
}
ret = net_recv_data(iface, pkt);
if (ret < 0) {
net_pkt_unref(pkt);
LOG_ERR("Port %u failed to enqueue frame to RX queue, %d",
port_idx, ret);
return ret;
}
}
update_pos:
rx_pos += ctx->oa_cps + sizeof(uint32_t);
}
return ret;
}
/*
* Setting up for a single dma transfer.
*/
static int eth_adin2111_send_oa_frame(const struct device *dev, struct net_pkt *pkt,
const uint16_t port_idx)
{
struct adin2111_data *ctx = dev->data;
uint16_t clen, len = net_pkt_get_len(pkt);
uint32_t hdr;
uint8_t chunks, i;
int ret, txc, cur;
chunks = len / ctx->oa_cps;
if (len % ctx->oa_cps) {
chunks++;
}
ret = eth_adin2111_reg_read(dev, ADIN2111_BUFSTS, &txc);
if (ret < 0) {
LOG_ERR("Cannot read txc");
return -EIO;
}
txc = (txc & ADIN2111_BUFSTS_TXC_MASK) >> ADIN2111_BUFSTS_TXC;
if (txc < chunks) {
return -EIO;
}
/* Prepare for single dma transfer */
for (i = 1, cur = 0; i <= chunks; i++) {
hdr = ADIN2111_OA_DATA_HDR_DNC | ADIN2111_OA_DATA_HDR_DV |
ADIN2111_OA_DATA_HDR_NORX;
hdr |= (!!port_idx << ADIN2111_OA_DATA_HDR_VS);
if (i == 1) {
hdr |= ADIN2111_OA_DATA_HDR_SV;
}
if (i == chunks) {
hdr |= ADIN2111_OA_DATA_HDR_EV;
hdr |= (ctx->oa_cps - 1) << ADIN2111_OA_DATA_HDR_EBO;
}
hdr |= eth_adin2111_oa_get_parity(hdr);
*(uint32_t *)&ctx->oa_tx_buf[cur] = sys_cpu_to_be32(hdr);
cur += sizeof(uint32_t);
clen = len > ctx->oa_cps ? ctx->oa_cps : len;
ret = net_pkt_read(pkt, &ctx->oa_tx_buf[cur], clen);
if (ret < 0) {
LOG_ERR("Cannot read from tx packet");
return ret;
}
cur += ctx->oa_cps;
len -= clen;
}
ret = eth_adin2111_oa_spi_xfer(dev, ctx->oa_rx_buf, ctx->oa_tx_buf, cur);
if (ret < 0) {
LOG_ERR("Error on SPI xfer");
return ret;
}
return 0;
}
static int eth_adin2111_reg_read_generic(const struct device *dev,
const uint16_t reg,
uint32_t *val)
{
const struct adin2111_config *cfg = dev->config;
size_t header_len = ADIN2111_READ_HEADER_SIZE;
size_t read_len = sizeof(uint32_t);
int ret;
#if CONFIG_ETH_ADIN2111_SPI_CFG0
uint8_t rcv_crc;
uint8_t comp_crc;
uint8_t buf[ADIN2111_REG_READ_BUF_SIZE_CRC] = { 0 };
#else
uint8_t buf[ADIN2111_REG_READ_BUF_SIZE] = { 0 };
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
/* spi header */
*(uint16_t *)buf = htons((ADIN2111_READ_TXN_CTRL | reg));
#if CONFIG_ETH_ADIN2111_SPI_CFG0
buf[2] = crc8_ccitt(0, buf, ADIN2111_SPI_HEADER_SIZE);
/* TA */
buf[3] = 0U;
++header_len;
++read_len;
#else
/* TA */
buf[2] = 0U;
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
const struct spi_buf tx_buf = { .buf = buf, .len = header_len + read_len };
const struct spi_buf rx_buf = { .buf = buf, .len = header_len + read_len };
const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1U };
const struct spi_buf_set rx = { .buffers = &rx_buf, .count = 1U };
ret = spi_transceive_dt(&cfg->spi, &tx, &rx);
if (ret < 0) {
return ret;
}
#if CONFIG_ETH_ADIN2111_SPI_CFG0
comp_crc = crc8_ccitt(0, &buf[header_len], sizeof(uint32_t));
rcv_crc = buf[header_len + sizeof(uint32_t)];
if (rcv_crc != comp_crc) {
/* invalid crc */
return -EIO;
}
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
*val = ntohl((*(uint32_t *)(&buf[header_len])));
return ret;
}
static int eth_adin2111_reg_write_generic(const struct device *dev,
const uint16_t reg,
const uint32_t val)
{
const struct adin2111_config *cfg = dev->config;
size_t header_size = ADIN2111_WRITE_HEADER_SIZE;
size_t data_size = sizeof(uint32_t);
#if CONFIG_ETH_ADIN2111_SPI_CFG0
uint8_t buf[ADIN2111_REG_WRITE_BUF_SIZE_CRC] = { 0 };
#else
uint8_t buf[ADIN2111_REG_WRITE_BUF_SIZE] = { 0 };
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
/* spi header */
*(uint16_t *)buf = htons((ADIN2111_WRITE_TXN_CTRL | reg));
#if CONFIG_ETH_ADIN2111_SPI_CFG0
buf[2] = crc8_ccitt(0, buf, header_size);
++header_size;
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
/* reg */
*(uint32_t *)(buf + header_size) = htonl(val);
#if CONFIG_ETH_ADIN2111_SPI_CFG0
buf[header_size + data_size] = crc8_ccitt(0, &buf[header_size], data_size);
++data_size;
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
const struct spi_buf spi_tx_buf = {
.buf = buf,
.len = header_size + data_size
};
const struct spi_buf_set tx = { .buffers = &spi_tx_buf, .count = 1U };
return spi_write_dt(&cfg->spi, &tx);
}
int eth_adin2111_reg_read(const struct device *dev, const uint16_t reg,
uint32_t *val)
{
struct adin2111_data *ctx = dev->data;
int rval;
if (ctx->oa) {
rval = eth_adin2111_reg_read_oa(dev, reg, val);
} else {
rval = eth_adin2111_reg_read_generic(dev, reg, val);
}
return rval;
}
int eth_adin2111_reg_write(const struct device *dev, const uint16_t reg,
const uint32_t val)
{
struct adin2111_data *ctx = dev->data;
int rval;
if (ctx->oa) {
rval = eth_adin2111_reg_write_oa(dev, reg, val);
} else {
rval = eth_adin2111_reg_write_generic(dev, reg, val);
}
return rval;
}
static int adin2111_read_fifo(const struct device *dev, const uint16_t port_idx)
{
const struct adin2111_config *cfg = dev->config;
struct adin2111_data *ctx = dev->data;
struct net_if *iface;
struct net_pkt *pkt;
uint16_t fsize_reg = ((port_idx == 0U) ? ADIN2111_P1_RX_FSIZE : ADIN2111_P2_RX_FSIZE);
uint16_t rx_reg = ((port_idx == 0U) ? ADIN2111_P1_RX : ADIN2111_P2_RX);
uint32_t fsize;
uint32_t fsize_real;
uint32_t padding_len;
#if CONFIG_ETH_ADIN2111_SPI_CFG0
uint8_t cmd_buf[ADIN2111_FIFO_READ_CMD_BUF_SIZE_CRC] = { 0 };
#else
uint8_t cmd_buf[ADIN2111_FIFO_READ_CMD_BUF_SIZE] = { 0 };
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
int ret;
iface = ((struct adin2111_port_data *)ctx->port[port_idx]->data)->iface;
/* get received frame size in bytes */
ret = eth_adin2111_reg_read(dev, fsize_reg, &fsize);
if (ret < 0) {
eth_stats_update_errors_rx(iface);
LOG_ERR("Port %u failed to read RX FSIZE, %d", port_idx, ret);
return ret;
}
/* burst read must be in multiples of 4 */
padding_len = ((fsize % 4) == 0) ? 0U : (ROUND_UP(fsize, 4U) - fsize);
/* actual available frame length is FSIZE - FRAME HEADER */
fsize -= ADIN2111_FRAME_HEADER_SIZE;
/* spi header */
*(uint16_t *)cmd_buf = htons((ADIN2111_READ_TXN_CTRL | rx_reg));
#if CONFIG_ETH_ADIN2111_SPI_CFG0
cmd_buf[2] = crc8_ccitt(0, cmd_buf, ADIN2111_SPI_HEADER_SIZE);
/* TA */
cmd_buf[3] = 0U;
#else
/* TA */
cmd_buf[2] = 0U;
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
const struct spi_buf tx_buf = { .buf = cmd_buf, .len = sizeof(cmd_buf) };
const struct spi_buf rx_buf[3] = {
{.buf = NULL, .len = sizeof(cmd_buf) + ADIN2111_FRAME_HEADER_SIZE},
{.buf = ctx->buf, .len = fsize},
{.buf = NULL, .len = padding_len }
};
const struct spi_buf_set tx = { .buffers = &tx_buf, .count = 1U };
const struct spi_buf_set rx = {
.buffers = rx_buf,
.count = ((padding_len == 0U) ? 2U : 3U)
};
ret = spi_transceive_dt(&cfg->spi, &tx, &rx);
if (ret < 0) {
eth_stats_update_errors_rx(iface);
LOG_ERR("Port %u failed to read RX FIFO, %d", port_idx, ret);
return ret;
}
/* remove CRC32 and pass to the stack */
fsize_real = fsize - sizeof(uint32_t);
pkt = net_pkt_rx_alloc_with_buffer(iface, fsize_real, AF_UNSPEC, 0,
K_MSEC(CONFIG_ETH_ADIN2111_TIMEOUT));
if (!pkt) {
eth_stats_update_errors_rx(iface);
LOG_ERR("Port %u failed to alloc frame RX buffer, %u bytes",
port_idx, fsize_real);
return -ENOMEM;
}
ret = net_pkt_write(pkt, ctx->buf, fsize_real);
if (ret < 0) {
eth_stats_update_errors_rx(iface);
net_pkt_unref(pkt);
LOG_ERR("Port %u failed to fill RX frame, %d", port_idx, ret);
return ret;
}
ret = net_recv_data(iface, pkt);
if (ret < 0) {
eth_stats_update_errors_rx(iface);
net_pkt_unref(pkt);
LOG_ERR("Port %u failed to enqueue frame to RX queue, %d",
port_idx, ret);
return ret;
}
eth_stats_update_bytes_rx(iface, fsize_real);
eth_stats_update_pkts_rx(iface);
return ret;
}
static inline void adin2111_port_on_phyint(const struct device *dev)
{
const struct adin2111_port_config *cfg = dev->config;
struct adin2111_port_data *data = dev->data;
struct phy_link_state state;
if (phy_adin2111_handle_phy_irq(cfg->phy, &state) < 0) {
/* no change or error */
return;
}
if (state.is_up) {
net_eth_carrier_on(data->iface);
} else {
net_eth_carrier_off(data->iface);
}
}
static void adin2111_offload_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p2);
ARG_UNUSED(p3);
const struct device *dev = p1;
struct adin2111_data *ctx = dev->data;
const struct adin2111_config *adin_cfg = dev->config;
const bool is_adin2111 = (adin_cfg->id == ADIN2111_MAC);
uint32_t status0;
uint32_t status1;
int ret;
for (;;) {
/* await INT */
k_sem_take(&ctx->offload_sem, K_FOREVER);
/* lock device */
eth_adin2111_lock(dev, K_FOREVER);
/* disable interrupts */
ret = eth_adin2111_reg_write(dev, ADIN2111_IMASK0, UINT32_MAX);
if (ret < 0) {
goto continue_unlock;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_IMASK1, UINT32_MAX);
if (ret < 0) {
goto continue_unlock;
}
/* read interrupts */
ret = eth_adin2111_reg_read(dev, ADIN2111_STATUS0, &status0);
if (ret < 0) {
goto continue_unlock;
}
ret = eth_adin2111_reg_read(dev, ADIN2111_STATUS1, &status1);
if (ret < 0) {
goto continue_unlock;
}
/* handle port 1 phy interrupts */
if (status0 & ADIN2111_STATUS0_PHYINT) {
adin2111_port_on_phyint(ctx->port[0]);
}
/* handle port 2 phy interrupts */
if (is_adin2111 && (status1 & ADIN2111_STATUS1_PHYINT)) {
adin2111_port_on_phyint(ctx->port[1]);
}
if (ctx->oa) {
if (status1 & ADIN2111_STATUS1_P1_RX_RDY) {
ret = eth_adin2111_oa_data_read(dev, 0);
if (ret < 0) {
goto continue_unlock;
}
}
if (is_adin2111 && (status1 & ADIN2111_STATUS1_P2_RX_RDY)) {
ret = eth_adin2111_oa_data_read(dev, 1);
if (ret < 0) {
goto continue_unlock;
}
}
} else {
#if CONFIG_ETH_ADIN2111_SPI_CFG0
if (status0 & ADIN2111_STATUS1_SPI_ERR) {
LOG_WRN("Detected TX SPI CRC error");
}
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
/* handle port 1 rx */
if (status1 & ADIN2111_STATUS1_P1_RX_RDY) {
do {
ret = adin2111_read_fifo(dev, 0U);
if (ret < 0) {
break;
}
ret = eth_adin2111_reg_read(dev, ADIN2111_STATUS1,
&status1);
if (ret < 0) {
goto continue_unlock;
}
} while (!!(status1 & ADIN2111_STATUS1_P1_RX_RDY));
}
/* handle port 2 rx */
if (is_adin2111 && (status1 & ADIN2111_STATUS1_P2_RX_RDY)) {
do {
ret = adin2111_read_fifo(dev, 1U);
if (ret < 0) {
break;
}
ret = eth_adin2111_reg_read(dev, ADIN2111_STATUS1,
&status1);
if (ret < 0) {
goto continue_unlock;
}
} while (!!(status1 & ADIN2111_STATUS1_P2_RX_RDY));
}
}
continue_unlock:
/* clear interrupts */
ret = eth_adin2111_reg_write(dev, ADIN2111_STATUS0, ADIN2111_STATUS0_CLEAR);
if (ret < 0) {
LOG_ERR("Failed to clear STATUS0, %d", ret);
}
ret = eth_adin2111_reg_write(dev, ADIN2111_STATUS1, ADIN2111_STATUS1_CLEAR);
if (ret < 0) {
LOG_ERR("Failed to clear STATUS1, %d", ret);
}
/* enable interrupts */
ret = eth_adin2111_reg_write(dev, ADIN2111_IMASK0, ctx->imask0);
if (ret < 0) {
LOG_ERR("Failed to write IMASK0, %d", ret);
}
ret = eth_adin2111_reg_write(dev, ADIN2111_IMASK1, ctx->imask1);
if (ret < 0) {
LOG_ERR("Failed to write IMASK1, %d", ret);
}
eth_adin2111_unlock(dev);
}
}
static void adin2111_int_callback(const struct device *dev,
struct gpio_callback *cb,
uint32_t pins)
{
ARG_UNUSED(dev);
ARG_UNUSED(pins);
struct adin2111_data *ctx = CONTAINER_OF(cb, struct adin2111_data, gpio_int_callback);
k_sem_give(&ctx->offload_sem);
}
static int adin2111_read_tx_space(const struct device *dev, uint32_t *space)
{
uint32_t val;
int ret;
ret = eth_adin2111_reg_read(dev, ADIN2111_TX_SPACE, &val);
if (ret < 0) {
return ret;
}
/* tx space is a number of halfwords (16-bits), multiply by 2 for bytes */
*space = val * 2;
return ret;
}
static int adin2111_port_send(const struct device *dev, struct net_pkt *pkt)
{
const struct adin2111_port_config *cfg = dev->config;
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
struct adin2111_port_data *data = dev->data;
#endif /* CONFIG_NET_STATISTICS_ETHERNET */
const struct device *adin = cfg->adin;
struct adin2111_data *ctx = cfg->adin->data;
size_t pkt_len = net_pkt_get_len(pkt);
size_t header_size = ADIN2111_WRITE_HEADER_SIZE;
size_t padded_size;
size_t burst_size;
uint32_t tx_space;
int ret;
eth_adin2111_lock(adin, K_FOREVER);
if (ctx->oa) {
uint32_t val, rca = 0;
/*
* By high-traffic zperf test, noted that ADIN2111 does not like we send
* if there is something to be received. It stops to issue rx interrupts
* and zperf transfer hangs. Forcing a receive for this case.
*/
ret = eth_adin2111_reg_read(adin, ADIN2111_BUFSTS, &val);
if (ret < 0) {
return ret;
}
rca = val & ADIN2111_BUFSTS_RCA_MASK;
if (rca > 0) {
eth_adin2111_unlock(adin);
k_sem_give(&ctx->offload_sem);
k_yield();
eth_adin2111_lock(adin, K_FOREVER);
}
ret = eth_adin2111_send_oa_frame(cfg->adin, pkt, htons(cfg->port_idx));
goto end_check;
}
/* query remaining tx fifo space */
ret = adin2111_read_tx_space(adin, &tx_space);
if (ret < 0) {
eth_stats_update_errors_tx(data->iface);
LOG_ERR("Failed to read TX FIFO space, %d", ret);
goto end_unlock;
}
/**
* verify that there is space for the frame
* (frame + 2b header + 2b size field)
*/
if (tx_space <
(pkt_len + ADIN2111_FRAME_HEADER_SIZE + ADIN2111_INTERNAL_HEADER_SIZE)) {
/* tx buffer is full */
eth_stats_update_errors_tx(data->iface);
ret = -EBUSY;
goto end_unlock;
}
/**
* pad to 64 bytes, otherwise MAC/PHY has to do it
* internally MAC adds 4 bytes for forward error correction
*/
if ((pkt_len + ADIN2111_TX_FIFO_BUFFER_MARGIN) < 64) {
padded_size = pkt_len
+ (64 - (pkt_len + ADIN2111_TX_FIFO_BUFFER_MARGIN))
+ ADIN2111_FRAME_HEADER_SIZE;
} else {
padded_size = pkt_len + ADIN2111_FRAME_HEADER_SIZE;
}
/* prepare burst write (write data must be in multiples of 4) */
burst_size = ROUND_UP(padded_size, 4);
if ((burst_size + ADIN2111_WRITE_HEADER_SIZE) > CONFIG_ETH_ADIN2111_BUFFER_SIZE) {
ret = -ENOMEM;
eth_stats_update_errors_tx(data->iface);
goto end_unlock;
}
/* prepare tx buffer */
memset(ctx->buf, 0, burst_size + ADIN2111_WRITE_HEADER_SIZE);
/* spi header */
*(uint16_t *)ctx->buf = htons(ADIN2111_TXN_CTRL_TX_REG);
#if CONFIG_ETH_ADIN2111_SPI_CFG0
ctx->buf[2] = crc8_ccitt(0, ctx->buf, header_size);
++header_size;
#endif /* CONFIG_ETH_ADIN2111_SPI_CFG0 */
/* frame header */
*(uint16_t *)(ctx->buf + header_size) = htons(cfg->port_idx);
/* read pkt into tx buffer */
ret = net_pkt_read(pkt,
(ctx->buf + header_size + ADIN2111_FRAME_HEADER_SIZE),
pkt_len);
if (ret < 0) {
eth_stats_update_errors_tx(data->iface);
LOG_ERR("Port %u failed to read PKT into TX buffer, %d",
cfg->port_idx, ret);
goto end_unlock;
}
/* write transmit size */
ret = eth_adin2111_reg_write(adin, ADIN2111_TX_FSIZE, padded_size);
if (ret < 0) {
eth_stats_update_errors_tx(data->iface);
LOG_ERR("Port %u write FSIZE failed, %d", cfg->port_idx, ret);
goto end_unlock;
}
/* write transaction */
const struct spi_buf buf = {
.buf = ctx->buf,
.len = header_size + burst_size
};
const struct spi_buf_set tx = { .buffers = &buf, .count = 1U };
ret = spi_write_dt(&((const struct adin2111_config *) adin->config)->spi,
&tx);
end_check:
if (ret < 0) {
eth_stats_update_errors_tx(data->iface);
LOG_ERR("Port %u frame SPI write failed, %d", cfg->port_idx, ret);
goto end_unlock;
}
eth_stats_update_bytes_tx(data->iface, pkt_len);
eth_stats_update_pkts_tx(data->iface);
end_unlock:
eth_adin2111_unlock(adin);
return ret;
}
static int adin2111_config_sync(const struct device *dev)
{
int ret;
uint32_t val;
ret = eth_adin2111_reg_read(dev, ADIN2111_CONFIG0, &val);
if (ret < 0) {
return ret;
}
val |= ADIN2111_CONFIG0_SYNC;
ret = eth_adin2111_reg_write(dev, ADIN2111_CONFIG0, val);
if (ret < 0) {
return ret;
}
return 0;
}
static int adin2111_write_filter_address(const struct device *dev,
uint8_t *addr, uint8_t *mask,
uint32_t rules, uint16_t slot)
{
uint16_t offset = slot * 2U;
int ret;
ret = eth_adin2111_reg_write(dev, ADIN2111_ADDR_FILT_UPR + offset,
rules | sys_get_be16(&addr[0]));
if (ret < 0) {
return ret;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_ADDR_FILT_LWR + offset,
sys_get_be32(&addr[2]));
if (ret < 0) {
return ret;
}
if (offset > 2U) {
/* mask filter addresses are limited to 2 */
return 0;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_ADDR_MSK_UPR + offset,
sys_get_be16(&mask[0]));
if (ret < 0) {
return ret;
}
ret = eth_adin2111_reg_write(dev, ADIN2111_ADDR_MSK_LWR + offset,
sys_get_be32(&mask[2]));
if (ret < 0) {
return ret;
}
return ret;
}
static int adin2111_filter_multicast(const struct device *dev)
{
const struct adin2111_config *cfg = dev->config;
const bool is_adin2111 = (cfg->id == ADIN2111_MAC);