Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

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

#include <linux/netdevice.h>

#include <linux/ptp_clock_kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/net_tstamp.h>
#include "lan743x_main.h"

#include "lan743x_ptp.h"

#define LAN743X_LED0_ENABLE		20	/* LED0 offset in HW_CFG */
#define LAN743X_LED_ENABLE(pin)		BIT(LAN743X_LED0_ENABLE + (pin))

#define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB		(31249999)
#define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM	(2047999934)

static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
				  u32 seconds, u32 nano_seconds,
				  u32 sub_nano_seconds);

static int lan743x_get_channel(u32 ch_map)
{
	int idx;

	for (idx = 0; idx < 32; idx++) {
		if (ch_map & (0x1 << idx))
			return idx;
	}

	return -EINVAL;
}

int lan743x_gpio_init(struct lan743x_adapter *adapter)
{
	struct lan743x_gpio *gpio = &adapter->gpio;

	spin_lock_init(&gpio->gpio_lock);

	gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
	gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
	gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
	gpio->gpio_cfg3 = 0;/* disable all 1588 output */
	lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
	lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
	lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
	lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);

	return 0;
}

static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
					   u32 bit_mask)
{
	int timeout = 1000;
	u32 data = 0;

	while (timeout &&
	       (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
	       bit_mask))) {
		usleep_range(1000, 20000);
		timeout--;
	}
	if (data) {
		netif_err(adapter, drv, adapter->netdev,
			  "timeout waiting for cmd to be done, cmd = 0x%08X\n",
			  bit_mask);
	}
}

static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
					 u32 seconds, u32 nano_seconds,
					 u32 header)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	spin_lock_bh(&ptp->tx_ts_lock);
	if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
		ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
		ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
		ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
		ptp->tx_ts_queue_size++;
	} else {
		netif_err(adapter, drv, adapter->netdev,
			  "tx ts queue overflow\n");
	}
	spin_unlock_bh(&ptp->tx_ts_lock);
}

static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	struct skb_shared_hwtstamps tstamps;
	u32 header, nseconds, seconds;
	bool ignore_sync = false;
	struct sk_buff *skb;
	int c, i;

	spin_lock_bh(&ptp->tx_ts_lock);
	c = ptp->tx_ts_skb_queue_size;

	if (c > ptp->tx_ts_queue_size)
		c = ptp->tx_ts_queue_size;
	if (c <= 0)
		goto done;

	for (i = 0; i < c; i++) {
		ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
				BIT(i)) != 0);
		skb = ptp->tx_ts_skb_queue[i];
		nseconds = ptp->tx_ts_nseconds_queue[i];
		seconds = ptp->tx_ts_seconds_queue[i];
		header = ptp->tx_ts_header_queue[i];

		memset(&tstamps, 0, sizeof(tstamps));
		tstamps.hwtstamp = ktime_set(seconds, nseconds);
		if (!ignore_sync ||
		    ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
		    PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
			skb_tstamp_tx(skb, &tstamps);

		dev_kfree_skb(skb);

		ptp->tx_ts_skb_queue[i] = NULL;
		ptp->tx_ts_seconds_queue[i] = 0;
		ptp->tx_ts_nseconds_queue[i] = 0;
		ptp->tx_ts_header_queue[i] = 0;
	}

	/* shift queue */
	ptp->tx_ts_ignore_sync_queue >>= c;
	for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
		ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
		ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
		ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
		ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];

		ptp->tx_ts_skb_queue[i] = NULL;
		ptp->tx_ts_seconds_queue[i] = 0;
		ptp->tx_ts_nseconds_queue[i] = 0;
		ptp->tx_ts_header_queue[i] = 0;
	}
	ptp->tx_ts_skb_queue_size -= c;
	ptp->tx_ts_queue_size -= c;
done:
	ptp->pending_tx_timestamps -= c;
	spin_unlock_bh(&ptp->tx_ts_lock);
}

static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter,
					int event_channel)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	int result = -ENODEV;

	mutex_lock(&ptp->command_lock);
	if (!(test_bit(event_channel, &ptp->used_event_ch))) {
		ptp->used_event_ch |= BIT(event_channel);
		result = event_channel;
	} else {
		netif_warn(adapter, drv, adapter->netdev,
			   "attempted to reserved a used event_channel = %d\n",
			   event_channel);
	}
	mutex_unlock(&ptp->command_lock);
	return result;
}

static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
					 int event_channel)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	mutex_lock(&ptp->command_lock);
	if (test_bit(event_channel, &ptp->used_event_ch)) {
		ptp->used_event_ch &= ~BIT(event_channel);
	} else {
		netif_warn(adapter, drv, adapter->netdev,
			   "attempted release on a not used event_channel = %d\n",
			   event_channel);
	}
	mutex_unlock(&ptp->command_lock);
}

static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
				  u32 *seconds, u32 *nano_seconds,
				  u32 *sub_nano_seconds);
static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
				     u32 *sec, u32 *nsec, u32 *sub_nsec);
static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
				   s64 time_step_ns);

static void lan743x_led_mux_enable(struct lan743x_adapter *adapter,
				   int pin, bool enable)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	if (ptp->leds_multiplexed &&
	    ptp->led_enabled[pin]) {
		u32 val = lan743x_csr_read(adapter, HW_CFG);

		if (enable)
			val |= LAN743X_LED_ENABLE(pin);
		else
			val &= ~LAN743X_LED_ENABLE(pin);

		lan743x_csr_write(adapter, HW_CFG, val);
	}
}

static void lan743x_led_mux_save(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;

	if (id_rev == ID_REV_ID_LAN7430_) {
		int i;
		u32 val = lan743x_csr_read(adapter, HW_CFG);

		for (i = 0; i < LAN7430_N_LED; i++) {
			bool led_enabled = (val & LAN743X_LED_ENABLE(i)) != 0;

			ptp->led_enabled[i] = led_enabled;
		}
		ptp->leds_multiplexed = true;
	} else {
		ptp->leds_multiplexed = false;
	}
}

