1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
4 #include "ice_common.h"
5 #include "ice_ptp_hw.h"
7 /* Low level functions for interacting with and managing the device clock used
8 * for the Precision Time Protocol.
10 * The ice hardware represents the current time using three registers:
12 * GLTSYN_TIME_H GLTSYN_TIME_L GLTSYN_TIME_R
13 * +---------------+ +---------------+ +---------------+
14 * | 32 bits | | 32 bits | | 32 bits |
15 * +---------------+ +---------------+ +---------------+
17 * The registers are incremented every clock tick using a 40bit increment
18 * value defined over two registers:
20 * GLTSYN_INCVAL_H GLTSYN_INCVAL_L
21 * +---------------+ +---------------+
22 * | 8 bit s | | 32 bits |
23 * +---------------+ +---------------+
25 * The increment value is added to the GLSTYN_TIME_R and GLSTYN_TIME_L
26 * registers every clock source tick. Depending on the specific device
27 * configuration, the clock source frequency could be one of a number of
30 * For E810 devices, the increment frequency is 812.5 MHz
32 * The hardware captures timestamps in the PHY for incoming packets, and for
33 * outgoing packets on request. To support this, the PHY maintains a timer
34 * that matches the lower 64 bits of the global source timer.
36 * In order to ensure that the PHY timers and the source timer are equivalent,
37 * shadow registers are used to prepare the desired initial values. A special
38 * sync command is issued to trigger copying from the shadow registers into
39 * the appropriate source and PHY registers simultaneously.
43 * ice_get_ptp_src_clock_index - determine source clock index
44 * @hw: pointer to HW struct
46 * Determine the source clock index currently in use, based on device
47 * capabilities reported during initialization.
49 u8 ice_get_ptp_src_clock_index(struct ice_hw *hw)
51 return hw->func_caps.ts_func_info.tmr_index_assoc;
56 * The following functions operate on the E810 series devices which use
57 * a separate external PHY.
61 * ice_read_phy_reg_e810 - Read register from external PHY on E810
62 * @hw: pointer to the HW struct
63 * @addr: the address to read from
64 * @val: On return, the value read from the PHY
66 * Read a register from the external PHY on the E810 device.
68 static int ice_read_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 *val)
70 struct ice_sbq_msg_input msg = {0};
73 msg.msg_addr_low = lower_16_bits(addr);
74 msg.msg_addr_high = upper_16_bits(addr);
75 msg.opcode = ice_sbq_msg_rd;
78 status = ice_sbq_rw_reg(hw, &msg);
80 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, status %d\n",
91 * ice_write_phy_reg_e810 - Write register on external PHY on E810
92 * @hw: pointer to the HW struct
93 * @addr: the address to writem to
94 * @val: the value to write to the PHY
96 * Write a value to a register of the external PHY on the E810 device.
98 static int ice_write_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 val)
100 struct ice_sbq_msg_input msg = {0};
103 msg.msg_addr_low = lower_16_bits(addr);
104 msg.msg_addr_high = upper_16_bits(addr);
105 msg.opcode = ice_sbq_msg_wr;
106 msg.dest_dev = rmn_0;
109 status = ice_sbq_rw_reg(hw, &msg);
111 ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, status %d\n",
120 * ice_read_phy_tstamp_e810 - Read a PHY timestamp out of the external PHY
121 * @hw: pointer to the HW struct
122 * @lport: the lport to read from
123 * @idx: the timestamp index to read
124 * @tstamp: on return, the 40bit timestamp value
126 * Read a 40bit timestamp value out of the timestamp block of the external PHY
127 * on the E810 device.
130 ice_read_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx, u64 *tstamp)
132 u32 lo_addr, hi_addr, lo, hi;
135 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
136 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
138 status = ice_read_phy_reg_e810(hw, lo_addr, &lo);
140 ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, status %d\n",
145 status = ice_read_phy_reg_e810(hw, hi_addr, &hi);
147 ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, status %d\n",
152 /* For E810 devices, the timestamp is reported with the lower 32 bits
153 * in the low register, and the upper 8 bits in the high register.
155 *tstamp = ((u64)hi) << TS_HIGH_S | ((u64)lo & TS_LOW_M);
161 * ice_clear_phy_tstamp_e810 - Clear a timestamp from the external PHY
162 * @hw: pointer to the HW struct
163 * @lport: the lport to read from
164 * @idx: the timestamp index to reset
166 * Clear a timestamp, resetting its valid bit, from the timestamp block of the
167 * external PHY on the E810 device.
169 static int ice_clear_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx)
171 u32 lo_addr, hi_addr;
174 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
175 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
177 status = ice_write_phy_reg_e810(hw, lo_addr, 0);
179 ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register, status %d\n",
184 status = ice_write_phy_reg_e810(hw, hi_addr, 0);
186 ice_debug(hw, ICE_DBG_PTP, "Failed to clear high PTP timestamp register, status %d\n",
195 * ice_ptp_init_phy_e810 - Enable PTP function on the external PHY
196 * @hw: pointer to HW struct
198 * Enable the timesync PTP functionality for the external PHY connected to
201 int ice_ptp_init_phy_e810(struct ice_hw *hw)
206 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
207 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_ENA(tmr_idx),
208 GLTSYN_ENA_TSYN_ENA_M);
210 ice_debug(hw, ICE_DBG_PTP, "PTP failed in ena_phy_time_syn %d\n",
217 * ice_ptp_prep_phy_time_e810 - Prepare PHY port with initial time
218 * @hw: Board private structure
219 * @time: Time to initialize the PHY port clock to
221 * Program the PHY port ETH_GLTSYN_SHTIME registers in preparation setting the
222 * initial clock time. The time will not actually be programmed until the
223 * driver issues an INIT_TIME command.
225 * The time value is the upper 32 bits of the PHY timer, usually in units of
226 * nominal nanoseconds.
228 static int ice_ptp_prep_phy_time_e810(struct ice_hw *hw, u32 time)
233 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
234 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_0(tmr_idx), 0);
236 ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_0, status %d\n",
241 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_L(tmr_idx), time);
243 ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_L, status %d\n",
252 * ice_ptp_prep_phy_adj_e810 - Prep PHY port for a time adjustment
253 * @hw: pointer to HW struct
254 * @adj: adjustment value to program
256 * Prepare the PHY port for an atomic adjustment by programming the PHY
257 * ETH_GLTSYN_SHADJ_L and ETH_GLTSYN_SHADJ_H registers. The actual adjustment
258 * is completed by issuing an ADJ_TIME sync command.
260 * The adjustment value only contains the portion used for the upper 32bits of
261 * the PHY timer, usually in units of nominal nanoseconds. Negative
262 * adjustments are supported using 2s complement arithmetic.
264 static int ice_ptp_prep_phy_adj_e810(struct ice_hw *hw, s32 adj)
269 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
271 /* Adjustments are represented as signed 2's complement values in
272 * nanoseconds. Sub-nanosecond adjustment is not supported.
274 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), 0);
276 ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_L, status %d\n",
281 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), adj);
283 ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_H, status %d\n",
292 * ice_ptp_prep_phy_incval_e810 - Prep PHY port increment value change
293 * @hw: pointer to HW struct
294 * @incval: The new 40bit increment value to prepare
296 * Prepare the PHY port for a new increment value by programming the PHY
297 * ETH_GLTSYN_SHADJ_L and ETH_GLTSYN_SHADJ_H registers. The actual change is
298 * completed by issuing an INIT_INCVAL command.
300 static int ice_ptp_prep_phy_incval_e810(struct ice_hw *hw, u64 incval)
306 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
307 low = lower_32_bits(incval);
308 high = upper_32_bits(incval);
310 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), low);
312 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval to PHY SHADJ_L, status %d\n",
317 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), high);
319 ice_debug(hw, ICE_DBG_PTP, "Failed to write incval PHY SHADJ_H, status %d\n",
328 * ice_ptp_port_cmd_e810 - Prepare all external PHYs for a timer command
329 * @hw: pointer to HW struct
330 * @cmd: Command to be sent to the port
332 * Prepare the external PHYs connected to this device for a timer sync
335 static int ice_ptp_port_cmd_e810(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
342 cmd_val = GLTSYN_CMD_INIT_TIME;
345 cmd_val = GLTSYN_CMD_INIT_INCVAL;
348 cmd_val = GLTSYN_CMD_ADJ_TIME;
351 cmd_val = GLTSYN_CMD_READ_TIME;
353 case ADJ_TIME_AT_TIME:
354 cmd_val = GLTSYN_CMD_ADJ_INIT_TIME;
358 /* Read, modify, write */
359 status = ice_read_phy_reg_e810(hw, ETH_GLTSYN_CMD, &val);
361 ice_debug(hw, ICE_DBG_PTP, "Failed to read GLTSYN_CMD, status %d\n", status);
365 /* Modify necessary bits only and perform write */
366 val &= ~TS_CMD_MASK_E810;
369 status = ice_write_phy_reg_e810(hw, ETH_GLTSYN_CMD, val);
371 ice_debug(hw, ICE_DBG_PTP, "Failed to write back GLTSYN_CMD, status %d\n", status);
378 /* Device agnostic functions
380 * The following functions implement useful behavior to hide the differences
381 * between E810 and other devices. They call the device-specific
382 * implementations where necessary.
384 * Currently, the driver only supports E810, but future work will enable
385 * support for E822-based devices.
389 * ice_ptp_lock - Acquire PTP global semaphore register lock
390 * @hw: pointer to the HW struct
392 * Acquire the global PTP hardware semaphore lock. Returns true if the lock
393 * was acquired, false otherwise.
395 * The PFTSYN_SEM register sets the busy bit on read, returning the previous
396 * value. If software sees the busy bit cleared, this means that this function
397 * acquired the lock (and the busy bit is now set). If software sees the busy
398 * bit set, it means that another function acquired the lock.
400 * Software must clear the busy bit with a write to release the lock for other
401 * functions when done.
403 bool ice_ptp_lock(struct ice_hw *hw)
410 for (i = 0; i < MAX_TRIES; i++) {
411 hw_lock = rd32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
412 hw_lock = hw_lock & PFTSYN_SEM_BUSY_M;
416 /* Somebody is holding the lock */
417 usleep_range(10000, 20000);
424 * ice_ptp_unlock - Release PTP global semaphore register lock
425 * @hw: pointer to the HW struct
427 * Release the global PTP hardware semaphore lock. This is done by writing to
428 * the PFTSYN_SEM register.
430 void ice_ptp_unlock(struct ice_hw *hw)
432 wr32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), 0);
436 * ice_ptp_src_cmd - Prepare source timer for a timer command
437 * @hw: pointer to HW structure
438 * @cmd: Timer command
440 * Prepare the source timer for an upcoming timer sync command.
442 static void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
447 tmr_idx = ice_get_ptp_src_clock_index(hw);
448 cmd_val = tmr_idx << SEL_CPK_SRC;
452 cmd_val |= GLTSYN_CMD_INIT_TIME;
455 cmd_val |= GLTSYN_CMD_INIT_INCVAL;
458 cmd_val |= GLTSYN_CMD_ADJ_TIME;
460 case ADJ_TIME_AT_TIME:
461 cmd_val |= GLTSYN_CMD_ADJ_INIT_TIME;
464 cmd_val |= GLTSYN_CMD_READ_TIME;
468 wr32(hw, GLTSYN_CMD, cmd_val);
472 * ice_ptp_tmr_cmd - Prepare and trigger a timer sync command
473 * @hw: pointer to HW struct
474 * @cmd: the command to issue
476 * Prepare the source timer and PHY timers and then trigger the requested
477 * command. This causes the shadow registers previously written in preparation
478 * for the command to be synchronously applied to both the source and PHY
481 static int ice_ptp_tmr_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
485 /* First, prepare the source timer */
486 ice_ptp_src_cmd(hw, cmd);
488 /* Next, prepare the ports */
489 status = ice_ptp_port_cmd_e810(hw, cmd);
491 ice_debug(hw, ICE_DBG_PTP, "Failed to prepare PHY ports for timer command %u, status %d\n",
496 /* Write the sync command register to drive both source and PHY timer commands
499 wr32(hw, GLTSYN_CMD_SYNC, SYNC_EXEC_CMD);
505 * ice_ptp_init_time - Initialize device time to provided value
506 * @hw: pointer to HW struct
507 * @time: 64bits of time (GLTSYN_TIME_L and GLTSYN_TIME_H)
509 * Initialize the device to the specified time provided. This requires a three
512 * 1) write the new init time to the source timer shadow registers
513 * 2) write the new init time to the PHY timer shadow registers
514 * 3) issue an init_time timer command to synchronously switch both the source
515 * and port timers to the new init time value at the next clock cycle.
517 int ice_ptp_init_time(struct ice_hw *hw, u64 time)
522 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
525 wr32(hw, GLTSYN_SHTIME_L(tmr_idx), lower_32_bits(time));
526 wr32(hw, GLTSYN_SHTIME_H(tmr_idx), upper_32_bits(time));
527 wr32(hw, GLTSYN_SHTIME_0(tmr_idx), 0);
530 /* Fill Rx and Tx ports and send msg to PHY */
531 status = ice_ptp_prep_phy_time_e810(hw, time & 0xFFFFFFFF);
535 return ice_ptp_tmr_cmd(hw, INIT_TIME);
539 * ice_ptp_write_incval - Program PHC with new increment value
540 * @hw: pointer to HW struct
541 * @incval: Source timer increment value per clock cycle
543 * Program the PHC with a new increment value. This requires a three-step
546 * 1) Write the increment value to the source timer shadow registers
547 * 2) Write the increment value to the PHY timer shadow registers
548 * 3) Issue an INIT_INCVAL timer command to synchronously switch both the
549 * source and port timers to the new increment value at the next clock
552 int ice_ptp_write_incval(struct ice_hw *hw, u64 incval)
557 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
560 wr32(hw, GLTSYN_SHADJ_L(tmr_idx), lower_32_bits(incval));
561 wr32(hw, GLTSYN_SHADJ_H(tmr_idx), upper_32_bits(incval));
563 status = ice_ptp_prep_phy_incval_e810(hw, incval);
567 return ice_ptp_tmr_cmd(hw, INIT_INCVAL);
571 * ice_ptp_write_incval_locked - Program new incval while holding semaphore
572 * @hw: pointer to HW struct
573 * @incval: Source timer increment value per clock cycle
575 * Program a new PHC incval while holding the PTP semaphore.
577 int ice_ptp_write_incval_locked(struct ice_hw *hw, u64 incval)
581 if (!ice_ptp_lock(hw))
584 status = ice_ptp_write_incval(hw, incval);
592 * ice_ptp_adj_clock - Adjust PHC clock time atomically
593 * @hw: pointer to HW struct
594 * @adj: Adjustment in nanoseconds
596 * Perform an atomic adjustment of the PHC time by the specified number of
597 * nanoseconds. This requires a three-step process:
599 * 1) Write the adjustment to the source timer shadow registers
600 * 2) Write the adjustment to the PHY timer shadow registers
601 * 3) Issue an ADJ_TIME timer command to synchronously apply the adjustment to
602 * both the source and port timers at the next clock cycle.
604 int ice_ptp_adj_clock(struct ice_hw *hw, s32 adj)
609 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
611 /* Write the desired clock adjustment into the GLTSYN_SHADJ register.
612 * For an ADJ_TIME command, this set of registers represents the value
613 * to add to the clock time. It supports subtraction by interpreting
614 * the value as a 2's complement integer.
616 wr32(hw, GLTSYN_SHADJ_L(tmr_idx), 0);
617 wr32(hw, GLTSYN_SHADJ_H(tmr_idx), adj);
619 status = ice_ptp_prep_phy_adj_e810(hw, adj);
623 return ice_ptp_tmr_cmd(hw, ADJ_TIME);
627 * ice_read_phy_tstamp - Read a PHY timestamp from the timestamo block
628 * @hw: pointer to the HW struct
629 * @block: the block to read from
630 * @idx: the timestamp index to read
631 * @tstamp: on return, the 40bit timestamp value
633 * Read a 40bit timestamp value out of the timestamp block.
635 int ice_read_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx, u64 *tstamp)
637 return ice_read_phy_tstamp_e810(hw, block, idx, tstamp);
641 * ice_clear_phy_tstamp - Clear a timestamp from the timestamp block
642 * @hw: pointer to the HW struct
643 * @block: the block to read from
644 * @idx: the timestamp index to reset
646 * Clear a timestamp, resetting its valid bit, from the timestamp block.
648 int ice_clear_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx)
650 return ice_clear_phy_tstamp_e810(hw, block, idx);