-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax_ptp.c
1167 lines (990 loc) · 28.5 KB
/
ax_ptp.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
// SPDX-License-Identifier: GPL-2.0
/*******************************************************************************
* Copyright (c) 2022 ASIX Electronic Corporation All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/
#include "ax_main.h"
#include "ax_ptp.h"
#include "ax88179a_772d.h"
#define ptp_to_dev(ptp) container_of(ptp, struct ax_ptp_cfg, ptp_caps)
static void ax_reset_ptp_queue(struct ax_device *axdev)
{
struct ax_ptp_cfg *ptp_cfg = axdev->ptp_cfg;
if (!ptp_cfg)
return;
ptp_cfg->ptp_head = 0;
ptp_cfg->ptp_tail = 0;
ptp_cfg->num_items = 0;
ptp_cfg->get_timestamp_retry = 0;
memset(ptp_cfg->tx_ptp_info, 0, AX_PTP_INFO_SIZE * AX_PTP_QUEUE_SIZE);
}
static int ax88179a_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
{
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u32 remainder = 0;
u64 high_timer = 0;
s64 sec = 0, rnsec = 0;
s64 nsec = 0;
u8 timestamp[10] = {0};
int ret;
ret = ax_read_cmd(axdev, AX_PTP_OP, AX_GET_LOCAL_CLOCK, 0,
AX_GET_LOCAL_CLOCK_SIZE, ×tamp, 0);
if (ret < 0)
return ret;
memcpy(&nsec, timestamp, 4);
memcpy(&sec, ×tamp[4], 6);
sec *= NSEC_PER_SEC;
rnsec = (nsec + sec + delta);
high_timer = div_u64_rem(rnsec, NSEC_PER_SEC, &remainder);
memcpy(timestamp, &remainder, 4);
memcpy(×tamp[4], &high_timer, 6);
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_LOCAL_CLOCK, 0,
AX_SET_LOCAL_CLOCK_SIZE, ×tamp);
if (ret < 0)
return ret;
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0)
static int
ax88179a_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
#else
static int
ax88179a_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0)
long ppb = scaled_ppm_to_ppb(scaled_ppm);
#endif
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u32 new_addend_val;
u64 adjust_val;
int neg_adj = 0, ret;
if (ppb < 0) {
neg_adj = 1;
ppb = -ppb;
}
adjust_val = AX_BASE_ADDEND;
adjust_val *= ppb;
adjust_val = div_u64(adjust_val, NSEC_PER_SEC);
if (neg_adj)
new_addend_val = (u32)(AX_BASE_ADDEND - adjust_val);
else
new_addend_val = (u32)(AX_BASE_ADDEND + adjust_val);
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_ADDEND, 0,
AX_SET_ADDEND_SIZE, &new_addend_val);
if (ret < 0)
return ret;
return 0;
}
static int ax88179a_ptp_gettime64
(struct ptp_clock_info *ptp, struct timespec64 *ts)
{
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u64 sec = 0;
u32 nsec = 0;
u8 timestamp[10] = {0};
int ret;
ret = ax_read_cmd(axdev, AX_PTP_OP, AX_GET_LOCAL_CLOCK, 0,
AX_GET_LOCAL_CLOCK_SIZE, ×tamp, 0);
if (ret < 0)
return ret;
memcpy(&nsec, timestamp, 4);
memcpy(&sec, ×tamp[4], 6);
ts->tv_nsec = nsec;
ts->tv_sec = sec;
return 0;
}
static int ax88179a_ptp_settime64
(struct ptp_clock_info *ptp, const struct timespec64 *ts)
{
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u64 sec;
u32 nsec;
u8 timestamp[10] = {0};
int ret;
nsec = (u32)ts->tv_nsec;
memcpy(timestamp, &nsec, 4);
sec = (u64)ts->tv_sec;
memcpy(×tamp[4], &sec, 6);
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_LOCAL_CLOCK, 0,
AX_SET_LOCAL_CLOCK_SIZE, ×tamp);
if (ret < 0)
return ret;
return 0;
}
static int ax_ptp_enable(struct ptp_clock_info *ptp,
struct ptp_clock_request *rq, int on)
{
return -EOPNOTSUPP;
}
static struct ptp_clock_info ax88179a_772d_ptp_clock = {
.owner = THIS_MODULE,
.name = "asix ptp",
.max_adj = 100000000,
.n_ext_ts = 0,
.pps = 0,
#if KERNEL_VERSION(4, 10, 0) <= LINUX_VERSION_CODE
.adjfine = NULL,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0)
.adjfine = ax88179a_ptp_adjfine,
#else
.adjfreq = ax88179a_ptp_adjfreq,
#endif
.adjtime = ax88179a_ptp_adjtime,
.gettime64 = ax88179a_ptp_gettime64,
.settime64 = ax88179a_ptp_settime64,
.n_per_out = 0,
.enable = ax_ptp_enable,
.n_pins = 0,
.verify = NULL,
.pin_config = NULL,
};
int ax88179a_ptp_init(struct ax_device *axdev)
{
struct ax_link_info *link_info = &axdev->link_info;
u32 new_addend_val = AX_BASE_ADDEND;
u8 reg8;
u8 ptpset;
u8 ptp_tx_delay[5] = { 0 };
u8 ptp_rx_delay[5] = { 0 };
u32 reg32;
u32 timeout = 0;
int ret;
#ifdef ENABLE_PTP_FUNC
axdev->driver_info->ptp_pps_ctrl(axdev, 1);
#endif
if (axdev->sub_version < 3)
return 0;
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_ADDEND, 0,
AX_SET_ADDEND_SIZE, &new_addend_val);
if (ret < 0)
return ret;
reg8 = AX_PTP_PERIOD;
ret = ax_write_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_TIMER_PERIOD,
0, 1, ®8);
if (ret < 0)
return ret;
ret = ax_read_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_CTRL_2,
0, 1, ®8, 0);
if (ret < 0)
return ret;
reg8 |= AX_PTP_CTRL_SET_PERIOD;
ret = ax_write_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_CTRL_2, 0, 1, ®8);
if (ret < 0)
return ret;
ret = ax_read_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_CTRL_1,
0, 1, &ptpset, 0);
if (ret < 0)
return ret;
ptpset |= AX_PTP_CTRL_L3_EN | AX_PTP_CTRL_EN | AX_PTP_TX_PLUS_DELAY |
AX_PTP_TX_FILTER_GENERAL_MSG | AX_PTP_RX_FILTER_GENERAL_MSG;
ret = ax_write_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_CTRL_1,
0, 1, &ptpset);
if (ret < 0)
return ret;
reg8 = AX_179A_PTP_INFO_SEG_SIZE * AX_PTP_HW_QUEUE_SIZE;
ret = ax_write_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_MEM_SEG_SIZE,
0, 1, ®8);
if (ret < 0)
return ret;
do {
reg8 = 0;
ret = ax_write_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_MEM_SEG_SET,
0, 1, ®8);
if (ret < 0)
return ret;
ret = ax_read_cmd(axdev, AX_PTP_CMD,
AX88179A_PTP_MEM_SEG_STATUS, 0, 1, ®8, 0);
if (ret < 0)
return ret;
reg8 &= AX_PTP_MEM_SEG_MASK;
if (timeout++ > 5)
break;
} while (reg8 != AX_PTP_MEM_SEG_0);
reg32 = AX_PPS_ACTIVE_DEFAULT_TIME;
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_ACTIVE_TIME, 0,
AX_SET_ACTIVE_TIME_SIZE, ®32);
if (ret < 0)
return ret;
switch (link_info->eth_speed) {
case ETHER_LINK_100:
ptp_tx_delay[0] = 0x64;
ptp_tx_delay[4] = ptpset;
ptp_rx_delay[0] = 0x22;
ptp_rx_delay[1] = 0x01;
ptp_rx_delay[4] = ptpset;
break;
case ETHER_LINK_10:
ptp_tx_delay[0] = 0xB7;
ptp_tx_delay[1] = 0x0F;
ptp_tx_delay[4] = ptpset;
ptp_rx_delay[0] = 0xAC;
ptp_rx_delay[1] = 0x0A;
ptp_rx_delay[4] = ptpset;
break;
case ETHER_LINK_1000:
default:
ptp_tx_delay[0] = 0x6E;
ptp_tx_delay[4] = ptpset;
ptp_rx_delay[0] = 0xE4;
ptp_rx_delay[4] = ptpset;
break;
}
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_TX_PHY_DELAY, 0,
AX_SET_TX_PHY_DELAY_SIZE, ptp_tx_delay);
if (ret < 0)
return ret;
ret = ax_write_cmd(axdev, AX_PTP_OP, AX_SET_RX_PHY_DELAY, 0,
AX_SET_RX_PHY_DELAY_SIZE, ptp_rx_delay);
if (ret < 0)
return ret;
axdev->netdev->features &= ~(NETIF_F_SG | NETIF_F_TSO);
axdev->netdev->hw_features &= ~(NETIF_F_SG | NETIF_F_TSO);
axdev->netdev->vlan_features &= ~(NETIF_F_SG | NETIF_F_TSO);
return 0;
}
int ax88179a_ptp_pps_ctrl(struct ax_device *axdev, u8 enable)
{
u32 reg32 = 0;
int ret;
ret = ax_read_cmd(axdev, AX88179A_PBUS_REG, 0x1894, 0x000F, 4, ®32, 1);
if (ret < 0)
return ret;
reg32 &= ~0x01000000;
if (enable)
reg32 |= 0x01000000;
ret = ax_write_cmd(axdev, AX88179A_PBUS_REG, 0x1894, 0x000F, 4, ®32);
if (ret < 0)
return ret;
return 0;
}
int ax88279_ptp_pps_ctrl(struct ax_device *axdev, u8 enable)
{
u32 reg32 = 0;
int ret;
ret = ax_read_cmd(axdev, AX_PBUS_A32, 0xF8C8, 0x000C, 4, ®32, 1);
if (ret < 0)
return ret;
reg32 &= ~0x00004000;
if (enable)
reg32 |= 0x00004000;
ret = ax_write_cmd(axdev, AX_PBUS_A32, 0xF8C8, 0x000C, 4, ®32);
return 0;
}
void ax88179a_ptp_remove(struct ax_device *axdev)
{
u8 reg8;
#ifdef ENABLE_PTP_FUNC
axdev->driver_info->ptp_pps_ctrl(axdev, 0);
#endif
if (axdev->sub_version < 3)
return;
ax_read_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8, 0);
reg8 &= ~(AX_PTP_CTRL_L3_EN | AX_PTP_CTRL_EN);
ax_write_cmd(axdev, AX_PTP_CMD, AX88179A_PTP_CTRL_1, 0, 1, ®8);
}
void ax_ptp_unregister(struct ax_device *axdev)
{
struct ax_ptp_cfg *ptp_cfg = axdev->ptp_cfg;
if (axdev->driver_info->ptp_remove)
axdev->driver_info->ptp_remove(axdev);
if (ptp_cfg) {
if (ptp_cfg->ptp_clock)
ptp_clock_unregister(ptp_cfg->ptp_clock);
}
}
static u8 ax_find_ptp_item(struct ax_device *axdev, struct _ptp_header *ptp,
struct sk_buff *skb)
{
struct ax_ptp_cfg *ptp_cfg = axdev->ptp_cfg;
struct _ax_ptp_info *temp_ptp_info = ptp_cfg->tx_ptp_info;
u16 sequence_id;
u8 message_type = ptp->message_type;
int i, read_ptr;
read_ptr = ptp_cfg->ptp_head;
#ifdef ENABLE_PTP_DEBUG
printk("%s - ptp_head: %d", __func__, read_ptr);
#endif
if (axdev->chip_version == AX_VERSION_AX88179A_772D)
sequence_id = ntohs(ptp->sequence_id) & 0xFF;
else
sequence_id = ntohs(ptp->sequence_id) & 0xFFFF;
for (i = 0; i < ptp_cfg->num_items; i++) {
if ((temp_ptp_info[read_ptr].sequence_id == sequence_id) &&
(temp_ptp_info[read_ptr].msg_type == message_type)) {
struct skb_shared_hwtstamps shhwtstamps;
u64 timestamp_h, timestamp_l, temp;
u64 time64;
ptp_cfg->num_items--;
ptp_cfg->ptp_head++;
if (ptp_cfg->ptp_head == AX_PTP_QUEUE_SIZE)
ptp_cfg->ptp_head = 0;
timestamp_l = temp_ptp_info[read_ptr].nsec;
timestamp_h = temp_ptp_info[read_ptr].sec_l;
temp = temp_ptp_info[read_ptr].sec_h;
timestamp_h |= (temp << 32);
time64 = timestamp_h * NSEC_PER_SEC;
time64 += timestamp_l & 0xFFFFFFFF;
memset(&shhwtstamps, 0, sizeof(shhwtstamps));
shhwtstamps.hwtstamp = ns_to_ktime(time64);
if (ptp->flags & 0x2 ||
(ptp->message_type != 0 && ptp->message_type != 3))
skb_tstamp_tx(skb, &shhwtstamps);
#ifdef ENABLE_PTP_DEBUG
printk("%s - skb_tstamp_tx return", __func__);
#endif
dev_kfree_skb_any(skb);
return 0;
}
read_ptr++;
if (read_ptr == AX_PTP_QUEUE_SIZE)
read_ptr = 0;
}
return AX_PTP_QUEUE_SIZE;
}
static void ax_tx_check_timestamp(struct ax_device *axdev, struct sk_buff *skb)
{
if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
struct _ptp_header ptp;
unsigned int ptp_msg_offset;
u16 tmp, tx_ethertype, vlan_id = 0;
u8 vlan_size = 0, ptp_item = AX_PTP_QUEUE_SIZE;
skb_copy_from_linear_data_offset(skb, AX_ETHTYPE_OFFSET,
&tmp, 2);
tx_ethertype = ntohs(tmp);
if (tx_ethertype == ETH_P_8021Q) {
skb_copy_from_linear_data_offset(skb,
AX_ETHTYPE_OFFSET + 2,
&tmp, 2);
vlan_id = ntohs(tmp) & 0xFFF;
vlan_size = 4;
skb_copy_from_linear_data_offset(skb,
AX_ETHTYPE_OFFSET + 4,
&tmp, 2);
tx_ethertype = ntohs(tmp);
if (tx_ethertype == ETH_P_8021Q) {
vlan_size = 8;
skb_copy_from_linear_data_offset(skb,
AX_ETHTYPE_OFFSET + 4,
&tmp, 2);
tx_ethertype = ntohs(tmp);
}
}
ptp_msg_offset = vlan_size;
if (tx_ethertype == ETH_P_1588)
ptp_msg_offset += AX_TX_PTPHDR_OFFSET_L2;
else if (tx_ethertype == ETH_P_IP)
ptp_msg_offset += AX_TX_PTPHDR_OFFSET_L3_IP;
else if (tx_ethertype == ETH_P_IPV6)
ptp_msg_offset += AX_TX_PTPHDR_OFFSET_L3_IPV6;
else
return;
skb_copy_from_linear_data_offset(skb, ptp_msg_offset,
&ptp, PTP_HDR_SIZE);
ptp_item = ax_find_ptp_item(axdev, &ptp, skb);
if (ptp_item == AX_PTP_QUEUE_SIZE) {
dev_err(&axdev->intf->dev,
"Not found item from PTP queue");
return;
}
}
}
static void ax_tx_timestamp(struct ax_device *axdev)
{
struct sk_buff_head *tx_timestamp = &axdev->tx_timestamp;
while (!skb_queue_empty(tx_timestamp)) {
struct sk_buff *skb;
skb = __skb_dequeue(tx_timestamp);
if (!skb)
return;
ax_tx_check_timestamp(axdev, skb);
}
}
static struct _ax_ptp_info *ax_ptp_info_transform(struct ax_device *axdev,
void *data)
{
struct _ax_ptp_info temp[AX_PTP_HW_QUEUE_SIZE] = {0};
int i;
switch (axdev->chip_version) {
case AX_VERSION_AX88179A_772D:
{
struct _179a_ptp_info *_179a_ptp = (typeof(_179a_ptp))data;
for (i = 0; i < AX_PTP_HW_QUEUE_SIZE; i++) {
memcpy(&temp[i], &_179a_ptp[i], 2);
temp[i].sequence_id &= 0xFF;
memcpy(&temp[i].nsec, &_179a_ptp[i].nsec, 10);
}
memcpy(data, temp, AX_PTP_INFO_SIZE);
break;
}
};
return (struct _ax_ptp_info *)data;
}
#if KERNEL_VERSION(2, 6, 20) > LINUX_VERSION_CODE
static void ax_ptp_ts_callback(struct urb *urb, struct pt_regs *regs)
#else
static void ax_ptp_ts_callback(struct urb *urb)
#endif
{
struct _ax_ptp_usb_info *ptp_info = (typeof(ptp_info))urb->context;
struct ax_device *axdev = (struct ax_device *)ptp_info->axdev;
struct ax_ptp_cfg *ptp_cfg = axdev->ptp_cfg;
struct _ax_ptp_info *temp_ptp_info = ptp_info->ax_ptp_info;
int i, count = 0;
#ifdef ENABLE_PTP_DEBUG
printk("%s - Start urb->actual_length: %d",
__func__, urb->actual_length);
#endif
if (urb->status < 0) {
printk(KERN_ERR "failed get ts (%d)", urb->status);
goto free;
}
temp_ptp_info = ax_ptp_info_transform(axdev, ptp_info->ax_ptp_info);
if (temp_ptp_info == NULL) {
printk(KERN_ERR "Failed to transform ptp info.");
goto free;
}
for (i = 0; i < AX_PTP_HW_QUEUE_SIZE; i++) {
if (temp_ptp_info[i].status) {
ptp_cfg->tx_ptp_info[ptp_cfg->ptp_tail++] = temp_ptp_info[i];
if (ptp_cfg->ptp_tail == AX_PTP_QUEUE_SIZE)
ptp_cfg->ptp_tail = 0;
ptp_cfg->num_items++;
#ifdef ENABLE_PTP_DEBUG
printk("### ptp_tail: %ld, ptp_head: %ld, num_items: %ld",
ptp_cfg->ptp_tail, ptp_cfg->ptp_head, ptp_cfg->num_items);
printk("### (%s) - DATA %d -------------###", __func__, i);
printk("### status: %d", temp_ptp_info[i].status);
printk("### type: 0x%02x", temp_ptp_info[i].msg_type);
printk("### s_id: 0x%04x", temp_ptp_info[i].sequence_id);
printk("### nsec: 0x%08x", temp_ptp_info[i].nsec);
printk("### sec: 0x%04x%08x", temp_ptp_info[i].sec_h, temp_ptp_info[i].sec_l);
printk("### ----------------------------###\n");
#endif
count++;
ptp_cfg->get_timestamp_retry = 0;
}
}
if (count == 0 &&
ptp_cfg->get_timestamp_retry < EP0_GET_TIMESTAMP_RETRY) {
ax_ptp_ts_read_cmd_async(axdev);
ptp_cfg->get_timestamp_retry++;
goto free;
}
if (ptp_cfg->get_timestamp_retry == EP0_GET_TIMESTAMP_RETRY)
dev_err(&axdev->intf->dev, "Get timestamp failed.");
ax_tx_timestamp(axdev);
free:
kfree(temp_ptp_info);
usb_free_urb(urb);
}
int ax_ptp_ts_read_cmd_async(struct ax_device *axdev)
{
struct usb_ctrlrequest *req;
int status = 0;
struct urb *urb;
struct _ax_ptp_usb_info *info;
u16 size = AX_PTP_INFO_SIZE * AX_PTP_HW_QUEUE_SIZE;
if (axdev->chip_version > AX_VERSION_AX88179A_772D)
return 0;
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (urb == NULL) {
dev_err(&axdev->intf->dev,
"Error allocating URB in write_cmd_async!");
return -ENOMEM;
}
info = kzalloc(sizeof(struct _ax_ptp_usb_info), GFP_ATOMIC);
if (!info) {
usb_free_urb(urb);
return -ENOMEM;
}
info->axdev = axdev;
req = &info->req;
req->bRequestType = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
req->bRequest = AX_PTP_TIMESTAMP;
req->wValue = cpu_to_le16(0);
req->wIndex = cpu_to_le16(0);
req->wLength = cpu_to_le16(size);
memset(info->ax_ptp_info, 0, AX_PTP_INFO_SIZE * AX_PTP_QUEUE_SIZE);
usb_fill_control_urb(urb, axdev->udev,
usb_sndctrlpipe(axdev->udev, 0),
(void *)req, info->ax_ptp_info, size,
ax_ptp_ts_callback, info);
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status < 0) {
dev_err(&axdev->intf->dev,
"Error submitting the control message: status=%d",
status);
kfree(info);
usb_free_urb(urb);
}
return 0;
}
void ax_rx_get_timestamp(struct sk_buff *skb, u64 *pkt_hdr)
{
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
u64 timestamp_h, timestamp_l;
u64 time64;
timestamp_l = *((u64 *)(++pkt_hdr));
timestamp_h = *((u64 *)(++pkt_hdr));
#ifdef ENABLE_PTP_DEBUG
printk("### (%s) h: 0x%llx, l: 0x%llx %lld###", __func__,
timestamp_h, timestamp_l, timestamp_l);
#endif
timestamp_h <<= 32;
timestamp_h |= (timestamp_l >> 32) & 0xFFFFFFFF;
time64 = (timestamp_h * NSEC_PER_SEC);
time64 += (timestamp_l & 0xFFFFFFFF);
#ifdef ENABLE_PTP_DEBUG
printk("### (%s) h: %lld, time64: %lld ###", __func__,
timestamp_h, time64);
#endif
memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
shhwtstamps->hwtstamp = ns_to_ktime(time64);
}
#ifdef ENABLE_AX88279
static int ax_ptp_pbus_write(struct ax_device *axdev, u16 offset, u16 len,
void *data)
{
int ret = 0;
ret = ax_write_cmd(axdev,
AX_PBUS_A32,
offset,
AX_PTP_REG_BASE_ADDR_HI,
len,
data);
if (ret < 0)
return ret;
return 0;
}
static int ax_ptp_clk_write(struct ax_device *axdev, u16 offset, u16 len,
void *data)
{
int ret = 0;
ret = ax_write_cmd(axdev,
AX_PBUS_A32,
offset,
AX_PTP_REG_BASE_ADDR_HI,
len,
data);
if (ret < 0)
return ret;
return 0;
}
static int ax_ptp_clk_read(struct ax_device *axdev, u16 offset, u16 len,
void *data)
{
int ret = 0;
ret = ax_read_cmd(axdev,
AX_PTP_CLK,
0x0002,
offset,
len,
data,
0);
if (ret < 0)
return ret;
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0)
static int ax88279_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
#else
static int ax88279_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0)
long ppb = scaled_ppm_to_ppb(scaled_ppm);
#endif
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u32 new_addend_val;
u64 adjust_val;
int neg_adj = 0, ret;
if (ppb < 0) {
neg_adj = 1;
ppb = -ppb;
}
adjust_val = AX_BASE_ADDEND;
adjust_val *= ppb;
adjust_val = div_u64(adjust_val, NSEC_PER_SEC);
if (neg_adj)
new_addend_val = (u32)(AX_BASE_ADDEND - adjust_val);
else
new_addend_val = (u32)(AX_BASE_ADDEND + adjust_val);
ret = ax_ptp_pbus_write(axdev,
AX_PTP_TIMER_ADDEND,
sizeof(new_addend_val),
&new_addend_val);
if (ret < 0)
return ret;
return 0;
}
static int ax88279_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
{
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u32 remainder = 0;
u64 high_timer = 0;
s64 sec = 0;
s64 nsec = 0;
u8 timestamp[12] = {0};
int ret;
ret = ax_ptp_clk_read(axdev, AX_PTP_GET_80B_LCK_VAL0, 10, timestamp);
if (ret < 0)
return ret;
memcpy(&nsec, timestamp, 4);
memcpy(&sec, ×tamp[4], 6);
sec *= NSEC_PER_SEC;
nsec = (sec + delta);
high_timer = div_u64_rem(nsec, NSEC_PER_SEC, &remainder);
memcpy(timestamp, &remainder, 4);
memcpy(×tamp[4], &high_timer, 6);
ret = ax_ptp_clk_write(axdev, AX_PTP_SET_80B_LCK_VAL0, 12, timestamp);
if (ret < 0)
return ret;
return 0;
}
static int ax88279_ptp_gettime64(struct ptp_clock_info *ptp,
struct timespec64 *ts)
{
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u64 sec = 0;
u32 nsec = 0;
u8 timestamp[12] = {0};
int ret;
ret = ax_ptp_clk_read(axdev, AX_PTP_GET_80B_LCK_VAL0, 10, timestamp);
if (ret < 0)
return ret;
memcpy(&nsec, timestamp, 4);
memcpy(&sec, ×tamp[4], 6);
ts->tv_nsec = nsec;
ts->tv_sec = sec;
return 0;
}
static int ax88279_ptp_settime64(struct ptp_clock_info *ptp,
const struct timespec64 *ts)
{
struct ax_ptp_cfg *ptp_cfg = ptp_to_dev(ptp);
struct ax_device *axdev = (struct ax_device *)ptp_cfg->axdev;
u64 sec;
u32 nsec;
u8 timestamp[10] = {0};
int ret;
nsec = (u32)ts->tv_nsec;
memcpy(timestamp, &nsec, 4);
sec = (u64)ts->tv_sec;
memcpy(×tamp[4], &sec, 6);
ret = ax_ptp_clk_write(axdev, AX_PTP_SET_80B_LCK_VAL0, 10, timestamp);
if (ret < 0)
return ret;
return 0;
}
static struct ptp_clock_info ax88279_ptp_clock = {
.owner = THIS_MODULE,
.name = "asix ptp",
.max_adj = 100000000,
.n_ext_ts = 0,
.pps = 0,
#if KERNEL_VERSION(4, 10, 0) <= LINUX_VERSION_CODE
.adjfine = NULL,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0)
.adjfine = ax88279_ptp_adjfine,
#else
.adjfreq = ax88279_ptp_adjfreq,
#endif
.adjtime = ax88279_ptp_adjtime,
.gettime64 = ax88279_ptp_gettime64,
.settime64 = ax88279_ptp_settime64,
.n_per_out = 0,
.enable = ax_ptp_enable,
.n_pins = 0,
.verify = NULL,
.pin_config = NULL,
};
int ax88279_ptp_init(struct ax_device *axdev)
{
struct ax_link_info *link_info = &axdev->link_info;
u32 reg32;
u8 reg8;
int ret;
ax_reset_ptp_queue(axdev);
#ifdef ENABLE_PTP_FUNC
axdev->driver_info->ptp_pps_ctrl(axdev, 1);
#endif
reg32 = (AX_PTP_MEM_SEG_SIZE_279_5 << 24) |
(AX_PTP_MEM_START_ADDR << 8) | AX_PTP_PTP_CPU_EN;
ret = ax_ptp_pbus_write(axdev, AX_PTP_TX_MEM_SETTING, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PPS_ACTIVE_DEFAULT_TIME;
ret = ax_ptp_pbus_write(axdev, AX_PTP_PPS_ACTIVE_TIME, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_LCK_CTRL0_EN | AX_PTP_LCK_CTRL0_80B_NS_EN |
AX_PTP_LCK_CTRL0_80B_S_EN | AX_PTP_LCK_CTRL0_48B_EN |
AX_PTP_LCK_CTRL0_PPS_EN | AX_PTP_LCK_CTRL0_TX_DEL_VEC;
ret = ax_ptp_pbus_write(axdev, AX_PTP_LCK_CTRL0, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_BASE_ADDEND;
ret = ax_ptp_pbus_write(axdev, AX_PTP_TIMER_ADDEND, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_PERIOD;
ret = ax_ptp_pbus_write(axdev, AX_PTP_TIMER_PERIOD, 4, ®32);
if (ret < 0)
return ret;
ret = ax_read_cmd(axdev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8, 0);
if (ret < 0)
return ret;
reg8 |= AX_CS_TRAIL_UDPV4_EN | AX_CS_TRAIL_UDPV6_EN;
ret = ax_write_cmd(axdev, AX_ACCESS_MAC, AX_MAC_BFM_CTRL, 1, 1, ®8);
if (ret < 0)
return ret;
switch (link_info->eth_speed) {
case ETHER_LINK_100:
reg32 = (AX_IPG_COUNTER_100M) |
(AX_SOF_DELAY_COUNTER_100M << 8) |
(AX_VAILD_DELAY_COUNTER_100M << 16);
ret = ax_write_cmd(axdev, AX_PBUS_A32, AX_TX_READY_CTRL,
AX_PBUS_REG_BASE_ADDR_HI, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_RX_CTRL0_DEFAULT;
ret = ax_ptp_pbus_write(axdev, AX_PTP_RX_CTRL0, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_TX_CTRL0_DEFAULT |
(0x10 << AX_TXC0_VAL_DELAY_CNT_SHIFT);
ret = ax_ptp_pbus_write(axdev, AX_PTP_TX_CTRL0, 4, ®32);
if (ret < 0)
return ret;
break;
case ETHER_LINK_1000:
reg32 = (AX_IPG_COUNTER_1G) |
(AX_SOF_DELAY_COUNTER_1G << 8) |
(AX_VAILD_DELAY_COUNTER_1G << 16);
ret = ax_write_cmd(axdev, AX_PBUS_A32, AX_TX_READY_CTRL,
AX_PBUS_REG_BASE_ADDR_HI, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_RX_CTRL0_DEFAULT;
ret = ax_ptp_pbus_write(axdev, AX_PTP_RX_CTRL0, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_TX_CTRL0_DEFAULT |
(0x8 << AX_TXC0_VAL_DELAY_CNT_SHIFT);
ret = ax_ptp_pbus_write(axdev, AX_PTP_TX_CTRL0, 4, ®32);
if (ret < 0)
return ret;
break;
case ETHER_LINK_2500:
reg32 = AX_PTP_RX_CTRL0_DEFAULT | AX_PTP_RXC0_XGMII_EN;
ret = ax_ptp_pbus_write(axdev, AX_PTP_RX_CTRL0, 4, ®32);
if (ret < 0)
return ret;
reg32 = AX_PTP_TX_CTRL0_DEFAULT | AX_PTP_TXC0_XGMII_EN |
(0x8 << AX_TXC0_VAL_DELAY_CNT_SHIFT);
ret = ax_ptp_pbus_write(axdev, AX_PTP_TX_CTRL0, 4, ®32);
if (ret < 0)
return ret;
break;
default:
break;
}
reg32 = 0;
ret = ax_ptp_pbus_write(axdev, AX_PTP_TX_DELAY, 4, ®32);
if (ret < 0)
return ret;
reg32 = 0;
ret = ax_ptp_pbus_write(axdev, AX_PTP_RX_DELAY, 4, ®32);
if (ret < 0)
return ret;
reg8 = AX_EXT_INT_ON | AX_PTP_TX_TX_INT_EN;
ret = ax_write_cmd(axdev, AX_PTP_TOD_CTRL,
(AX_PTP_TS_INT | AX_EXT_INT), 0, 1, ®8);
if (ret < 0)
return ret;
reg32 = 0;
ret = ax_write_cmd(axdev, AX_PBUS_A32, AX_MAC_CLK_CTRL,
AX_PBUS_REG_BASE_ADDR_HI, 4, ®32);
if (ret < 0)
return ret;
reg32 = (0 << AX_DIVIDE_PTP_CLK_SHIFT) |
(1 << AX_DIVIDE_AES_CLK_SHIFT) |
AX_PTP_CLK_EN | AX_AES_CLK_EN |
AX_PTP_CLK_SELECT_DIVIDE | AX_AES_CLK_SELECT_DIVIDE |
AX_XGMAC_TX_CLK_EN | AX_XGMAC_RX_CLK_EN;
ret = ax_write_cmd(axdev, AX_PBUS_A32, AX_MAC_CLK_CTRL,
AX_PBUS_REG_BASE_ADDR_HI, 4, ®32);
if (ret < 0)
return ret;
return 0;
}
void ax88279_ptp_remove(struct ax_device *axdev)
{
u32 reg32 = 0;
#ifdef ENABLE_PTP_FUNC
axdev->driver_info->ptp_pps_ctrl(axdev, 0);
#endif
ax_ptp_pbus_write(axdev, AX_PTP_LCK_CTRL0, 4, ®32);
ax_ptp_pbus_write(axdev, AX_PTP_RX_CTRL0, 4, ®32);
ax_ptp_pbus_write(axdev, AX_PTP_TX_CTRL0, 4, ®32);
}
static int ax88279_submit_ts(struct ax_device *axdev);
static void ax88279_read_ts_callback(struct urb *urb)