static void lan743x_led_mux_restore(struct lan743x_adapter *adapter)
{
	u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;

	if (id_rev == ID_REV_ID_LAN7430_) {
		int i;

		for (i = 0; i < LAN7430_N_LED; i++)
			lan743x_led_mux_enable(adapter, i, true);
	}
}

static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
				     int pin, int event_channel)
{
	struct lan743x_gpio *gpio = &adapter->gpio;
	unsigned long irq_flags = 0;
	int bit_mask = BIT(pin);
	int ret = -EBUSY;

	spin_lock_irqsave(&gpio->gpio_lock, irq_flags);

	if (!(gpio->used_bits & bit_mask)) {
		gpio->used_bits |= bit_mask;
		gpio->output_bits |= bit_mask;
		gpio->ptp_bits |= bit_mask;

		/* assign pin to GPIO function */
		lan743x_led_mux_enable(adapter, pin, false);

		/* set as output, and zero initial value */
		gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(pin);
		gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
		lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);

		/* enable gpio, and set buffer type to push pull */
		gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(pin);
		gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(pin);
		lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);

		/* set 1588 polarity to high */
		gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(pin);
		lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);

		if (event_channel == 0) {
			/* use channel A */
			gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(pin);
		} else {
			/* use channel B */
			gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(pin);
		}
		gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(pin);
		lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);

		ret = pin;
	}
	spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
	return ret;
}

static void lan743x_gpio_release(struct lan743x_adapter *adapter, int pin)
{
	struct lan743x_gpio *gpio = &adapter->gpio;
	unsigned long irq_flags = 0;
	int bit_mask = BIT(pin);

	spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
	if (gpio->used_bits & bit_mask) {
		gpio->used_bits &= ~bit_mask;
		if (gpio->output_bits & bit_mask) {
			gpio->output_bits &= ~bit_mask;

			if (gpio->ptp_bits & bit_mask) {
				gpio->ptp_bits &= ~bit_mask;
				/* disable ptp output */
				gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(pin);
				lan743x_csr_write(adapter, GPIO_CFG3,
						  gpio->gpio_cfg3);
			}
			/* release gpio output */

			/* disable gpio */
			gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(pin);
			gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(pin);
			lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);

			/* reset back to input */
			gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(pin);
			gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
			lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);

			/* assign pin to original function */
			lan743x_led_mux_enable(adapter, pin, true);
		}
	}
	spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
}

static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
{
	struct lan743x_ptp *ptp =
		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(ptp, struct lan743x_adapter, ptp);
	u32 lan743x_rate_adj = 0;
	u64 u64_delta;

	if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
	    scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
		return -EINVAL;
	}

	/* diff_by_scaled_ppm returns true if the difference is negative */
	if (diff_by_scaled_ppm(1ULL << 35, scaled_ppm, &u64_delta))
		lan743x_rate_adj = (u32)u64_delta;
	else
		lan743x_rate_adj = (u32)u64_delta | PTP_CLOCK_RATE_ADJ_DIR_;

	lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
			  lan743x_rate_adj);

	return 0;
}

static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
{
	struct lan743x_ptp *ptp =
		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(ptp, struct lan743x_adapter, ptp);

	lan743x_ptp_clock_step(adapter, delta);

	return 0;
}

static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
				   struct timespec64 *ts)
{
	struct lan743x_ptp *ptp =
		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(ptp, struct lan743x_adapter, ptp);
	u32 nano_seconds = 0;
	u32 seconds = 0;

	if (adapter->is_pci11x1x)
		lan743x_ptp_io_clock_get(adapter, &seconds, &nano_seconds,
					 NULL);
	else
		lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
	ts->tv_sec = seconds;
	ts->tv_nsec = nano_seconds;

	return 0;
}

static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
				   const struct timespec64 *ts)
{
	struct lan743x_ptp *ptp =
		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(ptp, struct lan743x_adapter, ptp);
	u32 nano_seconds = 0;
	u32 seconds = 0;

	if (ts) {
		if (ts->tv_sec > 0xFFFFFFFFLL ||
		    ts->tv_sec < 0) {
			netif_warn(adapter, drv, adapter->netdev,
				   "ts->tv_sec out of range, %lld\n",
				   ts->tv_sec);
			return -ERANGE;
		}
		if (ts->tv_nsec >= 1000000000L ||
		    ts->tv_nsec < 0) {
			netif_warn(adapter, drv, adapter->netdev,
				   "ts->tv_nsec out of range, %ld\n",
				   ts->tv_nsec);
			return -ERANGE;
		}
		seconds = ts->tv_sec;
		nano_seconds = ts->tv_nsec;
		lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
	} else {
		netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
		return -EINVAL;
	}

	return 0;
}

static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter,
				   unsigned int index)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	u32 general_config = 0;
	struct lan743x_ptp_perout *perout = &ptp->perout[index];

	if (perout->gpio_pin >= 0) {
		lan743x_gpio_release(adapter, perout->gpio_pin);
		perout->gpio_pin = -1;
	}

	if (perout->event_ch >= 0) {
		/* set target to far in the future, effectively disabling it */
		lan743x_csr_write(adapter,
				  PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
				  0xFFFF0000);
		lan743x_csr_write(adapter,
				  PTP_CLOCK_TARGET_NS_X(perout->event_ch),
				  0);

		general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
		general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
				  (perout->event_ch);
		lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
		lan743x_ptp_release_event_ch(adapter, perout->event_ch);
		perout->event_ch = -1;
	}
}

