0001
0002
0003
0004 #include "ice_common.h"
0005 #include "ice_ptp_hw.h"
0006 #include "ice_ptp_consts.h"
0007 #include "ice_cgu_regs.h"
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 u8 ice_get_ptp_src_clock_index(struct ice_hw *hw)
0079 {
0080 return hw->func_caps.ts_func_info.tmr_index_assoc;
0081 }
0082
0083
0084
0085
0086
0087
0088
0089 static u64 ice_ptp_read_src_incval(struct ice_hw *hw)
0090 {
0091 u32 lo, hi;
0092 u8 tmr_idx;
0093
0094 tmr_idx = ice_get_ptp_src_clock_index(hw);
0095
0096 lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
0097 hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
0098
0099 return ((u64)(hi & INCVAL_HIGH_M) << 32) | lo;
0100 }
0101
0102
0103
0104
0105
0106
0107
0108
0109 static void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
0110 {
0111 u32 cmd_val;
0112 u8 tmr_idx;
0113
0114 tmr_idx = ice_get_ptp_src_clock_index(hw);
0115 cmd_val = tmr_idx << SEL_CPK_SRC;
0116
0117 switch (cmd) {
0118 case INIT_TIME:
0119 cmd_val |= GLTSYN_CMD_INIT_TIME;
0120 break;
0121 case INIT_INCVAL:
0122 cmd_val |= GLTSYN_CMD_INIT_INCVAL;
0123 break;
0124 case ADJ_TIME:
0125 cmd_val |= GLTSYN_CMD_ADJ_TIME;
0126 break;
0127 case ADJ_TIME_AT_TIME:
0128 cmd_val |= GLTSYN_CMD_ADJ_INIT_TIME;
0129 break;
0130 case READ_TIME:
0131 cmd_val |= GLTSYN_CMD_READ_TIME;
0132 break;
0133 }
0134
0135 wr32(hw, GLTSYN_CMD, cmd_val);
0136 }
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146 static void ice_ptp_exec_tmr_cmd(struct ice_hw *hw)
0147 {
0148 wr32(hw, GLTSYN_CMD_SYNC, SYNC_EXEC_CMD);
0149 ice_flush(hw);
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 static void
0164 ice_fill_phy_msg_e822(struct ice_sbq_msg_input *msg, u8 port, u16 offset)
0165 {
0166 int phy_port, phy, quadtype;
0167
0168 phy_port = port % ICE_PORTS_PER_PHY;
0169 phy = port / ICE_PORTS_PER_PHY;
0170 quadtype = (port / ICE_PORTS_PER_QUAD) % ICE_NUM_QUAD_TYPE;
0171
0172 if (quadtype == 0) {
0173 msg->msg_addr_low = P_Q0_L(P_0_BASE + offset, phy_port);
0174 msg->msg_addr_high = P_Q0_H(P_0_BASE + offset, phy_port);
0175 } else {
0176 msg->msg_addr_low = P_Q1_L(P_4_BASE + offset, phy_port);
0177 msg->msg_addr_high = P_Q1_H(P_4_BASE + offset, phy_port);
0178 }
0179
0180 if (phy == 0)
0181 msg->dest_dev = rmn_0;
0182 else if (phy == 1)
0183 msg->dest_dev = rmn_1;
0184 else
0185 msg->dest_dev = rmn_2;
0186 }
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197 static bool ice_is_64b_phy_reg_e822(u16 low_addr, u16 *high_addr)
0198 {
0199 switch (low_addr) {
0200 case P_REG_PAR_PCS_TX_OFFSET_L:
0201 *high_addr = P_REG_PAR_PCS_TX_OFFSET_U;
0202 return true;
0203 case P_REG_PAR_PCS_RX_OFFSET_L:
0204 *high_addr = P_REG_PAR_PCS_RX_OFFSET_U;
0205 return true;
0206 case P_REG_PAR_TX_TIME_L:
0207 *high_addr = P_REG_PAR_TX_TIME_U;
0208 return true;
0209 case P_REG_PAR_RX_TIME_L:
0210 *high_addr = P_REG_PAR_RX_TIME_U;
0211 return true;
0212 case P_REG_TOTAL_TX_OFFSET_L:
0213 *high_addr = P_REG_TOTAL_TX_OFFSET_U;
0214 return true;
0215 case P_REG_TOTAL_RX_OFFSET_L:
0216 *high_addr = P_REG_TOTAL_RX_OFFSET_U;
0217 return true;
0218 case P_REG_UIX66_10G_40G_L:
0219 *high_addr = P_REG_UIX66_10G_40G_U;
0220 return true;
0221 case P_REG_UIX66_25G_100G_L:
0222 *high_addr = P_REG_UIX66_25G_100G_U;
0223 return true;
0224 case P_REG_TX_CAPTURE_L:
0225 *high_addr = P_REG_TX_CAPTURE_U;
0226 return true;
0227 case P_REG_RX_CAPTURE_L:
0228 *high_addr = P_REG_RX_CAPTURE_U;
0229 return true;
0230 case P_REG_TX_TIMER_INC_PRE_L:
0231 *high_addr = P_REG_TX_TIMER_INC_PRE_U;
0232 return true;
0233 case P_REG_RX_TIMER_INC_PRE_L:
0234 *high_addr = P_REG_RX_TIMER_INC_PRE_U;
0235 return true;
0236 default:
0237 return false;
0238 }
0239 }
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 static bool ice_is_40b_phy_reg_e822(u16 low_addr, u16 *high_addr)
0252 {
0253 switch (low_addr) {
0254 case P_REG_TIMETUS_L:
0255 *high_addr = P_REG_TIMETUS_U;
0256 return true;
0257 case P_REG_PAR_RX_TUS_L:
0258 *high_addr = P_REG_PAR_RX_TUS_U;
0259 return true;
0260 case P_REG_PAR_TX_TUS_L:
0261 *high_addr = P_REG_PAR_TX_TUS_U;
0262 return true;
0263 case P_REG_PCS_RX_TUS_L:
0264 *high_addr = P_REG_PCS_RX_TUS_U;
0265 return true;
0266 case P_REG_PCS_TX_TUS_L:
0267 *high_addr = P_REG_PCS_TX_TUS_U;
0268 return true;
0269 case P_REG_DESK_PAR_RX_TUS_L:
0270 *high_addr = P_REG_DESK_PAR_RX_TUS_U;
0271 return true;
0272 case P_REG_DESK_PAR_TX_TUS_L:
0273 *high_addr = P_REG_DESK_PAR_TX_TUS_U;
0274 return true;
0275 case P_REG_DESK_PCS_RX_TUS_L:
0276 *high_addr = P_REG_DESK_PCS_RX_TUS_U;
0277 return true;
0278 case P_REG_DESK_PCS_TX_TUS_L:
0279 *high_addr = P_REG_DESK_PCS_TX_TUS_U;
0280 return true;
0281 default:
0282 return false;
0283 }
0284 }
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 int
0296 ice_read_phy_reg_e822(struct ice_hw *hw, u8 port, u16 offset, u32 *val)
0297 {
0298 struct ice_sbq_msg_input msg = {0};
0299 int err;
0300
0301 ice_fill_phy_msg_e822(&msg, port, offset);
0302 msg.opcode = ice_sbq_msg_rd;
0303
0304 err = ice_sbq_rw_reg(hw, &msg);
0305 if (err) {
0306 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
0307 err);
0308 return err;
0309 }
0310
0311 *val = msg.data;
0312
0313 return 0;
0314 }
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328 static int
0329 ice_read_64b_phy_reg_e822(struct ice_hw *hw, u8 port, u16 low_addr, u64 *val)
0330 {
0331 u32 low, high;
0332 u16 high_addr;
0333 int err;
0334
0335
0336
0337
0338 if (!ice_is_64b_phy_reg_e822(low_addr, &high_addr)) {
0339 ice_debug(hw, ICE_DBG_PTP, "Invalid 64b register addr 0x%08x\n",
0340 low_addr);
0341 return -EINVAL;
0342 }
0343
0344 err = ice_read_phy_reg_e822(hw, port, low_addr, &low);
0345 if (err) {
0346 ice_debug(hw, ICE_DBG_PTP, "Failed to read from low register 0x%08x\n, err %d",
0347 low_addr, err);
0348 return err;
0349 }
0350
0351 err = ice_read_phy_reg_e822(hw, port, high_addr, &high);
0352 if (err) {
0353 ice_debug(hw, ICE_DBG_PTP, "Failed to read from high register 0x%08x\n, err %d",
0354 high_addr, err);
0355 return err;
0356 }
0357
0358 *val = (u64)high << 32 | low;
0359
0360 return 0;
0361 }
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372 int
0373 ice_write_phy_reg_e822(struct ice_hw *hw, u8 port, u16 offset, u32 val)
0374 {
0375 struct ice_sbq_msg_input msg = {0};
0376 int err;
0377
0378 ice_fill_phy_msg_e822(&msg, port, offset);
0379 msg.opcode = ice_sbq_msg_wr;
0380 msg.data = val;
0381
0382 err = ice_sbq_rw_reg(hw, &msg);
0383 if (err) {
0384 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
0385 err);
0386 return err;
0387 }
0388
0389 return 0;
0390 }
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402 static int
0403 ice_write_40b_phy_reg_e822(struct ice_hw *hw, u8 port, u16 low_addr, u64 val)
0404 {
0405 u32 low, high;
0406 u16 high_addr;
0407 int err;
0408
0409
0410
0411
0412 if (!ice_is_40b_phy_reg_e822(low_addr, &high_addr)) {
0413 ice_debug(hw, ICE_DBG_PTP, "Invalid 40b register addr 0x%08x\n",
0414 low_addr);
0415 return -EINVAL;
0416 }
0417
0418 low = (u32)(val & P_REG_40B_LOW_M);
0419 high = (u32)(val >> P_REG_40B_HIGH_S);
0420
0421 err = ice_write_phy_reg_e822(hw, port, low_addr, low);
0422 if (err) {
0423 ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
0424 low_addr, err);
0425 return err;
0426 }
0427
0428 err = ice_write_phy_reg_e822(hw, port, high_addr, high);
0429 if (err) {
0430 ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
0431 high_addr, err);
0432 return err;
0433 }
0434
0435 return 0;
0436 }
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450 static int
0451 ice_write_64b_phy_reg_e822(struct ice_hw *hw, u8 port, u16 low_addr, u64 val)
0452 {
0453 u32 low, high;
0454 u16 high_addr;
0455 int err;
0456
0457
0458
0459
0460 if (!ice_is_64b_phy_reg_e822(low_addr, &high_addr)) {
0461 ice_debug(hw, ICE_DBG_PTP, "Invalid 64b register addr 0x%08x\n",
0462 low_addr);
0463 return -EINVAL;
0464 }
0465
0466 low = lower_32_bits(val);
0467 high = upper_32_bits(val);
0468
0469 err = ice_write_phy_reg_e822(hw, port, low_addr, low);
0470 if (err) {
0471 ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
0472 low_addr, err);
0473 return err;
0474 }
0475
0476 err = ice_write_phy_reg_e822(hw, port, high_addr, high);
0477 if (err) {
0478 ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
0479 high_addr, err);
0480 return err;
0481 }
0482
0483 return 0;
0484 }
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495 static void
0496 ice_fill_quad_msg_e822(struct ice_sbq_msg_input *msg, u8 quad, u16 offset)
0497 {
0498 u32 addr;
0499
0500 msg->dest_dev = rmn_0;
0501
0502 if ((quad % ICE_NUM_QUAD_TYPE) == 0)
0503 addr = Q_0_BASE + offset;
0504 else
0505 addr = Q_1_BASE + offset;
0506
0507 msg->msg_addr_low = lower_16_bits(addr);
0508 msg->msg_addr_high = upper_16_bits(addr);
0509 }
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521 int
0522 ice_read_quad_reg_e822(struct ice_hw *hw, u8 quad, u16 offset, u32 *val)
0523 {
0524 struct ice_sbq_msg_input msg = {0};
0525 int err;
0526
0527 if (quad >= ICE_MAX_QUAD)
0528 return -EINVAL;
0529
0530 ice_fill_quad_msg_e822(&msg, quad, offset);
0531 msg.opcode = ice_sbq_msg_rd;
0532
0533 err = ice_sbq_rw_reg(hw, &msg);
0534 if (err) {
0535 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
0536 err);
0537 return err;
0538 }
0539
0540 *val = msg.data;
0541
0542 return 0;
0543 }
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555 int
0556 ice_write_quad_reg_e822(struct ice_hw *hw, u8 quad, u16 offset, u32 val)
0557 {
0558 struct ice_sbq_msg_input msg = {0};
0559 int err;
0560
0561 if (quad >= ICE_MAX_QUAD)
0562 return -EINVAL;
0563
0564 ice_fill_quad_msg_e822(&msg, quad, offset);
0565 msg.opcode = ice_sbq_msg_wr;
0566 msg.data = val;
0567
0568 err = ice_sbq_rw_reg(hw, &msg);
0569 if (err) {
0570 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
0571 err);
0572 return err;
0573 }
0574
0575 return 0;
0576 }
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589 static int
0590 ice_read_phy_tstamp_e822(struct ice_hw *hw, u8 quad, u8 idx, u64 *tstamp)
0591 {
0592 u16 lo_addr, hi_addr;
0593 u32 lo, hi;
0594 int err;
0595
0596 lo_addr = (u16)TS_L(Q_REG_TX_MEMORY_BANK_START, idx);
0597 hi_addr = (u16)TS_H(Q_REG_TX_MEMORY_BANK_START, idx);
0598
0599 err = ice_read_quad_reg_e822(hw, quad, lo_addr, &lo);
0600 if (err) {
0601 ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
0602 err);
0603 return err;
0604 }
0605
0606 err = ice_read_quad_reg_e822(hw, quad, hi_addr, &hi);
0607 if (err) {
0608 ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
0609 err);
0610 return err;
0611 }
0612
0613
0614
0615
0616
0617 *tstamp = ((u64)hi) << TS_PHY_HIGH_S | ((u64)lo & TS_PHY_LOW_M);
0618
0619 return 0;
0620 }
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631 static int
0632 ice_clear_phy_tstamp_e822(struct ice_hw *hw, u8 quad, u8 idx)
0633 {
0634 u16 lo_addr, hi_addr;
0635 int err;
0636
0637 lo_addr = (u16)TS_L(Q_REG_TX_MEMORY_BANK_START, idx);
0638 hi_addr = (u16)TS_H(Q_REG_TX_MEMORY_BANK_START, idx);
0639
0640 err = ice_write_quad_reg_e822(hw, quad, lo_addr, 0);
0641 if (err) {
0642 ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register, err %d\n",
0643 err);
0644 return err;
0645 }
0646
0647 err = ice_write_quad_reg_e822(hw, quad, hi_addr, 0);
0648 if (err) {
0649 ice_debug(hw, ICE_DBG_PTP, "Failed to clear high PTP timestamp register, err %d\n",
0650 err);
0651 return err;
0652 }
0653
0654 return 0;
0655 }
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666 static int
0667 ice_read_cgu_reg_e822(struct ice_hw *hw, u32 addr, u32 *val)
0668 {
0669 struct ice_sbq_msg_input cgu_msg;
0670 int err;
0671
0672 cgu_msg.opcode = ice_sbq_msg_rd;
0673 cgu_msg.dest_dev = cgu;
0674 cgu_msg.msg_addr_low = addr;
0675 cgu_msg.msg_addr_high = 0x0;
0676
0677 err = ice_sbq_rw_reg(hw, &cgu_msg);
0678 if (err) {
0679 ice_debug(hw, ICE_DBG_PTP, "Failed to read CGU register 0x%04x, err %d\n",
0680 addr, err);
0681 return err;
0682 }
0683
0684 *val = cgu_msg.data;
0685
0686 return err;
0687 }
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698 static int
0699 ice_write_cgu_reg_e822(struct ice_hw *hw, u32 addr, u32 val)
0700 {
0701 struct ice_sbq_msg_input cgu_msg;
0702 int err;
0703
0704 cgu_msg.opcode = ice_sbq_msg_wr;
0705 cgu_msg.dest_dev = cgu;
0706 cgu_msg.msg_addr_low = addr;
0707 cgu_msg.msg_addr_high = 0x0;
0708 cgu_msg.data = val;
0709
0710 err = ice_sbq_rw_reg(hw, &cgu_msg);
0711 if (err) {
0712 ice_debug(hw, ICE_DBG_PTP, "Failed to write CGU register 0x%04x, err %d\n",
0713 addr, err);
0714 return err;
0715 }
0716
0717 return err;
0718 }
0719
0720
0721
0722
0723
0724
0725
0726 static const char *ice_clk_freq_str(u8 clk_freq)
0727 {
0728 switch ((enum ice_time_ref_freq)clk_freq) {
0729 case ICE_TIME_REF_FREQ_25_000:
0730 return "25 MHz";
0731 case ICE_TIME_REF_FREQ_122_880:
0732 return "122.88 MHz";
0733 case ICE_TIME_REF_FREQ_125_000:
0734 return "125 MHz";
0735 case ICE_TIME_REF_FREQ_153_600:
0736 return "153.6 MHz";
0737 case ICE_TIME_REF_FREQ_156_250:
0738 return "156.25 MHz";
0739 case ICE_TIME_REF_FREQ_245_760:
0740 return "245.76 MHz";
0741 default:
0742 return "Unknown";
0743 }
0744 }
0745
0746
0747
0748
0749
0750
0751
0752 static const char *ice_clk_src_str(u8 clk_src)
0753 {
0754 switch ((enum ice_clk_src)clk_src) {
0755 case ICE_CLK_SRC_TCX0:
0756 return "TCX0";
0757 case ICE_CLK_SRC_TIME_REF:
0758 return "TIME_REF";
0759 default:
0760 return "Unknown";
0761 }
0762 }
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773 static int
0774 ice_cfg_cgu_pll_e822(struct ice_hw *hw, enum ice_time_ref_freq clk_freq,
0775 enum ice_clk_src clk_src)
0776 {
0777 union tspll_ro_bwm_lf bwm_lf;
0778 union nac_cgu_dword19 dw19;
0779 union nac_cgu_dword22 dw22;
0780 union nac_cgu_dword24 dw24;
0781 union nac_cgu_dword9 dw9;
0782 int err;
0783
0784 if (clk_freq >= NUM_ICE_TIME_REF_FREQ) {
0785 dev_warn(ice_hw_to_dev(hw), "Invalid TIME_REF frequency %u\n",
0786 clk_freq);
0787 return -EINVAL;
0788 }
0789
0790 if (clk_src >= NUM_ICE_CLK_SRC) {
0791 dev_warn(ice_hw_to_dev(hw), "Invalid clock source %u\n",
0792 clk_src);
0793 return -EINVAL;
0794 }
0795
0796 if (clk_src == ICE_CLK_SRC_TCX0 &&
0797 clk_freq != ICE_TIME_REF_FREQ_25_000) {
0798 dev_warn(ice_hw_to_dev(hw),
0799 "TCX0 only supports 25 MHz frequency\n");
0800 return -EINVAL;
0801 }
0802
0803 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD9, &dw9.val);
0804 if (err)
0805 return err;
0806
0807 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD24, &dw24.val);
0808 if (err)
0809 return err;
0810
0811 err = ice_read_cgu_reg_e822(hw, TSPLL_RO_BWM_LF, &bwm_lf.val);
0812 if (err)
0813 return err;
0814
0815
0816 ice_debug(hw, ICE_DBG_PTP, "Current CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
0817 dw24.field.ts_pll_enable ? "enabled" : "disabled",
0818 ice_clk_src_str(dw24.field.time_ref_sel),
0819 ice_clk_freq_str(dw9.field.time_ref_freq_sel),
0820 bwm_lf.field.plllock_true_lock_cri ? "locked" : "unlocked");
0821
0822
0823 if (dw24.field.ts_pll_enable) {
0824 dw24.field.ts_pll_enable = 0;
0825
0826 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD24, dw24.val);
0827 if (err)
0828 return err;
0829 }
0830
0831
0832 dw9.field.time_ref_freq_sel = clk_freq;
0833 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD9, dw9.val);
0834 if (err)
0835 return err;
0836
0837
0838 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD19, &dw19.val);
0839 if (err)
0840 return err;
0841
0842 dw19.field.tspll_fbdiv_intgr = e822_cgu_params[clk_freq].feedback_div;
0843 dw19.field.tspll_ndivratio = 1;
0844
0845 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD19, dw19.val);
0846 if (err)
0847 return err;
0848
0849
0850 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD22, &dw22.val);
0851 if (err)
0852 return err;
0853
0854 dw22.field.time1588clk_div = e822_cgu_params[clk_freq].post_pll_div;
0855 dw22.field.time1588clk_sel_div2 = 0;
0856
0857 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD22, dw22.val);
0858 if (err)
0859 return err;
0860
0861
0862 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD24, &dw24.val);
0863 if (err)
0864 return err;
0865
0866 dw24.field.ref1588_ck_div = e822_cgu_params[clk_freq].refclk_pre_div;
0867 dw24.field.tspll_fbdiv_frac = e822_cgu_params[clk_freq].frac_n_div;
0868 dw24.field.time_ref_sel = clk_src;
0869
0870 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD24, dw24.val);
0871 if (err)
0872 return err;
0873
0874
0875 dw24.field.ts_pll_enable = 1;
0876
0877 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD24, dw24.val);
0878 if (err)
0879 return err;
0880
0881
0882 usleep_range(1000, 5000);
0883
0884 err = ice_read_cgu_reg_e822(hw, TSPLL_RO_BWM_LF, &bwm_lf.val);
0885 if (err)
0886 return err;
0887
0888 if (!bwm_lf.field.plllock_true_lock_cri) {
0889 dev_warn(ice_hw_to_dev(hw), "CGU PLL failed to lock\n");
0890 return -EBUSY;
0891 }
0892
0893
0894 ice_debug(hw, ICE_DBG_PTP, "New CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
0895 dw24.field.ts_pll_enable ? "enabled" : "disabled",
0896 ice_clk_src_str(dw24.field.time_ref_sel),
0897 ice_clk_freq_str(dw9.field.time_ref_freq_sel),
0898 bwm_lf.field.plllock_true_lock_cri ? "locked" : "unlocked");
0899
0900 return 0;
0901 }
0902
0903
0904
0905
0906
0907
0908
0909 static int ice_init_cgu_e822(struct ice_hw *hw)
0910 {
0911 struct ice_ts_func_info *ts_info = &hw->func_caps.ts_func_info;
0912 union tspll_cntr_bist_settings cntr_bist;
0913 int err;
0914
0915 err = ice_read_cgu_reg_e822(hw, TSPLL_CNTR_BIST_SETTINGS,
0916 &cntr_bist.val);
0917 if (err)
0918 return err;
0919
0920
0921 cntr_bist.field.i_plllock_sel_0 = 0;
0922 cntr_bist.field.i_plllock_sel_1 = 0;
0923
0924 err = ice_write_cgu_reg_e822(hw, TSPLL_CNTR_BIST_SETTINGS,
0925 cntr_bist.val);
0926 if (err)
0927 return err;
0928
0929
0930
0931
0932 err = ice_cfg_cgu_pll_e822(hw, ts_info->time_ref,
0933 (enum ice_clk_src)ts_info->clk_src);
0934 if (err)
0935 return err;
0936
0937 return 0;
0938 }
0939
0940
0941
0942
0943
0944
0945
0946 static int ice_ptp_set_vernier_wl(struct ice_hw *hw)
0947 {
0948 u8 port;
0949
0950 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
0951 int err;
0952
0953 err = ice_write_phy_reg_e822(hw, port, P_REG_WL,
0954 PTP_VERNIER_WL);
0955 if (err) {
0956 ice_debug(hw, ICE_DBG_PTP, "Failed to set vernier window length for port %u, err %d\n",
0957 port, err);
0958 return err;
0959 }
0960 }
0961
0962 return 0;
0963 }
0964
0965
0966
0967
0968
0969
0970
0971 static int ice_ptp_init_phc_e822(struct ice_hw *hw)
0972 {
0973 int err;
0974 u32 regval;
0975
0976
0977 #define PF_SB_REM_DEV_CTL_SWITCH_READ BIT(1)
0978 #define PF_SB_REM_DEV_CTL_PHY0 BIT(2)
0979 regval = rd32(hw, PF_SB_REM_DEV_CTL);
0980 regval |= (PF_SB_REM_DEV_CTL_SWITCH_READ |
0981 PF_SB_REM_DEV_CTL_PHY0);
0982 wr32(hw, PF_SB_REM_DEV_CTL, regval);
0983
0984
0985 err = ice_init_cgu_e822(hw);
0986 if (err)
0987 return err;
0988
0989
0990 return ice_ptp_set_vernier_wl(hw);
0991 }
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003 static int
1004 ice_ptp_prep_phy_time_e822(struct ice_hw *hw, u32 time)
1005 {
1006 u64 phy_time;
1007 u8 port;
1008 int err;
1009
1010
1011
1012
1013 phy_time = (u64)time << 32;
1014
1015 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1016
1017 err = ice_write_64b_phy_reg_e822(hw, port,
1018 P_REG_TX_TIMER_INC_PRE_L,
1019 phy_time);
1020 if (err)
1021 goto exit_err;
1022
1023
1024 err = ice_write_64b_phy_reg_e822(hw, port,
1025 P_REG_RX_TIMER_INC_PRE_L,
1026 phy_time);
1027 if (err)
1028 goto exit_err;
1029 }
1030
1031 return 0;
1032
1033 exit_err:
1034 ice_debug(hw, ICE_DBG_PTP, "Failed to write init time for port %u, err %d\n",
1035 port, err);
1036
1037 return err;
1038 }
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 int
1056 ice_ptp_prep_port_adj_e822(struct ice_hw *hw, u8 port, s64 time)
1057 {
1058 u32 l_time, u_time;
1059 int err;
1060
1061 l_time = lower_32_bits(time);
1062 u_time = upper_32_bits(time);
1063
1064
1065 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_TIMER_INC_PRE_L,
1066 l_time);
1067 if (err)
1068 goto exit_err;
1069
1070 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_TIMER_INC_PRE_U,
1071 u_time);
1072 if (err)
1073 goto exit_err;
1074
1075
1076 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_TIMER_INC_PRE_L,
1077 l_time);
1078 if (err)
1079 goto exit_err;
1080
1081 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_TIMER_INC_PRE_U,
1082 u_time);
1083 if (err)
1084 goto exit_err;
1085
1086 return 0;
1087
1088 exit_err:
1089 ice_debug(hw, ICE_DBG_PTP, "Failed to write time adjust for port %u, err %d\n",
1090 port, err);
1091 return err;
1092 }
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 static int
1104 ice_ptp_prep_phy_adj_e822(struct ice_hw *hw, s32 adj)
1105 {
1106 s64 cycles;
1107 u8 port;
1108
1109
1110
1111
1112
1113 if (adj > 0)
1114 cycles = (s64)adj << 32;
1115 else
1116 cycles = -(((s64)-adj) << 32);
1117
1118 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1119 int err;
1120
1121 err = ice_ptp_prep_port_adj_e822(hw, port, cycles);
1122 if (err)
1123 return err;
1124 }
1125
1126 return 0;
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 static int
1139 ice_ptp_prep_phy_incval_e822(struct ice_hw *hw, u64 incval)
1140 {
1141 int err;
1142 u8 port;
1143
1144 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1145 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_TIMETUS_L,
1146 incval);
1147 if (err)
1148 goto exit_err;
1149 }
1150
1151 return 0;
1152
1153 exit_err:
1154 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval for port %u, err %d\n",
1155 port, err);
1156
1157 return err;
1158 }
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171 static int
1172 ice_ptp_read_port_capture(struct ice_hw *hw, u8 port, u64 *tx_ts, u64 *rx_ts)
1173 {
1174 int err;
1175
1176
1177 err = ice_read_64b_phy_reg_e822(hw, port, P_REG_TX_CAPTURE_L, tx_ts);
1178 if (err) {
1179 ice_debug(hw, ICE_DBG_PTP, "Failed to read REG_TX_CAPTURE, err %d\n",
1180 err);
1181 return err;
1182 }
1183
1184 ice_debug(hw, ICE_DBG_PTP, "tx_init = 0x%016llx\n",
1185 (unsigned long long)*tx_ts);
1186
1187
1188 err = ice_read_64b_phy_reg_e822(hw, port, P_REG_RX_CAPTURE_L, rx_ts);
1189 if (err) {
1190 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_CAPTURE, err %d\n",
1191 err);
1192 return err;
1193 }
1194
1195 ice_debug(hw, ICE_DBG_PTP, "rx_init = 0x%016llx\n",
1196 (unsigned long long)*rx_ts);
1197
1198 return 0;
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 static int
1213 ice_ptp_one_port_cmd(struct ice_hw *hw, u8 port, enum ice_ptp_tmr_cmd cmd)
1214 {
1215 u32 cmd_val, val;
1216 u8 tmr_idx;
1217 int err;
1218
1219 tmr_idx = ice_get_ptp_src_clock_index(hw);
1220 cmd_val = tmr_idx << SEL_PHY_SRC;
1221 switch (cmd) {
1222 case INIT_TIME:
1223 cmd_val |= PHY_CMD_INIT_TIME;
1224 break;
1225 case INIT_INCVAL:
1226 cmd_val |= PHY_CMD_INIT_INCVAL;
1227 break;
1228 case ADJ_TIME:
1229 cmd_val |= PHY_CMD_ADJ_TIME;
1230 break;
1231 case READ_TIME:
1232 cmd_val |= PHY_CMD_READ_TIME;
1233 break;
1234 case ADJ_TIME_AT_TIME:
1235 cmd_val |= PHY_CMD_ADJ_TIME_AT_TIME;
1236 break;
1237 }
1238
1239
1240
1241 err = ice_read_phy_reg_e822(hw, port, P_REG_TX_TMR_CMD, &val);
1242 if (err) {
1243 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_TMR_CMD, err %d\n",
1244 err);
1245 return err;
1246 }
1247
1248
1249 val &= ~TS_CMD_MASK;
1250 val |= cmd_val;
1251
1252 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_TMR_CMD, val);
1253 if (err) {
1254 ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_TMR_CMD, err %d\n",
1255 err);
1256 return err;
1257 }
1258
1259
1260
1261 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_TMR_CMD, &val);
1262 if (err) {
1263 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_TMR_CMD, err %d\n",
1264 err);
1265 return err;
1266 }
1267
1268
1269 val &= ~TS_CMD_MASK;
1270 val |= cmd_val;
1271
1272 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_TMR_CMD, val);
1273 if (err) {
1274 ice_debug(hw, ICE_DBG_PTP, "Failed to write back RX_TMR_CMD, err %d\n",
1275 err);
1276 return err;
1277 }
1278
1279 return 0;
1280 }
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290 static int
1291 ice_ptp_port_cmd_e822(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
1292 {
1293 u8 port;
1294
1295 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1296 int err;
1297
1298 err = ice_ptp_one_port_cmd(hw, port, cmd);
1299 if (err)
1300 return err;
1301 }
1302
1303 return 0;
1304 }
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323 static int
1324 ice_phy_get_speed_and_fec_e822(struct ice_hw *hw, u8 port,
1325 enum ice_ptp_link_spd *link_out,
1326 enum ice_ptp_fec_mode *fec_out)
1327 {
1328 enum ice_ptp_link_spd link;
1329 enum ice_ptp_fec_mode fec;
1330 u32 serdes;
1331 int err;
1332
1333 err = ice_read_phy_reg_e822(hw, port, P_REG_LINK_SPEED, &serdes);
1334 if (err) {
1335 ice_debug(hw, ICE_DBG_PTP, "Failed to read serdes info\n");
1336 return err;
1337 }
1338
1339
1340 fec = (enum ice_ptp_fec_mode)P_REG_LINK_SPEED_FEC_MODE(serdes);
1341
1342 serdes &= P_REG_LINK_SPEED_SERDES_M;
1343
1344
1345 if (fec == ICE_PTP_FEC_MODE_RS_FEC) {
1346 switch (serdes) {
1347 case ICE_PTP_SERDES_25G:
1348 link = ICE_PTP_LNK_SPD_25G_RS;
1349 break;
1350 case ICE_PTP_SERDES_50G:
1351 link = ICE_PTP_LNK_SPD_50G_RS;
1352 break;
1353 case ICE_PTP_SERDES_100G:
1354 link = ICE_PTP_LNK_SPD_100G_RS;
1355 break;
1356 default:
1357 return -EIO;
1358 }
1359 } else {
1360 switch (serdes) {
1361 case ICE_PTP_SERDES_1G:
1362 link = ICE_PTP_LNK_SPD_1G;
1363 break;
1364 case ICE_PTP_SERDES_10G:
1365 link = ICE_PTP_LNK_SPD_10G;
1366 break;
1367 case ICE_PTP_SERDES_25G:
1368 link = ICE_PTP_LNK_SPD_25G;
1369 break;
1370 case ICE_PTP_SERDES_40G:
1371 link = ICE_PTP_LNK_SPD_40G;
1372 break;
1373 case ICE_PTP_SERDES_50G:
1374 link = ICE_PTP_LNK_SPD_50G;
1375 break;
1376 default:
1377 return -EIO;
1378 }
1379 }
1380
1381 if (link_out)
1382 *link_out = link;
1383 if (fec_out)
1384 *fec_out = fec;
1385
1386 return 0;
1387 }
1388
1389
1390
1391
1392
1393
1394 static void ice_phy_cfg_lane_e822(struct ice_hw *hw, u8 port)
1395 {
1396 enum ice_ptp_link_spd link_spd;
1397 int err;
1398 u32 val;
1399 u8 quad;
1400
1401 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, NULL);
1402 if (err) {
1403 ice_debug(hw, ICE_DBG_PTP, "Failed to get PHY link speed, err %d\n",
1404 err);
1405 return;
1406 }
1407
1408 quad = port / ICE_PORTS_PER_QUAD;
1409
1410 err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, &val);
1411 if (err) {
1412 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEM_GLB_CFG, err %d\n",
1413 err);
1414 return;
1415 }
1416
1417 if (link_spd >= ICE_PTP_LNK_SPD_40G)
1418 val &= ~Q_REG_TX_MEM_GBL_CFG_LANE_TYPE_M;
1419 else
1420 val |= Q_REG_TX_MEM_GBL_CFG_LANE_TYPE_M;
1421
1422 err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, val);
1423 if (err) {
1424 ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_MEM_GBL_CFG, err %d\n",
1425 err);
1426 return;
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 static int ice_phy_cfg_uix_e822(struct ice_hw *hw, u8 port)
1477 {
1478 u64 cur_freq, clk_incval, tu_per_sec, uix;
1479 int err;
1480
1481 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1482 clk_incval = ice_ptp_read_src_incval(hw);
1483
1484
1485 tu_per_sec = (cur_freq * clk_incval) >> 8;
1486
1487 #define LINE_UI_10G_40G 640
1488 #define LINE_UI_25G_100G 256
1489
1490
1491 uix = div_u64(tu_per_sec * LINE_UI_10G_40G, 390625000);
1492
1493 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_UIX66_10G_40G_L,
1494 uix);
1495 if (err) {
1496 ice_debug(hw, ICE_DBG_PTP, "Failed to write UIX66_10G_40G, err %d\n",
1497 err);
1498 return err;
1499 }
1500
1501
1502 uix = div_u64(tu_per_sec * LINE_UI_25G_100G, 390625000);
1503
1504 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_UIX66_25G_100G_L,
1505 uix);
1506 if (err) {
1507 ice_debug(hw, ICE_DBG_PTP, "Failed to write UIX66_25G_100G, err %d\n",
1508 err);
1509 return err;
1510 }
1511
1512 return 0;
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 static int ice_phy_cfg_parpcs_e822(struct ice_hw *hw, u8 port)
1559 {
1560 u64 cur_freq, clk_incval, tu_per_sec, phy_tus;
1561 enum ice_ptp_link_spd link_spd;
1562 enum ice_ptp_fec_mode fec_mode;
1563 int err;
1564
1565 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
1566 if (err)
1567 return err;
1568
1569 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1570 clk_incval = ice_ptp_read_src_incval(hw);
1571
1572
1573 tu_per_sec = cur_freq * clk_incval;
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583 if (e822_vernier[link_spd].tx_par_clk)
1584 phy_tus = div_u64(tu_per_sec,
1585 e822_vernier[link_spd].tx_par_clk);
1586 else
1587 phy_tus = 0;
1588
1589 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PAR_TX_TUS_L,
1590 phy_tus);
1591 if (err)
1592 return err;
1593
1594
1595 if (e822_vernier[link_spd].rx_par_clk)
1596 phy_tus = div_u64(tu_per_sec,
1597 e822_vernier[link_spd].rx_par_clk);
1598 else
1599 phy_tus = 0;
1600
1601 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PAR_RX_TUS_L,
1602 phy_tus);
1603 if (err)
1604 return err;
1605
1606
1607 if (e822_vernier[link_spd].tx_pcs_clk)
1608 phy_tus = div_u64(tu_per_sec,
1609 e822_vernier[link_spd].tx_pcs_clk);
1610 else
1611 phy_tus = 0;
1612
1613 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PCS_TX_TUS_L,
1614 phy_tus);
1615 if (err)
1616 return err;
1617
1618
1619 if (e822_vernier[link_spd].rx_pcs_clk)
1620 phy_tus = div_u64(tu_per_sec,
1621 e822_vernier[link_spd].rx_pcs_clk);
1622 else
1623 phy_tus = 0;
1624
1625 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PCS_RX_TUS_L,
1626 phy_tus);
1627 if (err)
1628 return err;
1629
1630
1631 if (e822_vernier[link_spd].tx_desk_rsgb_par)
1632 phy_tus = div_u64(tu_per_sec,
1633 e822_vernier[link_spd].tx_desk_rsgb_par);
1634 else
1635 phy_tus = 0;
1636
1637 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PAR_TX_TUS_L,
1638 phy_tus);
1639 if (err)
1640 return err;
1641
1642
1643 if (e822_vernier[link_spd].rx_desk_rsgb_par)
1644 phy_tus = div_u64(tu_per_sec,
1645 e822_vernier[link_spd].rx_desk_rsgb_par);
1646 else
1647 phy_tus = 0;
1648
1649 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PAR_RX_TUS_L,
1650 phy_tus);
1651 if (err)
1652 return err;
1653
1654
1655 if (e822_vernier[link_spd].tx_desk_rsgb_pcs)
1656 phy_tus = div_u64(tu_per_sec,
1657 e822_vernier[link_spd].tx_desk_rsgb_pcs);
1658 else
1659 phy_tus = 0;
1660
1661 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PCS_TX_TUS_L,
1662 phy_tus);
1663 if (err)
1664 return err;
1665
1666
1667 if (e822_vernier[link_spd].rx_desk_rsgb_pcs)
1668 phy_tus = div_u64(tu_per_sec,
1669 e822_vernier[link_spd].rx_desk_rsgb_pcs);
1670 else
1671 phy_tus = 0;
1672
1673 return ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PCS_RX_TUS_L,
1674 phy_tus);
1675 }
1676
1677
1678
1679
1680
1681
1682
1683
1684 static u64
1685 ice_calc_fixed_tx_offset_e822(struct ice_hw *hw, enum ice_ptp_link_spd link_spd)
1686 {
1687 u64 cur_freq, clk_incval, tu_per_sec, fixed_offset;
1688
1689 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1690 clk_incval = ice_ptp_read_src_incval(hw);
1691
1692
1693 tu_per_sec = cur_freq * clk_incval;
1694
1695
1696
1697
1698
1699
1700
1701 fixed_offset = div_u64(tu_per_sec, 10000);
1702 fixed_offset *= e822_vernier[link_spd].tx_fixed_delay;
1703 fixed_offset = div_u64(fixed_offset, 10000000);
1704
1705 return fixed_offset;
1706 }
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726 static int ice_phy_cfg_tx_offset_e822(struct ice_hw *hw, u8 port)
1727 {
1728 enum ice_ptp_link_spd link_spd;
1729 enum ice_ptp_fec_mode fec_mode;
1730 u64 total_offset, val;
1731 int err;
1732
1733 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
1734 if (err)
1735 return err;
1736
1737 total_offset = ice_calc_fixed_tx_offset_e822(hw, link_spd);
1738
1739
1740
1741
1742 if (link_spd == ICE_PTP_LNK_SPD_1G ||
1743 link_spd == ICE_PTP_LNK_SPD_10G ||
1744 link_spd == ICE_PTP_LNK_SPD_25G ||
1745 link_spd == ICE_PTP_LNK_SPD_25G_RS ||
1746 link_spd == ICE_PTP_LNK_SPD_40G ||
1747 link_spd == ICE_PTP_LNK_SPD_50G) {
1748 err = ice_read_64b_phy_reg_e822(hw, port,
1749 P_REG_PAR_PCS_TX_OFFSET_L,
1750 &val);
1751 if (err)
1752 return err;
1753
1754 total_offset += val;
1755 }
1756
1757
1758
1759
1760
1761 if (link_spd == ICE_PTP_LNK_SPD_50G_RS ||
1762 link_spd == ICE_PTP_LNK_SPD_100G_RS) {
1763 err = ice_read_64b_phy_reg_e822(hw, port,
1764 P_REG_PAR_TX_TIME_L,
1765 &val);
1766 if (err)
1767 return err;
1768
1769 total_offset += val;
1770 }
1771
1772
1773
1774
1775
1776 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_TOTAL_TX_OFFSET_L,
1777 total_offset);
1778 if (err)
1779 return err;
1780
1781 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_OR, 1);
1782 if (err)
1783 return err;
1784
1785 return 0;
1786 }
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796 static int
1797 ice_phy_cfg_fixed_tx_offset_e822(struct ice_hw *hw, u8 port)
1798 {
1799 enum ice_ptp_link_spd link_spd;
1800 enum ice_ptp_fec_mode fec_mode;
1801 u64 total_offset;
1802 int err;
1803
1804 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
1805 if (err)
1806 return err;
1807
1808 total_offset = ice_calc_fixed_tx_offset_e822(hw, link_spd);
1809
1810
1811
1812
1813
1814
1815
1816
1817 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_TOTAL_TX_OFFSET_L,
1818 total_offset);
1819 if (err)
1820 return err;
1821
1822 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_OR, 1);
1823 if (err)
1824 return err;
1825
1826 return 0;
1827 }
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841 static int
1842 ice_phy_calc_pmd_adj_e822(struct ice_hw *hw, u8 port,
1843 enum ice_ptp_link_spd link_spd,
1844 enum ice_ptp_fec_mode fec_mode, u64 *pmd_adj)
1845 {
1846 u64 cur_freq, clk_incval, tu_per_sec, mult, adj;
1847 u8 pmd_align;
1848 u32 val;
1849 int err;
1850
1851 err = ice_read_phy_reg_e822(hw, port, P_REG_PMD_ALIGNMENT, &val);
1852 if (err) {
1853 ice_debug(hw, ICE_DBG_PTP, "Failed to read PMD alignment, err %d\n",
1854 err);
1855 return err;
1856 }
1857
1858 pmd_align = (u8)val;
1859
1860 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1861 clk_incval = ice_ptp_read_src_incval(hw);
1862
1863
1864 tu_per_sec = cur_freq * clk_incval;
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890 if (link_spd == ICE_PTP_LNK_SPD_1G) {
1891 if (pmd_align == 4)
1892 mult = 10;
1893 else
1894 mult = (pmd_align + 6) % 10;
1895 } else if (link_spd == ICE_PTP_LNK_SPD_10G ||
1896 link_spd == ICE_PTP_LNK_SPD_25G ||
1897 link_spd == ICE_PTP_LNK_SPD_40G ||
1898 link_spd == ICE_PTP_LNK_SPD_50G) {
1899
1900 if (pmd_align != 65 || fec_mode == ICE_PTP_FEC_MODE_CLAUSE74)
1901 mult = pmd_align;
1902 else
1903 mult = 0;
1904 } else if (link_spd == ICE_PTP_LNK_SPD_25G_RS ||
1905 link_spd == ICE_PTP_LNK_SPD_50G_RS ||
1906 link_spd == ICE_PTP_LNK_SPD_100G_RS) {
1907 if (pmd_align < 17)
1908 mult = pmd_align + 40;
1909 else
1910 mult = pmd_align;
1911 } else {
1912 ice_debug(hw, ICE_DBG_PTP, "Unknown link speed %d, skipping PMD adjustment\n",
1913 link_spd);
1914 mult = 0;
1915 }
1916
1917
1918 if (!mult) {
1919 *pmd_adj = 0;
1920 return 0;
1921 }
1922
1923
1924
1925
1926
1927
1928 adj = div_u64(tu_per_sec, 125);
1929 adj *= mult;
1930 adj = div_u64(adj, e822_vernier[link_spd].pmd_adj_divisor);
1931
1932
1933
1934
1935 if (link_spd == ICE_PTP_LNK_SPD_25G_RS) {
1936 u64 cycle_adj;
1937 u8 rx_cycle;
1938
1939 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_40_TO_160_CNT,
1940 &val);
1941 if (err) {
1942 ice_debug(hw, ICE_DBG_PTP, "Failed to read 25G-RS Rx cycle count, err %d\n",
1943 err);
1944 return err;
1945 }
1946
1947 rx_cycle = val & P_REG_RX_40_TO_160_CNT_RXCYC_M;
1948 if (rx_cycle) {
1949 mult = (4 - rx_cycle) * 40;
1950
1951 cycle_adj = div_u64(tu_per_sec, 125);
1952 cycle_adj *= mult;
1953 cycle_adj = div_u64(cycle_adj, e822_vernier[link_spd].pmd_adj_divisor);
1954
1955 adj += cycle_adj;
1956 }
1957 } else if (link_spd == ICE_PTP_LNK_SPD_50G_RS) {
1958 u64 cycle_adj;
1959 u8 rx_cycle;
1960
1961 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_80_TO_160_CNT,
1962 &val);
1963 if (err) {
1964 ice_debug(hw, ICE_DBG_PTP, "Failed to read 50G-RS Rx cycle count, err %d\n",
1965 err);
1966 return err;
1967 }
1968
1969 rx_cycle = val & P_REG_RX_80_TO_160_CNT_RXCYC_M;
1970 if (rx_cycle) {
1971 mult = rx_cycle * 40;
1972
1973 cycle_adj = div_u64(tu_per_sec, 125);
1974 cycle_adj *= mult;
1975 cycle_adj = div_u64(cycle_adj, e822_vernier[link_spd].pmd_adj_divisor);
1976
1977 adj += cycle_adj;
1978 }
1979 }
1980
1981
1982 *pmd_adj = adj;
1983
1984 return 0;
1985 }
1986
1987
1988
1989
1990
1991
1992
1993
1994 static u64
1995 ice_calc_fixed_rx_offset_e822(struct ice_hw *hw, enum ice_ptp_link_spd link_spd)
1996 {
1997 u64 cur_freq, clk_incval, tu_per_sec, fixed_offset;
1998
1999 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
2000 clk_incval = ice_ptp_read_src_incval(hw);
2001
2002
2003 tu_per_sec = cur_freq * clk_incval;
2004
2005
2006
2007
2008
2009
2010
2011 fixed_offset = div_u64(tu_per_sec, 10000);
2012 fixed_offset *= e822_vernier[link_spd].rx_fixed_delay;
2013 fixed_offset = div_u64(fixed_offset, 10000000);
2014
2015 return fixed_offset;
2016 }
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037 static int ice_phy_cfg_rx_offset_e822(struct ice_hw *hw, u8 port)
2038 {
2039 enum ice_ptp_link_spd link_spd;
2040 enum ice_ptp_fec_mode fec_mode;
2041 u64 total_offset, pmd, val;
2042 int err;
2043
2044 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
2045 if (err)
2046 return err;
2047
2048 total_offset = ice_calc_fixed_rx_offset_e822(hw, link_spd);
2049
2050
2051
2052
2053 err = ice_read_64b_phy_reg_e822(hw, port,
2054 P_REG_PAR_PCS_RX_OFFSET_L,
2055 &val);
2056 if (err)
2057 return err;
2058
2059 total_offset += val;
2060
2061
2062
2063
2064 if (link_spd == ICE_PTP_LNK_SPD_40G ||
2065 link_spd == ICE_PTP_LNK_SPD_50G ||
2066 link_spd == ICE_PTP_LNK_SPD_50G_RS ||
2067 link_spd == ICE_PTP_LNK_SPD_100G_RS) {
2068 err = ice_read_64b_phy_reg_e822(hw, port,
2069 P_REG_PAR_RX_TIME_L,
2070 &val);
2071 if (err)
2072 return err;
2073
2074 total_offset += val;
2075 }
2076
2077
2078 err = ice_phy_calc_pmd_adj_e822(hw, port, link_spd, fec_mode, &pmd);
2079 if (err)
2080 return err;
2081
2082
2083
2084
2085 if (fec_mode == ICE_PTP_FEC_MODE_RS_FEC)
2086 total_offset += pmd;
2087 else
2088 total_offset -= pmd;
2089
2090
2091
2092
2093
2094 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_TOTAL_RX_OFFSET_L,
2095 total_offset);
2096 if (err)
2097 return err;
2098
2099 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_OR, 1);
2100 if (err)
2101 return err;
2102
2103 return 0;
2104 }
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114 static int
2115 ice_phy_cfg_fixed_rx_offset_e822(struct ice_hw *hw, u8 port)
2116 {
2117 enum ice_ptp_link_spd link_spd;
2118 enum ice_ptp_fec_mode fec_mode;
2119 u64 total_offset;
2120 int err;
2121
2122 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
2123 if (err)
2124 return err;
2125
2126 total_offset = ice_calc_fixed_rx_offset_e822(hw, link_spd);
2127
2128
2129
2130
2131
2132
2133
2134
2135 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_TOTAL_RX_OFFSET_L,
2136 total_offset);
2137 if (err)
2138 return err;
2139
2140 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_OR, 1);
2141 if (err)
2142 return err;
2143
2144 return 0;
2145 }
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157 static int
2158 ice_read_phy_and_phc_time_e822(struct ice_hw *hw, u8 port, u64 *phy_time,
2159 u64 *phc_time)
2160 {
2161 u64 tx_time, rx_time;
2162 u32 zo, lo;
2163 u8 tmr_idx;
2164 int err;
2165
2166 tmr_idx = ice_get_ptp_src_clock_index(hw);
2167
2168
2169 ice_ptp_src_cmd(hw, READ_TIME);
2170
2171
2172 err = ice_ptp_one_port_cmd(hw, port, READ_TIME);
2173 if (err)
2174 return err;
2175
2176
2177 ice_ptp_exec_tmr_cmd(hw);
2178
2179
2180 zo = rd32(hw, GLTSYN_SHTIME_0(tmr_idx));
2181 lo = rd32(hw, GLTSYN_SHTIME_L(tmr_idx));
2182 *phc_time = (u64)lo << 32 | zo;
2183
2184
2185 err = ice_ptp_read_port_capture(hw, port, &tx_time, &rx_time);
2186 if (err)
2187 return err;
2188
2189
2190
2191
2192
2193 if (tx_time != rx_time)
2194 dev_warn(ice_hw_to_dev(hw),
2195 "PHY port %u Tx and Rx timers do not match, tx_time 0x%016llX, rx_time 0x%016llX\n",
2196 port, (unsigned long long)tx_time,
2197 (unsigned long long)rx_time);
2198
2199 *phy_time = tx_time;
2200
2201 return 0;
2202 }
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215 static int ice_sync_phy_timer_e822(struct ice_hw *hw, u8 port)
2216 {
2217 u64 phc_time, phy_time, difference;
2218 int err;
2219
2220 if (!ice_ptp_lock(hw)) {
2221 ice_debug(hw, ICE_DBG_PTP, "Failed to acquire PTP semaphore\n");
2222 return -EBUSY;
2223 }
2224
2225 err = ice_read_phy_and_phc_time_e822(hw, port, &phy_time, &phc_time);
2226 if (err)
2227 goto err_unlock;
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237 difference = phc_time - phy_time;
2238
2239 err = ice_ptp_prep_port_adj_e822(hw, port, (s64)difference);
2240 if (err)
2241 goto err_unlock;
2242
2243 err = ice_ptp_one_port_cmd(hw, port, ADJ_TIME);
2244 if (err)
2245 goto err_unlock;
2246
2247
2248 ice_ptp_exec_tmr_cmd(hw);
2249
2250
2251
2252
2253 err = ice_read_phy_and_phc_time_e822(hw, port, &phy_time, &phc_time);
2254 if (err)
2255 goto err_unlock;
2256
2257 dev_info(ice_hw_to_dev(hw),
2258 "Port %u PHY time synced to PHC: 0x%016llX, 0x%016llX\n",
2259 port, (unsigned long long)phy_time,
2260 (unsigned long long)phc_time);
2261
2262 ice_ptp_unlock(hw);
2263
2264 return 0;
2265
2266 err_unlock:
2267 ice_ptp_unlock(hw);
2268 return err;
2269 }
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281 int
2282 ice_stop_phy_timer_e822(struct ice_hw *hw, u8 port, bool soft_reset)
2283 {
2284 int err;
2285 u32 val;
2286
2287 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_OR, 0);
2288 if (err)
2289 return err;
2290
2291 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_OR, 0);
2292 if (err)
2293 return err;
2294
2295 err = ice_read_phy_reg_e822(hw, port, P_REG_PS, &val);
2296 if (err)
2297 return err;
2298
2299 val &= ~P_REG_PS_START_M;
2300 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2301 if (err)
2302 return err;
2303
2304 val &= ~P_REG_PS_ENA_CLK_M;
2305 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2306 if (err)
2307 return err;
2308
2309 if (soft_reset) {
2310 val |= P_REG_PS_SFT_RESET_M;
2311 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2312 if (err)
2313 return err;
2314 }
2315
2316 ice_debug(hw, ICE_DBG_PTP, "Disabled clock on PHY port %u\n", port);
2317
2318 return 0;
2319 }
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337 int
2338 ice_start_phy_timer_e822(struct ice_hw *hw, u8 port, bool bypass)
2339 {
2340 u32 lo, hi, val;
2341 u64 incval;
2342 u8 tmr_idx;
2343 int err;
2344
2345 tmr_idx = ice_get_ptp_src_clock_index(hw);
2346
2347 err = ice_stop_phy_timer_e822(hw, port, false);
2348 if (err)
2349 return err;
2350
2351 ice_phy_cfg_lane_e822(hw, port);
2352
2353 err = ice_phy_cfg_uix_e822(hw, port);
2354 if (err)
2355 return err;
2356
2357 err = ice_phy_cfg_parpcs_e822(hw, port);
2358 if (err)
2359 return err;
2360
2361 lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
2362 hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
2363 incval = (u64)hi << 32 | lo;
2364
2365 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_TIMETUS_L, incval);
2366 if (err)
2367 return err;
2368
2369 err = ice_ptp_one_port_cmd(hw, port, INIT_INCVAL);
2370 if (err)
2371 return err;
2372
2373 ice_ptp_exec_tmr_cmd(hw);
2374
2375 err = ice_read_phy_reg_e822(hw, port, P_REG_PS, &val);
2376 if (err)
2377 return err;
2378
2379 val |= P_REG_PS_SFT_RESET_M;
2380 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2381 if (err)
2382 return err;
2383
2384 val |= P_REG_PS_START_M;
2385 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2386 if (err)
2387 return err;
2388
2389 val &= ~P_REG_PS_SFT_RESET_M;
2390 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2391 if (err)
2392 return err;
2393
2394 err = ice_ptp_one_port_cmd(hw, port, INIT_INCVAL);
2395 if (err)
2396 return err;
2397
2398 ice_ptp_exec_tmr_cmd(hw);
2399
2400 val |= P_REG_PS_ENA_CLK_M;
2401 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2402 if (err)
2403 return err;
2404
2405 val |= P_REG_PS_LOAD_OFFSET_M;
2406 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2407 if (err)
2408 return err;
2409
2410 ice_ptp_exec_tmr_cmd(hw);
2411
2412 err = ice_sync_phy_timer_e822(hw, port);
2413 if (err)
2414 return err;
2415
2416 if (bypass) {
2417 val |= P_REG_PS_BYPASS_MODE_M;
2418
2419 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2420 if (err)
2421 return err;
2422
2423
2424 err = ice_phy_cfg_fixed_tx_offset_e822(hw, port);
2425 if (err)
2426 return err;
2427
2428
2429 err = ice_phy_cfg_fixed_rx_offset_e822(hw, port);
2430 if (err)
2431 return err;
2432 }
2433
2434 ice_debug(hw, ICE_DBG_PTP, "Enabled clock on PHY port %u\n", port);
2435
2436 return 0;
2437 }
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453 int ice_phy_exit_bypass_e822(struct ice_hw *hw, u8 port)
2454 {
2455 int err;
2456 u32 val;
2457
2458 err = ice_read_phy_reg_e822(hw, port, P_REG_TX_OV_STATUS, &val);
2459 if (err) {
2460 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_OV_STATUS for port %u, err %d\n",
2461 port, err);
2462 return err;
2463 }
2464
2465 if (!(val & P_REG_TX_OV_STATUS_OV_M)) {
2466 ice_debug(hw, ICE_DBG_PTP, "Tx offset is not yet valid for port %u\n",
2467 port);
2468 return -EBUSY;
2469 }
2470
2471 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_OV_STATUS, &val);
2472 if (err) {
2473 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_OV_STATUS for port %u, err %d\n",
2474 port, err);
2475 return err;
2476 }
2477
2478 if (!(val & P_REG_TX_OV_STATUS_OV_M)) {
2479 ice_debug(hw, ICE_DBG_PTP, "Rx offset is not yet valid for port %u\n",
2480 port);
2481 return -EBUSY;
2482 }
2483
2484 err = ice_phy_cfg_tx_offset_e822(hw, port);
2485 if (err) {
2486 ice_debug(hw, ICE_DBG_PTP, "Failed to program total Tx offset for port %u, err %d\n",
2487 port, err);
2488 return err;
2489 }
2490
2491 err = ice_phy_cfg_rx_offset_e822(hw, port);
2492 if (err) {
2493 ice_debug(hw, ICE_DBG_PTP, "Failed to program total Rx offset for port %u, err %d\n",
2494 port, err);
2495 return err;
2496 }
2497
2498
2499 err = ice_read_phy_reg_e822(hw, port, P_REG_PS, &val);
2500 if (err) {
2501 ice_debug(hw, ICE_DBG_PTP, "Failed to read P_REG_PS for port %u, err %d\n",
2502 port, err);
2503 return err;
2504 }
2505
2506 if (!(val & P_REG_PS_BYPASS_MODE_M))
2507 ice_debug(hw, ICE_DBG_PTP, "Port %u not in bypass mode\n",
2508 port);
2509
2510 val &= ~P_REG_PS_BYPASS_MODE_M;
2511 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2512 if (err) {
2513 ice_debug(hw, ICE_DBG_PTP, "Failed to disable bypass for port %u, err %d\n",
2514 port, err);
2515 return err;
2516 }
2517
2518 dev_info(ice_hw_to_dev(hw), "Exiting bypass mode on PHY port %u\n",
2519 port);
2520
2521 return 0;
2522 }
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538 static int ice_read_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 *val)
2539 {
2540 struct ice_sbq_msg_input msg = {0};
2541 int err;
2542
2543 msg.msg_addr_low = lower_16_bits(addr);
2544 msg.msg_addr_high = upper_16_bits(addr);
2545 msg.opcode = ice_sbq_msg_rd;
2546 msg.dest_dev = rmn_0;
2547
2548 err = ice_sbq_rw_reg(hw, &msg);
2549 if (err) {
2550 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
2551 err);
2552 return err;
2553 }
2554
2555 *val = msg.data;
2556
2557 return 0;
2558 }
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568 static int ice_write_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 val)
2569 {
2570 struct ice_sbq_msg_input msg = {0};
2571 int err;
2572
2573 msg.msg_addr_low = lower_16_bits(addr);
2574 msg.msg_addr_high = upper_16_bits(addr);
2575 msg.opcode = ice_sbq_msg_wr;
2576 msg.dest_dev = rmn_0;
2577 msg.data = val;
2578
2579 err = ice_sbq_rw_reg(hw, &msg);
2580 if (err) {
2581 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
2582 err);
2583 return err;
2584 }
2585
2586 return 0;
2587 }
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599 static int
2600 ice_read_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx, u64 *tstamp)
2601 {
2602 u32 lo_addr, hi_addr, lo, hi;
2603 int err;
2604
2605 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
2606 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
2607
2608 err = ice_read_phy_reg_e810(hw, lo_addr, &lo);
2609 if (err) {
2610 ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
2611 err);
2612 return err;
2613 }
2614
2615 err = ice_read_phy_reg_e810(hw, hi_addr, &hi);
2616 if (err) {
2617 ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
2618 err);
2619 return err;
2620 }
2621
2622
2623
2624
2625 *tstamp = ((u64)hi) << TS_HIGH_S | ((u64)lo & TS_LOW_M);
2626
2627 return 0;
2628 }
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639 static int ice_clear_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx)
2640 {
2641 u32 lo_addr, hi_addr;
2642 int err;
2643
2644 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
2645 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
2646
2647 err = ice_write_phy_reg_e810(hw, lo_addr, 0);
2648 if (err) {
2649 ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register, err %d\n",
2650 err);
2651 return err;
2652 }
2653
2654 err = ice_write_phy_reg_e810(hw, hi_addr, 0);
2655 if (err) {
2656 ice_debug(hw, ICE_DBG_PTP, "Failed to clear high PTP timestamp register, err %d\n",
2657 err);
2658 return err;
2659 }
2660
2661 return 0;
2662 }
2663
2664
2665
2666
2667
2668
2669
2670
2671 int ice_ptp_init_phy_e810(struct ice_hw *hw)
2672 {
2673 u8 tmr_idx;
2674 int err;
2675
2676 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2677 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_ENA(tmr_idx),
2678 GLTSYN_ENA_TSYN_ENA_M);
2679 if (err)
2680 ice_debug(hw, ICE_DBG_PTP, "PTP failed in ena_phy_time_syn %d\n",
2681 err);
2682
2683 return err;
2684 }
2685
2686
2687
2688
2689
2690
2691
2692 static int ice_ptp_init_phc_e810(struct ice_hw *hw)
2693 {
2694
2695 wr32(hw, GLTSYN_SYNC_DLAY, 0);
2696
2697
2698 return ice_ptp_init_phy_e810(hw);
2699 }
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713 static int ice_ptp_prep_phy_time_e810(struct ice_hw *hw, u32 time)
2714 {
2715 u8 tmr_idx;
2716 int err;
2717
2718 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2719 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_0(tmr_idx), 0);
2720 if (err) {
2721 ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_0, err %d\n",
2722 err);
2723 return err;
2724 }
2725
2726 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_L(tmr_idx), time);
2727 if (err) {
2728 ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_L, err %d\n",
2729 err);
2730 return err;
2731 }
2732
2733 return 0;
2734 }
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749 static int ice_ptp_prep_phy_adj_e810(struct ice_hw *hw, s32 adj)
2750 {
2751 u8 tmr_idx;
2752 int err;
2753
2754 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2755
2756
2757
2758
2759 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), 0);
2760 if (err) {
2761 ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_L, err %d\n",
2762 err);
2763 return err;
2764 }
2765
2766 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), adj);
2767 if (err) {
2768 ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_H, err %d\n",
2769 err);
2770 return err;
2771 }
2772
2773 return 0;
2774 }
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785 static int ice_ptp_prep_phy_incval_e810(struct ice_hw *hw, u64 incval)
2786 {
2787 u32 high, low;
2788 u8 tmr_idx;
2789 int err;
2790
2791 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2792 low = lower_32_bits(incval);
2793 high = upper_32_bits(incval);
2794
2795 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), low);
2796 if (err) {
2797 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval to PHY SHADJ_L, err %d\n",
2798 err);
2799 return err;
2800 }
2801
2802 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), high);
2803 if (err) {
2804 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval PHY SHADJ_H, err %d\n",
2805 err);
2806 return err;
2807 }
2808
2809 return 0;
2810 }
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820 static int ice_ptp_port_cmd_e810(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
2821 {
2822 u32 cmd_val, val;
2823 int err;
2824
2825 switch (cmd) {
2826 case INIT_TIME:
2827 cmd_val = GLTSYN_CMD_INIT_TIME;
2828 break;
2829 case INIT_INCVAL:
2830 cmd_val = GLTSYN_CMD_INIT_INCVAL;
2831 break;
2832 case ADJ_TIME:
2833 cmd_val = GLTSYN_CMD_ADJ_TIME;
2834 break;
2835 case READ_TIME:
2836 cmd_val = GLTSYN_CMD_READ_TIME;
2837 break;
2838 case ADJ_TIME_AT_TIME:
2839 cmd_val = GLTSYN_CMD_ADJ_INIT_TIME;
2840 break;
2841 }
2842
2843
2844 err = ice_read_phy_reg_e810(hw, ETH_GLTSYN_CMD, &val);
2845 if (err) {
2846 ice_debug(hw, ICE_DBG_PTP, "Failed to read GLTSYN_CMD, err %d\n", err);
2847 return err;
2848 }
2849
2850
2851 val &= ~TS_CMD_MASK_E810;
2852 val |= cmd_val;
2853
2854 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_CMD, val);
2855 if (err) {
2856 ice_debug(hw, ICE_DBG_PTP, "Failed to write back GLTSYN_CMD, err %d\n", err);
2857 return err;
2858 }
2859
2860 return 0;
2861 }
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885 bool ice_ptp_lock(struct ice_hw *hw)
2886 {
2887 u32 hw_lock;
2888 int i;
2889
2890 #define MAX_TRIES 5
2891
2892 for (i = 0; i < MAX_TRIES; i++) {
2893 hw_lock = rd32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
2894 hw_lock = hw_lock & PFTSYN_SEM_BUSY_M;
2895 if (!hw_lock)
2896 break;
2897
2898
2899 usleep_range(10000, 20000);
2900 }
2901
2902 return !hw_lock;
2903 }
2904
2905
2906
2907
2908
2909
2910
2911
2912 void ice_ptp_unlock(struct ice_hw *hw)
2913 {
2914 wr32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), 0);
2915 }
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927 static int ice_ptp_tmr_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
2928 {
2929 int err;
2930
2931
2932 ice_ptp_src_cmd(hw, cmd);
2933
2934
2935 if (ice_is_e810(hw))
2936 err = ice_ptp_port_cmd_e810(hw, cmd);
2937 else
2938 err = ice_ptp_port_cmd_e822(hw, cmd);
2939 if (err) {
2940 ice_debug(hw, ICE_DBG_PTP, "Failed to prepare PHY ports for timer command %u, err %d\n",
2941 cmd, err);
2942 return err;
2943 }
2944
2945
2946
2947
2948 ice_ptp_exec_tmr_cmd(hw);
2949
2950 return 0;
2951 }
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966 int ice_ptp_init_time(struct ice_hw *hw, u64 time)
2967 {
2968 u8 tmr_idx;
2969 int err;
2970
2971 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2972
2973
2974 wr32(hw, GLTSYN_SHTIME_L(tmr_idx), lower_32_bits(time));
2975 wr32(hw, GLTSYN_SHTIME_H(tmr_idx), upper_32_bits(time));
2976 wr32(hw, GLTSYN_SHTIME_0(tmr_idx), 0);
2977
2978
2979
2980 if (ice_is_e810(hw))
2981 err = ice_ptp_prep_phy_time_e810(hw, time & 0xFFFFFFFF);
2982 else
2983 err = ice_ptp_prep_phy_time_e822(hw, time & 0xFFFFFFFF);
2984 if (err)
2985 return err;
2986
2987 return ice_ptp_tmr_cmd(hw, INIT_TIME);
2988 }
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004 int ice_ptp_write_incval(struct ice_hw *hw, u64 incval)
3005 {
3006 u8 tmr_idx;
3007 int err;
3008
3009 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3010
3011
3012 wr32(hw, GLTSYN_SHADJ_L(tmr_idx), lower_32_bits(incval));
3013 wr32(hw, GLTSYN_SHADJ_H(tmr_idx), upper_32_bits(incval));
3014
3015 if (ice_is_e810(hw))
3016 err = ice_ptp_prep_phy_incval_e810(hw, incval);
3017 else
3018 err = ice_ptp_prep_phy_incval_e822(hw, incval);
3019 if (err)
3020 return err;
3021
3022 return ice_ptp_tmr_cmd(hw, INIT_INCVAL);
3023 }
3024
3025
3026
3027
3028
3029
3030
3031
3032 int ice_ptp_write_incval_locked(struct ice_hw *hw, u64 incval)
3033 {
3034 int err;
3035
3036 if (!ice_ptp_lock(hw))
3037 return -EBUSY;
3038
3039 err = ice_ptp_write_incval(hw, incval);
3040
3041 ice_ptp_unlock(hw);
3042
3043 return err;
3044 }
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059 int ice_ptp_adj_clock(struct ice_hw *hw, s32 adj)
3060 {
3061 u8 tmr_idx;
3062 int err;
3063
3064 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3065
3066
3067
3068
3069
3070
3071 wr32(hw, GLTSYN_SHADJ_L(tmr_idx), 0);
3072 wr32(hw, GLTSYN_SHADJ_H(tmr_idx), adj);
3073
3074 if (ice_is_e810(hw))
3075 err = ice_ptp_prep_phy_adj_e810(hw, adj);
3076 else
3077 err = ice_ptp_prep_phy_adj_e822(hw, adj);
3078 if (err)
3079 return err;
3080
3081 return ice_ptp_tmr_cmd(hw, ADJ_TIME);
3082 }
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095 int ice_read_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx, u64 *tstamp)
3096 {
3097 if (ice_is_e810(hw))
3098 return ice_read_phy_tstamp_e810(hw, block, idx, tstamp);
3099 else
3100 return ice_read_phy_tstamp_e822(hw, block, idx, tstamp);
3101 }
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113 int ice_clear_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx)
3114 {
3115 if (ice_is_e810(hw))
3116 return ice_clear_phy_tstamp_e810(hw, block, idx);
3117 else
3118 return ice_clear_phy_tstamp_e822(hw, block, idx);
3119 }
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136 static int
3137 ice_get_pca9575_handle(struct ice_hw *hw, u16 *pca9575_handle)
3138 {
3139 struct ice_aqc_get_link_topo *cmd;
3140 struct ice_aq_desc desc;
3141 int status;
3142 u8 idx;
3143
3144
3145 if (hw->io_expander_handle) {
3146 *pca9575_handle = hw->io_expander_handle;
3147 return 0;
3148 }
3149
3150
3151 cmd = &desc.params.get_link_topo;
3152 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_topo);
3153
3154
3155 cmd->addr.topo_params.node_type_ctx =
3156 (ICE_AQC_LINK_TOPO_NODE_TYPE_M &
3157 ICE_AQC_LINK_TOPO_NODE_TYPE_GPIO_CTRL);
3158
3159 #define SW_PCA9575_SFP_TOPO_IDX 2
3160 #define SW_PCA9575_QSFP_TOPO_IDX 1
3161
3162
3163 if (hw->device_id == ICE_DEV_ID_E810C_SFP)
3164 idx = SW_PCA9575_SFP_TOPO_IDX;
3165 else if (hw->device_id == ICE_DEV_ID_E810C_QSFP)
3166 idx = SW_PCA9575_QSFP_TOPO_IDX;
3167 else
3168 return -EOPNOTSUPP;
3169
3170 cmd->addr.topo_params.index = idx;
3171
3172 status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
3173 if (status)
3174 return -EOPNOTSUPP;
3175
3176
3177 if (desc.params.get_link_topo.node_part_num !=
3178 ICE_AQC_GET_LINK_TOPO_NODE_NR_PCA9575)
3179 return -EOPNOTSUPP;
3180
3181
3182 hw->io_expander_handle =
3183 le16_to_cpu(desc.params.get_link_topo.addr.handle);
3184 *pca9575_handle = hw->io_expander_handle;
3185
3186 return 0;
3187 }
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197 int ice_read_sma_ctrl_e810t(struct ice_hw *hw, u8 *data)
3198 {
3199 int status;
3200 u16 handle;
3201 u8 i;
3202
3203 status = ice_get_pca9575_handle(hw, &handle);
3204 if (status)
3205 return status;
3206
3207 *data = 0;
3208
3209 for (i = ICE_SMA_MIN_BIT_E810T; i <= ICE_SMA_MAX_BIT_E810T; i++) {
3210 bool pin;
3211
3212 status = ice_aq_get_gpio(hw, handle, i + ICE_PCA9575_P1_OFFSET,
3213 &pin, NULL);
3214 if (status)
3215 break;
3216 *data |= (u8)(!pin) << i;
3217 }
3218
3219 return status;
3220 }
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230 int ice_write_sma_ctrl_e810t(struct ice_hw *hw, u8 data)
3231 {
3232 int status;
3233 u16 handle;
3234 u8 i;
3235
3236 status = ice_get_pca9575_handle(hw, &handle);
3237 if (status)
3238 return status;
3239
3240 for (i = ICE_SMA_MIN_BIT_E810T; i <= ICE_SMA_MAX_BIT_E810T; i++) {
3241 bool pin;
3242
3243 pin = !(data & (1 << i));
3244 status = ice_aq_set_gpio(hw, handle, i + ICE_PCA9575_P1_OFFSET,
3245 pin, NULL);
3246 if (status)
3247 break;
3248 }
3249
3250 return status;
3251 }
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261 int ice_read_pca9575_reg_e810t(struct ice_hw *hw, u8 offset, u8 *data)
3262 {
3263 struct ice_aqc_link_topo_addr link_topo;
3264 __le16 addr;
3265 u16 handle;
3266 int err;
3267
3268 memset(&link_topo, 0, sizeof(link_topo));
3269
3270 err = ice_get_pca9575_handle(hw, &handle);
3271 if (err)
3272 return err;
3273
3274 link_topo.handle = cpu_to_le16(handle);
3275 link_topo.topo_params.node_type_ctx =
3276 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
3277 ICE_AQC_LINK_TOPO_NODE_CTX_PROVIDED);
3278
3279 addr = cpu_to_le16((u16)offset);
3280
3281 return ice_aq_read_i2c(hw, link_topo, 0, addr, 1, data, NULL);
3282 }
3283
3284
3285
3286
3287
3288
3289
3290 bool ice_is_pca9575_present(struct ice_hw *hw)
3291 {
3292 u16 handle = 0;
3293 int status;
3294
3295 if (!ice_is_e810t(hw))
3296 return false;
3297
3298 status = ice_get_pca9575_handle(hw, &handle);
3299
3300 return !status && handle;
3301 }
3302
3303
3304
3305
3306
3307
3308
3309 int ice_ptp_init_phc(struct ice_hw *hw)
3310 {
3311 u8 src_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3312
3313
3314 wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M);
3315
3316
3317 (void)rd32(hw, GLTSYN_STAT(src_idx));
3318
3319 if (ice_is_e810(hw))
3320 return ice_ptp_init_phc_e810(hw);
3321 else
3322 return ice_ptp_init_phc_e822(hw);
3323 }