1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
4 #include <linux/delay.h>
5 #include "ice_common.h"
6 #include "ice_ptp_hw.h"
7 #include "ice_ptp_consts.h"
8 #include "ice_cgu_regs.h"
10 /* Low level functions for interacting with and managing the device clock used
11 * for the Precision Time Protocol.
13 * The ice hardware represents the current time using three registers:
15 * GLTSYN_TIME_H GLTSYN_TIME_L GLTSYN_TIME_R
16 * +---------------+ +---------------+ +---------------+
17 * | 32 bits | | 32 bits | | 32 bits |
18 * +---------------+ +---------------+ +---------------+
20 * The registers are incremented every clock tick using a 40bit increment
21 * value defined over two registers:
23 * GLTSYN_INCVAL_H GLTSYN_INCVAL_L
24 * +---------------+ +---------------+
25 * | 8 bit s | | 32 bits |
26 * +---------------+ +---------------+
28 * The increment value is added to the GLSTYN_TIME_R and GLSTYN_TIME_L
29 * registers every clock source tick. Depending on the specific device
30 * configuration, the clock source frequency could be one of a number of
33 * For E810 devices, the increment frequency is 812.5 MHz
35 * For E822 devices the clock can be derived from different sources, and the
36 * increment has an effective frequency of one of the following:
44 * The hardware captures timestamps in the PHY for incoming packets, and for
45 * outgoing packets on request. To support this, the PHY maintains a timer
46 * that matches the lower 64 bits of the global source timer.
48 * In order to ensure that the PHY timers and the source timer are equivalent,
49 * shadow registers are used to prepare the desired initial values. A special
50 * sync command is issued to trigger copying from the shadow registers into
51 * the appropriate source and PHY registers simultaneously.
53 * The driver supports devices which have different PHYs with subtly different
54 * mechanisms to program and control the timers. We divide the devices into
55 * families named after the first major device, E810 and similar devices, and
56 * E822 and similar devices.
58 * - E822 based devices have additional support for fine grained Vernier
59 * calibration which requires significant setup
60 * - The layout of timestamp data in the PHY register blocks is different
61 * - The way timer synchronization commands are issued is different.
63 * To support this, very low level functions have an e810 or e822 suffix
64 * indicating what type of device they work on. Higher level abstractions for
65 * tasks that can be done on both devices do not have the suffix and will
66 * correctly look up the appropriate low level function when running.
68 * Functions which only make sense on a single device family may not have
69 * a suitable generic implementation
73 * ice_get_ptp_src_clock_index - determine source clock index
74 * @hw: pointer to HW struct
76 * Determine the source clock index currently in use, based on device
77 * capabilities reported during initialization.
79 u8 ice_get_ptp_src_clock_index(struct ice_hw *hw)
81 return hw->func_caps.ts_func_info.tmr_index_assoc;
85 * ice_ptp_read_src_incval - Read source timer increment value
86 * @hw: pointer to HW struct
88 * Read the increment value of the source timer and return it.
90 static u64 ice_ptp_read_src_incval(struct ice_hw *hw)
95 tmr_idx = ice_get_ptp_src_clock_index(hw);
97 lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
98 hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
100 return ((u64)(hi & INCVAL_HIGH_M) << 32) | lo;
104 * ice_ptp_src_cmd - Prepare source timer for a timer command
105 * @hw: pointer to HW structure
106 * @cmd: Timer command
108 * Prepare the source timer for an upcoming timer sync command.
110 static void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
115 tmr_idx = ice_get_ptp_src_clock_index(hw);
116 cmd_val = tmr_idx << SEL_CPK_SRC;
120 cmd_val |= GLTSYN_CMD_INIT_TIME;
123 cmd_val |= GLTSYN_CMD_INIT_INCVAL;
126 cmd_val |= GLTSYN_CMD_ADJ_TIME;
128 case ADJ_TIME_AT_TIME:
129 cmd_val |= GLTSYN_CMD_ADJ_INIT_TIME;
132 cmd_val |= GLTSYN_CMD_READ_TIME;
136 wr32(hw, GLTSYN_CMD, cmd_val);
140 * ice_ptp_exec_tmr_cmd - Execute all prepared timer commands
141 * @hw: pointer to HW struct
143 * Write the SYNC_EXEC_CMD bit to the GLTSYN_CMD_SYNC register, and flush the
144 * write immediately. This triggers the hardware to begin executing all of the
145 * source and PHY timer commands synchronously.
147 static void ice_ptp_exec_tmr_cmd(struct ice_hw *hw)
149 wr32(hw, GLTSYN_CMD_SYNC, SYNC_EXEC_CMD);
153 /* E822 family functions
155 * The following functions operate on the E822 family of devices.
159 * ice_fill_phy_msg_e822 - Fill message data for a PHY register access
160 * @msg: the PHY message buffer to fill in
161 * @port: the port to access
162 * @offset: the register offset
165 ice_fill_phy_msg_e822(struct ice_sbq_msg_input *msg, u8 port, u16 offset)
167 int phy_port, phy, quadtype;
169 phy_port = port % ICE_PORTS_PER_PHY;
170 phy = port / ICE_PORTS_PER_PHY;
171 quadtype = (port / ICE_PORTS_PER_QUAD) % ICE_NUM_QUAD_TYPE;
174 msg->msg_addr_low = P_Q0_L(P_0_BASE + offset, phy_port);
175 msg->msg_addr_high = P_Q0_H(P_0_BASE + offset, phy_port);
177 msg->msg_addr_low = P_Q1_L(P_4_BASE + offset, phy_port);
178 msg->msg_addr_high = P_Q1_H(P_4_BASE + offset, phy_port);
182 msg->dest_dev = rmn_0;
184 msg->dest_dev = rmn_1;
186 msg->dest_dev = rmn_2;
190 * ice_is_64b_phy_reg_e822 - Check if this is a 64bit PHY register
191 * @low_addr: the low address to check
192 * @high_addr: on return, contains the high address of the 64bit register
194 * Checks if the provided low address is one of the known 64bit PHY values
195 * represented as two 32bit registers. If it is, return the appropriate high
196 * register offset to use.
198 static bool ice_is_64b_phy_reg_e822(u16 low_addr, u16 *high_addr)
201 case P_REG_PAR_PCS_TX_OFFSET_L:
202 *high_addr = P_REG_PAR_PCS_TX_OFFSET_U;
204 case P_REG_PAR_PCS_RX_OFFSET_L:
205 *high_addr = P_REG_PAR_PCS_RX_OFFSET_U;
207 case P_REG_PAR_TX_TIME_L:
208 *high_addr = P_REG_PAR_TX_TIME_U;
210 case P_REG_PAR_RX_TIME_L:
211 *high_addr = P_REG_PAR_RX_TIME_U;
213 case P_REG_TOTAL_TX_OFFSET_L:
214 *high_addr = P_REG_TOTAL_TX_OFFSET_U;
216 case P_REG_TOTAL_RX_OFFSET_L:
217 *high_addr = P_REG_TOTAL_RX_OFFSET_U;
219 case P_REG_UIX66_10G_40G_L:
220 *high_addr = P_REG_UIX66_10G_40G_U;
222 case P_REG_UIX66_25G_100G_L:
223 *high_addr = P_REG_UIX66_25G_100G_U;
225 case P_REG_TX_CAPTURE_L:
226 *high_addr = P_REG_TX_CAPTURE_U;
228 case P_REG_RX_CAPTURE_L:
229 *high_addr = P_REG_RX_CAPTURE_U;
231 case P_REG_TX_TIMER_INC_PRE_L:
232 *high_addr = P_REG_TX_TIMER_INC_PRE_U;
234 case P_REG_RX_TIMER_INC_PRE_L:
235 *high_addr = P_REG_RX_TIMER_INC_PRE_U;
243 * ice_is_40b_phy_reg_e822 - Check if this is a 40bit PHY register
244 * @low_addr: the low address to check
245 * @high_addr: on return, contains the high address of the 40bit value
247 * Checks if the provided low address is one of the known 40bit PHY values
248 * split into two registers with the lower 8 bits in the low register and the
249 * upper 32 bits in the high register. If it is, return the appropriate high
250 * register offset to use.
252 static bool ice_is_40b_phy_reg_e822(u16 low_addr, u16 *high_addr)
255 case P_REG_TIMETUS_L:
256 *high_addr = P_REG_TIMETUS_U;
258 case P_REG_PAR_RX_TUS_L:
259 *high_addr = P_REG_PAR_RX_TUS_U;
261 case P_REG_PAR_TX_TUS_L:
262 *high_addr = P_REG_PAR_TX_TUS_U;
264 case P_REG_PCS_RX_TUS_L:
265 *high_addr = P_REG_PCS_RX_TUS_U;
267 case P_REG_PCS_TX_TUS_L:
268 *high_addr = P_REG_PCS_TX_TUS_U;
270 case P_REG_DESK_PAR_RX_TUS_L:
271 *high_addr = P_REG_DESK_PAR_RX_TUS_U;
273 case P_REG_DESK_PAR_TX_TUS_L:
274 *high_addr = P_REG_DESK_PAR_TX_TUS_U;
276 case P_REG_DESK_PCS_RX_TUS_L:
277 *high_addr = P_REG_DESK_PCS_RX_TUS_U;
279 case P_REG_DESK_PCS_TX_TUS_L:
280 *high_addr = P_REG_DESK_PCS_TX_TUS_U;
288 * ice_read_phy_reg_e822 - Read a PHY register
289 * @hw: pointer to the HW struct
290 * @port: PHY port to read from
291 * @offset: PHY register offset to read
292 * @val: on return, the contents read from the PHY
294 * Read a PHY register for the given port over the device sideband queue.
297 ice_read_phy_reg_e822(struct ice_hw *hw, u8 port, u16 offset, u32 *val)
299 struct ice_sbq_msg_input msg = {0};
302 ice_fill_phy_msg_e822(&msg, port, offset);
303 msg.opcode = ice_sbq_msg_rd;
305 err = ice_sbq_rw_reg(hw, &msg);
307 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
318 * ice_read_64b_phy_reg_e822 - Read a 64bit value from PHY registers
319 * @hw: pointer to the HW struct
320 * @port: PHY port to read from
321 * @low_addr: offset of the lower register to read from
322 * @val: on return, the contents of the 64bit value from the PHY registers
324 * Reads the two registers associated with a 64bit value and returns it in the
325 * val pointer. The offset always specifies the lower register offset to use.
326 * The high offset is looked up. This function only operates on registers
327 * known to be two parts of a 64bit value.
330 ice_read_64b_phy_reg_e822(struct ice_hw *hw, u8 port, u16 low_addr, u64 *val)
336 /* Only operate on registers known to be split into two 32bit
339 if (!ice_is_64b_phy_reg_e822(low_addr, &high_addr)) {
340 ice_debug(hw, ICE_DBG_PTP, "Invalid 64b register addr 0x%08x\n",
345 err = ice_read_phy_reg_e822(hw, port, low_addr, &low);
347 ice_debug(hw, ICE_DBG_PTP, "Failed to read from low register 0x%08x\n, err %d",
352 err = ice_read_phy_reg_e822(hw, port, high_addr, &high);
354 ice_debug(hw, ICE_DBG_PTP, "Failed to read from high register 0x%08x\n, err %d",
359 *val = (u64)high << 32 | low;
365 * ice_write_phy_reg_e822 - Write a PHY register
366 * @hw: pointer to the HW struct
367 * @port: PHY port to write to
368 * @offset: PHY register offset to write
369 * @val: The value to write to the register
371 * Write a PHY register for the given port over the device sideband queue.
374 ice_write_phy_reg_e822(struct ice_hw *hw, u8 port, u16 offset, u32 val)
376 struct ice_sbq_msg_input msg = {0};
379 ice_fill_phy_msg_e822(&msg, port, offset);
380 msg.opcode = ice_sbq_msg_wr;
383 err = ice_sbq_rw_reg(hw, &msg);
385 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
394 * ice_write_40b_phy_reg_e822 - Write a 40b value to the PHY
395 * @hw: pointer to the HW struct
396 * @port: port to write to
397 * @low_addr: offset of the low register
398 * @val: 40b value to write
400 * Write the provided 40b value to the two associated registers by splitting
401 * it up into two chunks, the lower 8 bits and the upper 32 bits.
404 ice_write_40b_phy_reg_e822(struct ice_hw *hw, u8 port, u16 low_addr, u64 val)
410 /* Only operate on registers known to be split into a lower 8 bit
411 * register and an upper 32 bit register.
413 if (!ice_is_40b_phy_reg_e822(low_addr, &high_addr)) {
414 ice_debug(hw, ICE_DBG_PTP, "Invalid 40b register addr 0x%08x\n",
419 low = (u32)(val & P_REG_40B_LOW_M);
420 high = (u32)(val >> P_REG_40B_HIGH_S);
422 err = ice_write_phy_reg_e822(hw, port, low_addr, low);
424 ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
429 err = ice_write_phy_reg_e822(hw, port, high_addr, high);
431 ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
440 * ice_write_64b_phy_reg_e822 - Write a 64bit value to PHY registers
441 * @hw: pointer to the HW struct
442 * @port: PHY port to read from
443 * @low_addr: offset of the lower register to read from
444 * @val: the contents of the 64bit value to write to PHY
446 * Write the 64bit value to the two associated 32bit PHY registers. The offset
447 * is always specified as the lower register, and the high address is looked
448 * up. This function only operates on registers known to be two parts of
452 ice_write_64b_phy_reg_e822(struct ice_hw *hw, u8 port, u16 low_addr, u64 val)
458 /* Only operate on registers known to be split into two 32bit
461 if (!ice_is_64b_phy_reg_e822(low_addr, &high_addr)) {
462 ice_debug(hw, ICE_DBG_PTP, "Invalid 64b register addr 0x%08x\n",
467 low = lower_32_bits(val);
468 high = upper_32_bits(val);
470 err = ice_write_phy_reg_e822(hw, port, low_addr, low);
472 ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
477 err = ice_write_phy_reg_e822(hw, port, high_addr, high);
479 ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
488 * ice_fill_quad_msg_e822 - Fill message data for quad register access
489 * @msg: the PHY message buffer to fill in
490 * @quad: the quad to access
491 * @offset: the register offset
493 * Fill a message buffer for accessing a register in a quad shared between
497 ice_fill_quad_msg_e822(struct ice_sbq_msg_input *msg, u8 quad, u16 offset)
501 msg->dest_dev = rmn_0;
503 if ((quad % ICE_NUM_QUAD_TYPE) == 0)
504 addr = Q_0_BASE + offset;
506 addr = Q_1_BASE + offset;
508 msg->msg_addr_low = lower_16_bits(addr);
509 msg->msg_addr_high = upper_16_bits(addr);
513 * ice_read_quad_reg_e822 - Read a PHY quad register
514 * @hw: pointer to the HW struct
515 * @quad: quad to read from
516 * @offset: quad register offset to read
517 * @val: on return, the contents read from the quad
519 * Read a quad register over the device sideband queue. Quad registers are
520 * shared between multiple PHYs.
523 ice_read_quad_reg_e822(struct ice_hw *hw, u8 quad, u16 offset, u32 *val)
525 struct ice_sbq_msg_input msg = {0};
528 if (quad >= ICE_MAX_QUAD)
531 ice_fill_quad_msg_e822(&msg, quad, offset);
532 msg.opcode = ice_sbq_msg_rd;
534 err = ice_sbq_rw_reg(hw, &msg);
536 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
547 * ice_write_quad_reg_e822 - Write a PHY quad register
548 * @hw: pointer to the HW struct
549 * @quad: quad to write to
550 * @offset: quad register offset to write
551 * @val: The value to write to the register
553 * Write a quad register over the device sideband queue. Quad registers are
554 * shared between multiple PHYs.
557 ice_write_quad_reg_e822(struct ice_hw *hw, u8 quad, u16 offset, u32 val)
559 struct ice_sbq_msg_input msg = {0};
562 if (quad >= ICE_MAX_QUAD)
565 ice_fill_quad_msg_e822(&msg, quad, offset);
566 msg.opcode = ice_sbq_msg_wr;
569 err = ice_sbq_rw_reg(hw, &msg);
571 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
580 * ice_read_phy_tstamp_e822 - Read a PHY timestamp out of the quad block
581 * @hw: pointer to the HW struct
582 * @quad: the quad to read from
583 * @idx: the timestamp index to read
584 * @tstamp: on return, the 40bit timestamp value
586 * Read a 40bit timestamp value out of the two associated registers in the
587 * quad memory block that is shared between the internal PHYs of the E822
591 ice_read_phy_tstamp_e822(struct ice_hw *hw, u8 quad, u8 idx, u64 *tstamp)
593 u16 lo_addr, hi_addr;
597 lo_addr = (u16)TS_L(Q_REG_TX_MEMORY_BANK_START, idx);
598 hi_addr = (u16)TS_H(Q_REG_TX_MEMORY_BANK_START, idx);
600 err = ice_read_quad_reg_e822(hw, quad, lo_addr, &lo);
602 ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
607 err = ice_read_quad_reg_e822(hw, quad, hi_addr, &hi);
609 ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
614 /* For E822 based internal PHYs, the timestamp is reported with the
615 * lower 8 bits in the low register, and the upper 32 bits in the high
618 *tstamp = ((u64)hi) << TS_PHY_HIGH_S | ((u64)lo & TS_PHY_LOW_M);
624 * ice_clear_phy_tstamp_e822 - Clear a timestamp from the quad block
625 * @hw: pointer to the HW struct
626 * @quad: the quad to read from
627 * @idx: the timestamp index to reset
629 * Clear a timestamp, resetting its valid bit, from the PHY quad block that is
630 * shared between the internal PHYs on the E822 devices.
633 ice_clear_phy_tstamp_e822(struct ice_hw *hw, u8 quad, u8 idx)
635 u16 lo_addr, hi_addr;
638 lo_addr = (u16)TS_L(Q_REG_TX_MEMORY_BANK_START, idx);
639 hi_addr = (u16)TS_H(Q_REG_TX_MEMORY_BANK_START, idx);
641 err = ice_write_quad_reg_e822(hw, quad, lo_addr, 0);
643 ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register, err %d\n",
648 err = ice_write_quad_reg_e822(hw, quad, hi_addr, 0);
650 ice_debug(hw, ICE_DBG_PTP, "Failed to clear high PTP timestamp register, err %d\n",
659 * ice_ptp_reset_ts_memory_quad_e822 - Clear all timestamps from the quad block
660 * @hw: pointer to the HW struct
661 * @quad: the quad to read from
663 * Clear all timestamps from the PHY quad block that is shared between the
664 * internal PHYs on the E822 devices.
666 void ice_ptp_reset_ts_memory_quad_e822(struct ice_hw *hw, u8 quad)
668 ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, Q_REG_TS_CTRL_M);
669 ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, ~(u32)Q_REG_TS_CTRL_M);
673 * ice_ptp_reset_ts_memory_e822 - Clear all timestamps from all quad blocks
674 * @hw: pointer to the HW struct
676 static void ice_ptp_reset_ts_memory_e822(struct ice_hw *hw)
680 for (quad = 0; quad < ICE_MAX_QUAD; quad++)
681 ice_ptp_reset_ts_memory_quad_e822(hw, quad);
685 * ice_read_cgu_reg_e822 - Read a CGU register
686 * @hw: pointer to the HW struct
687 * @addr: Register address to read
688 * @val: storage for register value read
690 * Read the contents of a register of the Clock Generation Unit. Only
691 * applicable to E822 devices.
694 ice_read_cgu_reg_e822(struct ice_hw *hw, u32 addr, u32 *val)
696 struct ice_sbq_msg_input cgu_msg;
699 cgu_msg.opcode = ice_sbq_msg_rd;
700 cgu_msg.dest_dev = cgu;
701 cgu_msg.msg_addr_low = addr;
702 cgu_msg.msg_addr_high = 0x0;
704 err = ice_sbq_rw_reg(hw, &cgu_msg);
706 ice_debug(hw, ICE_DBG_PTP, "Failed to read CGU register 0x%04x, err %d\n",
717 * ice_write_cgu_reg_e822 - Write a CGU register
718 * @hw: pointer to the HW struct
719 * @addr: Register address to write
720 * @val: value to write into the register
722 * Write the specified value to a register of the Clock Generation Unit. Only
723 * applicable to E822 devices.
726 ice_write_cgu_reg_e822(struct ice_hw *hw, u32 addr, u32 val)
728 struct ice_sbq_msg_input cgu_msg;
731 cgu_msg.opcode = ice_sbq_msg_wr;
732 cgu_msg.dest_dev = cgu;
733 cgu_msg.msg_addr_low = addr;
734 cgu_msg.msg_addr_high = 0x0;
737 err = ice_sbq_rw_reg(hw, &cgu_msg);
739 ice_debug(hw, ICE_DBG_PTP, "Failed to write CGU register 0x%04x, err %d\n",
748 * ice_clk_freq_str - Convert time_ref_freq to string
749 * @clk_freq: Clock frequency
751 * Convert the specified TIME_REF clock frequency to a string.
753 static const char *ice_clk_freq_str(u8 clk_freq)
755 switch ((enum ice_time_ref_freq)clk_freq) {
756 case ICE_TIME_REF_FREQ_25_000:
758 case ICE_TIME_REF_FREQ_122_880:
760 case ICE_TIME_REF_FREQ_125_000:
762 case ICE_TIME_REF_FREQ_153_600:
764 case ICE_TIME_REF_FREQ_156_250:
766 case ICE_TIME_REF_FREQ_245_760:
774 * ice_clk_src_str - Convert time_ref_src to string
775 * @clk_src: Clock source
777 * Convert the specified clock source to its string name.
779 static const char *ice_clk_src_str(u8 clk_src)
781 switch ((enum ice_clk_src)clk_src) {
782 case ICE_CLK_SRC_TCX0:
784 case ICE_CLK_SRC_TIME_REF:
792 * ice_cfg_cgu_pll_e822 - Configure the Clock Generation Unit
793 * @hw: pointer to the HW struct
794 * @clk_freq: Clock frequency to program
795 * @clk_src: Clock source to select (TIME_REF, or TCX0)
797 * Configure the Clock Generation Unit with the desired clock frequency and
798 * time reference, enabling the PLL which drives the PTP hardware clock.
801 ice_cfg_cgu_pll_e822(struct ice_hw *hw, enum ice_time_ref_freq clk_freq,
802 enum ice_clk_src clk_src)
804 union tspll_ro_bwm_lf bwm_lf;
805 union nac_cgu_dword19 dw19;
806 union nac_cgu_dword22 dw22;
807 union nac_cgu_dword24 dw24;
808 union nac_cgu_dword9 dw9;
811 if (clk_freq >= NUM_ICE_TIME_REF_FREQ) {
812 dev_warn(ice_hw_to_dev(hw), "Invalid TIME_REF frequency %u\n",
817 if (clk_src >= NUM_ICE_CLK_SRC) {
818 dev_warn(ice_hw_to_dev(hw), "Invalid clock source %u\n",
823 if (clk_src == ICE_CLK_SRC_TCX0 &&
824 clk_freq != ICE_TIME_REF_FREQ_25_000) {
825 dev_warn(ice_hw_to_dev(hw),
826 "TCX0 only supports 25 MHz frequency\n");
830 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD9, &dw9.val);
834 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD24, &dw24.val);
838 err = ice_read_cgu_reg_e822(hw, TSPLL_RO_BWM_LF, &bwm_lf.val);
842 /* Log the current clock configuration */
843 ice_debug(hw, ICE_DBG_PTP, "Current CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
844 dw24.field.ts_pll_enable ? "enabled" : "disabled",
845 ice_clk_src_str(dw24.field.time_ref_sel),
846 ice_clk_freq_str(dw9.field.time_ref_freq_sel),
847 bwm_lf.field.plllock_true_lock_cri ? "locked" : "unlocked");
849 /* Disable the PLL before changing the clock source or frequency */
850 if (dw24.field.ts_pll_enable) {
851 dw24.field.ts_pll_enable = 0;
853 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD24, dw24.val);
858 /* Set the frequency */
859 dw9.field.time_ref_freq_sel = clk_freq;
860 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD9, dw9.val);
864 /* Configure the TS PLL feedback divisor */
865 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD19, &dw19.val);
869 dw19.field.tspll_fbdiv_intgr = e822_cgu_params[clk_freq].feedback_div;
870 dw19.field.tspll_ndivratio = 1;
872 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD19, dw19.val);
876 /* Configure the TS PLL post divisor */
877 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD22, &dw22.val);
881 dw22.field.time1588clk_div = e822_cgu_params[clk_freq].post_pll_div;
882 dw22.field.time1588clk_sel_div2 = 0;
884 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD22, dw22.val);
888 /* Configure the TS PLL pre divisor and clock source */
889 err = ice_read_cgu_reg_e822(hw, NAC_CGU_DWORD24, &dw24.val);
893 dw24.field.ref1588_ck_div = e822_cgu_params[clk_freq].refclk_pre_div;
894 dw24.field.tspll_fbdiv_frac = e822_cgu_params[clk_freq].frac_n_div;
895 dw24.field.time_ref_sel = clk_src;
897 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD24, dw24.val);
901 /* Finally, enable the PLL */
902 dw24.field.ts_pll_enable = 1;
904 err = ice_write_cgu_reg_e822(hw, NAC_CGU_DWORD24, dw24.val);
908 /* Wait to verify if the PLL locks */
909 usleep_range(1000, 5000);
911 err = ice_read_cgu_reg_e822(hw, TSPLL_RO_BWM_LF, &bwm_lf.val);
915 if (!bwm_lf.field.plllock_true_lock_cri) {
916 dev_warn(ice_hw_to_dev(hw), "CGU PLL failed to lock\n");
920 /* Log the current clock configuration */
921 ice_debug(hw, ICE_DBG_PTP, "New CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
922 dw24.field.ts_pll_enable ? "enabled" : "disabled",
923 ice_clk_src_str(dw24.field.time_ref_sel),
924 ice_clk_freq_str(dw9.field.time_ref_freq_sel),
925 bwm_lf.field.plllock_true_lock_cri ? "locked" : "unlocked");
931 * ice_init_cgu_e822 - Initialize CGU with settings from firmware
932 * @hw: pointer to the HW structure
934 * Initialize the Clock Generation Unit of the E822 device.
936 static int ice_init_cgu_e822(struct ice_hw *hw)
938 struct ice_ts_func_info *ts_info = &hw->func_caps.ts_func_info;
939 union tspll_cntr_bist_settings cntr_bist;
942 err = ice_read_cgu_reg_e822(hw, TSPLL_CNTR_BIST_SETTINGS,
947 /* Disable sticky lock detection so lock err reported is accurate */
948 cntr_bist.field.i_plllock_sel_0 = 0;
949 cntr_bist.field.i_plllock_sel_1 = 0;
951 err = ice_write_cgu_reg_e822(hw, TSPLL_CNTR_BIST_SETTINGS,
956 /* Configure the CGU PLL using the parameters from the function
959 err = ice_cfg_cgu_pll_e822(hw, ts_info->time_ref,
960 (enum ice_clk_src)ts_info->clk_src);
968 * ice_ptp_set_vernier_wl - Set the window length for vernier calibration
969 * @hw: pointer to the HW struct
971 * Set the window length used for the vernier port calibration process.
973 static int ice_ptp_set_vernier_wl(struct ice_hw *hw)
977 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
980 err = ice_write_phy_reg_e822(hw, port, P_REG_WL,
983 ice_debug(hw, ICE_DBG_PTP, "Failed to set vernier window length for port %u, err %d\n",
993 * ice_ptp_init_phc_e822 - Perform E822 specific PHC initialization
994 * @hw: pointer to HW struct
996 * Perform PHC initialization steps specific to E822 devices.
998 static int ice_ptp_init_phc_e822(struct ice_hw *hw)
1003 /* Enable reading switch and PHY registers over the sideband queue */
1004 #define PF_SB_REM_DEV_CTL_SWITCH_READ BIT(1)
1005 #define PF_SB_REM_DEV_CTL_PHY0 BIT(2)
1006 regval = rd32(hw, PF_SB_REM_DEV_CTL);
1007 regval |= (PF_SB_REM_DEV_CTL_SWITCH_READ |
1008 PF_SB_REM_DEV_CTL_PHY0);
1009 wr32(hw, PF_SB_REM_DEV_CTL, regval);
1011 /* Initialize the Clock Generation Unit */
1012 err = ice_init_cgu_e822(hw);
1016 /* Set window length for all the ports */
1017 return ice_ptp_set_vernier_wl(hw);
1021 * ice_ptp_prep_phy_time_e822 - Prepare PHY port with initial time
1022 * @hw: pointer to the HW struct
1023 * @time: Time to initialize the PHY port clocks to
1025 * Program the PHY port registers with a new initial time value. The port
1026 * clock will be initialized once the driver issues an INIT_TIME sync
1027 * command. The time value is the upper 32 bits of the PHY timer, usually in
1028 * units of nominal nanoseconds.
1031 ice_ptp_prep_phy_time_e822(struct ice_hw *hw, u32 time)
1037 /* The time represents the upper 32 bits of the PHY timer, so we need
1038 * to shift to account for this when programming.
1040 phy_time = (u64)time << 32;
1042 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1044 err = ice_write_64b_phy_reg_e822(hw, port,
1045 P_REG_TX_TIMER_INC_PRE_L,
1051 err = ice_write_64b_phy_reg_e822(hw, port,
1052 P_REG_RX_TIMER_INC_PRE_L,
1061 ice_debug(hw, ICE_DBG_PTP, "Failed to write init time for port %u, err %d\n",
1068 * ice_ptp_prep_port_adj_e822 - Prepare a single port for time adjust
1069 * @hw: pointer to HW struct
1070 * @port: Port number to be programmed
1071 * @time: time in cycles to adjust the port Tx and Rx clocks
1073 * Program the port for an atomic adjustment by writing the Tx and Rx timer
1074 * registers. The atomic adjustment won't be completed until the driver issues
1075 * an ADJ_TIME command.
1077 * Note that time is not in units of nanoseconds. It is in clock time
1078 * including the lower sub-nanosecond portion of the port timer.
1080 * Negative adjustments are supported using 2s complement arithmetic.
1083 ice_ptp_prep_port_adj_e822(struct ice_hw *hw, u8 port, s64 time)
1088 l_time = lower_32_bits(time);
1089 u_time = upper_32_bits(time);
1092 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_TIMER_INC_PRE_L,
1097 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_TIMER_INC_PRE_U,
1103 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_TIMER_INC_PRE_L,
1108 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_TIMER_INC_PRE_U,
1116 ice_debug(hw, ICE_DBG_PTP, "Failed to write time adjust for port %u, err %d\n",
1122 * ice_ptp_prep_phy_adj_e822 - Prep PHY ports for a time adjustment
1123 * @hw: pointer to HW struct
1124 * @adj: adjustment in nanoseconds
1126 * Prepare the PHY ports for an atomic time adjustment by programming the PHY
1127 * Tx and Rx port registers. The actual adjustment is completed by issuing an
1128 * ADJ_TIME or ADJ_TIME_AT_TIME sync command.
1131 ice_ptp_prep_phy_adj_e822(struct ice_hw *hw, s32 adj)
1136 /* The port clock supports adjustment of the sub-nanosecond portion of
1137 * the clock. We shift the provided adjustment in nanoseconds to
1138 * calculate the appropriate adjustment to program into the PHY ports.
1141 cycles = (s64)adj << 32;
1143 cycles = -(((s64)-adj) << 32);
1145 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1148 err = ice_ptp_prep_port_adj_e822(hw, port, cycles);
1157 * ice_ptp_prep_phy_incval_e822 - Prepare PHY ports for time adjustment
1158 * @hw: pointer to HW struct
1159 * @incval: new increment value to prepare
1161 * Prepare each of the PHY ports for a new increment value by programming the
1162 * port's TIMETUS registers. The new increment value will be updated after
1163 * issuing an INIT_INCVAL command.
1166 ice_ptp_prep_phy_incval_e822(struct ice_hw *hw, u64 incval)
1171 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1172 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_TIMETUS_L,
1181 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval for port %u, err %d\n",
1188 * ice_ptp_read_port_capture - Read a port's local time capture
1189 * @hw: pointer to HW struct
1190 * @port: Port number to read
1191 * @tx_ts: on return, the Tx port time capture
1192 * @rx_ts: on return, the Rx port time capture
1194 * Read the port's Tx and Rx local time capture values.
1196 * Note this has no equivalent for the E810 devices.
1199 ice_ptp_read_port_capture(struct ice_hw *hw, u8 port, u64 *tx_ts, u64 *rx_ts)
1204 err = ice_read_64b_phy_reg_e822(hw, port, P_REG_TX_CAPTURE_L, tx_ts);
1206 ice_debug(hw, ICE_DBG_PTP, "Failed to read REG_TX_CAPTURE, err %d\n",
1211 ice_debug(hw, ICE_DBG_PTP, "tx_init = 0x%016llx\n",
1212 (unsigned long long)*tx_ts);
1215 err = ice_read_64b_phy_reg_e822(hw, port, P_REG_RX_CAPTURE_L, rx_ts);
1217 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_CAPTURE, err %d\n",
1222 ice_debug(hw, ICE_DBG_PTP, "rx_init = 0x%016llx\n",
1223 (unsigned long long)*rx_ts);
1229 * ice_ptp_one_port_cmd - Prepare a single PHY port for a timer command
1230 * @hw: pointer to HW struct
1231 * @port: Port to which cmd has to be sent
1232 * @cmd: Command to be sent to the port
1234 * Prepare the requested port for an upcoming timer sync command.
1236 * Note there is no equivalent of this operation on E810, as that device
1237 * always handles all external PHYs internally.
1240 ice_ptp_one_port_cmd(struct ice_hw *hw, u8 port, enum ice_ptp_tmr_cmd cmd)
1246 tmr_idx = ice_get_ptp_src_clock_index(hw);
1247 cmd_val = tmr_idx << SEL_PHY_SRC;
1250 cmd_val |= PHY_CMD_INIT_TIME;
1253 cmd_val |= PHY_CMD_INIT_INCVAL;
1256 cmd_val |= PHY_CMD_ADJ_TIME;
1259 cmd_val |= PHY_CMD_READ_TIME;
1261 case ADJ_TIME_AT_TIME:
1262 cmd_val |= PHY_CMD_ADJ_TIME_AT_TIME;
1267 /* Read, modify, write */
1268 err = ice_read_phy_reg_e822(hw, port, P_REG_TX_TMR_CMD, &val);
1270 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_TMR_CMD, err %d\n",
1275 /* Modify necessary bits only and perform write */
1276 val &= ~TS_CMD_MASK;
1279 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_TMR_CMD, val);
1281 ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_TMR_CMD, err %d\n",
1287 /* Read, modify, write */
1288 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_TMR_CMD, &val);
1290 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_TMR_CMD, err %d\n",
1295 /* Modify necessary bits only and perform write */
1296 val &= ~TS_CMD_MASK;
1299 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_TMR_CMD, val);
1301 ice_debug(hw, ICE_DBG_PTP, "Failed to write back RX_TMR_CMD, err %d\n",
1310 * ice_ptp_port_cmd_e822 - Prepare all ports for a timer command
1311 * @hw: pointer to the HW struct
1312 * @cmd: timer command to prepare
1314 * Prepare all ports connected to this device for an upcoming timer sync
1318 ice_ptp_port_cmd_e822(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
1322 for (port = 0; port < ICE_NUM_EXTERNAL_PORTS; port++) {
1325 err = ice_ptp_one_port_cmd(hw, port, cmd);
1333 /* E822 Vernier calibration functions
1335 * The following functions are used as part of the vernier calibration of
1336 * a port. This calibration increases the precision of the timestamps on the
1341 * ice_phy_get_speed_and_fec_e822 - Get link speed and FEC based on serdes mode
1342 * @hw: pointer to HW struct
1343 * @port: the port to read from
1344 * @link_out: if non-NULL, holds link speed on success
1345 * @fec_out: if non-NULL, holds FEC algorithm on success
1347 * Read the serdes data for the PHY port and extract the link speed and FEC
1351 ice_phy_get_speed_and_fec_e822(struct ice_hw *hw, u8 port,
1352 enum ice_ptp_link_spd *link_out,
1353 enum ice_ptp_fec_mode *fec_out)
1355 enum ice_ptp_link_spd link;
1356 enum ice_ptp_fec_mode fec;
1360 err = ice_read_phy_reg_e822(hw, port, P_REG_LINK_SPEED, &serdes);
1362 ice_debug(hw, ICE_DBG_PTP, "Failed to read serdes info\n");
1366 /* Determine the FEC algorithm */
1367 fec = (enum ice_ptp_fec_mode)P_REG_LINK_SPEED_FEC_MODE(serdes);
1369 serdes &= P_REG_LINK_SPEED_SERDES_M;
1371 /* Determine the link speed */
1372 if (fec == ICE_PTP_FEC_MODE_RS_FEC) {
1374 case ICE_PTP_SERDES_25G:
1375 link = ICE_PTP_LNK_SPD_25G_RS;
1377 case ICE_PTP_SERDES_50G:
1378 link = ICE_PTP_LNK_SPD_50G_RS;
1380 case ICE_PTP_SERDES_100G:
1381 link = ICE_PTP_LNK_SPD_100G_RS;
1388 case ICE_PTP_SERDES_1G:
1389 link = ICE_PTP_LNK_SPD_1G;
1391 case ICE_PTP_SERDES_10G:
1392 link = ICE_PTP_LNK_SPD_10G;
1394 case ICE_PTP_SERDES_25G:
1395 link = ICE_PTP_LNK_SPD_25G;
1397 case ICE_PTP_SERDES_40G:
1398 link = ICE_PTP_LNK_SPD_40G;
1400 case ICE_PTP_SERDES_50G:
1401 link = ICE_PTP_LNK_SPD_50G;
1417 * ice_phy_cfg_lane_e822 - Configure PHY quad for single/multi-lane timestamp
1418 * @hw: pointer to HW struct
1419 * @port: to configure the quad for
1421 static void ice_phy_cfg_lane_e822(struct ice_hw *hw, u8 port)
1423 enum ice_ptp_link_spd link_spd;
1428 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, NULL);
1430 ice_debug(hw, ICE_DBG_PTP, "Failed to get PHY link speed, err %d\n",
1435 quad = port / ICE_PORTS_PER_QUAD;
1437 err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, &val);
1439 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEM_GLB_CFG, err %d\n",
1444 if (link_spd >= ICE_PTP_LNK_SPD_40G)
1445 val &= ~Q_REG_TX_MEM_GBL_CFG_LANE_TYPE_M;
1447 val |= Q_REG_TX_MEM_GBL_CFG_LANE_TYPE_M;
1449 err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, val);
1451 ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_MEM_GBL_CFG, err %d\n",
1458 * ice_phy_cfg_uix_e822 - Configure Serdes UI to TU conversion for E822
1459 * @hw: pointer to the HW structure
1460 * @port: the port to configure
1462 * Program the conversion ration of Serdes clock "unit intervals" (UIs) to PHC
1463 * hardware clock time units (TUs). That is, determine the number of TUs per
1464 * serdes unit interval, and program the UIX registers with this conversion.
1466 * This conversion is used as part of the calibration process when determining
1467 * the additional error of a timestamp vs the real time of transmission or
1468 * receipt of the packet.
1470 * Hardware uses the number of TUs per 66 UIs, written to the UIX registers
1471 * for the two main serdes clock rates, 10G/40G and 25G/100G serdes clocks.
1473 * To calculate the conversion ratio, we use the following facts:
1475 * a) the clock frequency in Hz (cycles per second)
1476 * b) the number of TUs per cycle (the increment value of the clock)
1477 * c) 1 second per 1 billion nanoseconds
1478 * d) the duration of 66 UIs in nanoseconds
1480 * Given these facts, we can use the following table to work out what ratios
1481 * to multiply in order to get the number of TUs per 66 UIs:
1483 * cycles | 1 second | incval (TUs) | nanoseconds
1484 * -------+--------------+--------------+-------------
1485 * second | 1 billion ns | cycle | 66 UIs
1487 * To perform the multiplication using integers without too much loss of
1488 * precision, we can take use the following equation:
1490 * (freq * incval * 6600 LINE_UI ) / ( 100 * 1 billion)
1492 * We scale up to using 6600 UI instead of 66 in order to avoid fractional
1493 * nanosecond UIs (66 UI at 10G/40G is 6.4 ns)
1495 * The increment value has a maximum expected range of about 34 bits, while
1496 * the frequency value is about 29 bits. Multiplying these values shouldn't
1497 * overflow the 64 bits. However, we must then further multiply them again by
1498 * the Serdes unit interval duration. To avoid overflow here, we split the
1499 * overall divide by 1e11 into a divide by 256 (shift down by 8 bits) and
1500 * a divide by 390,625,000. This does lose some precision, but avoids
1501 * miscalculation due to arithmetic overflow.
1503 static int ice_phy_cfg_uix_e822(struct ice_hw *hw, u8 port)
1505 u64 cur_freq, clk_incval, tu_per_sec, uix;
1508 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1509 clk_incval = ice_ptp_read_src_incval(hw);
1511 /* Calculate TUs per second divided by 256 */
1512 tu_per_sec = (cur_freq * clk_incval) >> 8;
1514 #define LINE_UI_10G_40G 640 /* 6600 UIs is 640 nanoseconds at 10Gb/40Gb */
1515 #define LINE_UI_25G_100G 256 /* 6600 UIs is 256 nanoseconds at 25Gb/100Gb */
1517 /* Program the 10Gb/40Gb conversion ratio */
1518 uix = div_u64(tu_per_sec * LINE_UI_10G_40G, 390625000);
1520 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_UIX66_10G_40G_L,
1523 ice_debug(hw, ICE_DBG_PTP, "Failed to write UIX66_10G_40G, err %d\n",
1528 /* Program the 25Gb/100Gb conversion ratio */
1529 uix = div_u64(tu_per_sec * LINE_UI_25G_100G, 390625000);
1531 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_UIX66_25G_100G_L,
1534 ice_debug(hw, ICE_DBG_PTP, "Failed to write UIX66_25G_100G, err %d\n",
1543 * ice_phy_cfg_parpcs_e822 - Configure TUs per PAR/PCS clock cycle
1544 * @hw: pointer to the HW struct
1545 * @port: port to configure
1547 * Configure the number of TUs for the PAR and PCS clocks used as part of the
1548 * timestamp calibration process. This depends on the link speed, as the PHY
1549 * uses different markers depending on the speed.
1552 * - Tx/Rx PAR/PCS markers
1555 * - Tx/Rx Reed Solomon gearbox PAR/PCS markers
1558 * - Tx/Rx PAR/PCS markers
1559 * - Rx Deskew PAR/PCS markers
1561 * 50G RS and 100GB RS:
1562 * - Tx/Rx Reed Solomon gearbox PAR/PCS markers
1563 * - Rx Deskew PAR/PCS markers
1564 * - Tx PAR/PCS markers
1566 * To calculate the conversion, we use the PHC clock frequency (cycles per
1567 * second), the increment value (TUs per cycle), and the related PHY clock
1568 * frequency to calculate the TUs per unit of the PHY link clock. The
1569 * following table shows how the units convert:
1571 * cycles | TUs | second
1572 * -------+-------+--------
1573 * second | cycle | cycles
1575 * For each conversion register, look up the appropriate frequency from the
1576 * e822 PAR/PCS table and calculate the TUs per unit of that clock. Program
1577 * this to the appropriate register, preparing hardware to perform timestamp
1578 * calibration to calculate the total Tx or Rx offset to adjust the timestamp
1579 * in order to calibrate for the internal PHY delays.
1581 * Note that the increment value ranges up to ~34 bits, and the clock
1582 * frequency is ~29 bits, so multiplying them together should fit within the
1583 * 64 bit arithmetic.
1585 static int ice_phy_cfg_parpcs_e822(struct ice_hw *hw, u8 port)
1587 u64 cur_freq, clk_incval, tu_per_sec, phy_tus;
1588 enum ice_ptp_link_spd link_spd;
1589 enum ice_ptp_fec_mode fec_mode;
1592 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
1596 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1597 clk_incval = ice_ptp_read_src_incval(hw);
1599 /* Calculate TUs per cycle of the PHC clock */
1600 tu_per_sec = cur_freq * clk_incval;
1602 /* For each PHY conversion register, look up the appropriate link
1603 * speed frequency and determine the TUs per that clock's cycle time.
1604 * Split this into a high and low value and then program the
1605 * appropriate register. If that link speed does not use the
1606 * associated register, write zeros to clear it instead.
1609 /* P_REG_PAR_TX_TUS */
1610 if (e822_vernier[link_spd].tx_par_clk)
1611 phy_tus = div_u64(tu_per_sec,
1612 e822_vernier[link_spd].tx_par_clk);
1616 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PAR_TX_TUS_L,
1621 /* P_REG_PAR_RX_TUS */
1622 if (e822_vernier[link_spd].rx_par_clk)
1623 phy_tus = div_u64(tu_per_sec,
1624 e822_vernier[link_spd].rx_par_clk);
1628 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PAR_RX_TUS_L,
1633 /* P_REG_PCS_TX_TUS */
1634 if (e822_vernier[link_spd].tx_pcs_clk)
1635 phy_tus = div_u64(tu_per_sec,
1636 e822_vernier[link_spd].tx_pcs_clk);
1640 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PCS_TX_TUS_L,
1645 /* P_REG_PCS_RX_TUS */
1646 if (e822_vernier[link_spd].rx_pcs_clk)
1647 phy_tus = div_u64(tu_per_sec,
1648 e822_vernier[link_spd].rx_pcs_clk);
1652 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_PCS_RX_TUS_L,
1657 /* P_REG_DESK_PAR_TX_TUS */
1658 if (e822_vernier[link_spd].tx_desk_rsgb_par)
1659 phy_tus = div_u64(tu_per_sec,
1660 e822_vernier[link_spd].tx_desk_rsgb_par);
1664 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PAR_TX_TUS_L,
1669 /* P_REG_DESK_PAR_RX_TUS */
1670 if (e822_vernier[link_spd].rx_desk_rsgb_par)
1671 phy_tus = div_u64(tu_per_sec,
1672 e822_vernier[link_spd].rx_desk_rsgb_par);
1676 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PAR_RX_TUS_L,
1681 /* P_REG_DESK_PCS_TX_TUS */
1682 if (e822_vernier[link_spd].tx_desk_rsgb_pcs)
1683 phy_tus = div_u64(tu_per_sec,
1684 e822_vernier[link_spd].tx_desk_rsgb_pcs);
1688 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PCS_TX_TUS_L,
1693 /* P_REG_DESK_PCS_RX_TUS */
1694 if (e822_vernier[link_spd].rx_desk_rsgb_pcs)
1695 phy_tus = div_u64(tu_per_sec,
1696 e822_vernier[link_spd].rx_desk_rsgb_pcs);
1700 return ice_write_40b_phy_reg_e822(hw, port, P_REG_DESK_PCS_RX_TUS_L,
1705 * ice_calc_fixed_tx_offset_e822 - Calculated Fixed Tx offset for a port
1706 * @hw: pointer to the HW struct
1707 * @link_spd: the Link speed to calculate for
1709 * Calculate the fixed offset due to known static latency data.
1712 ice_calc_fixed_tx_offset_e822(struct ice_hw *hw, enum ice_ptp_link_spd link_spd)
1714 u64 cur_freq, clk_incval, tu_per_sec, fixed_offset;
1716 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1717 clk_incval = ice_ptp_read_src_incval(hw);
1719 /* Calculate TUs per second */
1720 tu_per_sec = cur_freq * clk_incval;
1722 /* Calculate number of TUs to add for the fixed Tx latency. Since the
1723 * latency measurement is in 1/100th of a nanosecond, we need to
1724 * multiply by tu_per_sec and then divide by 1e11. This calculation
1725 * overflows 64 bit integer arithmetic, so break it up into two
1726 * divisions by 1e4 first then by 1e7.
1728 fixed_offset = div_u64(tu_per_sec, 10000);
1729 fixed_offset *= e822_vernier[link_spd].tx_fixed_delay;
1730 fixed_offset = div_u64(fixed_offset, 10000000);
1732 return fixed_offset;
1736 * ice_phy_cfg_tx_offset_e822 - Configure total Tx timestamp offset
1737 * @hw: pointer to the HW struct
1738 * @port: the PHY port to configure
1740 * Program the P_REG_TOTAL_TX_OFFSET register with the total number of TUs to
1741 * adjust Tx timestamps by. This is calculated by combining some known static
1742 * latency along with the Vernier offset computations done by hardware.
1744 * This function will not return successfully until the Tx offset calculations
1745 * have been completed, which requires waiting until at least one packet has
1746 * been transmitted by the device. It is safe to call this function
1747 * periodically until calibration succeeds, as it will only program the offset
1750 * To avoid overflow, when calculating the offset based on the known static
1751 * latency values, we use measurements in 1/100th of a nanosecond, and divide
1752 * the TUs per second up front. This avoids overflow while allowing
1753 * calculation of the adjustment using integer arithmetic.
1755 * Returns zero on success, -EBUSY if the hardware vernier offset
1756 * calibration has not completed, or another error code on failure.
1758 int ice_phy_cfg_tx_offset_e822(struct ice_hw *hw, u8 port)
1760 enum ice_ptp_link_spd link_spd;
1761 enum ice_ptp_fec_mode fec_mode;
1762 u64 total_offset, val;
1766 /* Nothing to do if we've already programmed the offset */
1767 err = ice_read_phy_reg_e822(hw, port, P_REG_TX_OR, ®);
1769 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_OR for port %u, err %d\n",
1777 err = ice_read_phy_reg_e822(hw, port, P_REG_TX_OV_STATUS, ®);
1779 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_OV_STATUS for port %u, err %d\n",
1784 if (!(reg & P_REG_TX_OV_STATUS_OV_M))
1787 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
1791 total_offset = ice_calc_fixed_tx_offset_e822(hw, link_spd);
1793 /* Read the first Vernier offset from the PHY register and add it to
1796 if (link_spd == ICE_PTP_LNK_SPD_1G ||
1797 link_spd == ICE_PTP_LNK_SPD_10G ||
1798 link_spd == ICE_PTP_LNK_SPD_25G ||
1799 link_spd == ICE_PTP_LNK_SPD_25G_RS ||
1800 link_spd == ICE_PTP_LNK_SPD_40G ||
1801 link_spd == ICE_PTP_LNK_SPD_50G) {
1802 err = ice_read_64b_phy_reg_e822(hw, port,
1803 P_REG_PAR_PCS_TX_OFFSET_L,
1808 total_offset += val;
1811 /* For Tx, we only need to use the second Vernier offset for
1812 * multi-lane link speeds with RS-FEC. The lanes will always be
1815 if (link_spd == ICE_PTP_LNK_SPD_50G_RS ||
1816 link_spd == ICE_PTP_LNK_SPD_100G_RS) {
1817 err = ice_read_64b_phy_reg_e822(hw, port,
1818 P_REG_PAR_TX_TIME_L,
1823 total_offset += val;
1826 /* Now that the total offset has been calculated, program it to the
1827 * PHY and indicate that the Tx offset is ready. After this,
1828 * timestamps will be enabled.
1830 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_TOTAL_TX_OFFSET_L,
1835 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_OR, 1);
1839 dev_info(ice_hw_to_dev(hw), "Port=%d Tx vernier offset calibration complete\n",
1846 * ice_phy_calc_pmd_adj_e822 - Calculate PMD adjustment for Rx
1847 * @hw: pointer to the HW struct
1848 * @port: the PHY port to adjust for
1849 * @link_spd: the current link speed of the PHY
1850 * @fec_mode: the current FEC mode of the PHY
1851 * @pmd_adj: on return, the amount to adjust the Rx total offset by
1853 * Calculates the adjustment to Rx timestamps due to PMD alignment in the PHY.
1854 * This varies by link speed and FEC mode. The value calculated accounts for
1855 * various delays caused when receiving a packet.
1858 ice_phy_calc_pmd_adj_e822(struct ice_hw *hw, u8 port,
1859 enum ice_ptp_link_spd link_spd,
1860 enum ice_ptp_fec_mode fec_mode, u64 *pmd_adj)
1862 u64 cur_freq, clk_incval, tu_per_sec, mult, adj;
1867 err = ice_read_phy_reg_e822(hw, port, P_REG_PMD_ALIGNMENT, &val);
1869 ice_debug(hw, ICE_DBG_PTP, "Failed to read PMD alignment, err %d\n",
1874 pmd_align = (u8)val;
1876 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
1877 clk_incval = ice_ptp_read_src_incval(hw);
1879 /* Calculate TUs per second */
1880 tu_per_sec = cur_freq * clk_incval;
1882 /* The PMD alignment adjustment measurement depends on the link speed,
1883 * and whether FEC is enabled. For each link speed, the alignment
1884 * adjustment is calculated by dividing a value by the length of
1885 * a Time Unit in nanoseconds.
1887 * 1G: align == 4 ? 10 * 0.8 : (align + 6 % 10) * 0.8
1888 * 10G: align == 65 ? 0 : (align * 0.1 * 32/33)
1889 * 10G w/FEC: align * 0.1 * 32/33
1890 * 25G: align == 65 ? 0 : (align * 0.4 * 32/33)
1891 * 25G w/FEC: align * 0.4 * 32/33
1892 * 40G: align == 65 ? 0 : (align * 0.1 * 32/33)
1893 * 40G w/FEC: align * 0.1 * 32/33
1894 * 50G: align == 65 ? 0 : (align * 0.4 * 32/33)
1895 * 50G w/FEC: align * 0.8 * 32/33
1897 * For RS-FEC, if align is < 17 then we must also add 1.6 * 32/33.
1899 * To allow for calculating this value using integer arithmetic, we
1900 * instead start with the number of TUs per second, (inverse of the
1901 * length of a Time Unit in nanoseconds), multiply by a value based
1902 * on the PMD alignment register, and then divide by the right value
1903 * calculated based on the table above. To avoid integer overflow this
1904 * division is broken up into a step of dividing by 125 first.
1906 if (link_spd == ICE_PTP_LNK_SPD_1G) {
1910 mult = (pmd_align + 6) % 10;
1911 } else if (link_spd == ICE_PTP_LNK_SPD_10G ||
1912 link_spd == ICE_PTP_LNK_SPD_25G ||
1913 link_spd == ICE_PTP_LNK_SPD_40G ||
1914 link_spd == ICE_PTP_LNK_SPD_50G) {
1915 /* If Clause 74 FEC, always calculate PMD adjust */
1916 if (pmd_align != 65 || fec_mode == ICE_PTP_FEC_MODE_CLAUSE74)
1920 } else if (link_spd == ICE_PTP_LNK_SPD_25G_RS ||
1921 link_spd == ICE_PTP_LNK_SPD_50G_RS ||
1922 link_spd == ICE_PTP_LNK_SPD_100G_RS) {
1924 mult = pmd_align + 40;
1928 ice_debug(hw, ICE_DBG_PTP, "Unknown link speed %d, skipping PMD adjustment\n",
1933 /* In some cases, there's no need to adjust for the PMD alignment */
1939 /* Calculate the adjustment by multiplying TUs per second by the
1940 * appropriate multiplier and divisor. To avoid overflow, we first
1941 * divide by 125, and then handle remaining divisor based on the link
1942 * speed pmd_adj_divisor value.
1944 adj = div_u64(tu_per_sec, 125);
1946 adj = div_u64(adj, e822_vernier[link_spd].pmd_adj_divisor);
1948 /* Finally, for 25G-RS and 50G-RS, a further adjustment for the Rx
1949 * cycle count is necessary.
1951 if (link_spd == ICE_PTP_LNK_SPD_25G_RS) {
1955 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_40_TO_160_CNT,
1958 ice_debug(hw, ICE_DBG_PTP, "Failed to read 25G-RS Rx cycle count, err %d\n",
1963 rx_cycle = val & P_REG_RX_40_TO_160_CNT_RXCYC_M;
1965 mult = (4 - rx_cycle) * 40;
1967 cycle_adj = div_u64(tu_per_sec, 125);
1969 cycle_adj = div_u64(cycle_adj, e822_vernier[link_spd].pmd_adj_divisor);
1973 } else if (link_spd == ICE_PTP_LNK_SPD_50G_RS) {
1977 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_80_TO_160_CNT,
1980 ice_debug(hw, ICE_DBG_PTP, "Failed to read 50G-RS Rx cycle count, err %d\n",
1985 rx_cycle = val & P_REG_RX_80_TO_160_CNT_RXCYC_M;
1987 mult = rx_cycle * 40;
1989 cycle_adj = div_u64(tu_per_sec, 125);
1991 cycle_adj = div_u64(cycle_adj, e822_vernier[link_spd].pmd_adj_divisor);
1997 /* Return the calculated adjustment */
2004 * ice_calc_fixed_rx_offset_e822 - Calculated the fixed Rx offset for a port
2005 * @hw: pointer to HW struct
2006 * @link_spd: The Link speed to calculate for
2008 * Determine the fixed Rx latency for a given link speed.
2011 ice_calc_fixed_rx_offset_e822(struct ice_hw *hw, enum ice_ptp_link_spd link_spd)
2013 u64 cur_freq, clk_incval, tu_per_sec, fixed_offset;
2015 cur_freq = ice_e822_pll_freq(ice_e822_time_ref(hw));
2016 clk_incval = ice_ptp_read_src_incval(hw);
2018 /* Calculate TUs per second */
2019 tu_per_sec = cur_freq * clk_incval;
2021 /* Calculate number of TUs to add for the fixed Rx latency. Since the
2022 * latency measurement is in 1/100th of a nanosecond, we need to
2023 * multiply by tu_per_sec and then divide by 1e11. This calculation
2024 * overflows 64 bit integer arithmetic, so break it up into two
2025 * divisions by 1e4 first then by 1e7.
2027 fixed_offset = div_u64(tu_per_sec, 10000);
2028 fixed_offset *= e822_vernier[link_spd].rx_fixed_delay;
2029 fixed_offset = div_u64(fixed_offset, 10000000);
2031 return fixed_offset;
2035 * ice_phy_cfg_rx_offset_e822 - Configure total Rx timestamp offset
2036 * @hw: pointer to the HW struct
2037 * @port: the PHY port to configure
2039 * Program the P_REG_TOTAL_RX_OFFSET register with the number of Time Units to
2040 * adjust Rx timestamps by. This combines calculations from the Vernier offset
2041 * measurements taken in hardware with some data about known fixed delay as
2042 * well as adjusting for multi-lane alignment delay.
2044 * This function will not return successfully until the Rx offset calculations
2045 * have been completed, which requires waiting until at least one packet has
2046 * been received by the device. It is safe to call this function periodically
2047 * until calibration succeeds, as it will only program the offset once.
2049 * This function must be called only after the offset registers are valid,
2050 * i.e. after the Vernier calibration wait has passed, to ensure that the PHY
2051 * has measured the offset.
2053 * To avoid overflow, when calculating the offset based on the known static
2054 * latency values, we use measurements in 1/100th of a nanosecond, and divide
2055 * the TUs per second up front. This avoids overflow while allowing
2056 * calculation of the adjustment using integer arithmetic.
2058 * Returns zero on success, -EBUSY if the hardware vernier offset
2059 * calibration has not completed, or another error code on failure.
2061 int ice_phy_cfg_rx_offset_e822(struct ice_hw *hw, u8 port)
2063 enum ice_ptp_link_spd link_spd;
2064 enum ice_ptp_fec_mode fec_mode;
2065 u64 total_offset, pmd, val;
2069 /* Nothing to do if we've already programmed the offset */
2070 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_OR, ®);
2072 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_OR for port %u, err %d\n",
2080 err = ice_read_phy_reg_e822(hw, port, P_REG_RX_OV_STATUS, ®);
2082 ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_OV_STATUS for port %u, err %d\n",
2087 if (!(reg & P_REG_RX_OV_STATUS_OV_M))
2090 err = ice_phy_get_speed_and_fec_e822(hw, port, &link_spd, &fec_mode);
2094 total_offset = ice_calc_fixed_rx_offset_e822(hw, link_spd);
2096 /* Read the first Vernier offset from the PHY register and add it to
2099 err = ice_read_64b_phy_reg_e822(hw, port,
2100 P_REG_PAR_PCS_RX_OFFSET_L,
2105 total_offset += val;
2107 /* For Rx, all multi-lane link speeds include a second Vernier
2108 * calibration, because the lanes might not be aligned.
2110 if (link_spd == ICE_PTP_LNK_SPD_40G ||
2111 link_spd == ICE_PTP_LNK_SPD_50G ||
2112 link_spd == ICE_PTP_LNK_SPD_50G_RS ||
2113 link_spd == ICE_PTP_LNK_SPD_100G_RS) {
2114 err = ice_read_64b_phy_reg_e822(hw, port,
2115 P_REG_PAR_RX_TIME_L,
2120 total_offset += val;
2123 /* In addition, Rx must account for the PMD alignment */
2124 err = ice_phy_calc_pmd_adj_e822(hw, port, link_spd, fec_mode, &pmd);
2128 /* For RS-FEC, this adjustment adds delay, but for other modes, it
2131 if (fec_mode == ICE_PTP_FEC_MODE_RS_FEC)
2132 total_offset += pmd;
2134 total_offset -= pmd;
2136 /* Now that the total offset has been calculated, program it to the
2137 * PHY and indicate that the Rx offset is ready. After this,
2138 * timestamps will be enabled.
2140 err = ice_write_64b_phy_reg_e822(hw, port, P_REG_TOTAL_RX_OFFSET_L,
2145 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_OR, 1);
2149 dev_info(ice_hw_to_dev(hw), "Port=%d Rx vernier offset calibration complete\n",
2156 * ice_read_phy_and_phc_time_e822 - Simultaneously capture PHC and PHY time
2157 * @hw: pointer to the HW struct
2158 * @port: the PHY port to read
2159 * @phy_time: on return, the 64bit PHY timer value
2160 * @phc_time: on return, the lower 64bits of PHC time
2162 * Issue a READ_TIME timer command to simultaneously capture the PHY and PHC
2166 ice_read_phy_and_phc_time_e822(struct ice_hw *hw, u8 port, u64 *phy_time,
2169 u64 tx_time, rx_time;
2174 tmr_idx = ice_get_ptp_src_clock_index(hw);
2176 /* Prepare the PHC timer for a READ_TIME capture command */
2177 ice_ptp_src_cmd(hw, READ_TIME);
2179 /* Prepare the PHY timer for a READ_TIME capture command */
2180 err = ice_ptp_one_port_cmd(hw, port, READ_TIME);
2184 /* Issue the sync to start the READ_TIME capture */
2185 ice_ptp_exec_tmr_cmd(hw);
2187 /* Read the captured PHC time from the shadow time registers */
2188 zo = rd32(hw, GLTSYN_SHTIME_0(tmr_idx));
2189 lo = rd32(hw, GLTSYN_SHTIME_L(tmr_idx));
2190 *phc_time = (u64)lo << 32 | zo;
2192 /* Read the captured PHY time from the PHY shadow registers */
2193 err = ice_ptp_read_port_capture(hw, port, &tx_time, &rx_time);
2197 /* If the PHY Tx and Rx timers don't match, log a warning message.
2198 * Note that this should not happen in normal circumstances since the
2199 * driver always programs them together.
2201 if (tx_time != rx_time)
2202 dev_warn(ice_hw_to_dev(hw),
2203 "PHY port %u Tx and Rx timers do not match, tx_time 0x%016llX, rx_time 0x%016llX\n",
2204 port, (unsigned long long)tx_time,
2205 (unsigned long long)rx_time);
2207 *phy_time = tx_time;
2213 * ice_sync_phy_timer_e822 - Synchronize the PHY timer with PHC timer
2214 * @hw: pointer to the HW struct
2215 * @port: the PHY port to synchronize
2217 * Perform an adjustment to ensure that the PHY and PHC timers are in sync.
2218 * This is done by issuing a READ_TIME command which triggers a simultaneous
2219 * read of the PHY timer and PHC timer. Then we use the difference to
2220 * calculate an appropriate 2s complement addition to add to the PHY timer in
2221 * order to ensure it reads the same value as the primary PHC timer.
2223 static int ice_sync_phy_timer_e822(struct ice_hw *hw, u8 port)
2225 u64 phc_time, phy_time, difference;
2228 if (!ice_ptp_lock(hw)) {
2229 ice_debug(hw, ICE_DBG_PTP, "Failed to acquire PTP semaphore\n");
2233 err = ice_read_phy_and_phc_time_e822(hw, port, &phy_time, &phc_time);
2237 /* Calculate the amount required to add to the port time in order for
2238 * it to match the PHC time.
2240 * Note that the port adjustment is done using 2s complement
2241 * arithmetic. This is convenient since it means that we can simply
2242 * calculate the difference between the PHC time and the port time,
2243 * and it will be interpreted correctly.
2245 difference = phc_time - phy_time;
2247 err = ice_ptp_prep_port_adj_e822(hw, port, (s64)difference);
2251 err = ice_ptp_one_port_cmd(hw, port, ADJ_TIME);
2255 /* Issue the sync to activate the time adjustment */
2256 ice_ptp_exec_tmr_cmd(hw);
2258 /* Re-capture the timer values to flush the command registers and
2259 * verify that the time was properly adjusted.
2261 err = ice_read_phy_and_phc_time_e822(hw, port, &phy_time, &phc_time);
2265 dev_info(ice_hw_to_dev(hw),
2266 "Port %u PHY time synced to PHC: 0x%016llX, 0x%016llX\n",
2267 port, (unsigned long long)phy_time,
2268 (unsigned long long)phc_time);
2280 * ice_stop_phy_timer_e822 - Stop the PHY clock timer
2281 * @hw: pointer to the HW struct
2282 * @port: the PHY port to stop
2283 * @soft_reset: if true, hold the SOFT_RESET bit of P_REG_PS
2285 * Stop the clock of a PHY port. This must be done as part of the flow to
2286 * re-calibrate Tx and Rx timestamping offsets whenever the clock time is
2287 * initialized or when link speed changes.
2290 ice_stop_phy_timer_e822(struct ice_hw *hw, u8 port, bool soft_reset)
2295 err = ice_write_phy_reg_e822(hw, port, P_REG_TX_OR, 0);
2299 err = ice_write_phy_reg_e822(hw, port, P_REG_RX_OR, 0);
2303 err = ice_read_phy_reg_e822(hw, port, P_REG_PS, &val);
2307 val &= ~P_REG_PS_START_M;
2308 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2312 val &= ~P_REG_PS_ENA_CLK_M;
2313 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2318 val |= P_REG_PS_SFT_RESET_M;
2319 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2324 ice_debug(hw, ICE_DBG_PTP, "Disabled clock on PHY port %u\n", port);
2330 * ice_start_phy_timer_e822 - Start the PHY clock timer
2331 * @hw: pointer to the HW struct
2332 * @port: the PHY port to start
2334 * Start the clock of a PHY port. This must be done as part of the flow to
2335 * re-calibrate Tx and Rx timestamping offsets whenever the clock time is
2336 * initialized or when link speed changes.
2338 * Hardware will take Vernier measurements on Tx or Rx of packets.
2340 int ice_start_phy_timer_e822(struct ice_hw *hw, u8 port)
2347 tmr_idx = ice_get_ptp_src_clock_index(hw);
2349 err = ice_stop_phy_timer_e822(hw, port, false);
2353 ice_phy_cfg_lane_e822(hw, port);
2355 err = ice_phy_cfg_uix_e822(hw, port);
2359 err = ice_phy_cfg_parpcs_e822(hw, port);
2363 lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
2364 hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
2365 incval = (u64)hi << 32 | lo;
2367 err = ice_write_40b_phy_reg_e822(hw, port, P_REG_TIMETUS_L, incval);
2371 err = ice_ptp_one_port_cmd(hw, port, INIT_INCVAL);
2375 ice_ptp_exec_tmr_cmd(hw);
2377 err = ice_read_phy_reg_e822(hw, port, P_REG_PS, &val);
2381 val |= P_REG_PS_SFT_RESET_M;
2382 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2386 val |= P_REG_PS_START_M;
2387 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2391 val &= ~P_REG_PS_SFT_RESET_M;
2392 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2396 err = ice_ptp_one_port_cmd(hw, port, INIT_INCVAL);
2400 ice_ptp_exec_tmr_cmd(hw);
2402 val |= P_REG_PS_ENA_CLK_M;
2403 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2407 val |= P_REG_PS_LOAD_OFFSET_M;
2408 err = ice_write_phy_reg_e822(hw, port, P_REG_PS, val);
2412 ice_ptp_exec_tmr_cmd(hw);
2414 err = ice_sync_phy_timer_e822(hw, port);
2418 ice_debug(hw, ICE_DBG_PTP, "Enabled clock on PHY port %u\n", port);
2424 * ice_get_phy_tx_tstamp_ready_e822 - Read Tx memory status register
2425 * @hw: pointer to the HW struct
2426 * @quad: the timestamp quad to read from
2427 * @tstamp_ready: contents of the Tx memory status register
2429 * Read the Q_REG_TX_MEMORY_STATUS register indicating which timestamps in
2430 * the PHY are ready. A set bit means the corresponding timestamp is valid and
2431 * ready to be captured from the PHY timestamp block.
2434 ice_get_phy_tx_tstamp_ready_e822(struct ice_hw *hw, u8 quad, u64 *tstamp_ready)
2439 err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEMORY_STATUS_U, &hi);
2441 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEMORY_STATUS_U for quad %u, err %d\n",
2446 err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEMORY_STATUS_L, &lo);
2448 ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEMORY_STATUS_L for quad %u, err %d\n",
2453 *tstamp_ready = (u64)hi << 32 | (u64)lo;
2460 * The following functions operate on the E810 series devices which use
2461 * a separate external PHY.
2465 * ice_read_phy_reg_e810 - Read register from external PHY on E810
2466 * @hw: pointer to the HW struct
2467 * @addr: the address to read from
2468 * @val: On return, the value read from the PHY
2470 * Read a register from the external PHY on the E810 device.
2472 static int ice_read_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 *val)
2474 struct ice_sbq_msg_input msg = {0};
2477 msg.msg_addr_low = lower_16_bits(addr);
2478 msg.msg_addr_high = upper_16_bits(addr);
2479 msg.opcode = ice_sbq_msg_rd;
2480 msg.dest_dev = rmn_0;
2482 err = ice_sbq_rw_reg(hw, &msg);
2484 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
2495 * ice_write_phy_reg_e810 - Write register on external PHY on E810
2496 * @hw: pointer to the HW struct
2497 * @addr: the address to writem to
2498 * @val: the value to write to the PHY
2500 * Write a value to a register of the external PHY on the E810 device.
2502 static int ice_write_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 val)
2504 struct ice_sbq_msg_input msg = {0};
2507 msg.msg_addr_low = lower_16_bits(addr);
2508 msg.msg_addr_high = upper_16_bits(addr);
2509 msg.opcode = ice_sbq_msg_wr;
2510 msg.dest_dev = rmn_0;
2513 err = ice_sbq_rw_reg(hw, &msg);
2515 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
2524 * ice_read_phy_tstamp_ll_e810 - Read a PHY timestamp registers through the FW
2525 * @hw: pointer to the HW struct
2526 * @idx: the timestamp index to read
2527 * @hi: 8 bit timestamp high value
2528 * @lo: 32 bit timestamp low value
2530 * Read a 8bit timestamp high value and 32 bit timestamp low value out of the
2531 * timestamp block of the external PHY on the E810 device using the low latency
2535 ice_read_phy_tstamp_ll_e810(struct ice_hw *hw, u8 idx, u8 *hi, u32 *lo)
2540 /* Write TS index to read to the PF register so the FW can read it */
2541 val = FIELD_PREP(TS_LL_READ_TS_IDX, idx) | TS_LL_READ_TS;
2542 wr32(hw, PF_SB_ATQBAL, val);
2544 /* Read the register repeatedly until the FW provides us the TS */
2545 for (i = TS_LL_READ_RETRIES; i > 0; i--) {
2546 val = rd32(hw, PF_SB_ATQBAL);
2548 /* When the bit is cleared, the TS is ready in the register */
2549 if (!(FIELD_GET(TS_LL_READ_TS, val))) {
2550 /* High 8 bit value of the TS is on the bits 16:23 */
2551 *hi = FIELD_GET(TS_LL_READ_TS_HIGH, val);
2553 /* Read the low 32 bit value and set the TS valid bit */
2554 *lo = rd32(hw, PF_SB_ATQBAH) | TS_VALID;
2561 /* FW failed to provide the TS in time */
2562 ice_debug(hw, ICE_DBG_PTP, "Failed to read PTP timestamp using low latency read\n");
2567 * ice_read_phy_tstamp_sbq_e810 - Read a PHY timestamp registers through the sbq
2568 * @hw: pointer to the HW struct
2569 * @lport: the lport to read from
2570 * @idx: the timestamp index to read
2571 * @hi: 8 bit timestamp high value
2572 * @lo: 32 bit timestamp low value
2574 * Read a 8bit timestamp high value and 32 bit timestamp low value out of the
2575 * timestamp block of the external PHY on the E810 device using sideband queue.
2578 ice_read_phy_tstamp_sbq_e810(struct ice_hw *hw, u8 lport, u8 idx, u8 *hi,
2581 u32 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
2582 u32 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
2586 err = ice_read_phy_reg_e810(hw, lo_addr, &lo_val);
2588 ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
2593 err = ice_read_phy_reg_e810(hw, hi_addr, &hi_val);
2595 ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
2607 * ice_read_phy_tstamp_e810 - Read a PHY timestamp out of the external PHY
2608 * @hw: pointer to the HW struct
2609 * @lport: the lport to read from
2610 * @idx: the timestamp index to read
2611 * @tstamp: on return, the 40bit timestamp value
2613 * Read a 40bit timestamp value out of the timestamp block of the external PHY
2614 * on the E810 device.
2617 ice_read_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx, u64 *tstamp)
2623 if (hw->dev_caps.ts_dev_info.ts_ll_read)
2624 err = ice_read_phy_tstamp_ll_e810(hw, idx, &hi, &lo);
2626 err = ice_read_phy_tstamp_sbq_e810(hw, lport, idx, &hi, &lo);
2631 /* For E810 devices, the timestamp is reported with the lower 32 bits
2632 * in the low register, and the upper 8 bits in the high register.
2634 *tstamp = ((u64)hi) << TS_HIGH_S | ((u64)lo & TS_LOW_M);
2640 * ice_clear_phy_tstamp_e810 - Clear a timestamp from the external PHY
2641 * @hw: pointer to the HW struct
2642 * @lport: the lport to read from
2643 * @idx: the timestamp index to reset
2645 * Clear a timestamp, resetting its valid bit, from the timestamp block of the
2646 * external PHY on the E810 device.
2648 static int ice_clear_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx)
2650 u32 lo_addr, hi_addr;
2653 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
2654 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
2656 err = ice_write_phy_reg_e810(hw, lo_addr, 0);
2658 ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register, err %d\n",
2663 err = ice_write_phy_reg_e810(hw, hi_addr, 0);
2665 ice_debug(hw, ICE_DBG_PTP, "Failed to clear high PTP timestamp register, err %d\n",
2674 * ice_ptp_init_phy_e810 - Enable PTP function on the external PHY
2675 * @hw: pointer to HW struct
2677 * Enable the timesync PTP functionality for the external PHY connected to
2680 int ice_ptp_init_phy_e810(struct ice_hw *hw)
2685 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2686 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_ENA(tmr_idx),
2687 GLTSYN_ENA_TSYN_ENA_M);
2689 ice_debug(hw, ICE_DBG_PTP, "PTP failed in ena_phy_time_syn %d\n",
2696 * ice_ptp_init_phc_e810 - Perform E810 specific PHC initialization
2697 * @hw: pointer to HW struct
2699 * Perform E810-specific PTP hardware clock initialization steps.
2701 static int ice_ptp_init_phc_e810(struct ice_hw *hw)
2703 /* Ensure synchronization delay is zero */
2704 wr32(hw, GLTSYN_SYNC_DLAY, 0);
2706 /* Initialize the PHY */
2707 return ice_ptp_init_phy_e810(hw);
2711 * ice_ptp_prep_phy_time_e810 - Prepare PHY port with initial time
2712 * @hw: Board private structure
2713 * @time: Time to initialize the PHY port clock to
2715 * Program the PHY port ETH_GLTSYN_SHTIME registers in preparation setting the
2716 * initial clock time. The time will not actually be programmed until the
2717 * driver issues an INIT_TIME command.
2719 * The time value is the upper 32 bits of the PHY timer, usually in units of
2720 * nominal nanoseconds.
2722 static int ice_ptp_prep_phy_time_e810(struct ice_hw *hw, u32 time)
2727 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2728 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_0(tmr_idx), 0);
2730 ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_0, err %d\n",
2735 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_L(tmr_idx), time);
2737 ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_L, err %d\n",
2746 * ice_ptp_prep_phy_adj_e810 - Prep PHY port for a time adjustment
2747 * @hw: pointer to HW struct
2748 * @adj: adjustment value to program
2750 * Prepare the PHY port for an atomic adjustment by programming the PHY
2751 * ETH_GLTSYN_SHADJ_L and ETH_GLTSYN_SHADJ_H registers. The actual adjustment
2752 * is completed by issuing an ADJ_TIME sync command.
2754 * The adjustment value only contains the portion used for the upper 32bits of
2755 * the PHY timer, usually in units of nominal nanoseconds. Negative
2756 * adjustments are supported using 2s complement arithmetic.
2758 static int ice_ptp_prep_phy_adj_e810(struct ice_hw *hw, s32 adj)
2763 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2765 /* Adjustments are represented as signed 2's complement values in
2766 * nanoseconds. Sub-nanosecond adjustment is not supported.
2768 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), 0);
2770 ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_L, err %d\n",
2775 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), adj);
2777 ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_H, err %d\n",
2786 * ice_ptp_prep_phy_incval_e810 - Prep PHY port increment value change
2787 * @hw: pointer to HW struct
2788 * @incval: The new 40bit increment value to prepare
2790 * Prepare the PHY port for a new increment value by programming the PHY
2791 * ETH_GLTSYN_SHADJ_L and ETH_GLTSYN_SHADJ_H registers. The actual change is
2792 * completed by issuing an INIT_INCVAL command.
2794 static int ice_ptp_prep_phy_incval_e810(struct ice_hw *hw, u64 incval)
2800 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2801 low = lower_32_bits(incval);
2802 high = upper_32_bits(incval);
2804 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), low);
2806 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval to PHY SHADJ_L, err %d\n",
2811 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), high);
2813 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval PHY SHADJ_H, err %d\n",
2822 * ice_ptp_port_cmd_e810 - Prepare all external PHYs for a timer command
2823 * @hw: pointer to HW struct
2824 * @cmd: Command to be sent to the port
2826 * Prepare the external PHYs connected to this device for a timer sync
2829 static int ice_ptp_port_cmd_e810(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
2836 cmd_val = GLTSYN_CMD_INIT_TIME;
2839 cmd_val = GLTSYN_CMD_INIT_INCVAL;
2842 cmd_val = GLTSYN_CMD_ADJ_TIME;
2845 cmd_val = GLTSYN_CMD_READ_TIME;
2847 case ADJ_TIME_AT_TIME:
2848 cmd_val = GLTSYN_CMD_ADJ_INIT_TIME;
2852 /* Read, modify, write */
2853 err = ice_read_phy_reg_e810(hw, ETH_GLTSYN_CMD, &val);
2855 ice_debug(hw, ICE_DBG_PTP, "Failed to read GLTSYN_CMD, err %d\n", err);
2859 /* Modify necessary bits only and perform write */
2860 val &= ~TS_CMD_MASK_E810;
2863 err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_CMD, val);
2865 ice_debug(hw, ICE_DBG_PTP, "Failed to write back GLTSYN_CMD, err %d\n", err);
2872 /* Device agnostic functions
2874 * The following functions implement shared behavior common to both E822 and
2875 * E810 devices, possibly calling a device specific implementation where
2880 * ice_ptp_lock - Acquire PTP global semaphore register lock
2881 * @hw: pointer to the HW struct
2883 * Acquire the global PTP hardware semaphore lock. Returns true if the lock
2884 * was acquired, false otherwise.
2886 * The PFTSYN_SEM register sets the busy bit on read, returning the previous
2887 * value. If software sees the busy bit cleared, this means that this function
2888 * acquired the lock (and the busy bit is now set). If software sees the busy
2889 * bit set, it means that another function acquired the lock.
2891 * Software must clear the busy bit with a write to release the lock for other
2892 * functions when done.
2894 bool ice_ptp_lock(struct ice_hw *hw)
2899 #define MAX_TRIES 15
2901 for (i = 0; i < MAX_TRIES; i++) {
2902 hw_lock = rd32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
2903 hw_lock = hw_lock & PFTSYN_SEM_BUSY_M;
2905 /* Somebody is holding the lock */
2906 usleep_range(5000, 6000);
2917 * ice_ptp_unlock - Release PTP global semaphore register lock
2918 * @hw: pointer to the HW struct
2920 * Release the global PTP hardware semaphore lock. This is done by writing to
2921 * the PFTSYN_SEM register.
2923 void ice_ptp_unlock(struct ice_hw *hw)
2925 wr32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), 0);
2929 * ice_ptp_tmr_cmd - Prepare and trigger a timer sync command
2930 * @hw: pointer to HW struct
2931 * @cmd: the command to issue
2933 * Prepare the source timer and PHY timers and then trigger the requested
2934 * command. This causes the shadow registers previously written in preparation
2935 * for the command to be synchronously applied to both the source and PHY
2938 static int ice_ptp_tmr_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
2942 /* First, prepare the source timer */
2943 ice_ptp_src_cmd(hw, cmd);
2945 /* Next, prepare the ports */
2946 if (ice_is_e810(hw))
2947 err = ice_ptp_port_cmd_e810(hw, cmd);
2949 err = ice_ptp_port_cmd_e822(hw, cmd);
2951 ice_debug(hw, ICE_DBG_PTP, "Failed to prepare PHY ports for timer command %u, err %d\n",
2956 /* Write the sync command register to drive both source and PHY timer
2957 * commands synchronously
2959 ice_ptp_exec_tmr_cmd(hw);
2965 * ice_ptp_init_time - Initialize device time to provided value
2966 * @hw: pointer to HW struct
2967 * @time: 64bits of time (GLTSYN_TIME_L and GLTSYN_TIME_H)
2969 * Initialize the device to the specified time provided. This requires a three
2972 * 1) write the new init time to the source timer shadow registers
2973 * 2) write the new init time to the PHY timer shadow registers
2974 * 3) issue an init_time timer command to synchronously switch both the source
2975 * and port timers to the new init time value at the next clock cycle.
2977 int ice_ptp_init_time(struct ice_hw *hw, u64 time)
2982 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
2985 wr32(hw, GLTSYN_SHTIME_L(tmr_idx), lower_32_bits(time));
2986 wr32(hw, GLTSYN_SHTIME_H(tmr_idx), upper_32_bits(time));
2987 wr32(hw, GLTSYN_SHTIME_0(tmr_idx), 0);
2990 /* Fill Rx and Tx ports and send msg to PHY */
2991 if (ice_is_e810(hw))
2992 err = ice_ptp_prep_phy_time_e810(hw, time & 0xFFFFFFFF);
2994 err = ice_ptp_prep_phy_time_e822(hw, time & 0xFFFFFFFF);
2998 return ice_ptp_tmr_cmd(hw, INIT_TIME);
3002 * ice_ptp_write_incval - Program PHC with new increment value
3003 * @hw: pointer to HW struct
3004 * @incval: Source timer increment value per clock cycle
3006 * Program the PHC with a new increment value. This requires a three-step
3009 * 1) Write the increment value to the source timer shadow registers
3010 * 2) Write the increment value to the PHY timer shadow registers
3011 * 3) Issue an INIT_INCVAL timer command to synchronously switch both the
3012 * source and port timers to the new increment value at the next clock
3015 int ice_ptp_write_incval(struct ice_hw *hw, u64 incval)
3020 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3023 wr32(hw, GLTSYN_SHADJ_L(tmr_idx), lower_32_bits(incval));
3024 wr32(hw, GLTSYN_SHADJ_H(tmr_idx), upper_32_bits(incval));
3026 if (ice_is_e810(hw))
3027 err = ice_ptp_prep_phy_incval_e810(hw, incval);
3029 err = ice_ptp_prep_phy_incval_e822(hw, incval);
3033 return ice_ptp_tmr_cmd(hw, INIT_INCVAL);
3037 * ice_ptp_write_incval_locked - Program new incval while holding semaphore
3038 * @hw: pointer to HW struct
3039 * @incval: Source timer increment value per clock cycle
3041 * Program a new PHC incval while holding the PTP semaphore.
3043 int ice_ptp_write_incval_locked(struct ice_hw *hw, u64 incval)
3047 if (!ice_ptp_lock(hw))
3050 err = ice_ptp_write_incval(hw, incval);
3058 * ice_ptp_adj_clock - Adjust PHC clock time atomically
3059 * @hw: pointer to HW struct
3060 * @adj: Adjustment in nanoseconds
3062 * Perform an atomic adjustment of the PHC time by the specified number of
3063 * nanoseconds. This requires a three-step process:
3065 * 1) Write the adjustment to the source timer shadow registers
3066 * 2) Write the adjustment to the PHY timer shadow registers
3067 * 3) Issue an ADJ_TIME timer command to synchronously apply the adjustment to
3068 * both the source and port timers at the next clock cycle.
3070 int ice_ptp_adj_clock(struct ice_hw *hw, s32 adj)
3075 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3077 /* Write the desired clock adjustment into the GLTSYN_SHADJ register.
3078 * For an ADJ_TIME command, this set of registers represents the value
3079 * to add to the clock time. It supports subtraction by interpreting
3080 * the value as a 2's complement integer.
3082 wr32(hw, GLTSYN_SHADJ_L(tmr_idx), 0);
3083 wr32(hw, GLTSYN_SHADJ_H(tmr_idx), adj);
3085 if (ice_is_e810(hw))
3086 err = ice_ptp_prep_phy_adj_e810(hw, adj);
3088 err = ice_ptp_prep_phy_adj_e822(hw, adj);
3092 return ice_ptp_tmr_cmd(hw, ADJ_TIME);
3096 * ice_read_phy_tstamp - Read a PHY timestamp from the timestamo block
3097 * @hw: pointer to the HW struct
3098 * @block: the block to read from
3099 * @idx: the timestamp index to read
3100 * @tstamp: on return, the 40bit timestamp value
3102 * Read a 40bit timestamp value out of the timestamp block. For E822 devices,
3103 * the block is the quad to read from. For E810 devices, the block is the
3104 * logical port to read from.
3106 int ice_read_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx, u64 *tstamp)
3108 if (ice_is_e810(hw))
3109 return ice_read_phy_tstamp_e810(hw, block, idx, tstamp);
3111 return ice_read_phy_tstamp_e822(hw, block, idx, tstamp);
3115 * ice_clear_phy_tstamp - Clear a timestamp from the timestamp block
3116 * @hw: pointer to the HW struct
3117 * @block: the block to read from
3118 * @idx: the timestamp index to reset
3120 * Clear a timestamp, resetting its valid bit, from the timestamp block. For
3121 * E822 devices, the block is the quad to clear from. For E810 devices, the
3122 * block is the logical port to clear from.
3124 int ice_clear_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx)
3126 if (ice_is_e810(hw))
3127 return ice_clear_phy_tstamp_e810(hw, block, idx);
3129 return ice_clear_phy_tstamp_e822(hw, block, idx);
3133 * ice_get_phy_tx_tstamp_ready_e810 - Read Tx memory status register
3134 * @hw: pointer to the HW struct
3135 * @port: the PHY port to read
3136 * @tstamp_ready: contents of the Tx memory status register
3138 * E810 devices do not use a Tx memory status register. Instead simply
3139 * indicate that all timestamps are currently ready.
3142 ice_get_phy_tx_tstamp_ready_e810(struct ice_hw *hw, u8 port, u64 *tstamp_ready)
3144 *tstamp_ready = 0xFFFFFFFFFFFFFFFF;
3148 /* E810T SMA functions
3150 * The following functions operate specifically on E810T hardware and are used
3151 * to access the extended GPIOs available.
3155 * ice_get_pca9575_handle
3156 * @hw: pointer to the hw struct
3157 * @pca9575_handle: GPIO controller's handle
3159 * Find and return the GPIO controller's handle in the netlist.
3160 * When found - the value will be cached in the hw structure and following calls
3161 * will return cached value
3164 ice_get_pca9575_handle(struct ice_hw *hw, u16 *pca9575_handle)
3166 struct ice_aqc_get_link_topo *cmd;
3167 struct ice_aq_desc desc;
3171 /* If handle was read previously return cached value */
3172 if (hw->io_expander_handle) {
3173 *pca9575_handle = hw->io_expander_handle;
3177 /* If handle was not detected read it from the netlist */
3178 cmd = &desc.params.get_link_topo;
3179 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_topo);
3181 /* Set node type to GPIO controller */
3182 cmd->addr.topo_params.node_type_ctx =
3183 (ICE_AQC_LINK_TOPO_NODE_TYPE_M &
3184 ICE_AQC_LINK_TOPO_NODE_TYPE_GPIO_CTRL);
3186 #define SW_PCA9575_SFP_TOPO_IDX 2
3187 #define SW_PCA9575_QSFP_TOPO_IDX 1
3189 /* Check if the SW IO expander controlling SMA exists in the netlist. */
3190 if (hw->device_id == ICE_DEV_ID_E810C_SFP)
3191 idx = SW_PCA9575_SFP_TOPO_IDX;
3192 else if (hw->device_id == ICE_DEV_ID_E810C_QSFP)
3193 idx = SW_PCA9575_QSFP_TOPO_IDX;
3197 cmd->addr.topo_params.index = idx;
3199 status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
3203 /* Verify if we found the right IO expander type */
3204 if (desc.params.get_link_topo.node_part_num !=
3205 ICE_AQC_GET_LINK_TOPO_NODE_NR_PCA9575)
3208 /* If present save the handle and return it */
3209 hw->io_expander_handle =
3210 le16_to_cpu(desc.params.get_link_topo.addr.handle);
3211 *pca9575_handle = hw->io_expander_handle;
3217 * ice_read_sma_ctrl_e810t
3218 * @hw: pointer to the hw struct
3219 * @data: pointer to data to be read from the GPIO controller
3221 * Read the SMA controller state. It is connected to pins 3-7 of Port 1 of the
3222 * PCA9575 expander, so only bits 3-7 in data are valid.
3224 int ice_read_sma_ctrl_e810t(struct ice_hw *hw, u8 *data)
3230 status = ice_get_pca9575_handle(hw, &handle);
3236 for (i = ICE_SMA_MIN_BIT_E810T; i <= ICE_SMA_MAX_BIT_E810T; i++) {
3239 status = ice_aq_get_gpio(hw, handle, i + ICE_PCA9575_P1_OFFSET,
3243 *data |= (u8)(!pin) << i;
3250 * ice_write_sma_ctrl_e810t
3251 * @hw: pointer to the hw struct
3252 * @data: data to be written to the GPIO controller
3254 * Write the data to the SMA controller. It is connected to pins 3-7 of Port 1
3255 * of the PCA9575 expander, so only bits 3-7 in data are valid.
3257 int ice_write_sma_ctrl_e810t(struct ice_hw *hw, u8 data)
3263 status = ice_get_pca9575_handle(hw, &handle);
3267 for (i = ICE_SMA_MIN_BIT_E810T; i <= ICE_SMA_MAX_BIT_E810T; i++) {
3270 pin = !(data & (1 << i));
3271 status = ice_aq_set_gpio(hw, handle, i + ICE_PCA9575_P1_OFFSET,
3281 * ice_read_pca9575_reg_e810t
3282 * @hw: pointer to the hw struct
3283 * @offset: GPIO controller register offset
3284 * @data: pointer to data to be read from the GPIO controller
3286 * Read the register from the GPIO controller
3288 int ice_read_pca9575_reg_e810t(struct ice_hw *hw, u8 offset, u8 *data)
3290 struct ice_aqc_link_topo_addr link_topo;
3295 memset(&link_topo, 0, sizeof(link_topo));
3297 err = ice_get_pca9575_handle(hw, &handle);
3301 link_topo.handle = cpu_to_le16(handle);
3302 link_topo.topo_params.node_type_ctx =
3303 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
3304 ICE_AQC_LINK_TOPO_NODE_CTX_PROVIDED);
3306 addr = cpu_to_le16((u16)offset);
3308 return ice_aq_read_i2c(hw, link_topo, 0, addr, 1, data, NULL);
3312 * ice_is_pca9575_present
3313 * @hw: pointer to the hw struct
3315 * Check if the SW IO expander is present in the netlist
3317 bool ice_is_pca9575_present(struct ice_hw *hw)
3322 if (!ice_is_e810t(hw))
3325 status = ice_get_pca9575_handle(hw, &handle);
3327 return !status && handle;
3331 * ice_ptp_reset_ts_memory - Reset timestamp memory for all blocks
3332 * @hw: pointer to the HW struct
3334 void ice_ptp_reset_ts_memory(struct ice_hw *hw)
3336 if (ice_is_e810(hw))
3339 ice_ptp_reset_ts_memory_e822(hw);
3343 * ice_ptp_init_phc - Initialize PTP hardware clock
3344 * @hw: pointer to the HW struct
3346 * Perform the steps required to initialize the PTP hardware clock.
3348 int ice_ptp_init_phc(struct ice_hw *hw)
3350 u8 src_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3352 /* Enable source clocks */
3353 wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M);
3355 /* Clear event err indications for auxiliary pins */
3356 (void)rd32(hw, GLTSYN_STAT(src_idx));
3358 if (ice_is_e810(hw))
3359 return ice_ptp_init_phc_e810(hw);
3361 return ice_ptp_init_phc_e822(hw);
3365 * ice_get_phy_tx_tstamp_ready - Read PHY Tx memory status indication
3366 * @hw: pointer to the HW struct
3367 * @block: the timestamp block to check
3368 * @tstamp_ready: storage for the PHY Tx memory status information
3370 * Check the PHY for Tx timestamp memory status. This reports a 64 bit value
3371 * which indicates which timestamps in the block may be captured. A set bit
3372 * means the timestamp can be read. An unset bit means the timestamp is not
3373 * ready and software should avoid reading the register.
3375 int ice_get_phy_tx_tstamp_ready(struct ice_hw *hw, u8 block, u64 *tstamp_ready)
3377 if (ice_is_e810(hw))
3378 return ice_get_phy_tx_tstamp_ready_e810(hw, block,
3381 return ice_get_phy_tx_tstamp_ready_e822(hw, block,