static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
			      struct ptp_perout_request *perout_request)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	u32 period_sec = 0, period_nsec = 0;
	u32 start_sec = 0, start_nsec = 0;
	u32 general_config = 0;
	int pulse_width = 0;
	int perout_pin = 0;
	unsigned int index = perout_request->index;
	struct lan743x_ptp_perout *perout = &ptp->perout[index];
	int ret = 0;

	/* Reject requests with unsupported flags */
	if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
		return -EOPNOTSUPP;

	if (on) {
		perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
					  perout_request->index);
		if (perout_pin < 0)
			return -EBUSY;
	} else {
		lan743x_ptp_perout_off(adapter, index);
		return 0;
	}

	if (perout->event_ch >= 0 ||
	    perout->gpio_pin >= 0) {
		/* already on, turn off first */
		lan743x_ptp_perout_off(adapter, index);
	}

	perout->event_ch = lan743x_ptp_reserve_event_ch(adapter, index);

	if (perout->event_ch < 0) {
		netif_warn(adapter, drv, adapter->netdev,
			   "Failed to reserve event channel %d for PEROUT\n",
			   index);
		ret = -EBUSY;
		goto failed;
	}

	perout->gpio_pin = lan743x_gpio_rsrv_ptp_out(adapter,
						     perout_pin,
						     perout->event_ch);

	if (perout->gpio_pin < 0) {
		netif_warn(adapter, drv, adapter->netdev,
			   "Failed to reserve gpio %d for PEROUT\n",
			   perout_pin);
		ret = -EBUSY;
		goto failed;
	}

	start_sec = perout_request->start.sec;
	start_sec += perout_request->start.nsec / 1000000000;
	start_nsec = perout_request->start.nsec % 1000000000;

	period_sec = perout_request->period.sec;
	period_sec += perout_request->period.nsec / 1000000000;
	period_nsec = perout_request->period.nsec % 1000000000;

	if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
		struct timespec64 ts_on, ts_period;
		s64 wf_high, period64, half;
		s32 reminder;

		ts_on.tv_sec = perout_request->on.sec;
		ts_on.tv_nsec = perout_request->on.nsec;
		wf_high = timespec64_to_ns(&ts_on);
		ts_period.tv_sec = perout_request->period.sec;
		ts_period.tv_nsec = perout_request->period.nsec;
		period64 = timespec64_to_ns(&ts_period);

		if (period64 < 200) {
			netif_warn(adapter, drv, adapter->netdev,
				   "perout period too small, minimum is 200nS\n");
			ret = -EOPNOTSUPP;
			goto failed;
		}
		if (wf_high >= period64) {
			netif_warn(adapter, drv, adapter->netdev,
				   "pulse width must be smaller than period\n");
			ret = -EINVAL;
			goto failed;
		}

		/* Check if we can do 50% toggle on an even value of period.
		 * If the period number is odd, then check if the requested
		 * pulse width is the same as one of pre-defined width values.
		 * Otherwise, return failure.
		 */
		half = div_s64_rem(period64, 2, &reminder);
		if (!reminder) {
			if (half == wf_high) {
				/* It's 50% match. Use the toggle option */
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
				/* In this case, devide period value by 2 */
				ts_period = ns_to_timespec64(div_s64(period64, 2));
				period_sec = ts_period.tv_sec;
				period_nsec = ts_period.tv_nsec;

				goto program;
			}
		}
		/* if we can't do toggle, then the width option needs to be the exact match */
		if (wf_high == 200000000) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
		} else if (wf_high == 10000000) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
		} else if (wf_high == 1000000) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
		} else if (wf_high == 100000) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
		} else if (wf_high == 10000) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
		} else if (wf_high == 100) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
		} else {
			netif_warn(adapter, drv, adapter->netdev,
				   "duty cycle specified is not supported\n");
			ret = -EOPNOTSUPP;
			goto failed;
		}
	} else {
		if (period_sec == 0) {
			if (period_nsec >= 400000000) {
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
			} else if (period_nsec >= 20000000) {
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
			} else if (period_nsec >= 2000000) {
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
			} else if (period_nsec >= 200000) {
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
			} else if (period_nsec >= 20000) {
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
			} else if (period_nsec >= 200) {
				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
			} else {
				netif_warn(adapter, drv, adapter->netdev,
					   "perout period too small, minimum is 200nS\n");
				ret = -EOPNOTSUPP;
				goto failed;
			}
		} else {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
		}
	}
program:

	/* turn off by setting target far in future */
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
			  0xFFFF0000);
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_NS_X(perout->event_ch), 0);

	/* Configure to pulse every period */
	general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
	general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
			  (perout->event_ch));
	general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
			  (perout->event_ch, pulse_width);
	general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
			  (perout->event_ch);
	lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);

	/* set the reload to one toggle cycle */
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_RELOAD_SEC_X(perout->event_ch),
			  period_sec);
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_RELOAD_NS_X(perout->event_ch),
			  period_nsec);

	/* set the start time */
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
			  start_sec);
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_NS_X(perout->event_ch),
			  start_nsec);

	return 0;

failed:
	lan743x_ptp_perout_off(adapter, index);
	return ret;
}

static void lan743x_ptp_io_perout_off(struct lan743x_adapter *adapter,
				      u32 index)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	int perout_pin;
	int event_ch;
	u32 gen_cfg;
	int val;

	event_ch = ptp->ptp_io_perout[index];
	if (event_ch >= 0) {
		/* set target to far in the future, effectively disabling it */
		lan743x_csr_write(adapter,
				  PTP_CLOCK_TARGET_SEC_X(event_ch),
				  0xFFFF0000);
		lan743x_csr_write(adapter,
				  PTP_CLOCK_TARGET_NS_X(event_ch),
				  0);

		gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
		gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
				    (event_ch));
		gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch));
		gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch);
		lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
		if (event_ch)
			lan743x_csr_write(adapter, PTP_INT_STS,
					  PTP_INT_TIMER_INT_B_);
		else
			lan743x_csr_write(adapter, PTP_INT_STS,
					  PTP_INT_TIMER_INT_A_);
		lan743x_ptp_release_event_ch(adapter, event_ch);
		ptp->ptp_io_perout[index] = -1;
	}

	perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);

	/* Deselect Event output */
	val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);

	/* Disables the output of Local Time Target compare events */
	val &= ~PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
	lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);

	/* Configured as an opendrain driver*/
	val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
	val &= ~PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
	lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
	/* Dummy read to make sure write operation success */
	val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
}

static int lan743x_ptp_io_perout(struct lan743x_adapter *adapter, int on,
				 struct ptp_perout_request *perout_request)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	u32 period_sec, period_nsec;
	u32 start_sec, start_nsec;
	u32 pulse_sec, pulse_nsec;
	int pulse_width;
	int perout_pin;
	int event_ch;
	u32 gen_cfg;
	u32 index;
	int val;

	index = perout_request->index;
	event_ch = ptp->ptp_io_perout[index];

	if (on) {
		perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
		if (perout_pin < 0)
			return -EBUSY;
	} else {
		lan743x_ptp_io_perout_off(adapter, index);
		return 0;
	}

	if (event_ch >= LAN743X_PTP_N_EVENT_CHAN) {
		/* already on, turn off first */
		lan743x_ptp_io_perout_off(adapter, index);
	}

	event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
	if (event_ch < 0) {
		netif_warn(adapter, drv, adapter->netdev,
			   "Failed to reserve event channel %d for PEROUT\n",
			   index);
		goto failed;
	}
	ptp->ptp_io_perout[index] = event_ch;

	if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
		pulse_sec = perout_request->on.sec;
		pulse_sec += perout_request->on.nsec / 1000000000;
		pulse_nsec = perout_request->on.nsec % 1000000000;
	} else {
		pulse_sec = perout_request->period.sec;
		pulse_sec += perout_request->period.nsec / 1000000000;
		pulse_nsec = perout_request->period.nsec % 1000000000;
	}

	if (pulse_sec == 0) {
		if (pulse_nsec >= 400000000) {
			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
		} else if (pulse_nsec >= 200000000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100MS_;
		} else if (pulse_nsec >= 100000000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50MS_;
		} else if (pulse_nsec >= 20000000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
		} else if (pulse_nsec >= 10000000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5MS_;
		} else if (pulse_nsec >= 2000000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
		} else if (pulse_nsec >= 1000000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500US_;
		} else if (pulse_nsec >= 200000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
		} else if (pulse_nsec >= 100000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50US_;
		} else if (pulse_nsec >= 20000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
		} else if (pulse_nsec >= 10000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5US_;
		} else if (pulse_nsec >= 2000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1US_;
		} else if (pulse_nsec >= 1000) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500NS_;
		} else if (pulse_nsec >= 200) {
			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
		} else {
			netif_warn(adapter, drv, adapter->netdev,
				   "perout period too small, min is 200nS\n");
			goto failed;
		}
	} else {
		pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
	}

	/* turn off by setting target far in future */
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_SEC_X(event_ch),
			  0xFFFF0000);
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_NS_X(event_ch), 0);

	/* Configure to pulse every period */
	gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
	gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_(event_ch));
	gen_cfg |= HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
			  (event_ch, pulse_width);
	gen_cfg |= HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch);
	gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch));
	lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);

	/* set the reload to one toggle cycle */
	period_sec = perout_request->period.sec;
	period_sec += perout_request->period.nsec / 1000000000;
	period_nsec = perout_request->period.nsec % 1000000000;
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_RELOAD_SEC_X(event_ch),
			  period_sec);
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_RELOAD_NS_X(event_ch),
			  period_nsec);

	start_sec = perout_request->start.sec;
	start_sec += perout_request->start.nsec / 1000000000;
	start_nsec = perout_request->start.nsec % 1000000000;

	/* set the start time */
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_SEC_X(event_ch),
			  start_sec);
	lan743x_csr_write(adapter,
			  PTP_CLOCK_TARGET_NS_X(event_ch),
			  start_nsec);

	/* Enable LTC Target Read */
	val = lan743x_csr_read(adapter, PTP_CMD_CTL);
	val |= PTP_CMD_CTL_PTP_LTC_TARGET_READ_;
	lan743x_csr_write(adapter, PTP_CMD_CTL, val);

	/* Configure as an push/pull driver */
	val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
	val |= PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
	lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);

	/* Select Event output */
	val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
	if (event_ch)
		/* Channel B as the output */
		val |= PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
	else
		/* Channel A as the output */
		val &= ~PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);

	/* Enables the output of Local Time Target compare events */
	val |= PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
	lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);

	return 0;

failed:
	lan743x_ptp_io_perout_off(adapter, index);
	return -ENODEV;
}

static void lan743x_ptp_io_extts_off(struct lan743x_adapter *adapter,
				     u32 index)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	struct lan743x_extts *extts;
	int val;

	extts = &ptp->extts[index];
	/* PTP Interrupt Enable Clear Register */
	if (extts->flags & PTP_FALLING_EDGE)
		val = PTP_INT_EN_FE_EN_CLR_(index);
	else
		val = PTP_INT_EN_RE_EN_CLR_(index);
	lan743x_csr_write(adapter, PTP_INT_EN_CLR, val);

	/* Disables PTP-IO edge lock */
	val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
	if (extts->flags & PTP_FALLING_EDGE) {
		val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(index);
		val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(index);
	} else {
		val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(index);
		val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(index);
	}
	lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);

	/* PTP-IO De-select register */
	val = lan743x_csr_read(adapter, PTP_IO_SEL);
	val &= ~PTP_IO_SEL_MASK_;
	lan743x_csr_write(adapter, PTP_IO_SEL, val);

	/* Clear timestamp */
	memset(&extts->ts, 0, sizeof(struct timespec64));
	extts->flags = 0;
}

static int lan743x_ptp_io_event_cap_en(struct lan743x_adapter *adapter,
				       u32 flags, u32 channel)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	int val;

	if ((flags & PTP_EXTTS_EDGES) ==  PTP_EXTTS_EDGES)
		return -EOPNOTSUPP;

	mutex_lock(&ptp->command_lock);
	/* PTP-IO Event Capture Enable */
	val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
	if (flags & PTP_FALLING_EDGE) {
		val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
		val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
		val |= PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
		val |= PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
	} else {
		/* Rising eventing as Default */
		val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
		val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
		val |= PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
		val |= PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
	}
	lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);

	/* PTP-IO Select */
	val = lan743x_csr_read(adapter, PTP_IO_SEL);
	val &= ~PTP_IO_SEL_MASK_;
	val |= channel << PTP_IO_SEL_SHIFT_;
	lan743x_csr_write(adapter, PTP_IO_SEL, val);

	/* PTP Interrupt Enable Register */
	if (flags & PTP_FALLING_EDGE)
		val = PTP_INT_EN_FE_EN_SET_(channel);
	else
		val = PTP_INT_EN_RE_EN_SET_(channel);
	lan743x_csr_write(adapter, PTP_INT_EN_SET, val);

	mutex_unlock(&ptp->command_lock);

	return 0;
}

static int lan743x_ptp_io_extts(struct lan743x_adapter *adapter, int on,
				struct ptp_extts_request *extts_request)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	u32 flags = extts_request->flags;
	u32 index = extts_request->index;
	struct lan743x_extts *extts;
	int extts_pin;
	int ret = 0;

	extts = &ptp->extts[index];

	if (on) {
		extts_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, index);
		if (extts_pin < 0)
			return -EBUSY;

		ret = lan743x_ptp_io_event_cap_en(adapter, flags, index);
		if (!ret)
			extts->flags = flags;
	} else {
		lan743x_ptp_io_extts_off(adapter, index);
	}

	return ret;
}

static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
				struct ptp_clock_request *request, int on)
{
	struct lan743x_ptp *ptp =
		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(ptp, struct lan743x_adapter, ptp);

	if (request) {
		switch (request->type) {
		case PTP_CLK_REQ_EXTTS:
			if (request->extts.index < ptpci->n_ext_ts)
				return lan743x_ptp_io_extts(adapter, on,
							 &request->extts);
			return -EINVAL;
		case PTP_CLK_REQ_PEROUT:
			if (request->perout.index < ptpci->n_per_out) {
				if (adapter->is_pci11x1x)
					return lan743x_ptp_io_perout(adapter, on,
							     &request->perout);
				else
					return lan743x_ptp_perout(adapter, on,
							  &request->perout);
			}
			return -EINVAL;
		case PTP_CLK_REQ_PPS:
			return -EINVAL;
		default:
			netif_err(adapter, drv, adapter->netdev,
				  "request->type == %d, Unknown\n",
				  request->type);
			break;
		}
	} else {
		netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
	}
	return 0;
}

static int lan743x_ptpci_verify_pin_config(struct ptp_clock_info *ptp,
					   unsigned int pin,
					   enum ptp_pin_function func,
					   unsigned int chan)
{
	struct lan743x_ptp *lan_ptp =
		container_of(ptp, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(lan_ptp, struct lan743x_adapter, ptp);
	int result = 0;

	/* Confirm the requested function is supported. Parameter
	 * validation is done by the caller.
	 */
	switch (func) {
	case PTP_PF_NONE:
	case PTP_PF_PEROUT:
		break;
	case PTP_PF_EXTTS:
		if (!adapter->is_pci11x1x)
			result = -1;
		break;
	case PTP_PF_PHYSYNC:
	default:
		result = -1;
		break;
	}
	return result;
}

static void lan743x_ptp_io_event_clock_get(struct lan743x_adapter *adapter,
					   bool fe, u8 channel,
					   struct timespec64 *ts)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	struct lan743x_extts *extts;
	u32 sec, nsec;

	mutex_lock(&ptp->command_lock);
	if (fe) {
		sec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_SEC_CAP_X);
		nsec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_NS_CAP_X);
	} else {
		sec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_SEC_CAP_X);
		nsec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_NS_CAP_X);
	}

	mutex_unlock(&ptp->command_lock);

	/* Update Local timestamp */
	extts = &ptp->extts[channel];
	extts->ts.tv_sec = sec;
	extts->ts.tv_nsec = nsec;
	ts->tv_sec = sec;
	ts->tv_nsec = nsec;
}

static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
{
	struct lan743x_ptp *ptp =
		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
	struct lan743x_adapter *adapter =
		container_of(ptp, struct lan743x_adapter, ptp);
	u32 cap_info, cause, header, nsec, seconds;
	bool new_timestamp_available = false;
	struct ptp_clock_event ptp_event;
	struct timespec64 ts;
	int ptp_int_sts;
	int count = 0;
	int channel;
	s64 ns;

	ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
	while ((count < 100) && ptp_int_sts) {
		count++;

		if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
			cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);

			if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
				seconds = lan743x_csr_read(adapter,
							   PTP_TX_EGRESS_SEC);
				nsec = lan743x_csr_read(adapter,
							PTP_TX_EGRESS_NS);
				cause = (nsec &
					 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
				header = lan743x_csr_read(adapter,
							  PTP_TX_MSG_HEADER);

				if (cause ==
				    PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
					nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
					lan743x_ptp_tx_ts_enqueue_ts(adapter,
								     seconds,
								     nsec,
								     header);
					new_timestamp_available = true;
				} else if (cause ==
					   PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
					netif_err(adapter, drv, adapter->netdev,
						  "Auto capture cause not supported\n");
				} else {
					netif_warn(adapter, drv, adapter->netdev,
						   "unknown tx timestamp capture cause\n");
				}
			} else {
				netif_warn(adapter, drv, adapter->netdev,
					   "TX TS INT but no TX TS CNT\n");
			}
			lan743x_csr_write(adapter, PTP_INT_STS,
					  PTP_INT_BIT_TX_TS_);
		}

		if (ptp_int_sts & PTP_INT_IO_FE_MASK_) {
			do {
				channel = lan743x_get_channel((ptp_int_sts &
							PTP_INT_IO_FE_MASK_) >>
							PTP_INT_IO_FE_SHIFT_);
				if (channel >= 0 &&
				    channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
					lan743x_ptp_io_event_clock_get(adapter,
								       true,
								       channel,
								       &ts);
					/* PTP Falling Event post */
					ns = timespec64_to_ns(&ts);
					ptp_event.timestamp = ns;
					ptp_event.index = channel;
					ptp_event.type = PTP_CLOCK_EXTTS;
					ptp_clock_event(ptp->ptp_clock,
							&ptp_event);
					lan743x_csr_write(adapter, PTP_INT_STS,
							  PTP_INT_IO_FE_SET_
							  (channel));
					ptp_int_sts &= ~(1 <<
							 (PTP_INT_IO_FE_SHIFT_ +
							  channel));
				} else {
					/* Clear falling event interrupts */
					lan743x_csr_write(adapter, PTP_INT_STS,
							  PTP_INT_IO_FE_MASK_);
					ptp_int_sts &= ~PTP_INT_IO_FE_MASK_;
				}
			} while (ptp_int_sts & PTP_INT_IO_FE_MASK_);
		}

		if (ptp_int_sts & PTP_INT_IO_RE_MASK_) {
			do {
				channel = lan743x_get_channel((ptp_int_sts &
						       PTP_INT_IO_RE_MASK_) >>
						       PTP_INT_IO_RE_SHIFT_);
				if (channel >= 0 &&
				    channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
					lan743x_ptp_io_event_clock_get(adapter,
								       false,
								       channel,
								       &ts);
					/* PTP Rising Event post */
					ns = timespec64_to_ns(&ts);
					ptp_event.timestamp = ns;
					ptp_event.index = channel;
					ptp_event.type = PTP_CLOCK_EXTTS;
					ptp_clock_event(ptp->ptp_clock,
							&ptp_event);
					lan743x_csr_write(adapter, PTP_INT_STS,
							  PTP_INT_IO_RE_SET_
							  (channel));
					ptp_int_sts &= ~(1 <<
							 (PTP_INT_IO_RE_SHIFT_ +
							  channel));
				} else {
					/* Clear Rising event interrupt */
					lan743x_csr_write(adapter, PTP_INT_STS,
							  PTP_INT_IO_RE_MASK_);
					ptp_int_sts &= ~PTP_INT_IO_RE_MASK_;
				}
			} while (ptp_int_sts & PTP_INT_IO_RE_MASK_);
		}

		ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
	}

	if (new_timestamp_available)
		lan743x_ptp_tx_ts_complete(adapter);

	lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);

	return -1;
}

static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
				  u32 *seconds, u32 *nano_seconds,
				  u32 *sub_nano_seconds)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	mutex_lock(&ptp->command_lock);

	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);

	if (seconds)
		(*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);

	if (nano_seconds)
		(*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);

	if (sub_nano_seconds)
		(*sub_nano_seconds) =
		lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);

	mutex_unlock(&ptp->command_lock);
}

static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
				     u32 *sec, u32 *nsec, u32 *sub_nsec)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	mutex_lock(&ptp->command_lock);
	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);

	if (sec)
		(*sec) = lan743x_csr_read(adapter, PTP_LTC_RD_SEC_LO);

	if (nsec)
		(*nsec) = lan743x_csr_read(adapter, PTP_LTC_RD_NS);

	if (sub_nsec)
		(*sub_nsec) =
		lan743x_csr_read(adapter, PTP_LTC_RD_SUBNS);

	mutex_unlock(&ptp->command_lock);
}

static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
				   s64 time_step_ns)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	u32 nano_seconds_step = 0;
	u64 abs_time_step_ns = 0;
	u32 unsigned_seconds = 0;
	u32 nano_seconds = 0;
	u32 remainder = 0;
	s32 seconds = 0;

	if (time_step_ns >  15000000000LL) {
		/* convert to clock set */
		if (adapter->is_pci11x1x)
			lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
						 &nano_seconds, NULL);
		else
			lan743x_ptp_clock_get(adapter, &unsigned_seconds,
					      &nano_seconds, NULL);
		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
						&remainder);
		nano_seconds += remainder;
		if (nano_seconds >= 1000000000) {
			unsigned_seconds++;
			nano_seconds -= 1000000000;
		}
		lan743x_ptp_clock_set(adapter, unsigned_seconds,
				      nano_seconds, 0);
		return;
	} else if (time_step_ns < -15000000000LL) {
		/* convert to clock set */
		time_step_ns = -time_step_ns;

		if (adapter->is_pci11x1x) {
			lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
						 &nano_seconds, NULL);
		} else {
			lan743x_ptp_clock_get(adapter, &unsigned_seconds,
					      &nano_seconds, NULL);
		}
		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
						&remainder);
		nano_seconds_step = remainder;
		if (nano_seconds < nano_seconds_step) {
			unsigned_seconds--;
			nano_seconds += 1000000000;
		}
		nano_seconds -= nano_seconds_step;
		lan743x_ptp_clock_set(adapter, unsigned_seconds,
				      nano_seconds, 0);
		return;
	}

	/* do clock step */
	if (time_step_ns >= 0) {
		abs_time_step_ns = (u64)(time_step_ns);
		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
					   &remainder);
		nano_seconds = (u32)remainder;
	} else {
		abs_time_step_ns = (u64)(-time_step_ns);
		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
					     &remainder));
		nano_seconds = (u32)remainder;
		if (nano_seconds > 0) {
			/* subtracting nano seconds is not allowed
			 * convert to subtracting from seconds,
			 * and adding to nanoseconds
			 */
			seconds--;
			nano_seconds = (1000000000 - nano_seconds);
		}
	}

	if (nano_seconds > 0) {
		/* add 8 ns to cover the likely normal increment */
		nano_seconds += 8;
	}

	if (nano_seconds >= 1000000000) {
		/* carry into seconds */
		seconds++;
		nano_seconds -= 1000000000;
	}

	while (seconds) {
		mutex_lock(&ptp->command_lock);
		if (seconds > 0) {
			u32 adjustment_value = (u32)seconds;

			if (adjustment_value > 0xF)
				adjustment_value = 0xF;
			lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
					  PTP_CLOCK_STEP_ADJ_DIR_ |
					  adjustment_value);
			seconds -= ((s32)adjustment_value);
		} else {
			u32 adjustment_value = (u32)(-seconds);

			if (adjustment_value > 0xF)
				adjustment_value = 0xF;
			lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
					  adjustment_value);
			seconds += ((s32)adjustment_value);
		}
		lan743x_csr_write(adapter, PTP_CMD_CTL,
				  PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
		lan743x_ptp_wait_till_cmd_done(adapter,
					       PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
		mutex_unlock(&ptp->command_lock);
	}
	if (nano_seconds) {
		mutex_lock(&ptp->command_lock);
		lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
				  PTP_CLOCK_STEP_ADJ_DIR_ |
				  (nano_seconds &
				  PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
		lan743x_csr_write(adapter, PTP_CMD_CTL,
				  PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
		lan743x_ptp_wait_till_cmd_done(adapter,
					       PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
		mutex_unlock(&ptp->command_lock);
	}
}

void lan743x_ptp_isr(void *context)
{
	struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
	struct lan743x_ptp *ptp = NULL;
	int enable_flag = 1;
	u32 ptp_int_sts = 0;

	ptp = &adapter->ptp;

	lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);

	ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
	ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);

	if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
		ptp_schedule_worker(ptp->ptp_clock, 0);
		enable_flag = 0;/* tasklet will re-enable later */
	}
	if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
		netif_err(adapter, drv, adapter->netdev,
			  "PTP TX Software Timestamp Error\n");
		/* clear int status bit */
		lan743x_csr_write(adapter, PTP_INT_STS,
				  PTP_INT_BIT_TX_SWTS_ERR_);
	}
	if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
		/* clear int status bit */
		lan743x_csr_write(adapter, PTP_INT_STS,
				  PTP_INT_BIT_TIMER_B_);
	}
	if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
		/* clear int status bit */
		lan743x_csr_write(adapter, PTP_INT_STS,
				  PTP_INT_BIT_TIMER_A_);
	}

	if (enable_flag) {
		/* re-enable isr */
		lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
	}
}

static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
					  struct sk_buff *skb, bool ignore_sync)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	spin_lock_bh(&ptp->tx_ts_lock);
	if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
		ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
		if (ignore_sync)
			ptp->tx_ts_ignore_sync_queue |=
				BIT(ptp->tx_ts_skb_queue_size);
		ptp->tx_ts_skb_queue_size++;
	} else {
		/* this should never happen, so long as the tx channel
		 * calls and honors the result from
		 * lan743x_ptp_request_tx_timestamp
		 */
		netif_err(adapter, drv, adapter->netdev,
			  "tx ts skb queue overflow\n");
		dev_kfree_skb(skb);
	}
	spin_unlock_bh(&ptp->tx_ts_lock);
}

static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
{
	struct timespec64 ts;

	ktime_get_clocktai_ts64(&ts);

	lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
}

void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
				u32 link_speed)
{
	switch (link_speed) {
	case 10:
		lan743x_csr_write(adapter, PTP_LATENCY,
				  PTP_LATENCY_TX_SET_(0) |
				  PTP_LATENCY_RX_SET_(0));
		break;
	case 100:
		lan743x_csr_write(adapter, PTP_LATENCY,
				  PTP_LATENCY_TX_SET_(181) |
				  PTP_LATENCY_RX_SET_(594));
		break;
	case 1000:
		lan743x_csr_write(adapter, PTP_LATENCY,
				  PTP_LATENCY_TX_SET_(30) |
				  PTP_LATENCY_RX_SET_(525));
		break;
	}
}

int lan743x_ptp_init(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	int i;

	mutex_init(&ptp->command_lock);
	spin_lock_init(&ptp->tx_ts_lock);
	ptp->used_event_ch = 0;

	for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) {
		ptp->perout[i].event_ch = -1;
		ptp->perout[i].gpio_pin = -1;
	}

	lan743x_led_mux_save(adapter);

	return 0;
}

int lan743x_ptp_open(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	int ret = -ENODEV;
	u32 temp;
	int i;
	int n_pins;

	lan743x_ptp_reset(adapter);
	lan743x_ptp_sync_to_system_clock(adapter);
	temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
	temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
	lan743x_csr_write(adapter, PTP_TX_MOD2, temp);

	/* Default Timestamping */
	lan743x_rx_set_tstamp_mode(adapter, HWTSTAMP_FILTER_NONE);

	lan743x_ptp_enable(adapter);
	lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
	lan743x_csr_write(adapter, PTP_INT_EN_SET,
			  PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
	ptp->flags |= PTP_FLAG_ISR_ENABLED;

	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
		return 0;

	switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
	case ID_REV_ID_LAN7430_:
		n_pins = LAN7430_N_GPIO;
		break;
	case ID_REV_ID_LAN7431_:
	case ID_REV_ID_A011_:
	case ID_REV_ID_A041_:
		n_pins = LAN7431_N_GPIO;
		break;
	default:
		netif_warn(adapter, drv, adapter->netdev,
			   "Unknown LAN743x (%08x). Assuming no GPIO\n",
			   adapter->csr.id_rev);
		n_pins = 0;
		break;
	}

	if (n_pins > LAN743X_PTP_N_GPIO)
		n_pins = LAN743X_PTP_N_GPIO;

	for (i = 0; i < n_pins; i++) {
		struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i];

		snprintf(ptp_pin->name,
			 sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i);
		ptp_pin->index = i;
		ptp_pin->func = PTP_PF_NONE;
	}

	ptp->ptp_clock_info.owner = THIS_MODULE;
	snprintf(ptp->ptp_clock_info.name, 16, "%pm",
		 adapter->netdev->dev_addr);
	ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
	ptp->ptp_clock_info.n_alarm = 0;
	ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS;
	ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
	ptp->ptp_clock_info.n_pins = n_pins;
	ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
	ptp->ptp_clock_info.pin_config = ptp->pin_config;
	ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
	ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
	ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
	ptp->ptp_clock_info.getcrosststamp = NULL;
	ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
	ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
	ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
	ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config;

	ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
					    &adapter->pdev->dev);

	if (IS_ERR(ptp->ptp_clock)) {
		netif_err(adapter, ifup, adapter->netdev,
			  "ptp_clock_register failed\n");
		goto done;
	}
	ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
	netif_info(adapter, ifup, adapter->netdev,
		   "successfully registered ptp clock\n");

	return 0;
done:
	lan743x_ptp_close(adapter);
	return ret;
}

void lan743x_ptp_close(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	int index;

	if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
	    (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) {
		ptp_clock_unregister(ptp->ptp_clock);
		ptp->ptp_clock = NULL;
		ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
		netif_info(adapter, drv, adapter->netdev,
			   "ptp clock unregister\n");
	}

	if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
		lan743x_csr_write(adapter, PTP_INT_EN_CLR,
				  PTP_INT_BIT_TX_SWTS_ERR_ |
				  PTP_INT_BIT_TX_TS_);
		lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
		ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
	}

	/* clean up pending timestamp requests */
	lan743x_ptp_tx_ts_complete(adapter);
	spin_lock_bh(&ptp->tx_ts_lock);
	for (index = 0;
		index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
		index++) {
		struct sk_buff *skb = ptp->tx_ts_skb_queue[index];

		dev_kfree_skb(skb);
		ptp->tx_ts_skb_queue[index] = NULL;
		ptp->tx_ts_seconds_queue[index] = 0;
		ptp->tx_ts_nseconds_queue[index] = 0;
	}
	ptp->tx_ts_skb_queue_size = 0;
	ptp->tx_ts_queue_size = 0;
	ptp->pending_tx_timestamps = 0;
	spin_unlock_bh(&ptp->tx_ts_lock);

	lan743x_led_mux_restore(adapter);

	lan743x_ptp_disable(adapter);
}

static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
					   bool ts_insert_enable)
{
	u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);

	if (ts_insert_enable)
		ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
	else
		ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;

	lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
}

static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
{
	if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
		return true;
	return false;
}

static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	mutex_lock(&ptp->command_lock);

	if (lan743x_ptp_is_enabled(adapter)) {
		netif_warn(adapter, drv, adapter->netdev,
			   "PTP already enabled\n");
		goto done;
	}
	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
done:
	mutex_unlock(&ptp->command_lock);
}

static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	/* Disable Timestamping */
	lan743x_rx_set_tstamp_mode(adapter, HWTSTAMP_FILTER_NONE);

	mutex_lock(&ptp->command_lock);
	if (!lan743x_ptp_is_enabled(adapter)) {
		netif_warn(adapter, drv, adapter->netdev,
			   "PTP already disabled\n");
		goto done;
	}
	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
done:
	mutex_unlock(&ptp->command_lock);
}

static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	mutex_lock(&ptp->command_lock);

	if (lan743x_ptp_is_enabled(adapter)) {
		netif_err(adapter, drv, adapter->netdev,
			  "Attempting reset while enabled\n");
		goto done;
	}

	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
done:
	mutex_unlock(&ptp->command_lock);
}

static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
				  u32 seconds, u32 nano_seconds,
				  u32 sub_nano_seconds)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	mutex_lock(&ptp->command_lock);

	lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
	lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
	lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);

	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
	mutex_unlock(&ptp->command_lock);
}

bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;
	bool result = false;

	spin_lock_bh(&ptp->tx_ts_lock);
	if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
		/* request granted */
		ptp->pending_tx_timestamps++;
		result = true;
	}
	spin_unlock_bh(&ptp->tx_ts_lock);
	return result;
}

void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
{
	struct lan743x_ptp *ptp = &adapter->ptp;

	spin_lock_bh(&ptp->tx_ts_lock);
	if (ptp->pending_tx_timestamps > 0)
		ptp->pending_tx_timestamps--;
	else
		netif_err(adapter, drv, adapter->netdev,
			  "unrequest failed, pending_tx_timestamps==0\n");
	spin_unlock_bh(&ptp->tx_ts_lock);
}

void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
				  struct sk_buff *skb, bool ignore_sync)
{
	lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);

	lan743x_ptp_tx_ts_complete(adapter);
}

int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
{
	struct lan743x_adapter *adapter = netdev_priv(netdev);
	struct hwtstamp_config config;
	int ret = 0;
	int index;

	if (!ifr) {
		netif_err(adapter, drv, adapter->netdev,
			  "SIOCSHWTSTAMP, ifr == NULL\n");
		return -EINVAL;
	}

	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
		return -EFAULT;

	switch (config.tx_type) {
	case HWTSTAMP_TX_OFF:
		for (index = 0; index < adapter->used_tx_channels;
		     index++)
			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
							 false, false);
		lan743x_ptp_set_sync_ts_insert(adapter, false);
		break;
	case HWTSTAMP_TX_ON:
		for (index = 0; index < adapter->used_tx_channels;
			index++)
			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
							 true, false);
		lan743x_ptp_set_sync_ts_insert(adapter, false);
		break;
	case HWTSTAMP_TX_ONESTEP_SYNC:
		for (index = 0; index < adapter->used_tx_channels;
			index++)
			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
							 true, true);

		lan743x_ptp_set_sync_ts_insert(adapter, true);
		break;
	case HWTSTAMP_TX_ONESTEP_P2P:
		ret = -ERANGE;
		break;
	default:
		netif_warn(adapter, drv, adapter->netdev,
			   "  tx_type = %d, UNKNOWN\n", config.tx_type);
		ret = -EINVAL;
		break;
	}

	ret = lan743x_rx_set_tstamp_mode(adapter, config.rx_filter);

	if (!ret)
		return copy_to_user(ifr->ifr_data, &config,
			sizeof(config)) ? -EFAULT : 0;
	return ret;
}