1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
3 
4 #include <linux/delay.h>
5 #include <linux/iopoll.h>
6 #include "ice_common.h"
7 #include "ice_ptp_hw.h"
8 #include "ice_ptp_consts.h"
9 #include "ice_cgu_regs.h"
10 
11 static struct dpll_pin_frequency ice_cgu_pin_freq_common[] = {
12 	DPLL_PIN_FREQUENCY_1PPS,
13 	DPLL_PIN_FREQUENCY_10MHZ,
14 };
15 
16 static struct dpll_pin_frequency ice_cgu_pin_freq_1_hz[] = {
17 	DPLL_PIN_FREQUENCY_1PPS,
18 };
19 
20 static struct dpll_pin_frequency ice_cgu_pin_freq_10_mhz[] = {
21 	DPLL_PIN_FREQUENCY_10MHZ,
22 };
23 
24 static const struct ice_cgu_pin_desc ice_e810t_sfp_cgu_inputs[] = {
25 	{ "CVL-SDP22",	  ZL_REF0P, DPLL_PIN_TYPE_INT_OSCILLATOR,
26 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
27 	{ "CVL-SDP20",	  ZL_REF0N, DPLL_PIN_TYPE_INT_OSCILLATOR,
28 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
29 	{ "C827_0-RCLKA", ZL_REF1P, DPLL_PIN_TYPE_MUX, 0, },
30 	{ "C827_0-RCLKB", ZL_REF1N, DPLL_PIN_TYPE_MUX, 0, },
31 	{ "SMA1",	  ZL_REF3P, DPLL_PIN_TYPE_EXT,
32 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
33 	{ "SMA2/U.FL2",	  ZL_REF3N, DPLL_PIN_TYPE_EXT,
34 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
35 	{ "GNSS-1PPS",	  ZL_REF4P, DPLL_PIN_TYPE_GNSS,
36 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
37 };
38 
39 static const struct ice_cgu_pin_desc ice_e810t_qsfp_cgu_inputs[] = {
40 	{ "CVL-SDP22",	  ZL_REF0P, DPLL_PIN_TYPE_INT_OSCILLATOR,
41 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
42 	{ "CVL-SDP20",	  ZL_REF0N, DPLL_PIN_TYPE_INT_OSCILLATOR,
43 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
44 	{ "C827_0-RCLKA", ZL_REF1P, DPLL_PIN_TYPE_MUX, },
45 	{ "C827_0-RCLKB", ZL_REF1N, DPLL_PIN_TYPE_MUX, },
46 	{ "C827_1-RCLKA", ZL_REF2P, DPLL_PIN_TYPE_MUX, },
47 	{ "C827_1-RCLKB", ZL_REF2N, DPLL_PIN_TYPE_MUX, },
48 	{ "SMA1",	  ZL_REF3P, DPLL_PIN_TYPE_EXT,
49 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
50 	{ "SMA2/U.FL2",	  ZL_REF3N, DPLL_PIN_TYPE_EXT,
51 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
52 	{ "GNSS-1PPS",	  ZL_REF4P, DPLL_PIN_TYPE_GNSS,
53 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
54 };
55 
56 static const struct ice_cgu_pin_desc ice_e810t_sfp_cgu_outputs[] = {
57 	{ "REF-SMA1",	    ZL_OUT0, DPLL_PIN_TYPE_EXT,
58 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
59 	{ "REF-SMA2/U.FL2", ZL_OUT1, DPLL_PIN_TYPE_EXT,
60 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
61 	{ "PHY-CLK",	    ZL_OUT2, DPLL_PIN_TYPE_SYNCE_ETH_PORT, },
62 	{ "MAC-CLK",	    ZL_OUT3, DPLL_PIN_TYPE_SYNCE_ETH_PORT, },
63 	{ "CVL-SDP21",	    ZL_OUT4, DPLL_PIN_TYPE_EXT,
64 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
65 	{ "CVL-SDP23",	    ZL_OUT5, DPLL_PIN_TYPE_EXT,
66 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
67 };
68 
69 static const struct ice_cgu_pin_desc ice_e810t_qsfp_cgu_outputs[] = {
70 	{ "REF-SMA1",	    ZL_OUT0, DPLL_PIN_TYPE_EXT,
71 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
72 	{ "REF-SMA2/U.FL2", ZL_OUT1, DPLL_PIN_TYPE_EXT,
73 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
74 	{ "PHY-CLK",	    ZL_OUT2, DPLL_PIN_TYPE_SYNCE_ETH_PORT, 0 },
75 	{ "PHY2-CLK",	    ZL_OUT3, DPLL_PIN_TYPE_SYNCE_ETH_PORT, 0 },
76 	{ "MAC-CLK",	    ZL_OUT4, DPLL_PIN_TYPE_SYNCE_ETH_PORT, 0 },
77 	{ "CVL-SDP21",	    ZL_OUT5, DPLL_PIN_TYPE_EXT,
78 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
79 	{ "CVL-SDP23",	    ZL_OUT6, DPLL_PIN_TYPE_EXT,
80 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
81 };
82 
83 static const struct ice_cgu_pin_desc ice_e823_si_cgu_inputs[] = {
84 	{ "NONE",	  SI_REF0P, 0, 0 },
85 	{ "NONE",	  SI_REF0N, 0, 0 },
86 	{ "SYNCE0_DP",	  SI_REF1P, DPLL_PIN_TYPE_MUX, 0 },
87 	{ "SYNCE0_DN",	  SI_REF1N, DPLL_PIN_TYPE_MUX, 0 },
88 	{ "EXT_CLK_SYNC", SI_REF2P, DPLL_PIN_TYPE_EXT,
89 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
90 	{ "NONE",	  SI_REF2N, 0, 0 },
91 	{ "EXT_PPS_OUT",  SI_REF3,  DPLL_PIN_TYPE_EXT,
92 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
93 	{ "INT_PPS_OUT",  SI_REF4,  DPLL_PIN_TYPE_EXT,
94 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
95 };
96 
97 static const struct ice_cgu_pin_desc ice_e823_si_cgu_outputs[] = {
98 	{ "1588-TIME_SYNC", SI_OUT0, DPLL_PIN_TYPE_EXT,
99 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
100 	{ "PHY-CLK",	    SI_OUT1, DPLL_PIN_TYPE_SYNCE_ETH_PORT, 0 },
101 	{ "10MHZ-SMA2",	    SI_OUT2, DPLL_PIN_TYPE_EXT,
102 		ARRAY_SIZE(ice_cgu_pin_freq_10_mhz), ice_cgu_pin_freq_10_mhz },
103 	{ "PPS-SMA1",	    SI_OUT3, DPLL_PIN_TYPE_EXT,
104 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
105 };
106 
107 static const struct ice_cgu_pin_desc ice_e823_zl_cgu_inputs[] = {
108 	{ "NONE",	  ZL_REF0P, 0, 0 },
109 	{ "INT_PPS_OUT",  ZL_REF0N, DPLL_PIN_TYPE_EXT,
110 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
111 	{ "SYNCE0_DP",	  ZL_REF1P, DPLL_PIN_TYPE_MUX, 0 },
112 	{ "SYNCE0_DN",	  ZL_REF1N, DPLL_PIN_TYPE_MUX, 0 },
113 	{ "NONE",	  ZL_REF2P, 0, 0 },
114 	{ "NONE",	  ZL_REF2N, 0, 0 },
115 	{ "EXT_CLK_SYNC", ZL_REF3P, DPLL_PIN_TYPE_EXT,
116 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
117 	{ "NONE",	  ZL_REF3N, 0, 0 },
118 	{ "EXT_PPS_OUT",  ZL_REF4P, DPLL_PIN_TYPE_EXT,
119 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
120 	{ "OCXO",	  ZL_REF4N, DPLL_PIN_TYPE_INT_OSCILLATOR, 0 },
121 };
122 
123 static const struct ice_cgu_pin_desc ice_e823_zl_cgu_outputs[] = {
124 	{ "PPS-SMA1",	   ZL_OUT0, DPLL_PIN_TYPE_EXT,
125 		ARRAY_SIZE(ice_cgu_pin_freq_1_hz), ice_cgu_pin_freq_1_hz },
126 	{ "10MHZ-SMA2",	   ZL_OUT1, DPLL_PIN_TYPE_EXT,
127 		ARRAY_SIZE(ice_cgu_pin_freq_10_mhz), ice_cgu_pin_freq_10_mhz },
128 	{ "PHY-CLK",	   ZL_OUT2, DPLL_PIN_TYPE_SYNCE_ETH_PORT, 0 },
129 	{ "1588-TIME_REF", ZL_OUT3, DPLL_PIN_TYPE_SYNCE_ETH_PORT, 0 },
130 	{ "CPK-TIME_SYNC", ZL_OUT4, DPLL_PIN_TYPE_EXT,
131 		ARRAY_SIZE(ice_cgu_pin_freq_common), ice_cgu_pin_freq_common },
132 	{ "NONE",	   ZL_OUT5, 0, 0 },
133 };
134 
135 /* Low level functions for interacting with and managing the device clock used
136  * for the Precision Time Protocol.
137  *
138  * The ice hardware represents the current time using three registers:
139  *
140  *    GLTSYN_TIME_H     GLTSYN_TIME_L     GLTSYN_TIME_R
141  *  +---------------+ +---------------+ +---------------+
142  *  |    32 bits    | |    32 bits    | |    32 bits    |
143  *  +---------------+ +---------------+ +---------------+
144  *
145  * The registers are incremented every clock tick using a 40bit increment
146  * value defined over two registers:
147  *
148  *                     GLTSYN_INCVAL_H   GLTSYN_INCVAL_L
149  *                    +---------------+ +---------------+
150  *                    |    8 bit s    | |    32 bits    |
151  *                    +---------------+ +---------------+
152  *
153  * The increment value is added to the GLSTYN_TIME_R and GLSTYN_TIME_L
154  * registers every clock source tick. Depending on the specific device
155  * configuration, the clock source frequency could be one of a number of
156  * values.
157  *
158  * For E810 devices, the increment frequency is 812.5 MHz
159  *
160  * For E822 devices the clock can be derived from different sources, and the
161  * increment has an effective frequency of one of the following:
162  * - 823.4375 MHz
163  * - 783.36 MHz
164  * - 796.875 MHz
165  * - 816 MHz
166  * - 830.078125 MHz
167  * - 783.36 MHz
168  *
169  * The hardware captures timestamps in the PHY for incoming packets, and for
170  * outgoing packets on request. To support this, the PHY maintains a timer
171  * that matches the lower 64 bits of the global source timer.
172  *
173  * In order to ensure that the PHY timers and the source timer are equivalent,
174  * shadow registers are used to prepare the desired initial values. A special
175  * sync command is issued to trigger copying from the shadow registers into
176  * the appropriate source and PHY registers simultaneously.
177  *
178  * The driver supports devices which have different PHYs with subtly different
179  * mechanisms to program and control the timers. We divide the devices into
180  * families named after the first major device, E810 and similar devices, and
181  * E822 and similar devices.
182  *
183  * - E822 based devices have additional support for fine grained Vernier
184  *   calibration which requires significant setup
185  * - The layout of timestamp data in the PHY register blocks is different
186  * - The way timer synchronization commands are issued is different.
187  *
188  * To support this, very low level functions have an e810 or e822 suffix
189  * indicating what type of device they work on. Higher level abstractions for
190  * tasks that can be done on both devices do not have the suffix and will
191  * correctly look up the appropriate low level function when running.
192  *
193  * Functions which only make sense on a single device family may not have
194  * a suitable generic implementation
195  */
196 
197 /**
198  * ice_get_ptp_src_clock_index - determine source clock index
199  * @hw: pointer to HW struct
200  *
201  * Determine the source clock index currently in use, based on device
202  * capabilities reported during initialization.
203  */
ice_get_ptp_src_clock_index(struct ice_hw * hw)204 u8 ice_get_ptp_src_clock_index(struct ice_hw *hw)
205 {
206 	return hw->func_caps.ts_func_info.tmr_index_assoc;
207 }
208 
209 /**
210  * ice_ptp_read_src_incval - Read source timer increment value
211  * @hw: pointer to HW struct
212  *
213  * Read the increment value of the source timer and return it.
214  */
ice_ptp_read_src_incval(struct ice_hw * hw)215 static u64 ice_ptp_read_src_incval(struct ice_hw *hw)
216 {
217 	u32 lo, hi;
218 	u8 tmr_idx;
219 
220 	tmr_idx = ice_get_ptp_src_clock_index(hw);
221 
222 	lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
223 	hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
224 
225 	return ((u64)(hi & INCVAL_HIGH_M) << 32) | lo;
226 }
227 
228 /**
229  * ice_read_cgu_reg_e82x - Read a CGU register
230  * @hw: pointer to the HW struct
231  * @addr: Register address to read
232  * @val: storage for register value read
233  *
234  * Read the contents of a register of the Clock Generation Unit. Only
235  * applicable to E822 devices.
236  *
237  * Return: 0 on success, other error codes when failed to read from CGU
238  */
ice_read_cgu_reg_e82x(struct ice_hw * hw,u32 addr,u32 * val)239 static int ice_read_cgu_reg_e82x(struct ice_hw *hw, u32 addr, u32 *val)
240 {
241 	struct ice_sbq_msg_input cgu_msg = {
242 		.opcode = ice_sbq_msg_rd,
243 		.dest_dev = cgu,
244 		.msg_addr_low = addr
245 	};
246 	int err;
247 
248 	err = ice_sbq_rw_reg(hw, &cgu_msg, ICE_AQ_FLAG_RD);
249 	if (err) {
250 		ice_debug(hw, ICE_DBG_PTP, "Failed to read CGU register 0x%04x, err %d\n",
251 			  addr, err);
252 		return err;
253 	}
254 
255 	*val = cgu_msg.data;
256 
257 	return 0;
258 }
259 
260 /**
261  * ice_write_cgu_reg_e82x - Write a CGU register
262  * @hw: pointer to the HW struct
263  * @addr: Register address to write
264  * @val: value to write into the register
265  *
266  * Write the specified value to a register of the Clock Generation Unit. Only
267  * applicable to E822 devices.
268  *
269  * Return: 0 on success, other error codes when failed to write to CGU
270  */
ice_write_cgu_reg_e82x(struct ice_hw * hw,u32 addr,u32 val)271 static int ice_write_cgu_reg_e82x(struct ice_hw *hw, u32 addr, u32 val)
272 {
273 	struct ice_sbq_msg_input cgu_msg = {
274 		.opcode = ice_sbq_msg_wr,
275 		.dest_dev = cgu,
276 		.msg_addr_low = addr,
277 		.data = val
278 	};
279 	int err;
280 
281 	err = ice_sbq_rw_reg(hw, &cgu_msg, ICE_AQ_FLAG_RD);
282 	if (err) {
283 		ice_debug(hw, ICE_DBG_PTP, "Failed to write CGU register 0x%04x, err %d\n",
284 			  addr, err);
285 		return err;
286 	}
287 
288 	return err;
289 }
290 
291 /**
292  * ice_clk_freq_str - Convert time_ref_freq to string
293  * @clk_freq: Clock frequency
294  *
295  * Return: specified TIME_REF clock frequency converted to a string
296  */
ice_clk_freq_str(enum ice_time_ref_freq clk_freq)297 static const char *ice_clk_freq_str(enum ice_time_ref_freq clk_freq)
298 {
299 	switch (clk_freq) {
300 	case ICE_TIME_REF_FREQ_25_000:
301 		return "25 MHz";
302 	case ICE_TIME_REF_FREQ_122_880:
303 		return "122.88 MHz";
304 	case ICE_TIME_REF_FREQ_125_000:
305 		return "125 MHz";
306 	case ICE_TIME_REF_FREQ_153_600:
307 		return "153.6 MHz";
308 	case ICE_TIME_REF_FREQ_156_250:
309 		return "156.25 MHz";
310 	case ICE_TIME_REF_FREQ_245_760:
311 		return "245.76 MHz";
312 	default:
313 		return "Unknown";
314 	}
315 }
316 
317 /**
318  * ice_clk_src_str - Convert time_ref_src to string
319  * @clk_src: Clock source
320  *
321  * Return: specified clock source converted to its string name
322  */
ice_clk_src_str(enum ice_clk_src clk_src)323 static const char *ice_clk_src_str(enum ice_clk_src clk_src)
324 {
325 	switch (clk_src) {
326 	case ICE_CLK_SRC_TCXO:
327 		return "TCXO";
328 	case ICE_CLK_SRC_TIME_REF:
329 		return "TIME_REF";
330 	default:
331 		return "Unknown";
332 	}
333 }
334 
335 /**
336  * ice_cfg_cgu_pll_e82x - Configure the Clock Generation Unit
337  * @hw: pointer to the HW struct
338  * @clk_freq: Clock frequency to program
339  * @clk_src: Clock source to select (TIME_REF, or TCXO)
340  *
341  * Configure the Clock Generation Unit with the desired clock frequency and
342  * time reference, enabling the PLL which drives the PTP hardware clock.
343  *
344  * Return:
345  * * %0       - success
346  * * %-EINVAL - input parameters are incorrect
347  * * %-EBUSY  - failed to lock TS PLL
348  * * %other   - CGU read/write failure
349  */
ice_cfg_cgu_pll_e82x(struct ice_hw * hw,enum ice_time_ref_freq clk_freq,enum ice_clk_src clk_src)350 static int ice_cfg_cgu_pll_e82x(struct ice_hw *hw,
351 				enum ice_time_ref_freq clk_freq,
352 				enum ice_clk_src clk_src)
353 {
354 	union tspll_ro_bwm_lf bwm_lf;
355 	union nac_cgu_dword19 dw19;
356 	union nac_cgu_dword22 dw22;
357 	union nac_cgu_dword24 dw24;
358 	union nac_cgu_dword9 dw9;
359 	int err;
360 
361 	if (clk_freq >= NUM_ICE_TIME_REF_FREQ) {
362 		dev_warn(ice_hw_to_dev(hw), "Invalid TIME_REF frequency %u\n",
363 			 clk_freq);
364 		return -EINVAL;
365 	}
366 
367 	if (clk_src >= NUM_ICE_CLK_SRC) {
368 		dev_warn(ice_hw_to_dev(hw), "Invalid clock source %u\n",
369 			 clk_src);
370 		return -EINVAL;
371 	}
372 
373 	if (clk_src == ICE_CLK_SRC_TCXO &&
374 	    clk_freq != ICE_TIME_REF_FREQ_25_000) {
375 		dev_warn(ice_hw_to_dev(hw),
376 			 "TCXO only supports 25 MHz frequency\n");
377 		return -EINVAL;
378 	}
379 
380 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD9, &dw9.val);
381 	if (err)
382 		return err;
383 
384 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD24, &dw24.val);
385 	if (err)
386 		return err;
387 
388 	err = ice_read_cgu_reg_e82x(hw, TSPLL_RO_BWM_LF, &bwm_lf.val);
389 	if (err)
390 		return err;
391 
392 	/* Log the current clock configuration */
393 	ice_debug(hw, ICE_DBG_PTP, "Current CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
394 		  dw24.ts_pll_enable ? "enabled" : "disabled",
395 		  ice_clk_src_str(dw24.time_ref_sel),
396 		  ice_clk_freq_str(dw9.time_ref_freq_sel),
397 		  bwm_lf.plllock_true_lock_cri ? "locked" : "unlocked");
398 
399 	/* Disable the PLL before changing the clock source or frequency */
400 	if (dw24.ts_pll_enable) {
401 		dw24.ts_pll_enable = 0;
402 
403 		err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD24, dw24.val);
404 		if (err)
405 			return err;
406 	}
407 
408 	/* Set the frequency */
409 	dw9.time_ref_freq_sel = clk_freq;
410 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD9, dw9.val);
411 	if (err)
412 		return err;
413 
414 	/* Configure the TS PLL feedback divisor */
415 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD19, &dw19.val);
416 	if (err)
417 		return err;
418 
419 	dw19.tspll_fbdiv_intgr = e822_cgu_params[clk_freq].feedback_div;
420 	dw19.tspll_ndivratio = 1;
421 
422 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD19, dw19.val);
423 	if (err)
424 		return err;
425 
426 	/* Configure the TS PLL post divisor */
427 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD22, &dw22.val);
428 	if (err)
429 		return err;
430 
431 	dw22.time1588clk_div = e822_cgu_params[clk_freq].post_pll_div;
432 	dw22.time1588clk_sel_div2 = 0;
433 
434 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD22, dw22.val);
435 	if (err)
436 		return err;
437 
438 	/* Configure the TS PLL pre divisor and clock source */
439 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD24, &dw24.val);
440 	if (err)
441 		return err;
442 
443 	dw24.ref1588_ck_div = e822_cgu_params[clk_freq].refclk_pre_div;
444 	dw24.tspll_fbdiv_frac = e822_cgu_params[clk_freq].frac_n_div;
445 	dw24.time_ref_sel = clk_src;
446 
447 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD24, dw24.val);
448 	if (err)
449 		return err;
450 
451 	/* Finally, enable the PLL */
452 	dw24.ts_pll_enable = 1;
453 
454 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD24, dw24.val);
455 	if (err)
456 		return err;
457 
458 	/* Wait to verify if the PLL locks */
459 	usleep_range(1000, 5000);
460 
461 	err = ice_read_cgu_reg_e82x(hw, TSPLL_RO_BWM_LF, &bwm_lf.val);
462 	if (err)
463 		return err;
464 
465 	if (!bwm_lf.plllock_true_lock_cri) {
466 		dev_warn(ice_hw_to_dev(hw), "CGU PLL failed to lock\n");
467 		return -EBUSY;
468 	}
469 
470 	/* Log the current clock configuration */
471 	ice_debug(hw, ICE_DBG_PTP, "New CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
472 		  dw24.ts_pll_enable ? "enabled" : "disabled",
473 		  ice_clk_src_str(dw24.time_ref_sel),
474 		  ice_clk_freq_str(dw9.time_ref_freq_sel),
475 		  bwm_lf.plllock_true_lock_cri ? "locked" : "unlocked");
476 
477 	return 0;
478 }
479 
480 /**
481  * ice_cfg_cgu_pll_e825c - Configure the Clock Generation Unit for E825-C
482  * @hw: pointer to the HW struct
483  * @clk_freq: Clock frequency to program
484  * @clk_src: Clock source to select (TIME_REF, or TCXO)
485  *
486  * Configure the Clock Generation Unit with the desired clock frequency and
487  * time reference, enabling the PLL which drives the PTP hardware clock.
488  *
489  * Return:
490  * * %0       - success
491  * * %-EINVAL - input parameters are incorrect
492  * * %-EBUSY  - failed to lock TS PLL
493  * * %other   - CGU read/write failure
494  */
ice_cfg_cgu_pll_e825c(struct ice_hw * hw,enum ice_time_ref_freq clk_freq,enum ice_clk_src clk_src)495 static int ice_cfg_cgu_pll_e825c(struct ice_hw *hw,
496 				 enum ice_time_ref_freq clk_freq,
497 				 enum ice_clk_src clk_src)
498 {
499 	union tspll_ro_lock_e825c ro_lock;
500 	union nac_cgu_dword16_e825c dw16;
501 	union nac_cgu_dword23_e825c dw23;
502 	union nac_cgu_dword19 dw19;
503 	union nac_cgu_dword22 dw22;
504 	union nac_cgu_dword24 dw24;
505 	union nac_cgu_dword9 dw9;
506 	int err;
507 
508 	if (clk_freq >= NUM_ICE_TIME_REF_FREQ) {
509 		dev_warn(ice_hw_to_dev(hw), "Invalid TIME_REF frequency %u\n",
510 			 clk_freq);
511 		return -EINVAL;
512 	}
513 
514 	if (clk_src >= NUM_ICE_CLK_SRC) {
515 		dev_warn(ice_hw_to_dev(hw), "Invalid clock source %u\n",
516 			 clk_src);
517 		return -EINVAL;
518 	}
519 
520 	if (clk_src == ICE_CLK_SRC_TCXO &&
521 	    clk_freq != ICE_TIME_REF_FREQ_156_250) {
522 		dev_warn(ice_hw_to_dev(hw),
523 			 "TCXO only supports 156.25 MHz frequency\n");
524 		return -EINVAL;
525 	}
526 
527 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD9, &dw9.val);
528 	if (err)
529 		return err;
530 
531 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD24, &dw24.val);
532 	if (err)
533 		return err;
534 
535 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD16_E825C, &dw16.val);
536 	if (err)
537 		return err;
538 
539 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD23_E825C, &dw23.val);
540 	if (err)
541 		return err;
542 
543 	err = ice_read_cgu_reg_e82x(hw, TSPLL_RO_LOCK_E825C, &ro_lock.val);
544 	if (err)
545 		return err;
546 
547 	/* Log the current clock configuration */
548 	ice_debug(hw, ICE_DBG_PTP, "Current CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
549 		  dw24.ts_pll_enable ? "enabled" : "disabled",
550 		  ice_clk_src_str(dw23.time_ref_sel),
551 		  ice_clk_freq_str(dw9.time_ref_freq_sel),
552 		  ro_lock.plllock_true_lock_cri ? "locked" : "unlocked");
553 
554 	/* Disable the PLL before changing the clock source or frequency */
555 	if (dw23.ts_pll_enable) {
556 		dw23.ts_pll_enable = 0;
557 
558 		err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD23_E825C,
559 					     dw23.val);
560 		if (err)
561 			return err;
562 	}
563 
564 	/* Set the frequency */
565 	dw9.time_ref_freq_sel = clk_freq;
566 
567 	/* Enable the correct receiver */
568 	if (clk_src == ICE_CLK_SRC_TCXO) {
569 		dw9.time_ref_en = 0;
570 		dw9.clk_eref0_en = 1;
571 	} else {
572 		dw9.time_ref_en = 1;
573 		dw9.clk_eref0_en = 0;
574 	}
575 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD9, dw9.val);
576 	if (err)
577 		return err;
578 
579 	/* Choose the referenced frequency */
580 	dw16.tspll_ck_refclkfreq =
581 	e825c_cgu_params[clk_freq].tspll_ck_refclkfreq;
582 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD16_E825C, dw16.val);
583 	if (err)
584 		return err;
585 
586 	/* Configure the TS PLL feedback divisor */
587 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD19, &dw19.val);
588 	if (err)
589 		return err;
590 
591 	dw19.tspll_fbdiv_intgr =
592 		e825c_cgu_params[clk_freq].tspll_fbdiv_intgr;
593 	dw19.tspll_ndivratio =
594 		e825c_cgu_params[clk_freq].tspll_ndivratio;
595 
596 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD19, dw19.val);
597 	if (err)
598 		return err;
599 
600 	/* Configure the TS PLL post divisor */
601 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD22, &dw22.val);
602 	if (err)
603 		return err;
604 
605 	/* These two are constant for E825C */
606 	dw22.time1588clk_div = 5;
607 	dw22.time1588clk_sel_div2 = 0;
608 
609 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD22, dw22.val);
610 	if (err)
611 		return err;
612 
613 	/* Configure the TS PLL pre divisor and clock source */
614 	err = ice_read_cgu_reg_e82x(hw, NAC_CGU_DWORD23_E825C, &dw23.val);
615 	if (err)
616 		return err;
617 
618 	dw23.ref1588_ck_div =
619 		e825c_cgu_params[clk_freq].ref1588_ck_div;
620 	dw23.time_ref_sel = clk_src;
621 
622 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD23_E825C, dw23.val);
623 	if (err)
624 		return err;
625 
626 	dw24.tspll_fbdiv_frac =
627 		e825c_cgu_params[clk_freq].tspll_fbdiv_frac;
628 
629 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD24, dw24.val);
630 	if (err)
631 		return err;
632 
633 	/* Finally, enable the PLL */
634 	dw23.ts_pll_enable = 1;
635 
636 	err = ice_write_cgu_reg_e82x(hw, NAC_CGU_DWORD23_E825C, dw23.val);
637 	if (err)
638 		return err;
639 
640 	/* Wait to verify if the PLL locks */
641 	usleep_range(1000, 5000);
642 
643 	err = ice_read_cgu_reg_e82x(hw, TSPLL_RO_LOCK_E825C, &ro_lock.val);
644 	if (err)
645 		return err;
646 
647 	if (!ro_lock.plllock_true_lock_cri) {
648 		dev_warn(ice_hw_to_dev(hw), "CGU PLL failed to lock\n");
649 		return -EBUSY;
650 	}
651 
652 	/* Log the current clock configuration */
653 	ice_debug(hw, ICE_DBG_PTP, "New CGU configuration -- %s, clk_src %s, clk_freq %s, PLL %s\n",
654 		  dw24.ts_pll_enable ? "enabled" : "disabled",
655 		  ice_clk_src_str(dw23.time_ref_sel),
656 		  ice_clk_freq_str(dw9.time_ref_freq_sel),
657 		  ro_lock.plllock_true_lock_cri ? "locked" : "unlocked");
658 
659 	return 0;
660 }
661 
662 /**
663  * ice_cfg_cgu_pll_dis_sticky_bits_e82x - disable TS PLL sticky bits
664  * @hw: pointer to the HW struct
665  *
666  * Configure the Clock Generation Unit TS PLL sticky bits so they don't latch on
667  * losing TS PLL lock, but always show current state.
668  *
669  * Return: 0 on success, other error codes when failed to read/write CGU
670  */
ice_cfg_cgu_pll_dis_sticky_bits_e82x(struct ice_hw * hw)671 static int ice_cfg_cgu_pll_dis_sticky_bits_e82x(struct ice_hw *hw)
672 {
673 	union tspll_cntr_bist_settings cntr_bist;
674 	int err;
675 
676 	err = ice_read_cgu_reg_e82x(hw, TSPLL_CNTR_BIST_SETTINGS,
677 				    &cntr_bist.val);
678 	if (err)
679 		return err;
680 
681 	/* Disable sticky lock detection so lock err reported is accurate */
682 	cntr_bist.i_plllock_sel_0 = 0;
683 	cntr_bist.i_plllock_sel_1 = 0;
684 
685 	return ice_write_cgu_reg_e82x(hw, TSPLL_CNTR_BIST_SETTINGS,
686 				      cntr_bist.val);
687 }
688 
689 /**
690  * ice_cfg_cgu_pll_dis_sticky_bits_e825c - disable TS PLL sticky bits for E825-C
691  * @hw: pointer to the HW struct
692  *
693  * Configure the Clock Generation Unit TS PLL sticky bits so they don't latch on
694  * losing TS PLL lock, but always show current state.
695  *
696  * Return: 0 on success, other error codes when failed to read/write CGU
697  */
ice_cfg_cgu_pll_dis_sticky_bits_e825c(struct ice_hw * hw)698 static int ice_cfg_cgu_pll_dis_sticky_bits_e825c(struct ice_hw *hw)
699 {
700 	union tspll_bw_tdc_e825c bw_tdc;
701 	int err;
702 
703 	err = ice_read_cgu_reg_e82x(hw, TSPLL_BW_TDC_E825C, &bw_tdc.val);
704 	if (err)
705 		return err;
706 
707 	bw_tdc.i_plllock_sel_1_0 = 0;
708 
709 	return ice_write_cgu_reg_e82x(hw, TSPLL_BW_TDC_E825C, bw_tdc.val);
710 }
711 
712 /**
713  * ice_init_cgu_e82x - Initialize CGU with settings from firmware
714  * @hw: pointer to the HW structure
715  *
716  * Initialize the Clock Generation Unit of the E822 device.
717  *
718  * Return: 0 on success, other error codes when failed to read/write/cfg CGU
719  */
ice_init_cgu_e82x(struct ice_hw * hw)720 static int ice_init_cgu_e82x(struct ice_hw *hw)
721 {
722 	struct ice_ts_func_info *ts_info = &hw->func_caps.ts_func_info;
723 	int err;
724 
725 	/* Disable sticky lock detection so lock err reported is accurate */
726 	if (ice_is_e825c(hw))
727 		err = ice_cfg_cgu_pll_dis_sticky_bits_e825c(hw);
728 	else
729 		err = ice_cfg_cgu_pll_dis_sticky_bits_e82x(hw);
730 	if (err)
731 		return err;
732 
733 	/* Configure the CGU PLL using the parameters from the function
734 	 * capabilities.
735 	 */
736 	if (ice_is_e825c(hw))
737 		err = ice_cfg_cgu_pll_e825c(hw, ts_info->time_ref,
738 					    (enum ice_clk_src)ts_info->clk_src);
739 	else
740 		err = ice_cfg_cgu_pll_e82x(hw, ts_info->time_ref,
741 					   (enum ice_clk_src)ts_info->clk_src);
742 
743 	return err;
744 }
745 
746 /**
747  * ice_ptp_tmr_cmd_to_src_reg - Convert to source timer command value
748  * @hw: pointer to HW struct
749  * @cmd: Timer command
750  *
751  * Return: the source timer command register value for the given PTP timer
752  * command.
753  */
ice_ptp_tmr_cmd_to_src_reg(struct ice_hw * hw,enum ice_ptp_tmr_cmd cmd)754 static u32 ice_ptp_tmr_cmd_to_src_reg(struct ice_hw *hw,
755 				      enum ice_ptp_tmr_cmd cmd)
756 {
757 	u32 cmd_val, tmr_idx;
758 
759 	switch (cmd) {
760 	case ICE_PTP_INIT_TIME:
761 		cmd_val = GLTSYN_CMD_INIT_TIME;
762 		break;
763 	case ICE_PTP_INIT_INCVAL:
764 		cmd_val = GLTSYN_CMD_INIT_INCVAL;
765 		break;
766 	case ICE_PTP_ADJ_TIME:
767 		cmd_val = GLTSYN_CMD_ADJ_TIME;
768 		break;
769 	case ICE_PTP_ADJ_TIME_AT_TIME:
770 		cmd_val = GLTSYN_CMD_ADJ_INIT_TIME;
771 		break;
772 	case ICE_PTP_NOP:
773 	case ICE_PTP_READ_TIME:
774 		cmd_val = GLTSYN_CMD_READ_TIME;
775 		break;
776 	default:
777 		dev_warn(ice_hw_to_dev(hw),
778 			 "Ignoring unrecognized timer command %u\n", cmd);
779 		cmd_val = 0;
780 	}
781 
782 	tmr_idx = ice_get_ptp_src_clock_index(hw);
783 
784 	return tmr_idx << SEL_CPK_SRC | cmd_val;
785 }
786 
787 /**
788  * ice_ptp_tmr_cmd_to_port_reg- Convert to port timer command value
789  * @hw: pointer to HW struct
790  * @cmd: Timer command
791  *
792  * Note that some hardware families use a different command register value for
793  * the PHY ports, while other hardware families use the same register values
794  * as the source timer.
795  *
796  * Return: the PHY port timer command register value for the given PTP timer
797  * command.
798  */
ice_ptp_tmr_cmd_to_port_reg(struct ice_hw * hw,enum ice_ptp_tmr_cmd cmd)799 static u32 ice_ptp_tmr_cmd_to_port_reg(struct ice_hw *hw,
800 				       enum ice_ptp_tmr_cmd cmd)
801 {
802 	u32 cmd_val, tmr_idx;
803 
804 	/* Certain hardware families share the same register values for the
805 	 * port register and source timer register.
806 	 */
807 	switch (ice_get_phy_model(hw)) {
808 	case ICE_PHY_E810:
809 		return ice_ptp_tmr_cmd_to_src_reg(hw, cmd) & TS_CMD_MASK_E810;
810 	default:
811 		break;
812 	}
813 
814 	switch (cmd) {
815 	case ICE_PTP_INIT_TIME:
816 		cmd_val = PHY_CMD_INIT_TIME;
817 		break;
818 	case ICE_PTP_INIT_INCVAL:
819 		cmd_val = PHY_CMD_INIT_INCVAL;
820 		break;
821 	case ICE_PTP_ADJ_TIME:
822 		cmd_val = PHY_CMD_ADJ_TIME;
823 		break;
824 	case ICE_PTP_ADJ_TIME_AT_TIME:
825 		cmd_val = PHY_CMD_ADJ_TIME_AT_TIME;
826 		break;
827 	case ICE_PTP_READ_TIME:
828 		cmd_val = PHY_CMD_READ_TIME;
829 		break;
830 	case ICE_PTP_NOP:
831 		cmd_val = 0;
832 		break;
833 	default:
834 		dev_warn(ice_hw_to_dev(hw),
835 			 "Ignoring unrecognized timer command %u\n", cmd);
836 		cmd_val = 0;
837 	}
838 
839 	tmr_idx = ice_get_ptp_src_clock_index(hw);
840 
841 	return tmr_idx << SEL_PHY_SRC | cmd_val;
842 }
843 
844 /**
845  * ice_ptp_src_cmd - Prepare source timer for a timer command
846  * @hw: pointer to HW structure
847  * @cmd: Timer command
848  *
849  * Prepare the source timer for an upcoming timer sync command.
850  */
ice_ptp_src_cmd(struct ice_hw * hw,enum ice_ptp_tmr_cmd cmd)851 void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
852 {
853 	u32 cmd_val = ice_ptp_tmr_cmd_to_src_reg(hw, cmd);
854 
855 	wr32(hw, GLTSYN_CMD, cmd_val);
856 }
857 
858 /**
859  * ice_ptp_exec_tmr_cmd - Execute all prepared timer commands
860  * @hw: pointer to HW struct
861  *
862  * Write the SYNC_EXEC_CMD bit to the GLTSYN_CMD_SYNC register, and flush the
863  * write immediately. This triggers the hardware to begin executing all of the
864  * source and PHY timer commands synchronously.
865  */
ice_ptp_exec_tmr_cmd(struct ice_hw * hw)866 static void ice_ptp_exec_tmr_cmd(struct ice_hw *hw)
867 {
868 	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
869 
870 	guard(spinlock)(&pf->adapter->ptp_gltsyn_time_lock);
871 	wr32(hw, GLTSYN_CMD_SYNC, SYNC_EXEC_CMD);
872 	ice_flush(hw);
873 }
874 
875 /* 56G PHY device functions
876  *
877  * The following functions operate on devices with the ETH 56G PHY.
878  */
879 
880 /**
881  * ice_ptp_get_dest_dev_e825 - get destination PHY for given port number
882  * @hw: pointer to the HW struct
883  * @port: destination port
884  *
885  * Return: destination sideband queue PHY device.
886  */
ice_ptp_get_dest_dev_e825(struct ice_hw * hw,u8 port)887 static enum ice_sbq_msg_dev ice_ptp_get_dest_dev_e825(struct ice_hw *hw,
888 						      u8 port)
889 {
890 	/* On a single complex E825, PHY 0 is always destination device phy_0
891 	 * and PHY 1 is phy_0_peer.
892 	 */
893 	if (port >= hw->ptp.ports_per_phy)
894 		return eth56g_phy_1;
895 	else
896 		return eth56g_phy_0;
897 }
898 
899 /**
900  * ice_write_phy_eth56g - Write a PHY port register
901  * @hw: pointer to the HW struct
902  * @port: destination port
903  * @addr: PHY register address
904  * @val: Value to write
905  *
906  * Return: 0 on success, other error codes when failed to write to PHY
907  */
ice_write_phy_eth56g(struct ice_hw * hw,u8 port,u32 addr,u32 val)908 static int ice_write_phy_eth56g(struct ice_hw *hw, u8 port, u32 addr, u32 val)
909 {
910 	struct ice_sbq_msg_input msg = {
911 		.dest_dev = ice_ptp_get_dest_dev_e825(hw, port),
912 		.opcode = ice_sbq_msg_wr,
913 		.msg_addr_low = lower_16_bits(addr),
914 		.msg_addr_high = upper_16_bits(addr),
915 		.data = val
916 	};
917 	int err;
918 
919 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
920 	if (err)
921 		ice_debug(hw, ICE_DBG_PTP, "PTP failed to send msg to phy %d\n",
922 			  err);
923 
924 	return err;
925 }
926 
927 /**
928  * ice_read_phy_eth56g - Read a PHY port register
929  * @hw: pointer to the HW struct
930  * @port: destination port
931  * @addr: PHY register address
932  * @val: Value to write
933  *
934  * Return: 0 on success, other error codes when failed to read from PHY
935  */
ice_read_phy_eth56g(struct ice_hw * hw,u8 port,u32 addr,u32 * val)936 static int ice_read_phy_eth56g(struct ice_hw *hw, u8 port, u32 addr, u32 *val)
937 {
938 	struct ice_sbq_msg_input msg = {
939 		.dest_dev = ice_ptp_get_dest_dev_e825(hw, port),
940 		.opcode = ice_sbq_msg_rd,
941 		.msg_addr_low = lower_16_bits(addr),
942 		.msg_addr_high = upper_16_bits(addr)
943 	};
944 	int err;
945 
946 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
947 	if (err)
948 		ice_debug(hw, ICE_DBG_PTP, "PTP failed to send msg to phy %d\n",
949 			  err);
950 	else
951 		*val = msg.data;
952 
953 	return err;
954 }
955 
956 /**
957  * ice_phy_res_address_eth56g - Calculate a PHY port register address
958  * @hw: pointer to the HW struct
959  * @lane: Lane number to be written
960  * @res_type: resource type (register/memory)
961  * @offset: Offset from PHY port register base
962  * @addr: The result address
963  *
964  * Return:
965  * * %0      - success
966  * * %EINVAL - invalid port number or resource type
967  */
ice_phy_res_address_eth56g(struct ice_hw * hw,u8 lane,enum eth56g_res_type res_type,u32 offset,u32 * addr)968 static int ice_phy_res_address_eth56g(struct ice_hw *hw, u8 lane,
969 				      enum eth56g_res_type res_type,
970 				      u32 offset,
971 				      u32 *addr)
972 {
973 	if (res_type >= NUM_ETH56G_PHY_RES)
974 		return -EINVAL;
975 
976 	/* Lanes 4..7 are in fact 0..3 on a second PHY */
977 	lane %= hw->ptp.ports_per_phy;
978 	*addr = eth56g_phy_res[res_type].base[0] +
979 		lane * eth56g_phy_res[res_type].step + offset;
980 
981 	return 0;
982 }
983 
984 /**
985  * ice_write_port_eth56g - Write a PHY port register
986  * @hw: pointer to the HW struct
987  * @offset: PHY register offset
988  * @port: Port number
989  * @val: Value to write
990  * @res_type: resource type (register/memory)
991  *
992  * Return:
993  * * %0      - success
994  * * %EINVAL - invalid port number or resource type
995  * * %other  - failed to write to PHY
996  */
ice_write_port_eth56g(struct ice_hw * hw,u8 port,u32 offset,u32 val,enum eth56g_res_type res_type)997 static int ice_write_port_eth56g(struct ice_hw *hw, u8 port, u32 offset,
998 				 u32 val, enum eth56g_res_type res_type)
999 {
1000 	u32 addr;
1001 	int err;
1002 
1003 	if (port >= hw->ptp.num_lports)
1004 		return -EINVAL;
1005 
1006 	err = ice_phy_res_address_eth56g(hw, port, res_type, offset, &addr);
1007 	if (err)
1008 		return err;
1009 
1010 	return ice_write_phy_eth56g(hw, port, addr, val);
1011 }
1012 
1013 /**
1014  * ice_read_port_eth56g - Read a PHY port register
1015  * @hw: pointer to the HW struct
1016  * @offset: PHY register offset
1017  * @port: Port number
1018  * @val: Value to write
1019  * @res_type: resource type (register/memory)
1020  *
1021  * Return:
1022  * * %0      - success
1023  * * %EINVAL - invalid port number or resource type
1024  * * %other  - failed to read from PHY
1025  */
ice_read_port_eth56g(struct ice_hw * hw,u8 port,u32 offset,u32 * val,enum eth56g_res_type res_type)1026 static int ice_read_port_eth56g(struct ice_hw *hw, u8 port, u32 offset,
1027 				u32 *val, enum eth56g_res_type res_type)
1028 {
1029 	u32 addr;
1030 	int err;
1031 
1032 	if (port >= hw->ptp.num_lports)
1033 		return -EINVAL;
1034 
1035 	err = ice_phy_res_address_eth56g(hw, port, res_type, offset, &addr);
1036 	if (err)
1037 		return err;
1038 
1039 	return ice_read_phy_eth56g(hw, port, addr, val);
1040 }
1041 
1042 /**
1043  * ice_write_ptp_reg_eth56g - Write a PHY port register
1044  * @hw: pointer to the HW struct
1045  * @port: Port number to be written
1046  * @offset: Offset from PHY port register base
1047  * @val: Value to write
1048  *
1049  * Return:
1050  * * %0      - success
1051  * * %EINVAL - invalid port number or resource type
1052  * * %other  - failed to write to PHY
1053  */
ice_write_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u16 offset,u32 val)1054 static int ice_write_ptp_reg_eth56g(struct ice_hw *hw, u8 port, u16 offset,
1055 				    u32 val)
1056 {
1057 	return ice_write_port_eth56g(hw, port, offset, val, ETH56G_PHY_REG_PTP);
1058 }
1059 
1060 /**
1061  * ice_write_mac_reg_eth56g - Write a MAC PHY port register
1062  * parameter
1063  * @hw: pointer to the HW struct
1064  * @port: Port number to be written
1065  * @offset: Offset from PHY port register base
1066  * @val: Value to write
1067  *
1068  * Return:
1069  * * %0      - success
1070  * * %EINVAL - invalid port number or resource type
1071  * * %other  - failed to write to PHY
1072  */
ice_write_mac_reg_eth56g(struct ice_hw * hw,u8 port,u32 offset,u32 val)1073 static int ice_write_mac_reg_eth56g(struct ice_hw *hw, u8 port, u32 offset,
1074 				    u32 val)
1075 {
1076 	return ice_write_port_eth56g(hw, port, offset, val, ETH56G_PHY_REG_MAC);
1077 }
1078 
1079 /**
1080  * ice_write_xpcs_reg_eth56g - Write a PHY port register
1081  * @hw: pointer to the HW struct
1082  * @port: Port number to be written
1083  * @offset: Offset from PHY port register base
1084  * @val: Value to write
1085  *
1086  * Return:
1087  * * %0      - success
1088  * * %EINVAL - invalid port number or resource type
1089  * * %other  - failed to write to PHY
1090  */
ice_write_xpcs_reg_eth56g(struct ice_hw * hw,u8 port,u32 offset,u32 val)1091 static int ice_write_xpcs_reg_eth56g(struct ice_hw *hw, u8 port, u32 offset,
1092 				     u32 val)
1093 {
1094 	return ice_write_port_eth56g(hw, port, offset, val,
1095 				     ETH56G_PHY_REG_XPCS);
1096 }
1097 
1098 /**
1099  * ice_read_ptp_reg_eth56g - Read a PHY port register
1100  * @hw: pointer to the HW struct
1101  * @port: Port number to be read
1102  * @offset: Offset from PHY port register base
1103  * @val: Pointer to the value to read (out param)
1104  *
1105  * Return:
1106  * * %0      - success
1107  * * %EINVAL - invalid port number or resource type
1108  * * %other  - failed to read from PHY
1109  */
ice_read_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u16 offset,u32 * val)1110 static int ice_read_ptp_reg_eth56g(struct ice_hw *hw, u8 port, u16 offset,
1111 				   u32 *val)
1112 {
1113 	return ice_read_port_eth56g(hw, port, offset, val, ETH56G_PHY_REG_PTP);
1114 }
1115 
1116 /**
1117  * ice_read_mac_reg_eth56g - Read a PHY port register
1118  * @hw: pointer to the HW struct
1119  * @port: Port number to be read
1120  * @offset: Offset from PHY port register base
1121  * @val: Pointer to the value to read (out param)
1122  *
1123  * Return:
1124  * * %0      - success
1125  * * %EINVAL - invalid port number or resource type
1126  * * %other  - failed to read from PHY
1127  */
ice_read_mac_reg_eth56g(struct ice_hw * hw,u8 port,u16 offset,u32 * val)1128 static int ice_read_mac_reg_eth56g(struct ice_hw *hw, u8 port, u16 offset,
1129 				   u32 *val)
1130 {
1131 	return ice_read_port_eth56g(hw, port, offset, val, ETH56G_PHY_REG_MAC);
1132 }
1133 
1134 /**
1135  * ice_read_gpcs_reg_eth56g - Read a PHY port register
1136  * @hw: pointer to the HW struct
1137  * @port: Port number to be read
1138  * @offset: Offset from PHY port register base
1139  * @val: Pointer to the value to read (out param)
1140  *
1141  * Return:
1142  * * %0      - success
1143  * * %EINVAL - invalid port number or resource type
1144  * * %other  - failed to read from PHY
1145  */
ice_read_gpcs_reg_eth56g(struct ice_hw * hw,u8 port,u16 offset,u32 * val)1146 static int ice_read_gpcs_reg_eth56g(struct ice_hw *hw, u8 port, u16 offset,
1147 				    u32 *val)
1148 {
1149 	return ice_read_port_eth56g(hw, port, offset, val, ETH56G_PHY_REG_GPCS);
1150 }
1151 
1152 /**
1153  * ice_read_port_mem_eth56g - Read a PHY port memory location
1154  * @hw: pointer to the HW struct
1155  * @port: Port number to be read
1156  * @offset: Offset from PHY port register base
1157  * @val: Pointer to the value to read (out param)
1158  *
1159  * Return:
1160  * * %0      - success
1161  * * %EINVAL - invalid port number or resource type
1162  * * %other  - failed to read from PHY
1163  */
ice_read_port_mem_eth56g(struct ice_hw * hw,u8 port,u16 offset,u32 * val)1164 static int ice_read_port_mem_eth56g(struct ice_hw *hw, u8 port, u16 offset,
1165 				    u32 *val)
1166 {
1167 	return ice_read_port_eth56g(hw, port, offset, val, ETH56G_PHY_MEM_PTP);
1168 }
1169 
1170 /**
1171  * ice_write_port_mem_eth56g - Write a PHY port memory location
1172  * @hw: pointer to the HW struct
1173  * @port: Port number to be read
1174  * @offset: Offset from PHY port register base
1175  * @val: Pointer to the value to read (out param)
1176  *
1177  * Return:
1178  * * %0      - success
1179  * * %EINVAL - invalid port number or resource type
1180  * * %other  - failed to write to PHY
1181  */
ice_write_port_mem_eth56g(struct ice_hw * hw,u8 port,u16 offset,u32 val)1182 static int ice_write_port_mem_eth56g(struct ice_hw *hw, u8 port, u16 offset,
1183 				     u32 val)
1184 {
1185 	return ice_write_port_eth56g(hw, port, offset, val, ETH56G_PHY_MEM_PTP);
1186 }
1187 
1188 /**
1189  * ice_write_quad_ptp_reg_eth56g - Write a PHY quad register
1190  * @hw: pointer to the HW struct
1191  * @offset: PHY register offset
1192  * @port: Port number
1193  * @val: Value to write
1194  *
1195  * Return:
1196  * * %0     - success
1197  * * %EIO  - invalid port number or resource type
1198  * * %other - failed to write to PHY
1199  */
ice_write_quad_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u32 offset,u32 val)1200 static int ice_write_quad_ptp_reg_eth56g(struct ice_hw *hw, u8 port,
1201 					 u32 offset, u32 val)
1202 {
1203 	u32 addr;
1204 
1205 	if (port >= hw->ptp.num_lports)
1206 		return -EIO;
1207 
1208 	addr = eth56g_phy_res[ETH56G_PHY_REG_PTP].base[0] + offset;
1209 
1210 	return ice_write_phy_eth56g(hw, port, addr, val);
1211 }
1212 
1213 /**
1214  * ice_read_quad_ptp_reg_eth56g - Read a PHY quad register
1215  * @hw: pointer to the HW struct
1216  * @offset: PHY register offset
1217  * @port: Port number
1218  * @val: Value to read
1219  *
1220  * Return:
1221  * * %0     - success
1222  * * %EIO  - invalid port number or resource type
1223  * * %other - failed to read from PHY
1224  */
ice_read_quad_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u32 offset,u32 * val)1225 static int ice_read_quad_ptp_reg_eth56g(struct ice_hw *hw, u8 port,
1226 					u32 offset, u32 *val)
1227 {
1228 	u32 addr;
1229 
1230 	if (port >= hw->ptp.num_lports)
1231 		return -EIO;
1232 
1233 	addr = eth56g_phy_res[ETH56G_PHY_REG_PTP].base[0] + offset;
1234 
1235 	return ice_read_phy_eth56g(hw, port, addr, val);
1236 }
1237 
1238 /**
1239  * ice_is_64b_phy_reg_eth56g - Check if this is a 64bit PHY register
1240  * @low_addr: the low address to check
1241  * @high_addr: on return, contains the high address of the 64bit register
1242  *
1243  * Write the appropriate high register offset to use.
1244  *
1245  * Return: true if the provided low address is one of the known 64bit PHY values
1246  * represented as two 32bit registers, false otherwise.
1247  */
ice_is_64b_phy_reg_eth56g(u16 low_addr,u16 * high_addr)1248 static bool ice_is_64b_phy_reg_eth56g(u16 low_addr, u16 *high_addr)
1249 {
1250 	switch (low_addr) {
1251 	case PHY_REG_TX_TIMER_INC_PRE_L:
1252 		*high_addr = PHY_REG_TX_TIMER_INC_PRE_U;
1253 		return true;
1254 	case PHY_REG_RX_TIMER_INC_PRE_L:
1255 		*high_addr = PHY_REG_RX_TIMER_INC_PRE_U;
1256 		return true;
1257 	case PHY_REG_TX_CAPTURE_L:
1258 		*high_addr = PHY_REG_TX_CAPTURE_U;
1259 		return true;
1260 	case PHY_REG_RX_CAPTURE_L:
1261 		*high_addr = PHY_REG_RX_CAPTURE_U;
1262 		return true;
1263 	case PHY_REG_TOTAL_TX_OFFSET_L:
1264 		*high_addr = PHY_REG_TOTAL_TX_OFFSET_U;
1265 		return true;
1266 	case PHY_REG_TOTAL_RX_OFFSET_L:
1267 		*high_addr = PHY_REG_TOTAL_RX_OFFSET_U;
1268 		return true;
1269 	case PHY_REG_TX_MEMORY_STATUS_L:
1270 		*high_addr = PHY_REG_TX_MEMORY_STATUS_U;
1271 		return true;
1272 	default:
1273 		return false;
1274 	}
1275 }
1276 
1277 /**
1278  * ice_is_40b_phy_reg_eth56g - Check if this is a 40bit PHY register
1279  * @low_addr: the low address to check
1280  * @high_addr: on return, contains the high address of the 40bit value
1281  *
1282  * Write the appropriate high register offset to use.
1283  *
1284  * Return: true if the provided low address is one of the known 40bit PHY
1285  * values split into two registers with the lower 8 bits in the low register and
1286  * the upper 32 bits in the high register, false otherwise.
1287  */
ice_is_40b_phy_reg_eth56g(u16 low_addr,u16 * high_addr)1288 static bool ice_is_40b_phy_reg_eth56g(u16 low_addr, u16 *high_addr)
1289 {
1290 	switch (low_addr) {
1291 	case PHY_REG_TIMETUS_L:
1292 		*high_addr = PHY_REG_TIMETUS_U;
1293 		return true;
1294 	case PHY_PCS_REF_TUS_L:
1295 		*high_addr = PHY_PCS_REF_TUS_U;
1296 		return true;
1297 	case PHY_PCS_REF_INC_L:
1298 		*high_addr = PHY_PCS_REF_INC_U;
1299 		return true;
1300 	default:
1301 		return false;
1302 	}
1303 }
1304 
1305 /**
1306  * ice_read_64b_phy_reg_eth56g - Read a 64bit value from PHY registers
1307  * @hw: pointer to the HW struct
1308  * @port: PHY port to read from
1309  * @low_addr: offset of the lower register to read from
1310  * @val: on return, the contents of the 64bit value from the PHY registers
1311  * @res_type: resource type
1312  *
1313  * Check if the caller has specified a known 40 bit register offset and read
1314  * the two registers associated with a 40bit value and return it in the val
1315  * pointer.
1316  *
1317  * Return:
1318  * * %0      - success
1319  * * %EINVAL - not a 64 bit register
1320  * * %other  - failed to read from PHY
1321  */
ice_read_64b_phy_reg_eth56g(struct ice_hw * hw,u8 port,u16 low_addr,u64 * val,enum eth56g_res_type res_type)1322 static int ice_read_64b_phy_reg_eth56g(struct ice_hw *hw, u8 port, u16 low_addr,
1323 				       u64 *val, enum eth56g_res_type res_type)
1324 {
1325 	u16 high_addr;
1326 	u32 lo, hi;
1327 	int err;
1328 
1329 	if (!ice_is_64b_phy_reg_eth56g(low_addr, &high_addr))
1330 		return -EINVAL;
1331 
1332 	err = ice_read_port_eth56g(hw, port, low_addr, &lo, res_type);
1333 	if (err) {
1334 		ice_debug(hw, ICE_DBG_PTP, "Failed to read from low register %#08x\n, err %d",
1335 			  low_addr, err);
1336 		return err;
1337 	}
1338 
1339 	err = ice_read_port_eth56g(hw, port, high_addr, &hi, res_type);
1340 	if (err) {
1341 		ice_debug(hw, ICE_DBG_PTP, "Failed to read from high register %#08x\n, err %d",
1342 			  high_addr, err);
1343 		return err;
1344 	}
1345 
1346 	*val = ((u64)hi << 32) | lo;
1347 
1348 	return 0;
1349 }
1350 
1351 /**
1352  * ice_read_64b_ptp_reg_eth56g - Read a 64bit value from PHY registers
1353  * @hw: pointer to the HW struct
1354  * @port: PHY port to read from
1355  * @low_addr: offset of the lower register to read from
1356  * @val: on return, the contents of the 64bit value from the PHY registers
1357  *
1358  * Check if the caller has specified a known 40 bit register offset and read
1359  * the two registers associated with a 40bit value and return it in the val
1360  * pointer.
1361  *
1362  * Return:
1363  * * %0      - success
1364  * * %EINVAL - not a 64 bit register
1365  * * %other  - failed to read from PHY
1366  */
ice_read_64b_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u16 low_addr,u64 * val)1367 static int ice_read_64b_ptp_reg_eth56g(struct ice_hw *hw, u8 port, u16 low_addr,
1368 				       u64 *val)
1369 {
1370 	return ice_read_64b_phy_reg_eth56g(hw, port, low_addr, val,
1371 					   ETH56G_PHY_REG_PTP);
1372 }
1373 
1374 /**
1375  * ice_write_40b_phy_reg_eth56g - Write a 40b value to the PHY
1376  * @hw: pointer to the HW struct
1377  * @port: port to write to
1378  * @low_addr: offset of the low register
1379  * @val: 40b value to write
1380  * @res_type: resource type
1381  *
1382  * Check if the caller has specified a known 40 bit register offset and write
1383  * provided 40b value to the two associated registers by splitting it up into
1384  * two chunks, the lower 8 bits and the upper 32 bits.
1385  *
1386  * Return:
1387  * * %0      - success
1388  * * %EINVAL - not a 40 bit register
1389  * * %other  - failed to write to PHY
1390  */
ice_write_40b_phy_reg_eth56g(struct ice_hw * hw,u8 port,u16 low_addr,u64 val,enum eth56g_res_type res_type)1391 static int ice_write_40b_phy_reg_eth56g(struct ice_hw *hw, u8 port,
1392 					u16 low_addr, u64 val,
1393 					enum eth56g_res_type res_type)
1394 {
1395 	u16 high_addr;
1396 	u32 lo, hi;
1397 	int err;
1398 
1399 	if (!ice_is_40b_phy_reg_eth56g(low_addr, &high_addr))
1400 		return -EINVAL;
1401 
1402 	lo = FIELD_GET(P_REG_40B_LOW_M, val);
1403 	hi = (u32)(val >> P_REG_40B_HIGH_S);
1404 
1405 	err = ice_write_port_eth56g(hw, port, low_addr, lo, res_type);
1406 	if (err) {
1407 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
1408 			  low_addr, err);
1409 		return err;
1410 	}
1411 
1412 	err = ice_write_port_eth56g(hw, port, high_addr, hi, res_type);
1413 	if (err) {
1414 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
1415 			  high_addr, err);
1416 		return err;
1417 	}
1418 
1419 	return 0;
1420 }
1421 
1422 /**
1423  * ice_write_40b_ptp_reg_eth56g - Write a 40b value to the PHY
1424  * @hw: pointer to the HW struct
1425  * @port: port to write to
1426  * @low_addr: offset of the low register
1427  * @val: 40b value to write
1428  *
1429  * Check if the caller has specified a known 40 bit register offset and write
1430  * provided 40b value to the two associated registers by splitting it up into
1431  * two chunks, the lower 8 bits and the upper 32 bits.
1432  *
1433  * Return:
1434  * * %0      - success
1435  * * %EINVAL - not a 40 bit register
1436  * * %other  - failed to write to PHY
1437  */
ice_write_40b_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u16 low_addr,u64 val)1438 static int ice_write_40b_ptp_reg_eth56g(struct ice_hw *hw, u8 port,
1439 					u16 low_addr, u64 val)
1440 {
1441 	return ice_write_40b_phy_reg_eth56g(hw, port, low_addr, val,
1442 					    ETH56G_PHY_REG_PTP);
1443 }
1444 
1445 /**
1446  * ice_write_64b_phy_reg_eth56g - Write a 64bit value to PHY registers
1447  * @hw: pointer to the HW struct
1448  * @port: PHY port to read from
1449  * @low_addr: offset of the lower register to read from
1450  * @val: the contents of the 64bit value to write to PHY
1451  * @res_type: resource type
1452  *
1453  * Check if the caller has specified a known 64 bit register offset and write
1454  * the 64bit value to the two associated 32bit PHY registers.
1455  *
1456  * Return:
1457  * * %0      - success
1458  * * %EINVAL - not a 64 bit register
1459  * * %other  - failed to write to PHY
1460  */
ice_write_64b_phy_reg_eth56g(struct ice_hw * hw,u8 port,u16 low_addr,u64 val,enum eth56g_res_type res_type)1461 static int ice_write_64b_phy_reg_eth56g(struct ice_hw *hw, u8 port,
1462 					u16 low_addr, u64 val,
1463 					enum eth56g_res_type res_type)
1464 {
1465 	u16 high_addr;
1466 	u32 lo, hi;
1467 	int err;
1468 
1469 	if (!ice_is_64b_phy_reg_eth56g(low_addr, &high_addr))
1470 		return -EINVAL;
1471 
1472 	lo = lower_32_bits(val);
1473 	hi = upper_32_bits(val);
1474 
1475 	err = ice_write_port_eth56g(hw, port, low_addr, lo, res_type);
1476 	if (err) {
1477 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
1478 			  low_addr, err);
1479 		return err;
1480 	}
1481 
1482 	err = ice_write_port_eth56g(hw, port, high_addr, hi, res_type);
1483 	if (err) {
1484 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
1485 			  high_addr, err);
1486 		return err;
1487 	}
1488 
1489 	return 0;
1490 }
1491 
1492 /**
1493  * ice_write_64b_ptp_reg_eth56g - Write a 64bit value to PHY registers
1494  * @hw: pointer to the HW struct
1495  * @port: PHY port to read from
1496  * @low_addr: offset of the lower register to read from
1497  * @val: the contents of the 64bit value to write to PHY
1498  *
1499  * Check if the caller has specified a known 64 bit register offset and write
1500  * the 64bit value to the two associated 32bit PHY registers.
1501  *
1502  * Return:
1503  * * %0      - success
1504  * * %EINVAL - not a 64 bit register
1505  * * %other  - failed to write to PHY
1506  */
ice_write_64b_ptp_reg_eth56g(struct ice_hw * hw,u8 port,u16 low_addr,u64 val)1507 static int ice_write_64b_ptp_reg_eth56g(struct ice_hw *hw, u8 port,
1508 					u16 low_addr, u64 val)
1509 {
1510 	return ice_write_64b_phy_reg_eth56g(hw, port, low_addr, val,
1511 					    ETH56G_PHY_REG_PTP);
1512 }
1513 
1514 /**
1515  * ice_read_ptp_tstamp_eth56g - Read a PHY timestamp out of the port memory
1516  * @hw: pointer to the HW struct
1517  * @port: the port to read from
1518  * @idx: the timestamp index to read
1519  * @tstamp: on return, the 40bit timestamp value
1520  *
1521  * Read a 40bit timestamp value out of the two associated entries in the
1522  * port memory block of the internal PHYs of the 56G devices.
1523  *
1524  * Return:
1525  * * %0     - success
1526  * * %other - failed to read from PHY
1527  */
ice_read_ptp_tstamp_eth56g(struct ice_hw * hw,u8 port,u8 idx,u64 * tstamp)1528 static int ice_read_ptp_tstamp_eth56g(struct ice_hw *hw, u8 port, u8 idx,
1529 				      u64 *tstamp)
1530 {
1531 	u16 lo_addr, hi_addr;
1532 	u32 lo, hi;
1533 	int err;
1534 
1535 	lo_addr = (u16)PHY_TSTAMP_L(idx);
1536 	hi_addr = (u16)PHY_TSTAMP_U(idx);
1537 
1538 	err = ice_read_port_mem_eth56g(hw, port, lo_addr, &lo);
1539 	if (err) {
1540 		ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
1541 			  err);
1542 		return err;
1543 	}
1544 
1545 	err = ice_read_port_mem_eth56g(hw, port, hi_addr, &hi);
1546 	if (err) {
1547 		ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
1548 			  err);
1549 		return err;
1550 	}
1551 
1552 	/* For 56G based internal PHYs, the timestamp is reported with the
1553 	 * lower 8 bits in the low register, and the upper 32 bits in the high
1554 	 * register.
1555 	 */
1556 	*tstamp = FIELD_PREP(TS_PHY_HIGH_M, hi) |
1557 		  FIELD_PREP(TS_PHY_LOW_M, lo);
1558 
1559 	return 0;
1560 }
1561 
1562 /**
1563  * ice_clear_ptp_tstamp_eth56g - Clear a timestamp from the quad block
1564  * @hw: pointer to the HW struct
1565  * @port: the quad to read from
1566  * @idx: the timestamp index to reset
1567  *
1568  * Read and then forcibly clear the timestamp index to ensure the valid bit is
1569  * cleared and the timestamp status bit is reset in the PHY port memory of
1570  * internal PHYs of the 56G devices.
1571  *
1572  * To directly clear the contents of the timestamp block entirely, discarding
1573  * all timestamp data at once, software should instead use
1574  * ice_ptp_reset_ts_memory_quad_eth56g().
1575  *
1576  * This function should only be called on an idx whose bit is set according to
1577  * ice_get_phy_tx_tstamp_ready().
1578  *
1579  * Return:
1580  * * %0     - success
1581  * * %other - failed to write to PHY
1582  */
ice_clear_ptp_tstamp_eth56g(struct ice_hw * hw,u8 port,u8 idx)1583 static int ice_clear_ptp_tstamp_eth56g(struct ice_hw *hw, u8 port, u8 idx)
1584 {
1585 	u64 unused_tstamp;
1586 	u16 lo_addr;
1587 	int err;
1588 
1589 	/* Read the timestamp register to ensure the timestamp status bit is
1590 	 * cleared.
1591 	 */
1592 	err = ice_read_ptp_tstamp_eth56g(hw, port, idx, &unused_tstamp);
1593 	if (err) {
1594 		ice_debug(hw, ICE_DBG_PTP, "Failed to read the PHY timestamp register for port %u, idx %u, err %d\n",
1595 			  port, idx, err);
1596 	}
1597 
1598 	lo_addr = (u16)PHY_TSTAMP_L(idx);
1599 
1600 	err = ice_write_port_mem_eth56g(hw, port, lo_addr, 0);
1601 	if (err) {
1602 		ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register for port %u, idx %u, err %d\n",
1603 			  port, idx, err);
1604 		return err;
1605 	}
1606 
1607 	return 0;
1608 }
1609 
1610 /**
1611  * ice_ptp_reset_ts_memory_eth56g - Clear all timestamps from the port block
1612  * @hw: pointer to the HW struct
1613  */
ice_ptp_reset_ts_memory_eth56g(struct ice_hw * hw)1614 static void ice_ptp_reset_ts_memory_eth56g(struct ice_hw *hw)
1615 {
1616 	unsigned int port;
1617 
1618 	for (port = 0; port < hw->ptp.num_lports; port++) {
1619 		ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_MEMORY_STATUS_L,
1620 					 0);
1621 		ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_MEMORY_STATUS_U,
1622 					 0);
1623 	}
1624 }
1625 
1626 /**
1627  * ice_ptp_prep_port_time_eth56g - Prepare one PHY port with initial time
1628  * @hw: pointer to the HW struct
1629  * @port: port number
1630  * @time: time to initialize the PHY port clocks to
1631  *
1632  * Write a new initial time value into registers of a specific PHY port.
1633  *
1634  * Return:
1635  * * %0     - success
1636  * * %other - failed to write to PHY
1637  */
ice_ptp_prep_port_time_eth56g(struct ice_hw * hw,u8 port,u64 time)1638 static int ice_ptp_prep_port_time_eth56g(struct ice_hw *hw, u8 port,
1639 					 u64 time)
1640 {
1641 	int err;
1642 
1643 	/* Tx case */
1644 	err = ice_write_64b_ptp_reg_eth56g(hw, port, PHY_REG_TX_TIMER_INC_PRE_L,
1645 					   time);
1646 	if (err)
1647 		return err;
1648 
1649 	/* Rx case */
1650 	return ice_write_64b_ptp_reg_eth56g(hw, port,
1651 					    PHY_REG_RX_TIMER_INC_PRE_L, time);
1652 }
1653 
1654 /**
1655  * ice_ptp_prep_phy_time_eth56g - Prepare PHY port with initial time
1656  * @hw: pointer to the HW struct
1657  * @time: Time to initialize the PHY port clocks to
1658  *
1659  * Program the PHY port registers with a new initial time value. The port
1660  * clock will be initialized once the driver issues an ICE_PTP_INIT_TIME sync
1661  * command. The time value is the upper 32 bits of the PHY timer, usually in
1662  * units of nominal nanoseconds.
1663  *
1664  * Return:
1665  * * %0     - success
1666  * * %other - failed to write to PHY
1667  */
ice_ptp_prep_phy_time_eth56g(struct ice_hw * hw,u32 time)1668 static int ice_ptp_prep_phy_time_eth56g(struct ice_hw *hw, u32 time)
1669 {
1670 	u64 phy_time;
1671 	u8 port;
1672 
1673 	/* The time represents the upper 32 bits of the PHY timer, so we need
1674 	 * to shift to account for this when programming.
1675 	 */
1676 	phy_time = (u64)time << 32;
1677 
1678 	for (port = 0; port < hw->ptp.num_lports; port++) {
1679 		int err;
1680 
1681 		err = ice_ptp_prep_port_time_eth56g(hw, port, phy_time);
1682 		if (err) {
1683 			ice_debug(hw, ICE_DBG_PTP, "Failed to write init time for port %u, err %d\n",
1684 				  port, err);
1685 			return err;
1686 		}
1687 	}
1688 
1689 	return 0;
1690 }
1691 
1692 /**
1693  * ice_ptp_prep_port_adj_eth56g - Prepare a single port for time adjust
1694  * @hw: pointer to HW struct
1695  * @port: Port number to be programmed
1696  * @time: time in cycles to adjust the port clocks
1697  *
1698  * Program the port for an atomic adjustment by writing the Tx and Rx timer
1699  * registers. The atomic adjustment won't be completed until the driver issues
1700  * an ICE_PTP_ADJ_TIME command.
1701  *
1702  * Note that time is not in units of nanoseconds. It is in clock time
1703  * including the lower sub-nanosecond portion of the port timer.
1704  *
1705  * Negative adjustments are supported using 2s complement arithmetic.
1706  *
1707  * Return:
1708  * * %0     - success
1709  * * %other - failed to write to PHY
1710  */
ice_ptp_prep_port_adj_eth56g(struct ice_hw * hw,u8 port,s64 time)1711 static int ice_ptp_prep_port_adj_eth56g(struct ice_hw *hw, u8 port, s64 time)
1712 {
1713 	u32 l_time, u_time;
1714 	int err;
1715 
1716 	l_time = lower_32_bits(time);
1717 	u_time = upper_32_bits(time);
1718 
1719 	/* Tx case */
1720 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_TIMER_INC_PRE_L,
1721 				       l_time);
1722 	if (err)
1723 		goto exit_err;
1724 
1725 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_TIMER_INC_PRE_U,
1726 				       u_time);
1727 	if (err)
1728 		goto exit_err;
1729 
1730 	/* Rx case */
1731 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_RX_TIMER_INC_PRE_L,
1732 				       l_time);
1733 	if (err)
1734 		goto exit_err;
1735 
1736 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_RX_TIMER_INC_PRE_U,
1737 				       u_time);
1738 	if (err)
1739 		goto exit_err;
1740 
1741 	return 0;
1742 
1743 exit_err:
1744 	ice_debug(hw, ICE_DBG_PTP, "Failed to write time adjust for port %u, err %d\n",
1745 		  port, err);
1746 	return err;
1747 }
1748 
1749 /**
1750  * ice_ptp_prep_phy_adj_eth56g - Prep PHY ports for a time adjustment
1751  * @hw: pointer to HW struct
1752  * @adj: adjustment in nanoseconds
1753  *
1754  * Prepare the PHY ports for an atomic time adjustment by programming the PHY
1755  * Tx and Rx port registers. The actual adjustment is completed by issuing an
1756  * ICE_PTP_ADJ_TIME or ICE_PTP_ADJ_TIME_AT_TIME sync command.
1757  *
1758  * Return:
1759  * * %0     - success
1760  * * %other - failed to write to PHY
1761  */
ice_ptp_prep_phy_adj_eth56g(struct ice_hw * hw,s32 adj)1762 static int ice_ptp_prep_phy_adj_eth56g(struct ice_hw *hw, s32 adj)
1763 {
1764 	s64 cycles;
1765 	u8 port;
1766 
1767 	/* The port clock supports adjustment of the sub-nanosecond portion of
1768 	 * the clock (lowest 32 bits). We shift the provided adjustment in
1769 	 * nanoseconds by 32 to calculate the appropriate adjustment to program
1770 	 * into the PHY ports.
1771 	 */
1772 	cycles = (s64)adj << 32;
1773 
1774 	for (port = 0; port < hw->ptp.num_lports; port++) {
1775 		int err;
1776 
1777 		err = ice_ptp_prep_port_adj_eth56g(hw, port, cycles);
1778 		if (err)
1779 			return err;
1780 	}
1781 
1782 	return 0;
1783 }
1784 
1785 /**
1786  * ice_ptp_prep_phy_incval_eth56g - Prepare PHY ports for time adjustment
1787  * @hw: pointer to HW struct
1788  * @incval: new increment value to prepare
1789  *
1790  * Prepare each of the PHY ports for a new increment value by programming the
1791  * port's TIMETUS registers. The new increment value will be updated after
1792  * issuing an ICE_PTP_INIT_INCVAL command.
1793  *
1794  * Return:
1795  * * %0     - success
1796  * * %other - failed to write to PHY
1797  */
ice_ptp_prep_phy_incval_eth56g(struct ice_hw * hw,u64 incval)1798 static int ice_ptp_prep_phy_incval_eth56g(struct ice_hw *hw, u64 incval)
1799 {
1800 	u8 port;
1801 
1802 	for (port = 0; port < hw->ptp.num_lports; port++) {
1803 		int err;
1804 
1805 		err = ice_write_40b_ptp_reg_eth56g(hw, port, PHY_REG_TIMETUS_L,
1806 						   incval);
1807 		if (err) {
1808 			ice_debug(hw, ICE_DBG_PTP, "Failed to write incval for port %u, err %d\n",
1809 				  port, err);
1810 			return err;
1811 		}
1812 	}
1813 
1814 	return 0;
1815 }
1816 
1817 /**
1818  * ice_ptp_read_port_capture_eth56g - Read a port's local time capture
1819  * @hw: pointer to HW struct
1820  * @port: Port number to read
1821  * @tx_ts: on return, the Tx port time capture
1822  * @rx_ts: on return, the Rx port time capture
1823  *
1824  * Read the port's Tx and Rx local time capture values.
1825  *
1826  * Return:
1827  * * %0     - success
1828  * * %other - failed to read from PHY
1829  */
ice_ptp_read_port_capture_eth56g(struct ice_hw * hw,u8 port,u64 * tx_ts,u64 * rx_ts)1830 static int ice_ptp_read_port_capture_eth56g(struct ice_hw *hw, u8 port,
1831 					    u64 *tx_ts, u64 *rx_ts)
1832 {
1833 	int err;
1834 
1835 	/* Tx case */
1836 	err = ice_read_64b_ptp_reg_eth56g(hw, port, PHY_REG_TX_CAPTURE_L,
1837 					  tx_ts);
1838 	if (err) {
1839 		ice_debug(hw, ICE_DBG_PTP, "Failed to read REG_TX_CAPTURE, err %d\n",
1840 			  err);
1841 		return err;
1842 	}
1843 
1844 	ice_debug(hw, ICE_DBG_PTP, "tx_init = %#016llx\n", *tx_ts);
1845 
1846 	/* Rx case */
1847 	err = ice_read_64b_ptp_reg_eth56g(hw, port, PHY_REG_RX_CAPTURE_L,
1848 					  rx_ts);
1849 	if (err) {
1850 		ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_CAPTURE, err %d\n",
1851 			  err);
1852 		return err;
1853 	}
1854 
1855 	ice_debug(hw, ICE_DBG_PTP, "rx_init = %#016llx\n", *rx_ts);
1856 
1857 	return 0;
1858 }
1859 
1860 /**
1861  * ice_ptp_write_port_cmd_eth56g - Prepare a single PHY port for a timer command
1862  * @hw: pointer to HW struct
1863  * @port: Port to which cmd has to be sent
1864  * @cmd: Command to be sent to the port
1865  *
1866  * Prepare the requested port for an upcoming timer sync command.
1867  *
1868  * Return:
1869  * * %0     - success
1870  * * %other - failed to write to PHY
1871  */
ice_ptp_write_port_cmd_eth56g(struct ice_hw * hw,u8 port,enum ice_ptp_tmr_cmd cmd)1872 static int ice_ptp_write_port_cmd_eth56g(struct ice_hw *hw, u8 port,
1873 					 enum ice_ptp_tmr_cmd cmd)
1874 {
1875 	u32 val = ice_ptp_tmr_cmd_to_port_reg(hw, cmd);
1876 	int err;
1877 
1878 	/* Tx case */
1879 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_TMR_CMD, val);
1880 	if (err) {
1881 		ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_TMR_CMD, err %d\n",
1882 			  err);
1883 		return err;
1884 	}
1885 
1886 	/* Rx case */
1887 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_RX_TMR_CMD, val);
1888 	if (err) {
1889 		ice_debug(hw, ICE_DBG_PTP, "Failed to write back RX_TMR_CMD, err %d\n",
1890 			  err);
1891 		return err;
1892 	}
1893 
1894 	return 0;
1895 }
1896 
1897 /**
1898  * ice_phy_get_speed_eth56g - Get link speed based on PHY link type
1899  * @li: pointer to link information struct
1900  *
1901  * Return: simplified ETH56G PHY speed
1902  */
1903 static enum ice_eth56g_link_spd
ice_phy_get_speed_eth56g(struct ice_link_status * li)1904 ice_phy_get_speed_eth56g(struct ice_link_status *li)
1905 {
1906 	u16 speed = ice_get_link_speed_based_on_phy_type(li->phy_type_low,
1907 							 li->phy_type_high);
1908 
1909 	switch (speed) {
1910 	case ICE_AQ_LINK_SPEED_1000MB:
1911 		return ICE_ETH56G_LNK_SPD_1G;
1912 	case ICE_AQ_LINK_SPEED_2500MB:
1913 		return ICE_ETH56G_LNK_SPD_2_5G;
1914 	case ICE_AQ_LINK_SPEED_10GB:
1915 		return ICE_ETH56G_LNK_SPD_10G;
1916 	case ICE_AQ_LINK_SPEED_25GB:
1917 		return ICE_ETH56G_LNK_SPD_25G;
1918 	case ICE_AQ_LINK_SPEED_40GB:
1919 		return ICE_ETH56G_LNK_SPD_40G;
1920 	case ICE_AQ_LINK_SPEED_50GB:
1921 		switch (li->phy_type_low) {
1922 		case ICE_PHY_TYPE_LOW_50GBASE_SR:
1923 		case ICE_PHY_TYPE_LOW_50GBASE_FR:
1924 		case ICE_PHY_TYPE_LOW_50GBASE_LR:
1925 		case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
1926 		case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
1927 		case ICE_PHY_TYPE_LOW_50G_AUI1:
1928 			return ICE_ETH56G_LNK_SPD_50G;
1929 		default:
1930 			return ICE_ETH56G_LNK_SPD_50G2;
1931 		}
1932 	case ICE_AQ_LINK_SPEED_100GB:
1933 		if (li->phy_type_high ||
1934 		    li->phy_type_low == ICE_PHY_TYPE_LOW_100GBASE_SR2)
1935 			return ICE_ETH56G_LNK_SPD_100G2;
1936 		else
1937 			return ICE_ETH56G_LNK_SPD_100G;
1938 	default:
1939 		return ICE_ETH56G_LNK_SPD_1G;
1940 	}
1941 }
1942 
1943 /**
1944  * ice_phy_cfg_parpcs_eth56g - Configure TUs per PAR/PCS clock cycle
1945  * @hw: pointer to the HW struct
1946  * @port: port to configure
1947  *
1948  * Configure the number of TUs for the PAR and PCS clocks used as part of the
1949  * timestamp calibration process.
1950  *
1951  * Return:
1952  * * %0     - success
1953  * * %other - PHY read/write failed
1954  */
ice_phy_cfg_parpcs_eth56g(struct ice_hw * hw,u8 port)1955 static int ice_phy_cfg_parpcs_eth56g(struct ice_hw *hw, u8 port)
1956 {
1957 	u32 val;
1958 	int err;
1959 
1960 	err = ice_write_xpcs_reg_eth56g(hw, port, PHY_VENDOR_TXLANE_THRESH,
1961 					ICE_ETH56G_NOMINAL_THRESH4);
1962 	if (err) {
1963 		ice_debug(hw, ICE_DBG_PTP, "Failed to read VENDOR_TXLANE_THRESH, status: %d",
1964 			  err);
1965 		return err;
1966 	}
1967 
1968 	switch (ice_phy_get_speed_eth56g(&hw->port_info->phy.link_info)) {
1969 	case ICE_ETH56G_LNK_SPD_1G:
1970 	case ICE_ETH56G_LNK_SPD_2_5G:
1971 		err = ice_read_quad_ptp_reg_eth56g(hw, port,
1972 						   PHY_GPCS_CONFIG_REG0, &val);
1973 		if (err) {
1974 			ice_debug(hw, ICE_DBG_PTP, "Failed to read PHY_GPCS_CONFIG_REG0, status: %d",
1975 				  err);
1976 			return err;
1977 		}
1978 
1979 		val &= ~PHY_GPCS_CONFIG_REG0_TX_THR_M;
1980 		val |= FIELD_PREP(PHY_GPCS_CONFIG_REG0_TX_THR_M,
1981 				  ICE_ETH56G_NOMINAL_TX_THRESH);
1982 
1983 		err = ice_write_quad_ptp_reg_eth56g(hw, port,
1984 						    PHY_GPCS_CONFIG_REG0, val);
1985 		if (err) {
1986 			ice_debug(hw, ICE_DBG_PTP, "Failed to write PHY_GPCS_CONFIG_REG0, status: %d",
1987 				  err);
1988 			return err;
1989 		}
1990 		break;
1991 	default:
1992 		break;
1993 	}
1994 
1995 	err = ice_write_40b_ptp_reg_eth56g(hw, port, PHY_PCS_REF_TUS_L,
1996 					   ICE_ETH56G_NOMINAL_PCS_REF_TUS);
1997 	if (err) {
1998 		ice_debug(hw, ICE_DBG_PTP, "Failed to write PHY_PCS_REF_TUS, status: %d",
1999 			  err);
2000 		return err;
2001 	}
2002 
2003 	err = ice_write_40b_ptp_reg_eth56g(hw, port, PHY_PCS_REF_INC_L,
2004 					   ICE_ETH56G_NOMINAL_PCS_REF_INC);
2005 	if (err) {
2006 		ice_debug(hw, ICE_DBG_PTP, "Failed to write PHY_PCS_REF_INC, status: %d",
2007 			  err);
2008 		return err;
2009 	}
2010 
2011 	return 0;
2012 }
2013 
2014 /**
2015  * ice_phy_cfg_ptp_1step_eth56g - Configure 1-step PTP settings
2016  * @hw: Pointer to the HW struct
2017  * @port: Port to configure
2018  *
2019  * Return:
2020  * * %0     - success
2021  * * %other - PHY read/write failed
2022  */
ice_phy_cfg_ptp_1step_eth56g(struct ice_hw * hw,u8 port)2023 int ice_phy_cfg_ptp_1step_eth56g(struct ice_hw *hw, u8 port)
2024 {
2025 	u8 quad_lane = port % ICE_PORTS_PER_QUAD;
2026 	u32 addr, val, peer_delay;
2027 	bool enable, sfd_ena;
2028 	int err;
2029 
2030 	enable = hw->ptp.phy.eth56g.onestep_ena;
2031 	peer_delay = hw->ptp.phy.eth56g.peer_delay;
2032 	sfd_ena = hw->ptp.phy.eth56g.sfd_ena;
2033 
2034 	addr = PHY_PTP_1STEP_CONFIG;
2035 	err = ice_read_quad_ptp_reg_eth56g(hw, port, addr, &val);
2036 	if (err)
2037 		return err;
2038 
2039 	if (enable)
2040 		val |= BIT(quad_lane);
2041 	else
2042 		val &= ~BIT(quad_lane);
2043 
2044 	val &= ~(PHY_PTP_1STEP_T1S_UP64_M | PHY_PTP_1STEP_T1S_DELTA_M);
2045 
2046 	err = ice_write_quad_ptp_reg_eth56g(hw, port, addr, val);
2047 	if (err)
2048 		return err;
2049 
2050 	addr = PHY_PTP_1STEP_PEER_DELAY(quad_lane);
2051 	val = FIELD_PREP(PHY_PTP_1STEP_PD_DELAY_M, peer_delay);
2052 	if (peer_delay)
2053 		val |= PHY_PTP_1STEP_PD_ADD_PD_M;
2054 	val |= PHY_PTP_1STEP_PD_DLY_V_M;
2055 	err = ice_write_quad_ptp_reg_eth56g(hw, port, addr, val);
2056 	if (err)
2057 		return err;
2058 
2059 	val &= ~PHY_PTP_1STEP_PD_DLY_V_M;
2060 	err = ice_write_quad_ptp_reg_eth56g(hw, port, addr, val);
2061 	if (err)
2062 		return err;
2063 
2064 	addr = PHY_MAC_XIF_MODE;
2065 	err = ice_read_mac_reg_eth56g(hw, port, addr, &val);
2066 	if (err)
2067 		return err;
2068 
2069 	val &= ~(PHY_MAC_XIF_1STEP_ENA_M | PHY_MAC_XIF_TS_BIN_MODE_M |
2070 		 PHY_MAC_XIF_TS_SFD_ENA_M | PHY_MAC_XIF_GMII_TS_SEL_M);
2071 
2072 	switch (ice_phy_get_speed_eth56g(&hw->port_info->phy.link_info)) {
2073 	case ICE_ETH56G_LNK_SPD_1G:
2074 	case ICE_ETH56G_LNK_SPD_2_5G:
2075 		val |= PHY_MAC_XIF_GMII_TS_SEL_M;
2076 		break;
2077 	default:
2078 		break;
2079 	}
2080 
2081 	val |= FIELD_PREP(PHY_MAC_XIF_1STEP_ENA_M, enable) |
2082 	       FIELD_PREP(PHY_MAC_XIF_TS_BIN_MODE_M, enable) |
2083 	       FIELD_PREP(PHY_MAC_XIF_TS_SFD_ENA_M, sfd_ena);
2084 
2085 	return ice_write_mac_reg_eth56g(hw, port, addr, val);
2086 }
2087 
2088 /**
2089  * mul_u32_u32_fx_q9 - Multiply two u32 fixed point Q9 values
2090  * @a: multiplier value
2091  * @b: multiplicand value
2092  *
2093  * Return: result of multiplication
2094  */
mul_u32_u32_fx_q9(u32 a,u32 b)2095 static u32 mul_u32_u32_fx_q9(u32 a, u32 b)
2096 {
2097 	return (u32)(((u64)a * b) >> ICE_ETH56G_MAC_CFG_FRAC_W);
2098 }
2099 
2100 /**
2101  * add_u32_u32_fx - Add two u32 fixed point values and discard overflow
2102  * @a: first value
2103  * @b: second value
2104  *
2105  * Return: result of addition
2106  */
add_u32_u32_fx(u32 a,u32 b)2107 static u32 add_u32_u32_fx(u32 a, u32 b)
2108 {
2109 	return lower_32_bits(((u64)a + b));
2110 }
2111 
2112 /**
2113  * ice_ptp_calc_bitslip_eth56g - Calculate bitslip value
2114  * @hw: pointer to the HW struct
2115  * @port: port to configure
2116  * @bs: bitslip multiplier
2117  * @fc: FC-FEC enabled
2118  * @rs: RS-FEC enabled
2119  * @spd: link speed
2120  *
2121  * Return: calculated bitslip value
2122  */
ice_ptp_calc_bitslip_eth56g(struct ice_hw * hw,u8 port,u32 bs,bool fc,bool rs,enum ice_eth56g_link_spd spd)2123 static u32 ice_ptp_calc_bitslip_eth56g(struct ice_hw *hw, u8 port, u32 bs,
2124 				       bool fc, bool rs,
2125 				       enum ice_eth56g_link_spd spd)
2126 {
2127 	u32 bitslip;
2128 	int err;
2129 
2130 	if (!bs || rs)
2131 		return 0;
2132 
2133 	if (spd == ICE_ETH56G_LNK_SPD_1G || spd == ICE_ETH56G_LNK_SPD_2_5G) {
2134 		err = ice_read_gpcs_reg_eth56g(hw, port, PHY_GPCS_BITSLIP,
2135 					       &bitslip);
2136 	} else {
2137 		u8 quad_lane = port % ICE_PORTS_PER_QUAD;
2138 		u32 addr;
2139 
2140 		addr = PHY_REG_SD_BIT_SLIP(quad_lane);
2141 		err = ice_read_quad_ptp_reg_eth56g(hw, port, addr, &bitslip);
2142 	}
2143 	if (err)
2144 		return 0;
2145 
2146 	if (spd == ICE_ETH56G_LNK_SPD_1G && !bitslip) {
2147 		/* Bitslip register value of 0 corresponds to 10 so substitute
2148 		 * it for calculations
2149 		 */
2150 		bitslip = 10;
2151 	} else if (spd == ICE_ETH56G_LNK_SPD_10G ||
2152 		   spd == ICE_ETH56G_LNK_SPD_25G) {
2153 		if (fc)
2154 			bitslip = bitslip * 2 + 32;
2155 		else
2156 			bitslip = (u32)((s32)bitslip * -1 + 20);
2157 	}
2158 
2159 	bitslip <<= ICE_ETH56G_MAC_CFG_FRAC_W;
2160 	return mul_u32_u32_fx_q9(bitslip, bs);
2161 }
2162 
2163 /**
2164  * ice_ptp_calc_deskew_eth56g - Calculate deskew value
2165  * @hw: pointer to the HW struct
2166  * @port: port to configure
2167  * @ds: deskew multiplier
2168  * @rs: RS-FEC enabled
2169  * @spd: link speed
2170  *
2171  * Return: calculated deskew value
2172  */
ice_ptp_calc_deskew_eth56g(struct ice_hw * hw,u8 port,u32 ds,bool rs,enum ice_eth56g_link_spd spd)2173 static u32 ice_ptp_calc_deskew_eth56g(struct ice_hw *hw, u8 port, u32 ds,
2174 				      bool rs, enum ice_eth56g_link_spd spd)
2175 {
2176 	u32 deskew_i, deskew_f;
2177 	int err;
2178 
2179 	if (!ds)
2180 		return 0;
2181 
2182 	read_poll_timeout(ice_read_ptp_reg_eth56g, err,
2183 			  FIELD_GET(PHY_REG_DESKEW_0_VALID, deskew_i), 500,
2184 			  50 * USEC_PER_MSEC, false, hw, port, PHY_REG_DESKEW_0,
2185 			  &deskew_i);
2186 	if (err)
2187 		return err;
2188 
2189 	deskew_f = FIELD_GET(PHY_REG_DESKEW_0_RLEVEL_FRAC, deskew_i);
2190 	deskew_i = FIELD_GET(PHY_REG_DESKEW_0_RLEVEL, deskew_i);
2191 
2192 	if (rs && spd == ICE_ETH56G_LNK_SPD_50G2)
2193 		ds = 0x633; /* 3.1 */
2194 	else if (rs && spd == ICE_ETH56G_LNK_SPD_100G)
2195 		ds = 0x31b; /* 1.552 */
2196 
2197 	deskew_i = FIELD_PREP(ICE_ETH56G_MAC_CFG_RX_OFFSET_INT, deskew_i);
2198 	/* Shift 3 fractional bits to the end of the integer part */
2199 	deskew_f <<= ICE_ETH56G_MAC_CFG_FRAC_W - PHY_REG_DESKEW_0_RLEVEL_FRAC_W;
2200 	return mul_u32_u32_fx_q9(deskew_i | deskew_f, ds);
2201 }
2202 
2203 /**
2204  * ice_phy_set_offsets_eth56g - Set Tx/Rx offset values
2205  * @hw: pointer to the HW struct
2206  * @port: port to configure
2207  * @spd: link speed
2208  * @cfg: structure to store output values
2209  * @fc: FC-FEC enabled
2210  * @rs: RS-FEC enabled
2211  *
2212  * Return:
2213  * * %0     - success
2214  * * %other - failed to write to PHY
2215  */
ice_phy_set_offsets_eth56g(struct ice_hw * hw,u8 port,enum ice_eth56g_link_spd spd,const struct ice_eth56g_mac_reg_cfg * cfg,bool fc,bool rs)2216 static int ice_phy_set_offsets_eth56g(struct ice_hw *hw, u8 port,
2217 				      enum ice_eth56g_link_spd spd,
2218 				      const struct ice_eth56g_mac_reg_cfg *cfg,
2219 				      bool fc, bool rs)
2220 {
2221 	u32 rx_offset, tx_offset, bs_ds;
2222 	bool onestep, sfd;
2223 
2224 	onestep = hw->ptp.phy.eth56g.onestep_ena;
2225 	sfd = hw->ptp.phy.eth56g.sfd_ena;
2226 	bs_ds = cfg->rx_offset.bs_ds;
2227 
2228 	if (fc)
2229 		rx_offset = cfg->rx_offset.fc;
2230 	else if (rs)
2231 		rx_offset = cfg->rx_offset.rs;
2232 	else
2233 		rx_offset = cfg->rx_offset.no_fec;
2234 
2235 	rx_offset = add_u32_u32_fx(rx_offset, cfg->rx_offset.serdes);
2236 	if (sfd)
2237 		rx_offset = add_u32_u32_fx(rx_offset, cfg->rx_offset.sfd);
2238 
2239 	if (spd < ICE_ETH56G_LNK_SPD_40G)
2240 		bs_ds = ice_ptp_calc_bitslip_eth56g(hw, port, bs_ds, fc, rs,
2241 						    spd);
2242 	else
2243 		bs_ds = ice_ptp_calc_deskew_eth56g(hw, port, bs_ds, rs, spd);
2244 	rx_offset = add_u32_u32_fx(rx_offset, bs_ds);
2245 	rx_offset &= ICE_ETH56G_MAC_CFG_RX_OFFSET_INT |
2246 		     ICE_ETH56G_MAC_CFG_RX_OFFSET_FRAC;
2247 
2248 	if (fc)
2249 		tx_offset = cfg->tx_offset.fc;
2250 	else if (rs)
2251 		tx_offset = cfg->tx_offset.rs;
2252 	else
2253 		tx_offset = cfg->tx_offset.no_fec;
2254 	tx_offset += cfg->tx_offset.serdes + cfg->tx_offset.sfd * sfd +
2255 		     cfg->tx_offset.onestep * onestep;
2256 
2257 	ice_write_mac_reg_eth56g(hw, port, PHY_MAC_RX_OFFSET, rx_offset);
2258 	return ice_write_mac_reg_eth56g(hw, port, PHY_MAC_TX_OFFSET, tx_offset);
2259 }
2260 
2261 /**
2262  * ice_phy_cfg_mac_eth56g - Configure MAC for PTP
2263  * @hw: Pointer to the HW struct
2264  * @port: Port to configure
2265  *
2266  * Return:
2267  * * %0     - success
2268  * * %other - failed to write to PHY
2269  */
ice_phy_cfg_mac_eth56g(struct ice_hw * hw,u8 port)2270 static int ice_phy_cfg_mac_eth56g(struct ice_hw *hw, u8 port)
2271 {
2272 	const struct ice_eth56g_mac_reg_cfg *cfg;
2273 	enum ice_eth56g_link_spd spd;
2274 	struct ice_link_status *li;
2275 	bool fc = false;
2276 	bool rs = false;
2277 	bool onestep;
2278 	u32 val;
2279 	int err;
2280 
2281 	onestep = hw->ptp.phy.eth56g.onestep_ena;
2282 	li = &hw->port_info->phy.link_info;
2283 	spd = ice_phy_get_speed_eth56g(li);
2284 	if (!!(li->an_info & ICE_AQ_FEC_EN)) {
2285 		if (spd == ICE_ETH56G_LNK_SPD_10G) {
2286 			fc = true;
2287 		} else {
2288 			fc = !!(li->fec_info & ICE_AQ_LINK_25G_KR_FEC_EN);
2289 			rs = !!(li->fec_info & ~ICE_AQ_LINK_25G_KR_FEC_EN);
2290 		}
2291 	}
2292 	cfg = ð56g_mac_cfg[spd];
2293 
2294 	err = ice_write_mac_reg_eth56g(hw, port, PHY_MAC_RX_MODULO, 0);
2295 	if (err)
2296 		return err;
2297 
2298 	err = ice_write_mac_reg_eth56g(hw, port, PHY_MAC_TX_MODULO, 0);
2299 	if (err)
2300 		return err;
2301 
2302 	val = FIELD_PREP(PHY_MAC_TSU_CFG_TX_MODE_M,
2303 			 cfg->tx_mode.def + rs * cfg->tx_mode.rs) |
2304 	      FIELD_PREP(PHY_MAC_TSU_CFG_TX_MII_MK_DLY_M, cfg->tx_mk_dly) |
2305 	      FIELD_PREP(PHY_MAC_TSU_CFG_TX_MII_CW_DLY_M,
2306 			 cfg->tx_cw_dly.def +
2307 			 onestep * cfg->tx_cw_dly.onestep) |
2308 	      FIELD_PREP(PHY_MAC_TSU_CFG_RX_MODE_M,
2309 			 cfg->rx_mode.def + rs * cfg->rx_mode.rs) |
2310 	      FIELD_PREP(PHY_MAC_TSU_CFG_RX_MII_MK_DLY_M,
2311 			 cfg->rx_mk_dly.def + rs * cfg->rx_mk_dly.rs) |
2312 	      FIELD_PREP(PHY_MAC_TSU_CFG_RX_MII_CW_DLY_M,
2313 			 cfg->rx_cw_dly.def + rs * cfg->rx_cw_dly.rs) |
2314 	      FIELD_PREP(PHY_MAC_TSU_CFG_BLKS_PER_CLK_M, cfg->blks_per_clk);
2315 	err = ice_write_mac_reg_eth56g(hw, port, PHY_MAC_TSU_CONFIG, val);
2316 	if (err)
2317 		return err;
2318 
2319 	err = ice_write_mac_reg_eth56g(hw, port, PHY_MAC_BLOCKTIME,
2320 				       cfg->blktime);
2321 	if (err)
2322 		return err;
2323 
2324 	err = ice_phy_set_offsets_eth56g(hw, port, spd, cfg, fc, rs);
2325 	if (err)
2326 		return err;
2327 
2328 	if (spd == ICE_ETH56G_LNK_SPD_25G && !rs)
2329 		val = 0;
2330 	else
2331 		val = cfg->mktime;
2332 
2333 	return ice_write_mac_reg_eth56g(hw, port, PHY_MAC_MARKERTIME, val);
2334 }
2335 
2336 /**
2337  * ice_phy_cfg_intr_eth56g - Configure TX timestamp interrupt
2338  * @hw: pointer to the HW struct
2339  * @port: the timestamp port
2340  * @ena: enable or disable interrupt
2341  * @threshold: interrupt threshold
2342  *
2343  * Configure TX timestamp interrupt for the specified port
2344  *
2345  * Return:
2346  * * %0     - success
2347  * * %other - PHY read/write failed
2348  */
ice_phy_cfg_intr_eth56g(struct ice_hw * hw,u8 port,bool ena,u8 threshold)2349 int ice_phy_cfg_intr_eth56g(struct ice_hw *hw, u8 port, bool ena, u8 threshold)
2350 {
2351 	int err;
2352 	u32 val;
2353 
2354 	err = ice_read_ptp_reg_eth56g(hw, port, PHY_REG_TS_INT_CONFIG, &val);
2355 	if (err)
2356 		return err;
2357 
2358 	if (ena) {
2359 		val |= PHY_TS_INT_CONFIG_ENA_M;
2360 		val &= ~PHY_TS_INT_CONFIG_THRESHOLD_M;
2361 		val |= FIELD_PREP(PHY_TS_INT_CONFIG_THRESHOLD_M, threshold);
2362 	} else {
2363 		val &= ~PHY_TS_INT_CONFIG_ENA_M;
2364 	}
2365 
2366 	return ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TS_INT_CONFIG, val);
2367 }
2368 
2369 /**
2370  * ice_read_phy_and_phc_time_eth56g - Simultaneously capture PHC and PHY time
2371  * @hw: pointer to the HW struct
2372  * @port: the PHY port to read
2373  * @phy_time: on return, the 64bit PHY timer value
2374  * @phc_time: on return, the lower 64bits of PHC time
2375  *
2376  * Issue a ICE_PTP_READ_TIME timer command to simultaneously capture the PHY
2377  * and PHC timer values.
2378  *
2379  * Return:
2380  * * %0     - success
2381  * * %other - PHY read/write failed
2382  */
ice_read_phy_and_phc_time_eth56g(struct ice_hw * hw,u8 port,u64 * phy_time,u64 * phc_time)2383 static int ice_read_phy_and_phc_time_eth56g(struct ice_hw *hw, u8 port,
2384 					    u64 *phy_time, u64 *phc_time)
2385 {
2386 	u64 tx_time, rx_time;
2387 	u32 zo, lo;
2388 	u8 tmr_idx;
2389 	int err;
2390 
2391 	tmr_idx = ice_get_ptp_src_clock_index(hw);
2392 
2393 	/* Prepare the PHC timer for a ICE_PTP_READ_TIME capture command */
2394 	ice_ptp_src_cmd(hw, ICE_PTP_READ_TIME);
2395 
2396 	/* Prepare the PHY timer for a ICE_PTP_READ_TIME capture command */
2397 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_READ_TIME);
2398 	if (err)
2399 		return err;
2400 
2401 	/* Issue the sync to start the ICE_PTP_READ_TIME capture */
2402 	ice_ptp_exec_tmr_cmd(hw);
2403 
2404 	/* Read the captured PHC time from the shadow time registers */
2405 	zo = rd32(hw, GLTSYN_SHTIME_0(tmr_idx));
2406 	lo = rd32(hw, GLTSYN_SHTIME_L(tmr_idx));
2407 	*phc_time = (u64)lo << 32 | zo;
2408 
2409 	/* Read the captured PHY time from the PHY shadow registers */
2410 	err = ice_ptp_read_port_capture_eth56g(hw, port, &tx_time, &rx_time);
2411 	if (err)
2412 		return err;
2413 
2414 	/* If the PHY Tx and Rx timers don't match, log a warning message.
2415 	 * Note that this should not happen in normal circumstances since the
2416 	 * driver always programs them together.
2417 	 */
2418 	if (tx_time != rx_time)
2419 		dev_warn(ice_hw_to_dev(hw), "PHY port %u Tx and Rx timers do not match, tx_time 0x%016llX, rx_time 0x%016llX\n",
2420 			 port, tx_time, rx_time);
2421 
2422 	*phy_time = tx_time;
2423 
2424 	return 0;
2425 }
2426 
2427 /**
2428  * ice_sync_phy_timer_eth56g - Synchronize the PHY timer with PHC timer
2429  * @hw: pointer to the HW struct
2430  * @port: the PHY port to synchronize
2431  *
2432  * Perform an adjustment to ensure that the PHY and PHC timers are in sync.
2433  * This is done by issuing a ICE_PTP_READ_TIME command which triggers a
2434  * simultaneous read of the PHY timer and PHC timer. Then we use the
2435  * difference to calculate an appropriate 2s complement addition to add
2436  * to the PHY timer in order to ensure it reads the same value as the
2437  * primary PHC timer.
2438  *
2439  * Return:
2440  * * %0     - success
2441  * * %-EBUSY- failed to acquire PTP semaphore
2442  * * %other - PHY read/write failed
2443  */
ice_sync_phy_timer_eth56g(struct ice_hw * hw,u8 port)2444 static int ice_sync_phy_timer_eth56g(struct ice_hw *hw, u8 port)
2445 {
2446 	u64 phc_time, phy_time, difference;
2447 	int err;
2448 
2449 	if (!ice_ptp_lock(hw)) {
2450 		ice_debug(hw, ICE_DBG_PTP, "Failed to acquire PTP semaphore\n");
2451 		return -EBUSY;
2452 	}
2453 
2454 	err = ice_read_phy_and_phc_time_eth56g(hw, port, &phy_time, &phc_time);
2455 	if (err)
2456 		goto err_unlock;
2457 
2458 	/* Calculate the amount required to add to the port time in order for
2459 	 * it to match the PHC time.
2460 	 *
2461 	 * Note that the port adjustment is done using 2s complement
2462 	 * arithmetic. This is convenient since it means that we can simply
2463 	 * calculate the difference between the PHC time and the port time,
2464 	 * and it will be interpreted correctly.
2465 	 */
2466 
2467 	ice_ptp_src_cmd(hw, ICE_PTP_NOP);
2468 	difference = phc_time - phy_time;
2469 
2470 	err = ice_ptp_prep_port_adj_eth56g(hw, port, (s64)difference);
2471 	if (err)
2472 		goto err_unlock;
2473 
2474 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_ADJ_TIME);
2475 	if (err)
2476 		goto err_unlock;
2477 
2478 	/* Issue the sync to activate the time adjustment */
2479 	ice_ptp_exec_tmr_cmd(hw);
2480 
2481 	/* Re-capture the timer values to flush the command registers and
2482 	 * verify that the time was properly adjusted.
2483 	 */
2484 	err = ice_read_phy_and_phc_time_eth56g(hw, port, &phy_time, &phc_time);
2485 	if (err)
2486 		goto err_unlock;
2487 
2488 	dev_info(ice_hw_to_dev(hw),
2489 		 "Port %u PHY time synced to PHC: 0x%016llX, 0x%016llX\n",
2490 		 port, phy_time, phc_time);
2491 
2492 err_unlock:
2493 	ice_ptp_unlock(hw);
2494 	return err;
2495 }
2496 
2497 /**
2498  * ice_stop_phy_timer_eth56g - Stop the PHY clock timer
2499  * @hw: pointer to the HW struct
2500  * @port: the PHY port to stop
2501  * @soft_reset: if true, hold the SOFT_RESET bit of PHY_REG_PS
2502  *
2503  * Stop the clock of a PHY port. This must be done as part of the flow to
2504  * re-calibrate Tx and Rx timestamping offsets whenever the clock time is
2505  * initialized or when link speed changes.
2506  *
2507  * Return:
2508  * * %0     - success
2509  * * %other - failed to write to PHY
2510  */
ice_stop_phy_timer_eth56g(struct ice_hw * hw,u8 port,bool soft_reset)2511 int ice_stop_phy_timer_eth56g(struct ice_hw *hw, u8 port, bool soft_reset)
2512 {
2513 	int err;
2514 
2515 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_OFFSET_READY, 0);
2516 	if (err)
2517 		return err;
2518 
2519 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_RX_OFFSET_READY, 0);
2520 	if (err)
2521 		return err;
2522 
2523 	ice_debug(hw, ICE_DBG_PTP, "Disabled clock on PHY port %u\n", port);
2524 
2525 	return 0;
2526 }
2527 
2528 /**
2529  * ice_start_phy_timer_eth56g - Start the PHY clock timer
2530  * @hw: pointer to the HW struct
2531  * @port: the PHY port to start
2532  *
2533  * Start the clock of a PHY port. This must be done as part of the flow to
2534  * re-calibrate Tx and Rx timestamping offsets whenever the clock time is
2535  * initialized or when link speed changes.
2536  *
2537  * Return:
2538  * * %0     - success
2539  * * %other - PHY read/write failed
2540  */
ice_start_phy_timer_eth56g(struct ice_hw * hw,u8 port)2541 int ice_start_phy_timer_eth56g(struct ice_hw *hw, u8 port)
2542 {
2543 	u32 lo, hi;
2544 	u64 incval;
2545 	u8 tmr_idx;
2546 	int err;
2547 
2548 	tmr_idx = ice_get_ptp_src_clock_index(hw);
2549 
2550 	err = ice_stop_phy_timer_eth56g(hw, port, false);
2551 	if (err)
2552 		return err;
2553 
2554 	ice_ptp_src_cmd(hw, ICE_PTP_NOP);
2555 
2556 	err = ice_phy_cfg_parpcs_eth56g(hw, port);
2557 	if (err)
2558 		return err;
2559 
2560 	err = ice_phy_cfg_ptp_1step_eth56g(hw, port);
2561 	if (err)
2562 		return err;
2563 
2564 	err = ice_phy_cfg_mac_eth56g(hw, port);
2565 	if (err)
2566 		return err;
2567 
2568 	lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
2569 	hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
2570 	incval = (u64)hi << 32 | lo;
2571 
2572 	err = ice_write_40b_ptp_reg_eth56g(hw, port, PHY_REG_TIMETUS_L, incval);
2573 	if (err)
2574 		return err;
2575 
2576 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_INIT_INCVAL);
2577 	if (err)
2578 		return err;
2579 
2580 	ice_ptp_exec_tmr_cmd(hw);
2581 
2582 	err = ice_sync_phy_timer_eth56g(hw, port);
2583 	if (err)
2584 		return err;
2585 
2586 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_TX_OFFSET_READY, 1);
2587 	if (err)
2588 		return err;
2589 
2590 	err = ice_write_ptp_reg_eth56g(hw, port, PHY_REG_RX_OFFSET_READY, 1);
2591 	if (err)
2592 		return err;
2593 
2594 	ice_debug(hw, ICE_DBG_PTP, "Enabled clock on PHY port %u\n", port);
2595 
2596 	return 0;
2597 }
2598 
2599 /**
2600  * ice_sb_access_ena_eth56g - Enable SB devices (PHY and others) access
2601  * @hw: pointer to HW struct
2602  * @enable: Enable or disable access
2603  *
2604  * Enable sideband devices (PHY and others) access.
2605  */
ice_sb_access_ena_eth56g(struct ice_hw * hw,bool enable)2606 static void ice_sb_access_ena_eth56g(struct ice_hw *hw, bool enable)
2607 {
2608 	u32 val = rd32(hw, PF_SB_REM_DEV_CTL);
2609 
2610 	if (enable)
2611 		val |= BIT(eth56g_phy_0) | BIT(cgu) | BIT(eth56g_phy_1);
2612 	else
2613 		val &= ~(BIT(eth56g_phy_0) | BIT(cgu) | BIT(eth56g_phy_1));
2614 
2615 	wr32(hw, PF_SB_REM_DEV_CTL, val);
2616 }
2617 
2618 /**
2619  * ice_ptp_init_phc_eth56g - Perform E82X specific PHC initialization
2620  * @hw: pointer to HW struct
2621  *
2622  * Perform PHC initialization steps specific to E82X devices.
2623  *
2624  * Return:
2625  * * %0     - success
2626  * * %other - failed to initialize CGU
2627  */
ice_ptp_init_phc_eth56g(struct ice_hw * hw)2628 static int ice_ptp_init_phc_eth56g(struct ice_hw *hw)
2629 {
2630 	ice_sb_access_ena_eth56g(hw, true);
2631 	/* Initialize the Clock Generation Unit */
2632 	return ice_init_cgu_e82x(hw);
2633 }
2634 
2635 /**
2636  * ice_ptp_read_tx_hwtstamp_status_eth56g - Get TX timestamp status
2637  * @hw: pointer to the HW struct
2638  * @ts_status: the timestamp mask pointer
2639  *
2640  * Read the PHY Tx timestamp status mask indicating which ports have Tx
2641  * timestamps available.
2642  *
2643  * Return:
2644  * * %0     - success
2645  * * %other - failed to read from PHY
2646  */
ice_ptp_read_tx_hwtstamp_status_eth56g(struct ice_hw * hw,u32 * ts_status)2647 int ice_ptp_read_tx_hwtstamp_status_eth56g(struct ice_hw *hw, u32 *ts_status)
2648 {
2649 	const struct ice_eth56g_params *params = &hw->ptp.phy.eth56g;
2650 	u8 phy, mask;
2651 	u32 status;
2652 
2653 	mask = (1 << hw->ptp.ports_per_phy) - 1;
2654 	*ts_status = 0;
2655 
2656 	for (phy = 0; phy < params->num_phys; phy++) {
2657 		int err;
2658 
2659 		err = ice_read_phy_eth56g(hw, phy, PHY_PTP_INT_STATUS, &status);
2660 		if (err)
2661 			return err;
2662 
2663 		*ts_status |= (status & mask) << (phy * hw->ptp.ports_per_phy);
2664 	}
2665 
2666 	ice_debug(hw, ICE_DBG_PTP, "PHY interrupt err: %x\n", *ts_status);
2667 
2668 	return 0;
2669 }
2670 
2671 /**
2672  * ice_get_phy_tx_tstamp_ready_eth56g - Read the Tx memory status register
2673  * @hw: pointer to the HW struct
2674  * @port: the PHY port to read from
2675  * @tstamp_ready: contents of the Tx memory status register
2676  *
2677  * Read the PHY_REG_TX_MEMORY_STATUS register indicating which timestamps in
2678  * the PHY are ready. A set bit means the corresponding timestamp is valid and
2679  * ready to be captured from the PHY timestamp block.
2680  *
2681  * Return:
2682  * * %0     - success
2683  * * %other - failed to read from PHY
2684  */
ice_get_phy_tx_tstamp_ready_eth56g(struct ice_hw * hw,u8 port,u64 * tstamp_ready)2685 static int ice_get_phy_tx_tstamp_ready_eth56g(struct ice_hw *hw, u8 port,
2686 					      u64 *tstamp_ready)
2687 {
2688 	int err;
2689 
2690 	err = ice_read_64b_ptp_reg_eth56g(hw, port, PHY_REG_TX_MEMORY_STATUS_L,
2691 					  tstamp_ready);
2692 	if (err) {
2693 		ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEMORY_STATUS for port %u, err %d\n",
2694 			  port, err);
2695 		return err;
2696 	}
2697 
2698 	return 0;
2699 }
2700 
2701 /**
2702  * ice_ptp_init_phy_e825 - initialize PHY parameters
2703  * @hw: pointer to the HW struct
2704  */
ice_ptp_init_phy_e825(struct ice_hw * hw)2705 static void ice_ptp_init_phy_e825(struct ice_hw *hw)
2706 {
2707 	struct ice_ptp_hw *ptp = &hw->ptp;
2708 	struct ice_eth56g_params *params;
2709 	u32 phy_rev;
2710 	int err;
2711 
2712 	ptp->phy_model = ICE_PHY_ETH56G;
2713 	params = &ptp->phy.eth56g;
2714 	params->onestep_ena = false;
2715 	params->peer_delay = 0;
2716 	params->sfd_ena = false;
2717 	params->num_phys = 2;
2718 	ptp->ports_per_phy = 4;
2719 	ptp->num_lports = params->num_phys * ptp->ports_per_phy;
2720 
2721 	ice_sb_access_ena_eth56g(hw, true);
2722 	err = ice_read_phy_eth56g(hw, hw->pf_id, PHY_REG_REVISION, &phy_rev);
2723 	if (err || phy_rev != PHY_REVISION_ETH56G)
2724 		ptp->phy_model = ICE_PHY_UNSUP;
2725 }
2726 
2727 /* E822 family functions
2728  *
2729  * The following functions operate on the E822 family of devices.
2730  */
2731 
2732 /**
2733  * ice_fill_phy_msg_e82x - Fill message data for a PHY register access
2734  * @hw: pointer to the HW struct
2735  * @msg: the PHY message buffer to fill in
2736  * @port: the port to access
2737  * @offset: the register offset
2738  */
ice_fill_phy_msg_e82x(struct ice_hw * hw,struct ice_sbq_msg_input * msg,u8 port,u16 offset)2739 static void ice_fill_phy_msg_e82x(struct ice_hw *hw,
2740 				  struct ice_sbq_msg_input *msg, u8 port,
2741 				  u16 offset)
2742 {
2743 	int phy_port, quadtype;
2744 
2745 	phy_port = port % hw->ptp.ports_per_phy;
2746 	quadtype = ICE_GET_QUAD_NUM(port) %
2747 		   ICE_GET_QUAD_NUM(hw->ptp.ports_per_phy);
2748 
2749 	if (quadtype == 0) {
2750 		msg->msg_addr_low = P_Q0_L(P_0_BASE + offset, phy_port);
2751 		msg->msg_addr_high = P_Q0_H(P_0_BASE + offset, phy_port);
2752 	} else {
2753 		msg->msg_addr_low = P_Q1_L(P_4_BASE + offset, phy_port);
2754 		msg->msg_addr_high = P_Q1_H(P_4_BASE + offset, phy_port);
2755 	}
2756 
2757 	msg->dest_dev = rmn_0;
2758 }
2759 
2760 /**
2761  * ice_is_64b_phy_reg_e82x - Check if this is a 64bit PHY register
2762  * @low_addr: the low address to check
2763  * @high_addr: on return, contains the high address of the 64bit register
2764  *
2765  * Checks if the provided low address is one of the known 64bit PHY values
2766  * represented as two 32bit registers. If it is, return the appropriate high
2767  * register offset to use.
2768  */
ice_is_64b_phy_reg_e82x(u16 low_addr,u16 * high_addr)2769 static bool ice_is_64b_phy_reg_e82x(u16 low_addr, u16 *high_addr)
2770 {
2771 	switch (low_addr) {
2772 	case P_REG_PAR_PCS_TX_OFFSET_L:
2773 		*high_addr = P_REG_PAR_PCS_TX_OFFSET_U;
2774 		return true;
2775 	case P_REG_PAR_PCS_RX_OFFSET_L:
2776 		*high_addr = P_REG_PAR_PCS_RX_OFFSET_U;
2777 		return true;
2778 	case P_REG_PAR_TX_TIME_L:
2779 		*high_addr = P_REG_PAR_TX_TIME_U;
2780 		return true;
2781 	case P_REG_PAR_RX_TIME_L:
2782 		*high_addr = P_REG_PAR_RX_TIME_U;
2783 		return true;
2784 	case P_REG_TOTAL_TX_OFFSET_L:
2785 		*high_addr = P_REG_TOTAL_TX_OFFSET_U;
2786 		return true;
2787 	case P_REG_TOTAL_RX_OFFSET_L:
2788 		*high_addr = P_REG_TOTAL_RX_OFFSET_U;
2789 		return true;
2790 	case P_REG_UIX66_10G_40G_L:
2791 		*high_addr = P_REG_UIX66_10G_40G_U;
2792 		return true;
2793 	case P_REG_UIX66_25G_100G_L:
2794 		*high_addr = P_REG_UIX66_25G_100G_U;
2795 		return true;
2796 	case P_REG_TX_CAPTURE_L:
2797 		*high_addr = P_REG_TX_CAPTURE_U;
2798 		return true;
2799 	case P_REG_RX_CAPTURE_L:
2800 		*high_addr = P_REG_RX_CAPTURE_U;
2801 		return true;
2802 	case P_REG_TX_TIMER_INC_PRE_L:
2803 		*high_addr = P_REG_TX_TIMER_INC_PRE_U;
2804 		return true;
2805 	case P_REG_RX_TIMER_INC_PRE_L:
2806 		*high_addr = P_REG_RX_TIMER_INC_PRE_U;
2807 		return true;
2808 	default:
2809 		return false;
2810 	}
2811 }
2812 
2813 /**
2814  * ice_is_40b_phy_reg_e82x - Check if this is a 40bit PHY register
2815  * @low_addr: the low address to check
2816  * @high_addr: on return, contains the high address of the 40bit value
2817  *
2818  * Checks if the provided low address is one of the known 40bit PHY values
2819  * split into two registers with the lower 8 bits in the low register and the
2820  * upper 32 bits in the high register. If it is, return the appropriate high
2821  * register offset to use.
2822  */
ice_is_40b_phy_reg_e82x(u16 low_addr,u16 * high_addr)2823 static bool ice_is_40b_phy_reg_e82x(u16 low_addr, u16 *high_addr)
2824 {
2825 	switch (low_addr) {
2826 	case P_REG_TIMETUS_L:
2827 		*high_addr = P_REG_TIMETUS_U;
2828 		return true;
2829 	case P_REG_PAR_RX_TUS_L:
2830 		*high_addr = P_REG_PAR_RX_TUS_U;
2831 		return true;
2832 	case P_REG_PAR_TX_TUS_L:
2833 		*high_addr = P_REG_PAR_TX_TUS_U;
2834 		return true;
2835 	case P_REG_PCS_RX_TUS_L:
2836 		*high_addr = P_REG_PCS_RX_TUS_U;
2837 		return true;
2838 	case P_REG_PCS_TX_TUS_L:
2839 		*high_addr = P_REG_PCS_TX_TUS_U;
2840 		return true;
2841 	case P_REG_DESK_PAR_RX_TUS_L:
2842 		*high_addr = P_REG_DESK_PAR_RX_TUS_U;
2843 		return true;
2844 	case P_REG_DESK_PAR_TX_TUS_L:
2845 		*high_addr = P_REG_DESK_PAR_TX_TUS_U;
2846 		return true;
2847 	case P_REG_DESK_PCS_RX_TUS_L:
2848 		*high_addr = P_REG_DESK_PCS_RX_TUS_U;
2849 		return true;
2850 	case P_REG_DESK_PCS_TX_TUS_L:
2851 		*high_addr = P_REG_DESK_PCS_TX_TUS_U;
2852 		return true;
2853 	default:
2854 		return false;
2855 	}
2856 }
2857 
2858 /**
2859  * ice_read_phy_reg_e82x - Read a PHY register
2860  * @hw: pointer to the HW struct
2861  * @port: PHY port to read from
2862  * @offset: PHY register offset to read
2863  * @val: on return, the contents read from the PHY
2864  *
2865  * Read a PHY register for the given port over the device sideband queue.
2866  */
2867 static int
ice_read_phy_reg_e82x(struct ice_hw * hw,u8 port,u16 offset,u32 * val)2868 ice_read_phy_reg_e82x(struct ice_hw *hw, u8 port, u16 offset, u32 *val)
2869 {
2870 	struct ice_sbq_msg_input msg = {0};
2871 	int err;
2872 
2873 	ice_fill_phy_msg_e82x(hw, &msg, port, offset);
2874 	msg.opcode = ice_sbq_msg_rd;
2875 
2876 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
2877 	if (err) {
2878 		ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
2879 			  err);
2880 		return err;
2881 	}
2882 
2883 	*val = msg.data;
2884 
2885 	return 0;
2886 }
2887 
2888 /**
2889  * ice_read_64b_phy_reg_e82x - Read a 64bit value from PHY registers
2890  * @hw: pointer to the HW struct
2891  * @port: PHY port to read from
2892  * @low_addr: offset of the lower register to read from
2893  * @val: on return, the contents of the 64bit value from the PHY registers
2894  *
2895  * Reads the two registers associated with a 64bit value and returns it in the
2896  * val pointer. The offset always specifies the lower register offset to use.
2897  * The high offset is looked up. This function only operates on registers
2898  * known to be two parts of a 64bit value.
2899  */
2900 static int
ice_read_64b_phy_reg_e82x(struct ice_hw * hw,u8 port,u16 low_addr,u64 * val)2901 ice_read_64b_phy_reg_e82x(struct ice_hw *hw, u8 port, u16 low_addr, u64 *val)
2902 {
2903 	u32 low, high;
2904 	u16 high_addr;
2905 	int err;
2906 
2907 	/* Only operate on registers known to be split into two 32bit
2908 	 * registers.
2909 	 */
2910 	if (!ice_is_64b_phy_reg_e82x(low_addr, &high_addr)) {
2911 		ice_debug(hw, ICE_DBG_PTP, "Invalid 64b register addr 0x%08x\n",
2912 			  low_addr);
2913 		return -EINVAL;
2914 	}
2915 
2916 	err = ice_read_phy_reg_e82x(hw, port, low_addr, &low);
2917 	if (err) {
2918 		ice_debug(hw, ICE_DBG_PTP, "Failed to read from low register 0x%08x\n, err %d",
2919 			  low_addr, err);
2920 		return err;
2921 	}
2922 
2923 	err = ice_read_phy_reg_e82x(hw, port, high_addr, &high);
2924 	if (err) {
2925 		ice_debug(hw, ICE_DBG_PTP, "Failed to read from high register 0x%08x\n, err %d",
2926 			  high_addr, err);
2927 		return err;
2928 	}
2929 
2930 	*val = (u64)high << 32 | low;
2931 
2932 	return 0;
2933 }
2934 
2935 /**
2936  * ice_write_phy_reg_e82x - Write a PHY register
2937  * @hw: pointer to the HW struct
2938  * @port: PHY port to write to
2939  * @offset: PHY register offset to write
2940  * @val: The value to write to the register
2941  *
2942  * Write a PHY register for the given port over the device sideband queue.
2943  */
2944 static int
ice_write_phy_reg_e82x(struct ice_hw * hw,u8 port,u16 offset,u32 val)2945 ice_write_phy_reg_e82x(struct ice_hw *hw, u8 port, u16 offset, u32 val)
2946 {
2947 	struct ice_sbq_msg_input msg = {0};
2948 	int err;
2949 
2950 	ice_fill_phy_msg_e82x(hw, &msg, port, offset);
2951 	msg.opcode = ice_sbq_msg_wr;
2952 	msg.data = val;
2953 
2954 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
2955 	if (err) {
2956 		ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
2957 			  err);
2958 		return err;
2959 	}
2960 
2961 	return 0;
2962 }
2963 
2964 /**
2965  * ice_write_40b_phy_reg_e82x - Write a 40b value to the PHY
2966  * @hw: pointer to the HW struct
2967  * @port: port to write to
2968  * @low_addr: offset of the low register
2969  * @val: 40b value to write
2970  *
2971  * Write the provided 40b value to the two associated registers by splitting
2972  * it up into two chunks, the lower 8 bits and the upper 32 bits.
2973  */
2974 static int
ice_write_40b_phy_reg_e82x(struct ice_hw * hw,u8 port,u16 low_addr,u64 val)2975 ice_write_40b_phy_reg_e82x(struct ice_hw *hw, u8 port, u16 low_addr, u64 val)
2976 {
2977 	u32 low, high;
2978 	u16 high_addr;
2979 	int err;
2980 
2981 	/* Only operate on registers known to be split into a lower 8 bit
2982 	 * register and an upper 32 bit register.
2983 	 */
2984 	if (!ice_is_40b_phy_reg_e82x(low_addr, &high_addr)) {
2985 		ice_debug(hw, ICE_DBG_PTP, "Invalid 40b register addr 0x%08x\n",
2986 			  low_addr);
2987 		return -EINVAL;
2988 	}
2989 	low = FIELD_GET(P_REG_40B_LOW_M, val);
2990 	high = (u32)(val >> P_REG_40B_HIGH_S);
2991 
2992 	err = ice_write_phy_reg_e82x(hw, port, low_addr, low);
2993 	if (err) {
2994 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
2995 			  low_addr, err);
2996 		return err;
2997 	}
2998 
2999 	err = ice_write_phy_reg_e82x(hw, port, high_addr, high);
3000 	if (err) {
3001 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
3002 			  high_addr, err);
3003 		return err;
3004 	}
3005 
3006 	return 0;
3007 }
3008 
3009 /**
3010  * ice_write_64b_phy_reg_e82x - Write a 64bit value to PHY registers
3011  * @hw: pointer to the HW struct
3012  * @port: PHY port to read from
3013  * @low_addr: offset of the lower register to read from
3014  * @val: the contents of the 64bit value to write to PHY
3015  *
3016  * Write the 64bit value to the two associated 32bit PHY registers. The offset
3017  * is always specified as the lower register, and the high address is looked
3018  * up. This function only operates on registers known to be two parts of
3019  * a 64bit value.
3020  */
3021 static int
ice_write_64b_phy_reg_e82x(struct ice_hw * hw,u8 port,u16 low_addr,u64 val)3022 ice_write_64b_phy_reg_e82x(struct ice_hw *hw, u8 port, u16 low_addr, u64 val)
3023 {
3024 	u32 low, high;
3025 	u16 high_addr;
3026 	int err;
3027 
3028 	/* Only operate on registers known to be split into two 32bit
3029 	 * registers.
3030 	 */
3031 	if (!ice_is_64b_phy_reg_e82x(low_addr, &high_addr)) {
3032 		ice_debug(hw, ICE_DBG_PTP, "Invalid 64b register addr 0x%08x\n",
3033 			  low_addr);
3034 		return -EINVAL;
3035 	}
3036 
3037 	low = lower_32_bits(val);
3038 	high = upper_32_bits(val);
3039 
3040 	err = ice_write_phy_reg_e82x(hw, port, low_addr, low);
3041 	if (err) {
3042 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to low register 0x%08x\n, err %d",
3043 			  low_addr, err);
3044 		return err;
3045 	}
3046 
3047 	err = ice_write_phy_reg_e82x(hw, port, high_addr, high);
3048 	if (err) {
3049 		ice_debug(hw, ICE_DBG_PTP, "Failed to write to high register 0x%08x\n, err %d",
3050 			  high_addr, err);
3051 		return err;
3052 	}
3053 
3054 	return 0;
3055 }
3056 
3057 /**
3058  * ice_fill_quad_msg_e82x - Fill message data for quad register access
3059  * @hw: pointer to the HW struct
3060  * @msg: the PHY message buffer to fill in
3061  * @quad: the quad to access
3062  * @offset: the register offset
3063  *
3064  * Fill a message buffer for accessing a register in a quad shared between
3065  * multiple PHYs.
3066  *
3067  * Return:
3068  * * %0       - OK
3069  * * %-EINVAL - invalid quad number
3070  */
ice_fill_quad_msg_e82x(struct ice_hw * hw,struct ice_sbq_msg_input * msg,u8 quad,u16 offset)3071 static int ice_fill_quad_msg_e82x(struct ice_hw *hw,
3072 				  struct ice_sbq_msg_input *msg, u8 quad,
3073 				  u16 offset)
3074 {
3075 	u32 addr;
3076 
3077 	if (quad >= ICE_GET_QUAD_NUM(hw->ptp.num_lports))
3078 		return -EINVAL;
3079 
3080 	msg->dest_dev = rmn_0;
3081 
3082 	if (!(quad % ICE_GET_QUAD_NUM(hw->ptp.ports_per_phy)))
3083 		addr = Q_0_BASE + offset;
3084 	else
3085 		addr = Q_1_BASE + offset;
3086 
3087 	msg->msg_addr_low = lower_16_bits(addr);
3088 	msg->msg_addr_high = upper_16_bits(addr);
3089 
3090 	return 0;
3091 }
3092 
3093 /**
3094  * ice_read_quad_reg_e82x - Read a PHY quad register
3095  * @hw: pointer to the HW struct
3096  * @quad: quad to read from
3097  * @offset: quad register offset to read
3098  * @val: on return, the contents read from the quad
3099  *
3100  * Read a quad register over the device sideband queue. Quad registers are
3101  * shared between multiple PHYs.
3102  */
3103 int
ice_read_quad_reg_e82x(struct ice_hw * hw,u8 quad,u16 offset,u32 * val)3104 ice_read_quad_reg_e82x(struct ice_hw *hw, u8 quad, u16 offset, u32 *val)
3105 {
3106 	struct ice_sbq_msg_input msg = {0};
3107 	int err;
3108 
3109 	err = ice_fill_quad_msg_e82x(hw, &msg, quad, offset);
3110 	if (err)
3111 		return err;
3112 
3113 	msg.opcode = ice_sbq_msg_rd;
3114 
3115 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
3116 	if (err) {
3117 		ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
3118 			  err);
3119 		return err;
3120 	}
3121 
3122 	*val = msg.data;
3123 
3124 	return 0;
3125 }
3126 
3127 /**
3128  * ice_write_quad_reg_e82x - Write a PHY quad register
3129  * @hw: pointer to the HW struct
3130  * @quad: quad to write to
3131  * @offset: quad register offset to write
3132  * @val: The value to write to the register
3133  *
3134  * Write a quad register over the device sideband queue. Quad registers are
3135  * shared between multiple PHYs.
3136  */
3137 int
ice_write_quad_reg_e82x(struct ice_hw * hw,u8 quad,u16 offset,u32 val)3138 ice_write_quad_reg_e82x(struct ice_hw *hw, u8 quad, u16 offset, u32 val)
3139 {
3140 	struct ice_sbq_msg_input msg = {0};
3141 	int err;
3142 
3143 	err = ice_fill_quad_msg_e82x(hw, &msg, quad, offset);
3144 	if (err)
3145 		return err;
3146 
3147 	msg.opcode = ice_sbq_msg_wr;
3148 	msg.data = val;
3149 
3150 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
3151 	if (err) {
3152 		ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
3153 			  err);
3154 		return err;
3155 	}
3156 
3157 	return 0;
3158 }
3159 
3160 /**
3161  * ice_read_phy_tstamp_e82x - Read a PHY timestamp out of the quad block
3162  * @hw: pointer to the HW struct
3163  * @quad: the quad to read from
3164  * @idx: the timestamp index to read
3165  * @tstamp: on return, the 40bit timestamp value
3166  *
3167  * Read a 40bit timestamp value out of the two associated registers in the
3168  * quad memory block that is shared between the internal PHYs of the E822
3169  * family of devices.
3170  */
3171 static int
ice_read_phy_tstamp_e82x(struct ice_hw * hw,u8 quad,u8 idx,u64 * tstamp)3172 ice_read_phy_tstamp_e82x(struct ice_hw *hw, u8 quad, u8 idx, u64 *tstamp)
3173 {
3174 	u16 lo_addr, hi_addr;
3175 	u32 lo, hi;
3176 	int err;
3177 
3178 	lo_addr = (u16)TS_L(Q_REG_TX_MEMORY_BANK_START, idx);
3179 	hi_addr = (u16)TS_H(Q_REG_TX_MEMORY_BANK_START, idx);
3180 
3181 	err = ice_read_quad_reg_e82x(hw, quad, lo_addr, &lo);
3182 	if (err) {
3183 		ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
3184 			  err);
3185 		return err;
3186 	}
3187 
3188 	err = ice_read_quad_reg_e82x(hw, quad, hi_addr, &hi);
3189 	if (err) {
3190 		ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
3191 			  err);
3192 		return err;
3193 	}
3194 
3195 	/* For E822 based internal PHYs, the timestamp is reported with the
3196 	 * lower 8 bits in the low register, and the upper 32 bits in the high
3197 	 * register.
3198 	 */
3199 	*tstamp = FIELD_PREP(TS_PHY_HIGH_M, hi) | FIELD_PREP(TS_PHY_LOW_M, lo);
3200 
3201 	return 0;
3202 }
3203 
3204 /**
3205  * ice_clear_phy_tstamp_e82x - Clear a timestamp from the quad block
3206  * @hw: pointer to the HW struct
3207  * @quad: the quad to read from
3208  * @idx: the timestamp index to reset
3209  *
3210  * Read the timestamp out of the quad to clear its timestamp status bit from
3211  * the PHY quad block that is shared between the internal PHYs of the E822
3212  * devices.
3213  *
3214  * Note that unlike E810, software cannot directly write to the quad memory
3215  * bank registers. E822 relies on the ice_get_phy_tx_tstamp_ready() function
3216  * to determine which timestamps are valid. Reading a timestamp auto-clears
3217  * the valid bit.
3218  *
3219  * To directly clear the contents of the timestamp block entirely, discarding
3220  * all timestamp data at once, software should instead use
3221  * ice_ptp_reset_ts_memory_quad_e82x().
3222  *
3223  * This function should only be called on an idx whose bit is set according to
3224  * ice_get_phy_tx_tstamp_ready().
3225  */
3226 static int
ice_clear_phy_tstamp_e82x(struct ice_hw * hw,u8 quad,u8 idx)3227 ice_clear_phy_tstamp_e82x(struct ice_hw *hw, u8 quad, u8 idx)
3228 {
3229 	u64 unused_tstamp;
3230 	int err;
3231 
3232 	err = ice_read_phy_tstamp_e82x(hw, quad, idx, &unused_tstamp);
3233 	if (err) {
3234 		ice_debug(hw, ICE_DBG_PTP, "Failed to read the timestamp register for quad %u, idx %u, err %d\n",
3235 			  quad, idx, err);
3236 		return err;
3237 	}
3238 
3239 	return 0;
3240 }
3241 
3242 /**
3243  * ice_ptp_reset_ts_memory_quad_e82x - Clear all timestamps from the quad block
3244  * @hw: pointer to the HW struct
3245  * @quad: the quad to read from
3246  *
3247  * Clear all timestamps from the PHY quad block that is shared between the
3248  * internal PHYs on the E822 devices.
3249  */
ice_ptp_reset_ts_memory_quad_e82x(struct ice_hw * hw,u8 quad)3250 void ice_ptp_reset_ts_memory_quad_e82x(struct ice_hw *hw, u8 quad)
3251 {
3252 	ice_write_quad_reg_e82x(hw, quad, Q_REG_TS_CTRL, Q_REG_TS_CTRL_M);
3253 	ice_write_quad_reg_e82x(hw, quad, Q_REG_TS_CTRL, ~(u32)Q_REG_TS_CTRL_M);
3254 }
3255 
3256 /**
3257  * ice_ptp_reset_ts_memory_e82x - Clear all timestamps from all quad blocks
3258  * @hw: pointer to the HW struct
3259  */
ice_ptp_reset_ts_memory_e82x(struct ice_hw * hw)3260 static void ice_ptp_reset_ts_memory_e82x(struct ice_hw *hw)
3261 {
3262 	unsigned int quad;
3263 
3264 	for (quad = 0; quad < ICE_GET_QUAD_NUM(hw->ptp.num_lports); quad++)
3265 		ice_ptp_reset_ts_memory_quad_e82x(hw, quad);
3266 }
3267 
3268 /**
3269  * ice_ptp_set_vernier_wl - Set the window length for vernier calibration
3270  * @hw: pointer to the HW struct
3271  *
3272  * Set the window length used for the vernier port calibration process.
3273  */
ice_ptp_set_vernier_wl(struct ice_hw * hw)3274 static int ice_ptp_set_vernier_wl(struct ice_hw *hw)
3275 {
3276 	u8 port;
3277 
3278 	for (port = 0; port < hw->ptp.num_lports; port++) {
3279 		int err;
3280 
3281 		err = ice_write_phy_reg_e82x(hw, port, P_REG_WL,
3282 					     PTP_VERNIER_WL);
3283 		if (err) {
3284 			ice_debug(hw, ICE_DBG_PTP, "Failed to set vernier window length for port %u, err %d\n",
3285 				  port, err);
3286 			return err;
3287 		}
3288 	}
3289 
3290 	return 0;
3291 }
3292 
3293 /**
3294  * ice_ptp_init_phc_e82x - Perform E822 specific PHC initialization
3295  * @hw: pointer to HW struct
3296  *
3297  * Perform PHC initialization steps specific to E822 devices.
3298  */
ice_ptp_init_phc_e82x(struct ice_hw * hw)3299 static int ice_ptp_init_phc_e82x(struct ice_hw *hw)
3300 {
3301 	int err;
3302 	u32 val;
3303 
3304 	/* Enable reading switch and PHY registers over the sideband queue */
3305 #define PF_SB_REM_DEV_CTL_SWITCH_READ BIT(1)
3306 #define PF_SB_REM_DEV_CTL_PHY0 BIT(2)
3307 	val = rd32(hw, PF_SB_REM_DEV_CTL);
3308 	val |= (PF_SB_REM_DEV_CTL_SWITCH_READ | PF_SB_REM_DEV_CTL_PHY0);
3309 	wr32(hw, PF_SB_REM_DEV_CTL, val);
3310 
3311 	/* Initialize the Clock Generation Unit */
3312 	err = ice_init_cgu_e82x(hw);
3313 	if (err)
3314 		return err;
3315 
3316 	/* Set window length for all the ports */
3317 	return ice_ptp_set_vernier_wl(hw);
3318 }
3319 
3320 /**
3321  * ice_ptp_prep_phy_time_e82x - Prepare PHY port with initial time
3322  * @hw: pointer to the HW struct
3323  * @time: Time to initialize the PHY port clocks to
3324  *
3325  * Program the PHY port registers with a new initial time value. The port
3326  * clock will be initialized once the driver issues an ICE_PTP_INIT_TIME sync
3327  * command. The time value is the upper 32 bits of the PHY timer, usually in
3328  * units of nominal nanoseconds.
3329  */
3330 static int
ice_ptp_prep_phy_time_e82x(struct ice_hw * hw,u32 time)3331 ice_ptp_prep_phy_time_e82x(struct ice_hw *hw, u32 time)
3332 {
3333 	u64 phy_time;
3334 	u8 port;
3335 	int err;
3336 
3337 	/* The time represents the upper 32 bits of the PHY timer, so we need
3338 	 * to shift to account for this when programming.
3339 	 */
3340 	phy_time = (u64)time << 32;
3341 
3342 	for (port = 0; port < hw->ptp.num_lports; port++) {
3343 		/* Tx case */
3344 		err = ice_write_64b_phy_reg_e82x(hw, port,
3345 						 P_REG_TX_TIMER_INC_PRE_L,
3346 						 phy_time);
3347 		if (err)
3348 			goto exit_err;
3349 
3350 		/* Rx case */
3351 		err = ice_write_64b_phy_reg_e82x(hw, port,
3352 						 P_REG_RX_TIMER_INC_PRE_L,
3353 						 phy_time);
3354 		if (err)
3355 			goto exit_err;
3356 	}
3357 
3358 	return 0;
3359 
3360 exit_err:
3361 	ice_debug(hw, ICE_DBG_PTP, "Failed to write init time for port %u, err %d\n",
3362 		  port, err);
3363 
3364 	return err;
3365 }
3366 
3367 /**
3368  * ice_ptp_prep_port_adj_e82x - Prepare a single port for time adjust
3369  * @hw: pointer to HW struct
3370  * @port: Port number to be programmed
3371  * @time: time in cycles to adjust the port Tx and Rx clocks
3372  *
3373  * Program the port for an atomic adjustment by writing the Tx and Rx timer
3374  * registers. The atomic adjustment won't be completed until the driver issues
3375  * an ICE_PTP_ADJ_TIME command.
3376  *
3377  * Note that time is not in units of nanoseconds. It is in clock time
3378  * including the lower sub-nanosecond portion of the port timer.
3379  *
3380  * Negative adjustments are supported using 2s complement arithmetic.
3381  */
3382 static int
ice_ptp_prep_port_adj_e82x(struct ice_hw * hw,u8 port,s64 time)3383 ice_ptp_prep_port_adj_e82x(struct ice_hw *hw, u8 port, s64 time)
3384 {
3385 	u32 l_time, u_time;
3386 	int err;
3387 
3388 	l_time = lower_32_bits(time);
3389 	u_time = upper_32_bits(time);
3390 
3391 	/* Tx case */
3392 	err = ice_write_phy_reg_e82x(hw, port, P_REG_TX_TIMER_INC_PRE_L,
3393 				     l_time);
3394 	if (err)
3395 		goto exit_err;
3396 
3397 	err = ice_write_phy_reg_e82x(hw, port, P_REG_TX_TIMER_INC_PRE_U,
3398 				     u_time);
3399 	if (err)
3400 		goto exit_err;
3401 
3402 	/* Rx case */
3403 	err = ice_write_phy_reg_e82x(hw, port, P_REG_RX_TIMER_INC_PRE_L,
3404 				     l_time);
3405 	if (err)
3406 		goto exit_err;
3407 
3408 	err = ice_write_phy_reg_e82x(hw, port, P_REG_RX_TIMER_INC_PRE_U,
3409 				     u_time);
3410 	if (err)
3411 		goto exit_err;
3412 
3413 	return 0;
3414 
3415 exit_err:
3416 	ice_debug(hw, ICE_DBG_PTP, "Failed to write time adjust for port %u, err %d\n",
3417 		  port, err);
3418 	return err;
3419 }
3420 
3421 /**
3422  * ice_ptp_prep_phy_adj_e82x - Prep PHY ports for a time adjustment
3423  * @hw: pointer to HW struct
3424  * @adj: adjustment in nanoseconds
3425  *
3426  * Prepare the PHY ports for an atomic time adjustment by programming the PHY
3427  * Tx and Rx port registers. The actual adjustment is completed by issuing an
3428  * ICE_PTP_ADJ_TIME or ICE_PTP_ADJ_TIME_AT_TIME sync command.
3429  */
3430 static int
ice_ptp_prep_phy_adj_e82x(struct ice_hw * hw,s32 adj)3431 ice_ptp_prep_phy_adj_e82x(struct ice_hw *hw, s32 adj)
3432 {
3433 	s64 cycles;
3434 	u8 port;
3435 
3436 	/* The port clock supports adjustment of the sub-nanosecond portion of
3437 	 * the clock. We shift the provided adjustment in nanoseconds to
3438 	 * calculate the appropriate adjustment to program into the PHY ports.
3439 	 */
3440 	if (adj > 0)
3441 		cycles = (s64)adj << 32;
3442 	else
3443 		cycles = -(((s64)-adj) << 32);
3444 
3445 	for (port = 0; port < hw->ptp.num_lports; port++) {
3446 		int err;
3447 
3448 		err = ice_ptp_prep_port_adj_e82x(hw, port, cycles);
3449 		if (err)
3450 			return err;
3451 	}
3452 
3453 	return 0;
3454 }
3455 
3456 /**
3457  * ice_ptp_prep_phy_incval_e82x - Prepare PHY ports for time adjustment
3458  * @hw: pointer to HW struct
3459  * @incval: new increment value to prepare
3460  *
3461  * Prepare each of the PHY ports for a new increment value by programming the
3462  * port's TIMETUS registers. The new increment value will be updated after
3463  * issuing an ICE_PTP_INIT_INCVAL command.
3464  */
3465 static int
ice_ptp_prep_phy_incval_e82x(struct ice_hw * hw,u64 incval)3466 ice_ptp_prep_phy_incval_e82x(struct ice_hw *hw, u64 incval)
3467 {
3468 	int err;
3469 	u8 port;
3470 
3471 	for (port = 0; port < hw->ptp.num_lports; port++) {
3472 		err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_TIMETUS_L,
3473 						 incval);
3474 		if (err)
3475 			goto exit_err;
3476 	}
3477 
3478 	return 0;
3479 
3480 exit_err:
3481 	ice_debug(hw, ICE_DBG_PTP, "Failed to write incval for port %u, err %d\n",
3482 		  port, err);
3483 
3484 	return err;
3485 }
3486 
3487 /**
3488  * ice_ptp_read_port_capture - Read a port's local time capture
3489  * @hw: pointer to HW struct
3490  * @port: Port number to read
3491  * @tx_ts: on return, the Tx port time capture
3492  * @rx_ts: on return, the Rx port time capture
3493  *
3494  * Read the port's Tx and Rx local time capture values.
3495  *
3496  * Note this has no equivalent for the E810 devices.
3497  */
3498 static int
ice_ptp_read_port_capture(struct ice_hw * hw,u8 port,u64 * tx_ts,u64 * rx_ts)3499 ice_ptp_read_port_capture(struct ice_hw *hw, u8 port, u64 *tx_ts, u64 *rx_ts)
3500 {
3501 	int err;
3502 
3503 	/* Tx case */
3504 	err = ice_read_64b_phy_reg_e82x(hw, port, P_REG_TX_CAPTURE_L, tx_ts);
3505 	if (err) {
3506 		ice_debug(hw, ICE_DBG_PTP, "Failed to read REG_TX_CAPTURE, err %d\n",
3507 			  err);
3508 		return err;
3509 	}
3510 
3511 	ice_debug(hw, ICE_DBG_PTP, "tx_init = 0x%016llx\n",
3512 		  (unsigned long long)*tx_ts);
3513 
3514 	/* Rx case */
3515 	err = ice_read_64b_phy_reg_e82x(hw, port, P_REG_RX_CAPTURE_L, rx_ts);
3516 	if (err) {
3517 		ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_CAPTURE, err %d\n",
3518 			  err);
3519 		return err;
3520 	}
3521 
3522 	ice_debug(hw, ICE_DBG_PTP, "rx_init = 0x%016llx\n",
3523 		  (unsigned long long)*rx_ts);
3524 
3525 	return 0;
3526 }
3527 
3528 /**
3529  * ice_ptp_write_port_cmd_e82x - Prepare a single PHY port for a timer command
3530  * @hw: pointer to HW struct
3531  * @port: Port to which cmd has to be sent
3532  * @cmd: Command to be sent to the port
3533  *
3534  * Prepare the requested port for an upcoming timer sync command.
3535  *
3536  * Note there is no equivalent of this operation on E810, as that device
3537  * always handles all external PHYs internally.
3538  *
3539  * Return:
3540  * * %0     - success
3541  * * %other - failed to write to PHY
3542  */
ice_ptp_write_port_cmd_e82x(struct ice_hw * hw,u8 port,enum ice_ptp_tmr_cmd cmd)3543 static int ice_ptp_write_port_cmd_e82x(struct ice_hw *hw, u8 port,
3544 				       enum ice_ptp_tmr_cmd cmd)
3545 {
3546 	u32 val = ice_ptp_tmr_cmd_to_port_reg(hw, cmd);
3547 	int err;
3548 
3549 	/* Tx case */
3550 	err = ice_write_phy_reg_e82x(hw, port, P_REG_TX_TMR_CMD, val);
3551 	if (err) {
3552 		ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_TMR_CMD, err %d\n",
3553 			  err);
3554 		return err;
3555 	}
3556 
3557 	/* Rx case */
3558 	err = ice_write_phy_reg_e82x(hw, port, P_REG_RX_TMR_CMD,
3559 				     val | TS_CMD_RX_TYPE);
3560 	if (err) {
3561 		ice_debug(hw, ICE_DBG_PTP, "Failed to write back RX_TMR_CMD, err %d\n",
3562 			  err);
3563 		return err;
3564 	}
3565 
3566 	return 0;
3567 }
3568 
3569 /* E822 Vernier calibration functions
3570  *
3571  * The following functions are used as part of the vernier calibration of
3572  * a port. This calibration increases the precision of the timestamps on the
3573  * port.
3574  */
3575 
3576 /**
3577  * ice_phy_get_speed_and_fec_e82x - Get link speed and FEC based on serdes mode
3578  * @hw: pointer to HW struct
3579  * @port: the port to read from
3580  * @link_out: if non-NULL, holds link speed on success
3581  * @fec_out: if non-NULL, holds FEC algorithm on success
3582  *
3583  * Read the serdes data for the PHY port and extract the link speed and FEC
3584  * algorithm.
3585  */
3586 static int
ice_phy_get_speed_and_fec_e82x(struct ice_hw * hw,u8 port,enum ice_ptp_link_spd * link_out,enum ice_ptp_fec_mode * fec_out)3587 ice_phy_get_speed_and_fec_e82x(struct ice_hw *hw, u8 port,
3588 			       enum ice_ptp_link_spd *link_out,
3589 			       enum ice_ptp_fec_mode *fec_out)
3590 {
3591 	enum ice_ptp_link_spd link;
3592 	enum ice_ptp_fec_mode fec;
3593 	u32 serdes;
3594 	int err;
3595 
3596 	err = ice_read_phy_reg_e82x(hw, port, P_REG_LINK_SPEED, &serdes);
3597 	if (err) {
3598 		ice_debug(hw, ICE_DBG_PTP, "Failed to read serdes info\n");
3599 		return err;
3600 	}
3601 
3602 	/* Determine the FEC algorithm */
3603 	fec = (enum ice_ptp_fec_mode)P_REG_LINK_SPEED_FEC_MODE(serdes);
3604 
3605 	serdes &= P_REG_LINK_SPEED_SERDES_M;
3606 
3607 	/* Determine the link speed */
3608 	if (fec == ICE_PTP_FEC_MODE_RS_FEC) {
3609 		switch (serdes) {
3610 		case ICE_PTP_SERDES_25G:
3611 			link = ICE_PTP_LNK_SPD_25G_RS;
3612 			break;
3613 		case ICE_PTP_SERDES_50G:
3614 			link = ICE_PTP_LNK_SPD_50G_RS;
3615 			break;
3616 		case ICE_PTP_SERDES_100G:
3617 			link = ICE_PTP_LNK_SPD_100G_RS;
3618 			break;
3619 		default:
3620 			return -EIO;
3621 		}
3622 	} else {
3623 		switch (serdes) {
3624 		case ICE_PTP_SERDES_1G:
3625 			link = ICE_PTP_LNK_SPD_1G;
3626 			break;
3627 		case ICE_PTP_SERDES_10G:
3628 			link = ICE_PTP_LNK_SPD_10G;
3629 			break;
3630 		case ICE_PTP_SERDES_25G:
3631 			link = ICE_PTP_LNK_SPD_25G;
3632 			break;
3633 		case ICE_PTP_SERDES_40G:
3634 			link = ICE_PTP_LNK_SPD_40G;
3635 			break;
3636 		case ICE_PTP_SERDES_50G:
3637 			link = ICE_PTP_LNK_SPD_50G;
3638 			break;
3639 		default:
3640 			return -EIO;
3641 		}
3642 	}
3643 
3644 	if (link_out)
3645 		*link_out = link;
3646 	if (fec_out)
3647 		*fec_out = fec;
3648 
3649 	return 0;
3650 }
3651 
3652 /**
3653  * ice_phy_cfg_lane_e82x - Configure PHY quad for single/multi-lane timestamp
3654  * @hw: pointer to HW struct
3655  * @port: to configure the quad for
3656  */
ice_phy_cfg_lane_e82x(struct ice_hw * hw,u8 port)3657 static void ice_phy_cfg_lane_e82x(struct ice_hw *hw, u8 port)
3658 {
3659 	enum ice_ptp_link_spd link_spd;
3660 	int err;
3661 	u32 val;
3662 	u8 quad;
3663 
3664 	err = ice_phy_get_speed_and_fec_e82x(hw, port, &link_spd, NULL);
3665 	if (err) {
3666 		ice_debug(hw, ICE_DBG_PTP, "Failed to get PHY link speed, err %d\n",
3667 			  err);
3668 		return;
3669 	}
3670 
3671 	quad = ICE_GET_QUAD_NUM(port);
3672 
3673 	err = ice_read_quad_reg_e82x(hw, quad, Q_REG_TX_MEM_GBL_CFG, &val);
3674 	if (err) {
3675 		ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEM_GLB_CFG, err %d\n",
3676 			  err);
3677 		return;
3678 	}
3679 
3680 	if (link_spd >= ICE_PTP_LNK_SPD_40G)
3681 		val &= ~Q_REG_TX_MEM_GBL_CFG_LANE_TYPE_M;
3682 	else
3683 		val |= Q_REG_TX_MEM_GBL_CFG_LANE_TYPE_M;
3684 
3685 	err = ice_write_quad_reg_e82x(hw, quad, Q_REG_TX_MEM_GBL_CFG, val);
3686 	if (err) {
3687 		ice_debug(hw, ICE_DBG_PTP, "Failed to write back TX_MEM_GBL_CFG, err %d\n",
3688 			  err);
3689 		return;
3690 	}
3691 }
3692 
3693 /**
3694  * ice_phy_cfg_uix_e82x - Configure Serdes UI to TU conversion for E822
3695  * @hw: pointer to the HW structure
3696  * @port: the port to configure
3697  *
3698  * Program the conversion ration of Serdes clock "unit intervals" (UIs) to PHC
3699  * hardware clock time units (TUs). That is, determine the number of TUs per
3700  * serdes unit interval, and program the UIX registers with this conversion.
3701  *
3702  * This conversion is used as part of the calibration process when determining
3703  * the additional error of a timestamp vs the real time of transmission or
3704  * receipt of the packet.
3705  *
3706  * Hardware uses the number of TUs per 66 UIs, written to the UIX registers
3707  * for the two main serdes clock rates, 10G/40G and 25G/100G serdes clocks.
3708  *
3709  * To calculate the conversion ratio, we use the following facts:
3710  *
3711  * a) the clock frequency in Hz (cycles per second)
3712  * b) the number of TUs per cycle (the increment value of the clock)
3713  * c) 1 second per 1 billion nanoseconds
3714  * d) the duration of 66 UIs in nanoseconds
3715  *
3716  * Given these facts, we can use the following table to work out what ratios
3717  * to multiply in order to get the number of TUs per 66 UIs:
3718  *
3719  * cycles |   1 second   | incval (TUs) | nanoseconds
3720  * -------+--------------+--------------+-------------
3721  * second | 1 billion ns |    cycle     |   66 UIs
3722  *
3723  * To perform the multiplication using integers without too much loss of
3724  * precision, we can take use the following equation:
3725  *
3726  * (freq * incval * 6600 LINE_UI ) / ( 100 * 1 billion)
3727  *
3728  * We scale up to using 6600 UI instead of 66 in order to avoid fractional
3729  * nanosecond UIs (66 UI at 10G/40G is 6.4 ns)
3730  *
3731  * The increment value has a maximum expected range of about 34 bits, while
3732  * the frequency value is about 29 bits. Multiplying these values shouldn't
3733  * overflow the 64 bits. However, we must then further multiply them again by
3734  * the Serdes unit interval duration. To avoid overflow here, we split the
3735  * overall divide by 1e11 into a divide by 256 (shift down by 8 bits) and
3736  * a divide by 390,625,000. This does lose some precision, but avoids
3737  * miscalculation due to arithmetic overflow.
3738  */
ice_phy_cfg_uix_e82x(struct ice_hw * hw,u8 port)3739 static int ice_phy_cfg_uix_e82x(struct ice_hw *hw, u8 port)
3740 {
3741 	u64 cur_freq, clk_incval, tu_per_sec, uix;
3742 	int err;
3743 
3744 	cur_freq = ice_e82x_pll_freq(ice_e82x_time_ref(hw));
3745 	clk_incval = ice_ptp_read_src_incval(hw);
3746 
3747 	/* Calculate TUs per second divided by 256 */
3748 	tu_per_sec = (cur_freq * clk_incval) >> 8;
3749 
3750 #define LINE_UI_10G_40G 640 /* 6600 UIs is 640 nanoseconds at 10Gb/40Gb */
3751 #define LINE_UI_25G_100G 256 /* 6600 UIs is 256 nanoseconds at 25Gb/100Gb */
3752 
3753 	/* Program the 10Gb/40Gb conversion ratio */
3754 	uix = div_u64(tu_per_sec * LINE_UI_10G_40G, 390625000);
3755 
3756 	err = ice_write_64b_phy_reg_e82x(hw, port, P_REG_UIX66_10G_40G_L,
3757 					 uix);
3758 	if (err) {
3759 		ice_debug(hw, ICE_DBG_PTP, "Failed to write UIX66_10G_40G, err %d\n",
3760 			  err);
3761 		return err;
3762 	}
3763 
3764 	/* Program the 25Gb/100Gb conversion ratio */
3765 	uix = div_u64(tu_per_sec * LINE_UI_25G_100G, 390625000);
3766 
3767 	err = ice_write_64b_phy_reg_e82x(hw, port, P_REG_UIX66_25G_100G_L,
3768 					 uix);
3769 	if (err) {
3770 		ice_debug(hw, ICE_DBG_PTP, "Failed to write UIX66_25G_100G, err %d\n",
3771 			  err);
3772 		return err;
3773 	}
3774 
3775 	return 0;
3776 }
3777 
3778 /**
3779  * ice_phy_cfg_parpcs_e82x - Configure TUs per PAR/PCS clock cycle
3780  * @hw: pointer to the HW struct
3781  * @port: port to configure
3782  *
3783  * Configure the number of TUs for the PAR and PCS clocks used as part of the
3784  * timestamp calibration process. This depends on the link speed, as the PHY
3785  * uses different markers depending on the speed.
3786  *
3787  * 1Gb/10Gb/25Gb:
3788  * - Tx/Rx PAR/PCS markers
3789  *
3790  * 25Gb RS:
3791  * - Tx/Rx Reed Solomon gearbox PAR/PCS markers
3792  *
3793  * 40Gb/50Gb:
3794  * - Tx/Rx PAR/PCS markers
3795  * - Rx Deskew PAR/PCS markers
3796  *
3797  * 50G RS and 100GB RS:
3798  * - Tx/Rx Reed Solomon gearbox PAR/PCS markers
3799  * - Rx Deskew PAR/PCS markers
3800  * - Tx PAR/PCS markers
3801  *
3802  * To calculate the conversion, we use the PHC clock frequency (cycles per
3803  * second), the increment value (TUs per cycle), and the related PHY clock
3804  * frequency to calculate the TUs per unit of the PHY link clock. The
3805  * following table shows how the units convert:
3806  *
3807  * cycles |  TUs  | second
3808  * -------+-------+--------
3809  * second | cycle | cycles
3810  *
3811  * For each conversion register, look up the appropriate frequency from the
3812  * e822 PAR/PCS table and calculate the TUs per unit of that clock. Program
3813  * this to the appropriate register, preparing hardware to perform timestamp
3814  * calibration to calculate the total Tx or Rx offset to adjust the timestamp
3815  * in order to calibrate for the internal PHY delays.
3816  *
3817  * Note that the increment value ranges up to ~34 bits, and the clock
3818  * frequency is ~29 bits, so multiplying them together should fit within the
3819  * 64 bit arithmetic.
3820  */
ice_phy_cfg_parpcs_e82x(struct ice_hw * hw,u8 port)3821 static int ice_phy_cfg_parpcs_e82x(struct ice_hw *hw, u8 port)
3822 {
3823 	u64 cur_freq, clk_incval, tu_per_sec, phy_tus;
3824 	enum ice_ptp_link_spd link_spd;
3825 	enum ice_ptp_fec_mode fec_mode;
3826 	int err;
3827 
3828 	err = ice_phy_get_speed_and_fec_e82x(hw, port, &link_spd, &fec_mode);
3829 	if (err)
3830 		return err;
3831 
3832 	cur_freq = ice_e82x_pll_freq(ice_e82x_time_ref(hw));
3833 	clk_incval = ice_ptp_read_src_incval(hw);
3834 
3835 	/* Calculate TUs per cycle of the PHC clock */
3836 	tu_per_sec = cur_freq * clk_incval;
3837 
3838 	/* For each PHY conversion register, look up the appropriate link
3839 	 * speed frequency and determine the TUs per that clock's cycle time.
3840 	 * Split this into a high and low value and then program the
3841 	 * appropriate register. If that link speed does not use the
3842 	 * associated register, write zeros to clear it instead.
3843 	 */
3844 
3845 	/* P_REG_PAR_TX_TUS */
3846 	if (e822_vernier[link_spd].tx_par_clk)
3847 		phy_tus = div_u64(tu_per_sec,
3848 				  e822_vernier[link_spd].tx_par_clk);
3849 	else
3850 		phy_tus = 0;
3851 
3852 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_PAR_TX_TUS_L,
3853 					 phy_tus);
3854 	if (err)
3855 		return err;
3856 
3857 	/* P_REG_PAR_RX_TUS */
3858 	if (e822_vernier[link_spd].rx_par_clk)
3859 		phy_tus = div_u64(tu_per_sec,
3860 				  e822_vernier[link_spd].rx_par_clk);
3861 	else
3862 		phy_tus = 0;
3863 
3864 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_PAR_RX_TUS_L,
3865 					 phy_tus);
3866 	if (err)
3867 		return err;
3868 
3869 	/* P_REG_PCS_TX_TUS */
3870 	if (e822_vernier[link_spd].tx_pcs_clk)
3871 		phy_tus = div_u64(tu_per_sec,
3872 				  e822_vernier[link_spd].tx_pcs_clk);
3873 	else
3874 		phy_tus = 0;
3875 
3876 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_PCS_TX_TUS_L,
3877 					 phy_tus);
3878 	if (err)
3879 		return err;
3880 
3881 	/* P_REG_PCS_RX_TUS */
3882 	if (e822_vernier[link_spd].rx_pcs_clk)
3883 		phy_tus = div_u64(tu_per_sec,
3884 				  e822_vernier[link_spd].rx_pcs_clk);
3885 	else
3886 		phy_tus = 0;
3887 
3888 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_PCS_RX_TUS_L,
3889 					 phy_tus);
3890 	if (err)
3891 		return err;
3892 
3893 	/* P_REG_DESK_PAR_TX_TUS */
3894 	if (e822_vernier[link_spd].tx_desk_rsgb_par)
3895 		phy_tus = div_u64(tu_per_sec,
3896 				  e822_vernier[link_spd].tx_desk_rsgb_par);
3897 	else
3898 		phy_tus = 0;
3899 
3900 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_DESK_PAR_TX_TUS_L,
3901 					 phy_tus);
3902 	if (err)
3903 		return err;
3904 
3905 	/* P_REG_DESK_PAR_RX_TUS */
3906 	if (e822_vernier[link_spd].rx_desk_rsgb_par)
3907 		phy_tus = div_u64(tu_per_sec,
3908 				  e822_vernier[link_spd].rx_desk_rsgb_par);
3909 	else
3910 		phy_tus = 0;
3911 
3912 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_DESK_PAR_RX_TUS_L,
3913 					 phy_tus);
3914 	if (err)
3915 		return err;
3916 
3917 	/* P_REG_DESK_PCS_TX_TUS */
3918 	if (e822_vernier[link_spd].tx_desk_rsgb_pcs)
3919 		phy_tus = div_u64(tu_per_sec,
3920 				  e822_vernier[link_spd].tx_desk_rsgb_pcs);
3921 	else
3922 		phy_tus = 0;
3923 
3924 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_DESK_PCS_TX_TUS_L,
3925 					 phy_tus);
3926 	if (err)
3927 		return err;
3928 
3929 	/* P_REG_DESK_PCS_RX_TUS */
3930 	if (e822_vernier[link_spd].rx_desk_rsgb_pcs)
3931 		phy_tus = div_u64(tu_per_sec,
3932 				  e822_vernier[link_spd].rx_desk_rsgb_pcs);
3933 	else
3934 		phy_tus = 0;
3935 
3936 	return ice_write_40b_phy_reg_e82x(hw, port, P_REG_DESK_PCS_RX_TUS_L,
3937 					  phy_tus);
3938 }
3939 
3940 /**
3941  * ice_calc_fixed_tx_offset_e82x - Calculated Fixed Tx offset for a port
3942  * @hw: pointer to the HW struct
3943  * @link_spd: the Link speed to calculate for
3944  *
3945  * Calculate the fixed offset due to known static latency data.
3946  */
3947 static u64
ice_calc_fixed_tx_offset_e82x(struct ice_hw * hw,enum ice_ptp_link_spd link_spd)3948 ice_calc_fixed_tx_offset_e82x(struct ice_hw *hw, enum ice_ptp_link_spd link_spd)
3949 {
3950 	u64 cur_freq, clk_incval, tu_per_sec, fixed_offset;
3951 
3952 	cur_freq = ice_e82x_pll_freq(ice_e82x_time_ref(hw));
3953 	clk_incval = ice_ptp_read_src_incval(hw);
3954 
3955 	/* Calculate TUs per second */
3956 	tu_per_sec = cur_freq * clk_incval;
3957 
3958 	/* Calculate number of TUs to add for the fixed Tx latency. Since the
3959 	 * latency measurement is in 1/100th of a nanosecond, we need to
3960 	 * multiply by tu_per_sec and then divide by 1e11. This calculation
3961 	 * overflows 64 bit integer arithmetic, so break it up into two
3962 	 * divisions by 1e4 first then by 1e7.
3963 	 */
3964 	fixed_offset = div_u64(tu_per_sec, 10000);
3965 	fixed_offset *= e822_vernier[link_spd].tx_fixed_delay;
3966 	fixed_offset = div_u64(fixed_offset, 10000000);
3967 
3968 	return fixed_offset;
3969 }
3970 
3971 /**
3972  * ice_phy_cfg_tx_offset_e82x - Configure total Tx timestamp offset
3973  * @hw: pointer to the HW struct
3974  * @port: the PHY port to configure
3975  *
3976  * Program the P_REG_TOTAL_TX_OFFSET register with the total number of TUs to
3977  * adjust Tx timestamps by. This is calculated by combining some known static
3978  * latency along with the Vernier offset computations done by hardware.
3979  *
3980  * This function will not return successfully until the Tx offset calculations
3981  * have been completed, which requires waiting until at least one packet has
3982  * been transmitted by the device. It is safe to call this function
3983  * periodically until calibration succeeds, as it will only program the offset
3984  * once.
3985  *
3986  * To avoid overflow, when calculating the offset based on the known static
3987  * latency values, we use measurements in 1/100th of a nanosecond, and divide
3988  * the TUs per second up front. This avoids overflow while allowing
3989  * calculation of the adjustment using integer arithmetic.
3990  *
3991  * Returns zero on success, -EBUSY if the hardware vernier offset
3992  * calibration has not completed, or another error code on failure.
3993  */
ice_phy_cfg_tx_offset_e82x(struct ice_hw * hw,u8 port)3994 int ice_phy_cfg_tx_offset_e82x(struct ice_hw *hw, u8 port)
3995 {
3996 	enum ice_ptp_link_spd link_spd;
3997 	enum ice_ptp_fec_mode fec_mode;
3998 	u64 total_offset, val;
3999 	int err;
4000 	u32 reg;
4001 
4002 	/* Nothing to do if we've already programmed the offset */
4003 	err = ice_read_phy_reg_e82x(hw, port, P_REG_TX_OR, ®);
4004 	if (err) {
4005 		ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_OR for port %u, err %d\n",
4006 			  port, err);
4007 		return err;
4008 	}
4009 
4010 	if (reg)
4011 		return 0;
4012 
4013 	err = ice_read_phy_reg_e82x(hw, port, P_REG_TX_OV_STATUS, ®);
4014 	if (err) {
4015 		ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_OV_STATUS for port %u, err %d\n",
4016 			  port, err);
4017 		return err;
4018 	}
4019 
4020 	if (!(reg & P_REG_TX_OV_STATUS_OV_M))
4021 		return -EBUSY;
4022 
4023 	err = ice_phy_get_speed_and_fec_e82x(hw, port, &link_spd, &fec_mode);
4024 	if (err)
4025 		return err;
4026 
4027 	total_offset = ice_calc_fixed_tx_offset_e82x(hw, link_spd);
4028 
4029 	/* Read the first Vernier offset from the PHY register and add it to
4030 	 * the total offset.
4031 	 */
4032 	if (link_spd == ICE_PTP_LNK_SPD_1G ||
4033 	    link_spd == ICE_PTP_LNK_SPD_10G ||
4034 	    link_spd == ICE_PTP_LNK_SPD_25G ||
4035 	    link_spd == ICE_PTP_LNK_SPD_25G_RS ||
4036 	    link_spd == ICE_PTP_LNK_SPD_40G ||
4037 	    link_spd == ICE_PTP_LNK_SPD_50G) {
4038 		err = ice_read_64b_phy_reg_e82x(hw, port,
4039 						P_REG_PAR_PCS_TX_OFFSET_L,
4040 						&val);
4041 		if (err)
4042 			return err;
4043 
4044 		total_offset += val;
4045 	}
4046 
4047 	/* For Tx, we only need to use the second Vernier offset for
4048 	 * multi-lane link speeds with RS-FEC. The lanes will always be
4049 	 * aligned.
4050 	 */
4051 	if (link_spd == ICE_PTP_LNK_SPD_50G_RS ||
4052 	    link_spd == ICE_PTP_LNK_SPD_100G_RS) {
4053 		err = ice_read_64b_phy_reg_e82x(hw, port,
4054 						P_REG_PAR_TX_TIME_L,
4055 						&val);
4056 		if (err)
4057 			return err;
4058 
4059 		total_offset += val;
4060 	}
4061 
4062 	/* Now that the total offset has been calculated, program it to the
4063 	 * PHY and indicate that the Tx offset is ready. After this,
4064 	 * timestamps will be enabled.
4065 	 */
4066 	err = ice_write_64b_phy_reg_e82x(hw, port, P_REG_TOTAL_TX_OFFSET_L,
4067 					 total_offset);
4068 	if (err)
4069 		return err;
4070 
4071 	err = ice_write_phy_reg_e82x(hw, port, P_REG_TX_OR, 1);
4072 	if (err)
4073 		return err;
4074 
4075 	dev_info(ice_hw_to_dev(hw), "Port=%d Tx vernier offset calibration complete\n",
4076 		 port);
4077 
4078 	return 0;
4079 }
4080 
4081 /**
4082  * ice_phy_calc_pmd_adj_e82x - Calculate PMD adjustment for Rx
4083  * @hw: pointer to the HW struct
4084  * @port: the PHY port to adjust for
4085  * @link_spd: the current link speed of the PHY
4086  * @fec_mode: the current FEC mode of the PHY
4087  * @pmd_adj: on return, the amount to adjust the Rx total offset by
4088  *
4089  * Calculates the adjustment to Rx timestamps due to PMD alignment in the PHY.
4090  * This varies by link speed and FEC mode. The value calculated accounts for
4091  * various delays caused when receiving a packet.
4092  */
4093 static int
ice_phy_calc_pmd_adj_e82x(struct ice_hw * hw,u8 port,enum ice_ptp_link_spd link_spd,enum ice_ptp_fec_mode fec_mode,u64 * pmd_adj)4094 ice_phy_calc_pmd_adj_e82x(struct ice_hw *hw, u8 port,
4095 			  enum ice_ptp_link_spd link_spd,
4096 			  enum ice_ptp_fec_mode fec_mode, u64 *pmd_adj)
4097 {
4098 	u64 cur_freq, clk_incval, tu_per_sec, mult, adj;
4099 	u8 pmd_align;
4100 	u32 val;
4101 	int err;
4102 
4103 	err = ice_read_phy_reg_e82x(hw, port, P_REG_PMD_ALIGNMENT, &val);
4104 	if (err) {
4105 		ice_debug(hw, ICE_DBG_PTP, "Failed to read PMD alignment, err %d\n",
4106 			  err);
4107 		return err;
4108 	}
4109 
4110 	pmd_align = (u8)val;
4111 
4112 	cur_freq = ice_e82x_pll_freq(ice_e82x_time_ref(hw));
4113 	clk_incval = ice_ptp_read_src_incval(hw);
4114 
4115 	/* Calculate TUs per second */
4116 	tu_per_sec = cur_freq * clk_incval;
4117 
4118 	/* The PMD alignment adjustment measurement depends on the link speed,
4119 	 * and whether FEC is enabled. For each link speed, the alignment
4120 	 * adjustment is calculated by dividing a value by the length of
4121 	 * a Time Unit in nanoseconds.
4122 	 *
4123 	 * 1G: align == 4 ? 10 * 0.8 : (align + 6 % 10) * 0.8
4124 	 * 10G: align == 65 ? 0 : (align * 0.1 * 32/33)
4125 	 * 10G w/FEC: align * 0.1 * 32/33
4126 	 * 25G: align == 65 ? 0 : (align * 0.4 * 32/33)
4127 	 * 25G w/FEC: align * 0.4 * 32/33
4128 	 * 40G: align == 65 ? 0 : (align * 0.1 * 32/33)
4129 	 * 40G w/FEC: align * 0.1 * 32/33
4130 	 * 50G: align == 65 ? 0 : (align * 0.4 * 32/33)
4131 	 * 50G w/FEC: align * 0.8 * 32/33
4132 	 *
4133 	 * For RS-FEC, if align is < 17 then we must also add 1.6 * 32/33.
4134 	 *
4135 	 * To allow for calculating this value using integer arithmetic, we
4136 	 * instead start with the number of TUs per second, (inverse of the
4137 	 * length of a Time Unit in nanoseconds), multiply by a value based
4138 	 * on the PMD alignment register, and then divide by the right value
4139 	 * calculated based on the table above. To avoid integer overflow this
4140 	 * division is broken up into a step of dividing by 125 first.
4141 	 */
4142 	if (link_spd == ICE_PTP_LNK_SPD_1G) {
4143 		if (pmd_align == 4)
4144 			mult = 10;
4145 		else
4146 			mult = (pmd_align + 6) % 10;
4147 	} else if (link_spd == ICE_PTP_LNK_SPD_10G ||
4148 		   link_spd == ICE_PTP_LNK_SPD_25G ||
4149 		   link_spd == ICE_PTP_LNK_SPD_40G ||
4150 		   link_spd == ICE_PTP_LNK_SPD_50G) {
4151 		/* If Clause 74 FEC, always calculate PMD adjust */
4152 		if (pmd_align != 65 || fec_mode == ICE_PTP_FEC_MODE_CLAUSE74)
4153 			mult = pmd_align;
4154 		else
4155 			mult = 0;
4156 	} else if (link_spd == ICE_PTP_LNK_SPD_25G_RS ||
4157 		   link_spd == ICE_PTP_LNK_SPD_50G_RS ||
4158 		   link_spd == ICE_PTP_LNK_SPD_100G_RS) {
4159 		if (pmd_align < 17)
4160 			mult = pmd_align + 40;
4161 		else
4162 			mult = pmd_align;
4163 	} else {
4164 		ice_debug(hw, ICE_DBG_PTP, "Unknown link speed %d, skipping PMD adjustment\n",
4165 			  link_spd);
4166 		mult = 0;
4167 	}
4168 
4169 	/* In some cases, there's no need to adjust for the PMD alignment */
4170 	if (!mult) {
4171 		*pmd_adj = 0;
4172 		return 0;
4173 	}
4174 
4175 	/* Calculate the adjustment by multiplying TUs per second by the
4176 	 * appropriate multiplier and divisor. To avoid overflow, we first
4177 	 * divide by 125, and then handle remaining divisor based on the link
4178 	 * speed pmd_adj_divisor value.
4179 	 */
4180 	adj = div_u64(tu_per_sec, 125);
4181 	adj *= mult;
4182 	adj = div_u64(adj, e822_vernier[link_spd].pmd_adj_divisor);
4183 
4184 	/* Finally, for 25G-RS and 50G-RS, a further adjustment for the Rx
4185 	 * cycle count is necessary.
4186 	 */
4187 	if (link_spd == ICE_PTP_LNK_SPD_25G_RS) {
4188 		u64 cycle_adj;
4189 		u8 rx_cycle;
4190 
4191 		err = ice_read_phy_reg_e82x(hw, port, P_REG_RX_40_TO_160_CNT,
4192 					    &val);
4193 		if (err) {
4194 			ice_debug(hw, ICE_DBG_PTP, "Failed to read 25G-RS Rx cycle count, err %d\n",
4195 				  err);
4196 			return err;
4197 		}
4198 
4199 		rx_cycle = val & P_REG_RX_40_TO_160_CNT_RXCYC_M;
4200 		if (rx_cycle) {
4201 			mult = (4 - rx_cycle) * 40;
4202 
4203 			cycle_adj = div_u64(tu_per_sec, 125);
4204 			cycle_adj *= mult;
4205 			cycle_adj = div_u64(cycle_adj, e822_vernier[link_spd].pmd_adj_divisor);
4206 
4207 			adj += cycle_adj;
4208 		}
4209 	} else if (link_spd == ICE_PTP_LNK_SPD_50G_RS) {
4210 		u64 cycle_adj;
4211 		u8 rx_cycle;
4212 
4213 		err = ice_read_phy_reg_e82x(hw, port, P_REG_RX_80_TO_160_CNT,
4214 					    &val);
4215 		if (err) {
4216 			ice_debug(hw, ICE_DBG_PTP, "Failed to read 50G-RS Rx cycle count, err %d\n",
4217 				  err);
4218 			return err;
4219 		}
4220 
4221 		rx_cycle = val & P_REG_RX_80_TO_160_CNT_RXCYC_M;
4222 		if (rx_cycle) {
4223 			mult = rx_cycle * 40;
4224 
4225 			cycle_adj = div_u64(tu_per_sec, 125);
4226 			cycle_adj *= mult;
4227 			cycle_adj = div_u64(cycle_adj, e822_vernier[link_spd].pmd_adj_divisor);
4228 
4229 			adj += cycle_adj;
4230 		}
4231 	}
4232 
4233 	/* Return the calculated adjustment */
4234 	*pmd_adj = adj;
4235 
4236 	return 0;
4237 }
4238 
4239 /**
4240  * ice_calc_fixed_rx_offset_e82x - Calculated the fixed Rx offset for a port
4241  * @hw: pointer to HW struct
4242  * @link_spd: The Link speed to calculate for
4243  *
4244  * Determine the fixed Rx latency for a given link speed.
4245  */
4246 static u64
ice_calc_fixed_rx_offset_e82x(struct ice_hw * hw,enum ice_ptp_link_spd link_spd)4247 ice_calc_fixed_rx_offset_e82x(struct ice_hw *hw, enum ice_ptp_link_spd link_spd)
4248 {
4249 	u64 cur_freq, clk_incval, tu_per_sec, fixed_offset;
4250 
4251 	cur_freq = ice_e82x_pll_freq(ice_e82x_time_ref(hw));
4252 	clk_incval = ice_ptp_read_src_incval(hw);
4253 
4254 	/* Calculate TUs per second */
4255 	tu_per_sec = cur_freq * clk_incval;
4256 
4257 	/* Calculate number of TUs to add for the fixed Rx latency. Since the
4258 	 * latency measurement is in 1/100th of a nanosecond, we need to
4259 	 * multiply by tu_per_sec and then divide by 1e11. This calculation
4260 	 * overflows 64 bit integer arithmetic, so break it up into two
4261 	 * divisions by 1e4 first then by 1e7.
4262 	 */
4263 	fixed_offset = div_u64(tu_per_sec, 10000);
4264 	fixed_offset *= e822_vernier[link_spd].rx_fixed_delay;
4265 	fixed_offset = div_u64(fixed_offset, 10000000);
4266 
4267 	return fixed_offset;
4268 }
4269 
4270 /**
4271  * ice_phy_cfg_rx_offset_e82x - Configure total Rx timestamp offset
4272  * @hw: pointer to the HW struct
4273  * @port: the PHY port to configure
4274  *
4275  * Program the P_REG_TOTAL_RX_OFFSET register with the number of Time Units to
4276  * adjust Rx timestamps by. This combines calculations from the Vernier offset
4277  * measurements taken in hardware with some data about known fixed delay as
4278  * well as adjusting for multi-lane alignment delay.
4279  *
4280  * This function will not return successfully until the Rx offset calculations
4281  * have been completed, which requires waiting until at least one packet has
4282  * been received by the device. It is safe to call this function periodically
4283  * until calibration succeeds, as it will only program the offset once.
4284  *
4285  * This function must be called only after the offset registers are valid,
4286  * i.e. after the Vernier calibration wait has passed, to ensure that the PHY
4287  * has measured the offset.
4288  *
4289  * To avoid overflow, when calculating the offset based on the known static
4290  * latency values, we use measurements in 1/100th of a nanosecond, and divide
4291  * the TUs per second up front. This avoids overflow while allowing
4292  * calculation of the adjustment using integer arithmetic.
4293  *
4294  * Returns zero on success, -EBUSY if the hardware vernier offset
4295  * calibration has not completed, or another error code on failure.
4296  */
ice_phy_cfg_rx_offset_e82x(struct ice_hw * hw,u8 port)4297 int ice_phy_cfg_rx_offset_e82x(struct ice_hw *hw, u8 port)
4298 {
4299 	enum ice_ptp_link_spd link_spd;
4300 	enum ice_ptp_fec_mode fec_mode;
4301 	u64 total_offset, pmd, val;
4302 	int err;
4303 	u32 reg;
4304 
4305 	/* Nothing to do if we've already programmed the offset */
4306 	err = ice_read_phy_reg_e82x(hw, port, P_REG_RX_OR, ®);
4307 	if (err) {
4308 		ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_OR for port %u, err %d\n",
4309 			  port, err);
4310 		return err;
4311 	}
4312 
4313 	if (reg)
4314 		return 0;
4315 
4316 	err = ice_read_phy_reg_e82x(hw, port, P_REG_RX_OV_STATUS, ®);
4317 	if (err) {
4318 		ice_debug(hw, ICE_DBG_PTP, "Failed to read RX_OV_STATUS for port %u, err %d\n",
4319 			  port, err);
4320 		return err;
4321 	}
4322 
4323 	if (!(reg & P_REG_RX_OV_STATUS_OV_M))
4324 		return -EBUSY;
4325 
4326 	err = ice_phy_get_speed_and_fec_e82x(hw, port, &link_spd, &fec_mode);
4327 	if (err)
4328 		return err;
4329 
4330 	total_offset = ice_calc_fixed_rx_offset_e82x(hw, link_spd);
4331 
4332 	/* Read the first Vernier offset from the PHY register and add it to
4333 	 * the total offset.
4334 	 */
4335 	err = ice_read_64b_phy_reg_e82x(hw, port,
4336 					P_REG_PAR_PCS_RX_OFFSET_L,
4337 					&val);
4338 	if (err)
4339 		return err;
4340 
4341 	total_offset += val;
4342 
4343 	/* For Rx, all multi-lane link speeds include a second Vernier
4344 	 * calibration, because the lanes might not be aligned.
4345 	 */
4346 	if (link_spd == ICE_PTP_LNK_SPD_40G ||
4347 	    link_spd == ICE_PTP_LNK_SPD_50G ||
4348 	    link_spd == ICE_PTP_LNK_SPD_50G_RS ||
4349 	    link_spd == ICE_PTP_LNK_SPD_100G_RS) {
4350 		err = ice_read_64b_phy_reg_e82x(hw, port,
4351 						P_REG_PAR_RX_TIME_L,
4352 						&val);
4353 		if (err)
4354 			return err;
4355 
4356 		total_offset += val;
4357 	}
4358 
4359 	/* In addition, Rx must account for the PMD alignment */
4360 	err = ice_phy_calc_pmd_adj_e82x(hw, port, link_spd, fec_mode, &pmd);
4361 	if (err)
4362 		return err;
4363 
4364 	/* For RS-FEC, this adjustment adds delay, but for other modes, it
4365 	 * subtracts delay.
4366 	 */
4367 	if (fec_mode == ICE_PTP_FEC_MODE_RS_FEC)
4368 		total_offset += pmd;
4369 	else
4370 		total_offset -= pmd;
4371 
4372 	/* Now that the total offset has been calculated, program it to the
4373 	 * PHY and indicate that the Rx offset is ready. After this,
4374 	 * timestamps will be enabled.
4375 	 */
4376 	err = ice_write_64b_phy_reg_e82x(hw, port, P_REG_TOTAL_RX_OFFSET_L,
4377 					 total_offset);
4378 	if (err)
4379 		return err;
4380 
4381 	err = ice_write_phy_reg_e82x(hw, port, P_REG_RX_OR, 1);
4382 	if (err)
4383 		return err;
4384 
4385 	dev_info(ice_hw_to_dev(hw), "Port=%d Rx vernier offset calibration complete\n",
4386 		 port);
4387 
4388 	return 0;
4389 }
4390 
4391 /**
4392  * ice_ptp_clear_phy_offset_ready_e82x - Clear PHY TX_/RX_OFFSET_READY registers
4393  * @hw: pointer to the HW struct
4394  *
4395  * Clear PHY TX_/RX_OFFSET_READY registers, effectively marking all transmitted
4396  * and received timestamps as invalid.
4397  *
4398  * Return: 0 on success, other error codes when failed to write to PHY
4399  */
ice_ptp_clear_phy_offset_ready_e82x(struct ice_hw * hw)4400 int ice_ptp_clear_phy_offset_ready_e82x(struct ice_hw *hw)
4401 {
4402 	u8 port;
4403 
4404 	for (port = 0; port < hw->ptp.num_lports; port++) {
4405 		int err;
4406 
4407 		err = ice_write_phy_reg_e82x(hw, port, P_REG_TX_OR, 0);
4408 		if (err) {
4409 			dev_warn(ice_hw_to_dev(hw),
4410 				 "Failed to clear PHY TX_OFFSET_READY register\n");
4411 			return err;
4412 		}
4413 
4414 		err = ice_write_phy_reg_e82x(hw, port, P_REG_RX_OR, 0);
4415 		if (err) {
4416 			dev_warn(ice_hw_to_dev(hw),
4417 				 "Failed to clear PHY RX_OFFSET_READY register\n");
4418 			return err;
4419 		}
4420 	}
4421 
4422 	return 0;
4423 }
4424 
4425 /**
4426  * ice_read_phy_and_phc_time_e82x - Simultaneously capture PHC and PHY time
4427  * @hw: pointer to the HW struct
4428  * @port: the PHY port to read
4429  * @phy_time: on return, the 64bit PHY timer value
4430  * @phc_time: on return, the lower 64bits of PHC time
4431  *
4432  * Issue a ICE_PTP_READ_TIME timer command to simultaneously capture the PHY
4433  * and PHC timer values.
4434  */
4435 static int
ice_read_phy_and_phc_time_e82x(struct ice_hw * hw,u8 port,u64 * phy_time,u64 * phc_time)4436 ice_read_phy_and_phc_time_e82x(struct ice_hw *hw, u8 port, u64 *phy_time,
4437 			       u64 *phc_time)
4438 {
4439 	u64 tx_time, rx_time;
4440 	u32 zo, lo;
4441 	u8 tmr_idx;
4442 	int err;
4443 
4444 	tmr_idx = ice_get_ptp_src_clock_index(hw);
4445 
4446 	/* Prepare the PHC timer for a ICE_PTP_READ_TIME capture command */
4447 	ice_ptp_src_cmd(hw, ICE_PTP_READ_TIME);
4448 
4449 	/* Prepare the PHY timer for a ICE_PTP_READ_TIME capture command */
4450 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_READ_TIME);
4451 	if (err)
4452 		return err;
4453 
4454 	/* Issue the sync to start the ICE_PTP_READ_TIME capture */
4455 	ice_ptp_exec_tmr_cmd(hw);
4456 
4457 	/* Read the captured PHC time from the shadow time registers */
4458 	zo = rd32(hw, GLTSYN_SHTIME_0(tmr_idx));
4459 	lo = rd32(hw, GLTSYN_SHTIME_L(tmr_idx));
4460 	*phc_time = (u64)lo << 32 | zo;
4461 
4462 	/* Read the captured PHY time from the PHY shadow registers */
4463 	err = ice_ptp_read_port_capture(hw, port, &tx_time, &rx_time);
4464 	if (err)
4465 		return err;
4466 
4467 	/* If the PHY Tx and Rx timers don't match, log a warning message.
4468 	 * Note that this should not happen in normal circumstances since the
4469 	 * driver always programs them together.
4470 	 */
4471 	if (tx_time != rx_time)
4472 		dev_warn(ice_hw_to_dev(hw),
4473 			 "PHY port %u Tx and Rx timers do not match, tx_time 0x%016llX, rx_time 0x%016llX\n",
4474 			 port, (unsigned long long)tx_time,
4475 			 (unsigned long long)rx_time);
4476 
4477 	*phy_time = tx_time;
4478 
4479 	return 0;
4480 }
4481 
4482 /**
4483  * ice_sync_phy_timer_e82x - Synchronize the PHY timer with PHC timer
4484  * @hw: pointer to the HW struct
4485  * @port: the PHY port to synchronize
4486  *
4487  * Perform an adjustment to ensure that the PHY and PHC timers are in sync.
4488  * This is done by issuing a ICE_PTP_READ_TIME command which triggers a
4489  * simultaneous read of the PHY timer and PHC timer. Then we use the
4490  * difference to calculate an appropriate 2s complement addition to add
4491  * to the PHY timer in order to ensure it reads the same value as the
4492  * primary PHC timer.
4493  */
ice_sync_phy_timer_e82x(struct ice_hw * hw,u8 port)4494 static int ice_sync_phy_timer_e82x(struct ice_hw *hw, u8 port)
4495 {
4496 	u64 phc_time, phy_time, difference;
4497 	int err;
4498 
4499 	if (!ice_ptp_lock(hw)) {
4500 		ice_debug(hw, ICE_DBG_PTP, "Failed to acquire PTP semaphore\n");
4501 		return -EBUSY;
4502 	}
4503 
4504 	err = ice_read_phy_and_phc_time_e82x(hw, port, &phy_time, &phc_time);
4505 	if (err)
4506 		goto err_unlock;
4507 
4508 	/* Calculate the amount required to add to the port time in order for
4509 	 * it to match the PHC time.
4510 	 *
4511 	 * Note that the port adjustment is done using 2s complement
4512 	 * arithmetic. This is convenient since it means that we can simply
4513 	 * calculate the difference between the PHC time and the port time,
4514 	 * and it will be interpreted correctly.
4515 	 */
4516 	difference = phc_time - phy_time;
4517 
4518 	err = ice_ptp_prep_port_adj_e82x(hw, port, (s64)difference);
4519 	if (err)
4520 		goto err_unlock;
4521 
4522 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_ADJ_TIME);
4523 	if (err)
4524 		goto err_unlock;
4525 
4526 	/* Do not perform any action on the main timer */
4527 	ice_ptp_src_cmd(hw, ICE_PTP_NOP);
4528 
4529 	/* Issue the sync to activate the time adjustment */
4530 	ice_ptp_exec_tmr_cmd(hw);
4531 
4532 	/* Re-capture the timer values to flush the command registers and
4533 	 * verify that the time was properly adjusted.
4534 	 */
4535 	err = ice_read_phy_and_phc_time_e82x(hw, port, &phy_time, &phc_time);
4536 	if (err)
4537 		goto err_unlock;
4538 
4539 	dev_info(ice_hw_to_dev(hw),
4540 		 "Port %u PHY time synced to PHC: 0x%016llX, 0x%016llX\n",
4541 		 port, (unsigned long long)phy_time,
4542 		 (unsigned long long)phc_time);
4543 
4544 	ice_ptp_unlock(hw);
4545 
4546 	return 0;
4547 
4548 err_unlock:
4549 	ice_ptp_unlock(hw);
4550 	return err;
4551 }
4552 
4553 /**
4554  * ice_stop_phy_timer_e82x - Stop the PHY clock timer
4555  * @hw: pointer to the HW struct
4556  * @port: the PHY port to stop
4557  * @soft_reset: if true, hold the SOFT_RESET bit of P_REG_PS
4558  *
4559  * Stop the clock of a PHY port. This must be done as part of the flow to
4560  * re-calibrate Tx and Rx timestamping offsets whenever the clock time is
4561  * initialized or when link speed changes.
4562  */
4563 int
ice_stop_phy_timer_e82x(struct ice_hw * hw,u8 port,bool soft_reset)4564 ice_stop_phy_timer_e82x(struct ice_hw *hw, u8 port, bool soft_reset)
4565 {
4566 	int err;
4567 	u32 val;
4568 
4569 	err = ice_write_phy_reg_e82x(hw, port, P_REG_TX_OR, 0);
4570 	if (err)
4571 		return err;
4572 
4573 	err = ice_write_phy_reg_e82x(hw, port, P_REG_RX_OR, 0);
4574 	if (err)
4575 		return err;
4576 
4577 	err = ice_read_phy_reg_e82x(hw, port, P_REG_PS, &val);
4578 	if (err)
4579 		return err;
4580 
4581 	val &= ~P_REG_PS_START_M;
4582 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4583 	if (err)
4584 		return err;
4585 
4586 	val &= ~P_REG_PS_ENA_CLK_M;
4587 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4588 	if (err)
4589 		return err;
4590 
4591 	if (soft_reset) {
4592 		val |= P_REG_PS_SFT_RESET_M;
4593 		err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4594 		if (err)
4595 			return err;
4596 	}
4597 
4598 	ice_debug(hw, ICE_DBG_PTP, "Disabled clock on PHY port %u\n", port);
4599 
4600 	return 0;
4601 }
4602 
4603 /**
4604  * ice_start_phy_timer_e82x - Start the PHY clock timer
4605  * @hw: pointer to the HW struct
4606  * @port: the PHY port to start
4607  *
4608  * Start the clock of a PHY port. This must be done as part of the flow to
4609  * re-calibrate Tx and Rx timestamping offsets whenever the clock time is
4610  * initialized or when link speed changes.
4611  *
4612  * Hardware will take Vernier measurements on Tx or Rx of packets.
4613  */
ice_start_phy_timer_e82x(struct ice_hw * hw,u8 port)4614 int ice_start_phy_timer_e82x(struct ice_hw *hw, u8 port)
4615 {
4616 	u32 lo, hi, val;
4617 	u64 incval;
4618 	u8 tmr_idx;
4619 	int err;
4620 
4621 	tmr_idx = ice_get_ptp_src_clock_index(hw);
4622 
4623 	err = ice_stop_phy_timer_e82x(hw, port, false);
4624 	if (err)
4625 		return err;
4626 
4627 	ice_phy_cfg_lane_e82x(hw, port);
4628 
4629 	err = ice_phy_cfg_uix_e82x(hw, port);
4630 	if (err)
4631 		return err;
4632 
4633 	err = ice_phy_cfg_parpcs_e82x(hw, port);
4634 	if (err)
4635 		return err;
4636 
4637 	lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
4638 	hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
4639 	incval = (u64)hi << 32 | lo;
4640 
4641 	err = ice_write_40b_phy_reg_e82x(hw, port, P_REG_TIMETUS_L, incval);
4642 	if (err)
4643 		return err;
4644 
4645 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_INIT_INCVAL);
4646 	if (err)
4647 		return err;
4648 
4649 	/* Do not perform any action on the main timer */
4650 	ice_ptp_src_cmd(hw, ICE_PTP_NOP);
4651 
4652 	ice_ptp_exec_tmr_cmd(hw);
4653 
4654 	err = ice_read_phy_reg_e82x(hw, port, P_REG_PS, &val);
4655 	if (err)
4656 		return err;
4657 
4658 	val |= P_REG_PS_SFT_RESET_M;
4659 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4660 	if (err)
4661 		return err;
4662 
4663 	val |= P_REG_PS_START_M;
4664 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4665 	if (err)
4666 		return err;
4667 
4668 	val &= ~P_REG_PS_SFT_RESET_M;
4669 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4670 	if (err)
4671 		return err;
4672 
4673 	err = ice_ptp_one_port_cmd(hw, port, ICE_PTP_INIT_INCVAL);
4674 	if (err)
4675 		return err;
4676 
4677 	ice_ptp_exec_tmr_cmd(hw);
4678 
4679 	val |= P_REG_PS_ENA_CLK_M;
4680 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4681 	if (err)
4682 		return err;
4683 
4684 	val |= P_REG_PS_LOAD_OFFSET_M;
4685 	err = ice_write_phy_reg_e82x(hw, port, P_REG_PS, val);
4686 	if (err)
4687 		return err;
4688 
4689 	ice_ptp_exec_tmr_cmd(hw);
4690 
4691 	err = ice_sync_phy_timer_e82x(hw, port);
4692 	if (err)
4693 		return err;
4694 
4695 	ice_debug(hw, ICE_DBG_PTP, "Enabled clock on PHY port %u\n", port);
4696 
4697 	return 0;
4698 }
4699 
4700 /**
4701  * ice_get_phy_tx_tstamp_ready_e82x - Read Tx memory status register
4702  * @hw: pointer to the HW struct
4703  * @quad: the timestamp quad to read from
4704  * @tstamp_ready: contents of the Tx memory status register
4705  *
4706  * Read the Q_REG_TX_MEMORY_STATUS register indicating which timestamps in
4707  * the PHY are ready. A set bit means the corresponding timestamp is valid and
4708  * ready to be captured from the PHY timestamp block.
4709  */
4710 static int
ice_get_phy_tx_tstamp_ready_e82x(struct ice_hw * hw,u8 quad,u64 * tstamp_ready)4711 ice_get_phy_tx_tstamp_ready_e82x(struct ice_hw *hw, u8 quad, u64 *tstamp_ready)
4712 {
4713 	u32 hi, lo;
4714 	int err;
4715 
4716 	err = ice_read_quad_reg_e82x(hw, quad, Q_REG_TX_MEMORY_STATUS_U, &hi);
4717 	if (err) {
4718 		ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEMORY_STATUS_U for quad %u, err %d\n",
4719 			  quad, err);
4720 		return err;
4721 	}
4722 
4723 	err = ice_read_quad_reg_e82x(hw, quad, Q_REG_TX_MEMORY_STATUS_L, &lo);
4724 	if (err) {
4725 		ice_debug(hw, ICE_DBG_PTP, "Failed to read TX_MEMORY_STATUS_L for quad %u, err %d\n",
4726 			  quad, err);
4727 		return err;
4728 	}
4729 
4730 	*tstamp_ready = (u64)hi << 32 | (u64)lo;
4731 
4732 	return 0;
4733 }
4734 
4735 /**
4736  * ice_phy_cfg_intr_e82x - Configure TX timestamp interrupt
4737  * @hw: pointer to the HW struct
4738  * @quad: the timestamp quad
4739  * @ena: enable or disable interrupt
4740  * @threshold: interrupt threshold
4741  *
4742  * Configure TX timestamp interrupt for the specified quad
4743  *
4744  * Return: 0 on success, other error codes when failed to read/write quad
4745  */
4746 
ice_phy_cfg_intr_e82x(struct ice_hw * hw,u8 quad,bool ena,u8 threshold)4747 int ice_phy_cfg_intr_e82x(struct ice_hw *hw, u8 quad, bool ena, u8 threshold)
4748 {
4749 	int err;
4750 	u32 val;
4751 
4752 	err = ice_read_quad_reg_e82x(hw, quad, Q_REG_TX_MEM_GBL_CFG, &val);
4753 	if (err)
4754 		return err;
4755 
4756 	val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M;
4757 	if (ena) {
4758 		val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M;
4759 		val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M;
4760 		val |= FIELD_PREP(Q_REG_TX_MEM_GBL_CFG_INTR_THR_M, threshold);
4761 	}
4762 
4763 	return ice_write_quad_reg_e82x(hw, quad, Q_REG_TX_MEM_GBL_CFG, val);
4764 }
4765 
4766 /**
4767  * ice_ptp_init_phy_e82x - initialize PHY parameters
4768  * @ptp: pointer to the PTP HW struct
4769  */
ice_ptp_init_phy_e82x(struct ice_ptp_hw * ptp)4770 static void ice_ptp_init_phy_e82x(struct ice_ptp_hw *ptp)
4771 {
4772 	ptp->phy_model = ICE_PHY_E82X;
4773 	ptp->num_lports = 8;
4774 	ptp->ports_per_phy = 8;
4775 }
4776 
4777 /* E810 functions
4778  *
4779  * The following functions operate on the E810 series devices which use
4780  * a separate external PHY.
4781  */
4782 
4783 /**
4784  * ice_read_phy_reg_e810 - Read register from external PHY on E810
4785  * @hw: pointer to the HW struct
4786  * @addr: the address to read from
4787  * @val: On return, the value read from the PHY
4788  *
4789  * Read a register from the external PHY on the E810 device.
4790  */
ice_read_phy_reg_e810(struct ice_hw * hw,u32 addr,u32 * val)4791 static int ice_read_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 *val)
4792 {
4793 	struct ice_sbq_msg_input msg = {0};
4794 	int err;
4795 
4796 	msg.msg_addr_low = lower_16_bits(addr);
4797 	msg.msg_addr_high = upper_16_bits(addr);
4798 	msg.opcode = ice_sbq_msg_rd;
4799 	msg.dest_dev = rmn_0;
4800 
4801 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
4802 	if (err) {
4803 		ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
4804 			  err);
4805 		return err;
4806 	}
4807 
4808 	*val = msg.data;
4809 
4810 	return 0;
4811 }
4812 
4813 /**
4814  * ice_write_phy_reg_e810 - Write register on external PHY on E810
4815  * @hw: pointer to the HW struct
4816  * @addr: the address to writem to
4817  * @val: the value to write to the PHY
4818  *
4819  * Write a value to a register of the external PHY on the E810 device.
4820  */
ice_write_phy_reg_e810(struct ice_hw * hw,u32 addr,u32 val)4821 static int ice_write_phy_reg_e810(struct ice_hw *hw, u32 addr, u32 val)
4822 {
4823 	struct ice_sbq_msg_input msg = {0};
4824 	int err;
4825 
4826 	msg.msg_addr_low = lower_16_bits(addr);
4827 	msg.msg_addr_high = upper_16_bits(addr);
4828 	msg.opcode = ice_sbq_msg_wr;
4829 	msg.dest_dev = rmn_0;
4830 	msg.data = val;
4831 
4832 	err = ice_sbq_rw_reg(hw, &msg, ICE_AQ_FLAG_RD);
4833 	if (err) {
4834 		ice_debug(hw, ICE_DBG_PTP, "Failed to send message to PHY, err %d\n",
4835 			  err);
4836 		return err;
4837 	}
4838 
4839 	return 0;
4840 }
4841 
4842 /**
4843  * ice_read_phy_tstamp_ll_e810 - Read a PHY timestamp registers through the FW
4844  * @hw: pointer to the HW struct
4845  * @idx: the timestamp index to read
4846  * @hi: 8 bit timestamp high value
4847  * @lo: 32 bit timestamp low value
4848  *
4849  * Read a 8bit timestamp high value and 32 bit timestamp low value out of the
4850  * timestamp block of the external PHY on the E810 device using the low latency
4851  * timestamp read.
4852  */
4853 static int
ice_read_phy_tstamp_ll_e810(struct ice_hw * hw,u8 idx,u8 * hi,u32 * lo)4854 ice_read_phy_tstamp_ll_e810(struct ice_hw *hw, u8 idx, u8 *hi, u32 *lo)
4855 {
4856 	u32 val;
4857 	u8 i;
4858 
4859 	/* Write TS index to read to the PF register so the FW can read it */
4860 	val = FIELD_PREP(TS_LL_READ_TS_IDX, idx) | TS_LL_READ_TS;
4861 	wr32(hw, PF_SB_ATQBAL, val);
4862 
4863 	/* Read the register repeatedly until the FW provides us the TS */
4864 	for (i = TS_LL_READ_RETRIES; i > 0; i--) {
4865 		val = rd32(hw, PF_SB_ATQBAL);
4866 
4867 		/* When the bit is cleared, the TS is ready in the register */
4868 		if (!(FIELD_GET(TS_LL_READ_TS, val))) {
4869 			/* High 8 bit value of the TS is on the bits 16:23 */
4870 			*hi = FIELD_GET(TS_LL_READ_TS_HIGH, val);
4871 
4872 			/* Read the low 32 bit value and set the TS valid bit */
4873 			*lo = rd32(hw, PF_SB_ATQBAH) | TS_VALID;
4874 			return 0;
4875 		}
4876 
4877 		udelay(10);
4878 	}
4879 
4880 	/* FW failed to provide the TS in time */
4881 	ice_debug(hw, ICE_DBG_PTP, "Failed to read PTP timestamp using low latency read\n");
4882 	return -EINVAL;
4883 }
4884 
4885 /**
4886  * ice_read_phy_tstamp_sbq_e810 - Read a PHY timestamp registers through the sbq
4887  * @hw: pointer to the HW struct
4888  * @lport: the lport to read from
4889  * @idx: the timestamp index to read
4890  * @hi: 8 bit timestamp high value
4891  * @lo: 32 bit timestamp low value
4892  *
4893  * Read a 8bit timestamp high value and 32 bit timestamp low value out of the
4894  * timestamp block of the external PHY on the E810 device using sideband queue.
4895  */
4896 static int
ice_read_phy_tstamp_sbq_e810(struct ice_hw * hw,u8 lport,u8 idx,u8 * hi,u32 * lo)4897 ice_read_phy_tstamp_sbq_e810(struct ice_hw *hw, u8 lport, u8 idx, u8 *hi,
4898 			     u32 *lo)
4899 {
4900 	u32 hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
4901 	u32 lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
4902 	u32 lo_val, hi_val;
4903 	int err;
4904 
4905 	err = ice_read_phy_reg_e810(hw, lo_addr, &lo_val);
4906 	if (err) {
4907 		ice_debug(hw, ICE_DBG_PTP, "Failed to read low PTP timestamp register, err %d\n",
4908 			  err);
4909 		return err;
4910 	}
4911 
4912 	err = ice_read_phy_reg_e810(hw, hi_addr, &hi_val);
4913 	if (err) {
4914 		ice_debug(hw, ICE_DBG_PTP, "Failed to read high PTP timestamp register, err %d\n",
4915 			  err);
4916 		return err;
4917 	}
4918 
4919 	*lo = lo_val;
4920 	*hi = (u8)hi_val;
4921 
4922 	return 0;
4923 }
4924 
4925 /**
4926  * ice_read_phy_tstamp_e810 - Read a PHY timestamp out of the external PHY
4927  * @hw: pointer to the HW struct
4928  * @lport: the lport to read from
4929  * @idx: the timestamp index to read
4930  * @tstamp: on return, the 40bit timestamp value
4931  *
4932  * Read a 40bit timestamp value out of the timestamp block of the external PHY
4933  * on the E810 device.
4934  */
4935 static int
ice_read_phy_tstamp_e810(struct ice_hw * hw,u8 lport,u8 idx,u64 * tstamp)4936 ice_read_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx, u64 *tstamp)
4937 {
4938 	u32 lo = 0;
4939 	u8 hi = 0;
4940 	int err;
4941 
4942 	if (hw->dev_caps.ts_dev_info.ts_ll_read)
4943 		err = ice_read_phy_tstamp_ll_e810(hw, idx, &hi, &lo);
4944 	else
4945 		err = ice_read_phy_tstamp_sbq_e810(hw, lport, idx, &hi, &lo);
4946 
4947 	if (err)
4948 		return err;
4949 
4950 	/* For E810 devices, the timestamp is reported with the lower 32 bits
4951 	 * in the low register, and the upper 8 bits in the high register.
4952 	 */
4953 	*tstamp = ((u64)hi) << TS_HIGH_S | ((u64)lo & TS_LOW_M);
4954 
4955 	return 0;
4956 }
4957 
4958 /**
4959  * ice_clear_phy_tstamp_e810 - Clear a timestamp from the external PHY
4960  * @hw: pointer to the HW struct
4961  * @lport: the lport to read from
4962  * @idx: the timestamp index to reset
4963  *
4964  * Read the timestamp and then forcibly overwrite its value to clear the valid
4965  * bit from the timestamp block of the external PHY on the E810 device.
4966  *
4967  * This function should only be called on an idx whose bit is set according to
4968  * ice_get_phy_tx_tstamp_ready().
4969  */
ice_clear_phy_tstamp_e810(struct ice_hw * hw,u8 lport,u8 idx)4970 static int ice_clear_phy_tstamp_e810(struct ice_hw *hw, u8 lport, u8 idx)
4971 {
4972 	u32 lo_addr, hi_addr;
4973 	u64 unused_tstamp;
4974 	int err;
4975 
4976 	err = ice_read_phy_tstamp_e810(hw, lport, idx, &unused_tstamp);
4977 	if (err) {
4978 		ice_debug(hw, ICE_DBG_PTP, "Failed to read the timestamp register for lport %u, idx %u, err %d\n",
4979 			  lport, idx, err);
4980 		return err;
4981 	}
4982 
4983 	lo_addr = TS_EXT(LOW_TX_MEMORY_BANK_START, lport, idx);
4984 	hi_addr = TS_EXT(HIGH_TX_MEMORY_BANK_START, lport, idx);
4985 
4986 	err = ice_write_phy_reg_e810(hw, lo_addr, 0);
4987 	if (err) {
4988 		ice_debug(hw, ICE_DBG_PTP, "Failed to clear low PTP timestamp register for lport %u, idx %u, err %d\n",
4989 			  lport, idx, err);
4990 		return err;
4991 	}
4992 
4993 	err = ice_write_phy_reg_e810(hw, hi_addr, 0);
4994 	if (err) {
4995 		ice_debug(hw, ICE_DBG_PTP, "Failed to clear high PTP timestamp register for lport %u, idx %u, err %d\n",
4996 			  lport, idx, err);
4997 		return err;
4998 	}
4999 
5000 	return 0;
5001 }
5002 
5003 /**
5004  * ice_ptp_init_phc_e810 - Perform E810 specific PHC initialization
5005  * @hw: pointer to HW struct
5006  *
5007  * Perform E810-specific PTP hardware clock initialization steps.
5008  *
5009  * Return: 0 on success, other error codes when failed to initialize TimeSync
5010  */
ice_ptp_init_phc_e810(struct ice_hw * hw)5011 static int ice_ptp_init_phc_e810(struct ice_hw *hw)
5012 {
5013 	u8 tmr_idx;
5014 	int err;
5015 
5016 	/* Ensure synchronization delay is zero */
5017 	wr32(hw, GLTSYN_SYNC_DLAY, 0);
5018 
5019 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5020 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_ENA(tmr_idx),
5021 				     GLTSYN_ENA_TSYN_ENA_M);
5022 	if (err)
5023 		ice_debug(hw, ICE_DBG_PTP, "PTP failed in ena_phy_time_syn %d\n",
5024 			  err);
5025 
5026 	return err;
5027 }
5028 
5029 /**
5030  * ice_ptp_prep_phy_time_e810 - Prepare PHY port with initial time
5031  * @hw: Board private structure
5032  * @time: Time to initialize the PHY port clock to
5033  *
5034  * Program the PHY port ETH_GLTSYN_SHTIME registers in preparation setting the
5035  * initial clock time. The time will not actually be programmed until the
5036  * driver issues an ICE_PTP_INIT_TIME command.
5037  *
5038  * The time value is the upper 32 bits of the PHY timer, usually in units of
5039  * nominal nanoseconds.
5040  */
ice_ptp_prep_phy_time_e810(struct ice_hw * hw,u32 time)5041 static int ice_ptp_prep_phy_time_e810(struct ice_hw *hw, u32 time)
5042 {
5043 	u8 tmr_idx;
5044 	int err;
5045 
5046 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5047 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_0(tmr_idx), 0);
5048 	if (err) {
5049 		ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_0, err %d\n",
5050 			  err);
5051 		return err;
5052 	}
5053 
5054 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHTIME_L(tmr_idx), time);
5055 	if (err) {
5056 		ice_debug(hw, ICE_DBG_PTP, "Failed to write SHTIME_L, err %d\n",
5057 			  err);
5058 		return err;
5059 	}
5060 
5061 	return 0;
5062 }
5063 
5064 /**
5065  * ice_ptp_prep_phy_adj_e810 - Prep PHY port for a time adjustment
5066  * @hw: pointer to HW struct
5067  * @adj: adjustment value to program
5068  *
5069  * Prepare the PHY port for an atomic adjustment by programming the PHY
5070  * ETH_GLTSYN_SHADJ_L and ETH_GLTSYN_SHADJ_H registers. The actual adjustment
5071  * is completed by issuing an ICE_PTP_ADJ_TIME sync command.
5072  *
5073  * The adjustment value only contains the portion used for the upper 32bits of
5074  * the PHY timer, usually in units of nominal nanoseconds. Negative
5075  * adjustments are supported using 2s complement arithmetic.
5076  */
ice_ptp_prep_phy_adj_e810(struct ice_hw * hw,s32 adj)5077 static int ice_ptp_prep_phy_adj_e810(struct ice_hw *hw, s32 adj)
5078 {
5079 	u8 tmr_idx;
5080 	int err;
5081 
5082 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5083 
5084 	/* Adjustments are represented as signed 2's complement values in
5085 	 * nanoseconds. Sub-nanosecond adjustment is not supported.
5086 	 */
5087 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), 0);
5088 	if (err) {
5089 		ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_L, err %d\n",
5090 			  err);
5091 		return err;
5092 	}
5093 
5094 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), adj);
5095 	if (err) {
5096 		ice_debug(hw, ICE_DBG_PTP, "Failed to write adj to PHY SHADJ_H, err %d\n",
5097 			  err);
5098 		return err;
5099 	}
5100 
5101 	return 0;
5102 }
5103 
5104 /**
5105  * ice_ptp_prep_phy_incval_e810 - Prep PHY port increment value change
5106  * @hw: pointer to HW struct
5107  * @incval: The new 40bit increment value to prepare
5108  *
5109  * Prepare the PHY port for a new increment value by programming the PHY
5110  * ETH_GLTSYN_SHADJ_L and ETH_GLTSYN_SHADJ_H registers. The actual change is
5111  * completed by issuing an ICE_PTP_INIT_INCVAL command.
5112  */
ice_ptp_prep_phy_incval_e810(struct ice_hw * hw,u64 incval)5113 static int ice_ptp_prep_phy_incval_e810(struct ice_hw *hw, u64 incval)
5114 {
5115 	u32 high, low;
5116 	u8 tmr_idx;
5117 	int err;
5118 
5119 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5120 	low = lower_32_bits(incval);
5121 	high = upper_32_bits(incval);
5122 
5123 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_L(tmr_idx), low);
5124 	if (err) {
5125 		ice_debug(hw, ICE_DBG_PTP, "Failed to write incval to PHY SHADJ_L, err %d\n",
5126 			  err);
5127 		return err;
5128 	}
5129 
5130 	err = ice_write_phy_reg_e810(hw, ETH_GLTSYN_SHADJ_H(tmr_idx), high);
5131 	if (err) {
5132 		ice_debug(hw, ICE_DBG_PTP, "Failed to write incval PHY SHADJ_H, err %d\n",
5133 			  err);
5134 		return err;
5135 	}
5136 
5137 	return 0;
5138 }
5139 
5140 /**
5141  * ice_ptp_port_cmd_e810 - Prepare all external PHYs for a timer command
5142  * @hw: pointer to HW struct
5143  * @cmd: Command to be sent to the port
5144  *
5145  * Prepare the external PHYs connected to this device for a timer sync
5146  * command.
5147  */
ice_ptp_port_cmd_e810(struct ice_hw * hw,enum ice_ptp_tmr_cmd cmd)5148 static int ice_ptp_port_cmd_e810(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
5149 {
5150 	u32 val = ice_ptp_tmr_cmd_to_port_reg(hw, cmd);
5151 
5152 	return ice_write_phy_reg_e810(hw, E810_ETH_GLTSYN_CMD, val);
5153 }
5154 
5155 /**
5156  * ice_get_phy_tx_tstamp_ready_e810 - Read Tx memory status register
5157  * @hw: pointer to the HW struct
5158  * @port: the PHY port to read
5159  * @tstamp_ready: contents of the Tx memory status register
5160  *
5161  * E810 devices do not use a Tx memory status register. Instead simply
5162  * indicate that all timestamps are currently ready.
5163  */
5164 static int
ice_get_phy_tx_tstamp_ready_e810(struct ice_hw * hw,u8 port,u64 * tstamp_ready)5165 ice_get_phy_tx_tstamp_ready_e810(struct ice_hw *hw, u8 port, u64 *tstamp_ready)
5166 {
5167 	*tstamp_ready = 0xFFFFFFFFFFFFFFFF;
5168 	return 0;
5169 }
5170 
5171 /* E810T SMA functions
5172  *
5173  * The following functions operate specifically on E810T hardware and are used
5174  * to access the extended GPIOs available.
5175  */
5176 
5177 /**
5178  * ice_get_pca9575_handle
5179  * @hw: pointer to the hw struct
5180  * @pca9575_handle: GPIO controller's handle
5181  *
5182  * Find and return the GPIO controller's handle in the netlist.
5183  * When found - the value will be cached in the hw structure and following calls
5184  * will return cached value
5185  */
5186 static int
ice_get_pca9575_handle(struct ice_hw * hw,u16 * pca9575_handle)5187 ice_get_pca9575_handle(struct ice_hw *hw, u16 *pca9575_handle)
5188 {
5189 	struct ice_aqc_get_link_topo *cmd;
5190 	struct ice_aq_desc desc;
5191 	int status;
5192 	u8 idx;
5193 
5194 	/* If handle was read previously return cached value */
5195 	if (hw->io_expander_handle) {
5196 		*pca9575_handle = hw->io_expander_handle;
5197 		return 0;
5198 	}
5199 
5200 	/* If handle was not detected read it from the netlist */
5201 	cmd = &desc.params.get_link_topo;
5202 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_link_topo);
5203 
5204 	/* Set node type to GPIO controller */
5205 	cmd->addr.topo_params.node_type_ctx =
5206 		(ICE_AQC_LINK_TOPO_NODE_TYPE_M &
5207 		 ICE_AQC_LINK_TOPO_NODE_TYPE_GPIO_CTRL);
5208 
5209 #define SW_PCA9575_SFP_TOPO_IDX		2
5210 #define SW_PCA9575_QSFP_TOPO_IDX	1
5211 
5212 	/* Check if the SW IO expander controlling SMA exists in the netlist. */
5213 	if (hw->device_id == ICE_DEV_ID_E810C_SFP)
5214 		idx = SW_PCA9575_SFP_TOPO_IDX;
5215 	else if (hw->device_id == ICE_DEV_ID_E810C_QSFP)
5216 		idx = SW_PCA9575_QSFP_TOPO_IDX;
5217 	else
5218 		return -EOPNOTSUPP;
5219 
5220 	cmd->addr.topo_params.index = idx;
5221 
5222 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
5223 	if (status)
5224 		return -EOPNOTSUPP;
5225 
5226 	/* Verify if we found the right IO expander type */
5227 	if (desc.params.get_link_topo.node_part_num !=
5228 		ICE_AQC_GET_LINK_TOPO_NODE_NR_PCA9575)
5229 		return -EOPNOTSUPP;
5230 
5231 	/* If present save the handle and return it */
5232 	hw->io_expander_handle =
5233 		le16_to_cpu(desc.params.get_link_topo.addr.handle);
5234 	*pca9575_handle = hw->io_expander_handle;
5235 
5236 	return 0;
5237 }
5238 
5239 /**
5240  * ice_read_sma_ctrl_e810t
5241  * @hw: pointer to the hw struct
5242  * @data: pointer to data to be read from the GPIO controller
5243  *
5244  * Read the SMA controller state. It is connected to pins 3-7 of Port 1 of the
5245  * PCA9575 expander, so only bits 3-7 in data are valid.
5246  */
ice_read_sma_ctrl_e810t(struct ice_hw * hw,u8 * data)5247 int ice_read_sma_ctrl_e810t(struct ice_hw *hw, u8 *data)
5248 {
5249 	int status;
5250 	u16 handle;
5251 	u8 i;
5252 
5253 	status = ice_get_pca9575_handle(hw, &handle);
5254 	if (status)
5255 		return status;
5256 
5257 	*data = 0;
5258 
5259 	for (i = ICE_SMA_MIN_BIT_E810T; i <= ICE_SMA_MAX_BIT_E810T; i++) {
5260 		bool pin;
5261 
5262 		status = ice_aq_get_gpio(hw, handle, i + ICE_PCA9575_P1_OFFSET,
5263 					 &pin, NULL);
5264 		if (status)
5265 			break;
5266 		*data |= (u8)(!pin) << i;
5267 	}
5268 
5269 	return status;
5270 }
5271 
5272 /**
5273  * ice_write_sma_ctrl_e810t
5274  * @hw: pointer to the hw struct
5275  * @data: data to be written to the GPIO controller
5276  *
5277  * Write the data to the SMA controller. It is connected to pins 3-7 of Port 1
5278  * of the PCA9575 expander, so only bits 3-7 in data are valid.
5279  */
ice_write_sma_ctrl_e810t(struct ice_hw * hw,u8 data)5280 int ice_write_sma_ctrl_e810t(struct ice_hw *hw, u8 data)
5281 {
5282 	int status;
5283 	u16 handle;
5284 	u8 i;
5285 
5286 	status = ice_get_pca9575_handle(hw, &handle);
5287 	if (status)
5288 		return status;
5289 
5290 	for (i = ICE_SMA_MIN_BIT_E810T; i <= ICE_SMA_MAX_BIT_E810T; i++) {
5291 		bool pin;
5292 
5293 		pin = !(data & (1 << i));
5294 		status = ice_aq_set_gpio(hw, handle, i + ICE_PCA9575_P1_OFFSET,
5295 					 pin, NULL);
5296 		if (status)
5297 			break;
5298 	}
5299 
5300 	return status;
5301 }
5302 
5303 /**
5304  * ice_read_pca9575_reg_e810t
5305  * @hw: pointer to the hw struct
5306  * @offset: GPIO controller register offset
5307  * @data: pointer to data to be read from the GPIO controller
5308  *
5309  * Read the register from the GPIO controller
5310  */
ice_read_pca9575_reg_e810t(struct ice_hw * hw,u8 offset,u8 * data)5311 int ice_read_pca9575_reg_e810t(struct ice_hw *hw, u8 offset, u8 *data)
5312 {
5313 	struct ice_aqc_link_topo_addr link_topo;
5314 	__le16 addr;
5315 	u16 handle;
5316 	int err;
5317 
5318 	memset(&link_topo, 0, sizeof(link_topo));
5319 
5320 	err = ice_get_pca9575_handle(hw, &handle);
5321 	if (err)
5322 		return err;
5323 
5324 	link_topo.handle = cpu_to_le16(handle);
5325 	link_topo.topo_params.node_type_ctx =
5326 		FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
5327 			   ICE_AQC_LINK_TOPO_NODE_CTX_PROVIDED);
5328 
5329 	addr = cpu_to_le16((u16)offset);
5330 
5331 	return ice_aq_read_i2c(hw, link_topo, 0, addr, 1, data, NULL);
5332 }
5333 
5334 /**
5335  * ice_ptp_init_phy_e810 - initialize PHY parameters
5336  * @ptp: pointer to the PTP HW struct
5337  */
ice_ptp_init_phy_e810(struct ice_ptp_hw * ptp)5338 static void ice_ptp_init_phy_e810(struct ice_ptp_hw *ptp)
5339 {
5340 	ptp->phy_model = ICE_PHY_E810;
5341 	ptp->num_lports = 8;
5342 	ptp->ports_per_phy = 4;
5343 }
5344 
5345 /* Device agnostic functions
5346  *
5347  * The following functions implement shared behavior common to both E822 and
5348  * E810 devices, possibly calling a device specific implementation where
5349  * necessary.
5350  */
5351 
5352 /**
5353  * ice_ptp_lock - Acquire PTP global semaphore register lock
5354  * @hw: pointer to the HW struct
5355  *
5356  * Acquire the global PTP hardware semaphore lock. Returns true if the lock
5357  * was acquired, false otherwise.
5358  *
5359  * The PFTSYN_SEM register sets the busy bit on read, returning the previous
5360  * value. If software sees the busy bit cleared, this means that this function
5361  * acquired the lock (and the busy bit is now set). If software sees the busy
5362  * bit set, it means that another function acquired the lock.
5363  *
5364  * Software must clear the busy bit with a write to release the lock for other
5365  * functions when done.
5366  */
ice_ptp_lock(struct ice_hw * hw)5367 bool ice_ptp_lock(struct ice_hw *hw)
5368 {
5369 	u32 hw_lock;
5370 	int i;
5371 
5372 #define MAX_TRIES 15
5373 
5374 	for (i = 0; i < MAX_TRIES; i++) {
5375 		hw_lock = rd32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
5376 		hw_lock = hw_lock & PFTSYN_SEM_BUSY_M;
5377 		if (hw_lock) {
5378 			/* Somebody is holding the lock */
5379 			usleep_range(5000, 6000);
5380 			continue;
5381 		}
5382 
5383 		break;
5384 	}
5385 
5386 	return !hw_lock;
5387 }
5388 
5389 /**
5390  * ice_ptp_unlock - Release PTP global semaphore register lock
5391  * @hw: pointer to the HW struct
5392  *
5393  * Release the global PTP hardware semaphore lock. This is done by writing to
5394  * the PFTSYN_SEM register.
5395  */
ice_ptp_unlock(struct ice_hw * hw)5396 void ice_ptp_unlock(struct ice_hw *hw)
5397 {
5398 	wr32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), 0);
5399 }
5400 
5401 /**
5402  * ice_ptp_init_hw - Initialize hw based on device type
5403  * @hw: pointer to the HW structure
5404  *
5405  * Determine the PHY model for the device, and initialize hw
5406  * for use by other functions.
5407  */
ice_ptp_init_hw(struct ice_hw * hw)5408 void ice_ptp_init_hw(struct ice_hw *hw)
5409 {
5410 	struct ice_ptp_hw *ptp = &hw->ptp;
5411 
5412 	if (ice_is_e822(hw) || ice_is_e823(hw))
5413 		ice_ptp_init_phy_e82x(ptp);
5414 	else if (ice_is_e810(hw))
5415 		ice_ptp_init_phy_e810(ptp);
5416 	else if (ice_is_e825c(hw))
5417 		ice_ptp_init_phy_e825(hw);
5418 	else
5419 		ptp->phy_model = ICE_PHY_UNSUP;
5420 }
5421 
5422 /**
5423  * ice_ptp_write_port_cmd - Prepare a single PHY port for a timer command
5424  * @hw: pointer to HW struct
5425  * @port: Port to which cmd has to be sent
5426  * @cmd: Command to be sent to the port
5427  *
5428  * Prepare one port for the upcoming timer sync command. Do not use this for
5429  * programming only a single port, instead use ice_ptp_one_port_cmd() to
5430  * ensure non-modified ports get properly initialized to ICE_PTP_NOP.
5431  *
5432  * Return:
5433  * * %0     - success
5434  *  %-EBUSY - PHY type not supported
5435  * * %other - failed to write port command
5436  */
ice_ptp_write_port_cmd(struct ice_hw * hw,u8 port,enum ice_ptp_tmr_cmd cmd)5437 static int ice_ptp_write_port_cmd(struct ice_hw *hw, u8 port,
5438 				  enum ice_ptp_tmr_cmd cmd)
5439 {
5440 	switch (ice_get_phy_model(hw)) {
5441 	case ICE_PHY_ETH56G:
5442 		return ice_ptp_write_port_cmd_eth56g(hw, port, cmd);
5443 	case ICE_PHY_E82X:
5444 		return ice_ptp_write_port_cmd_e82x(hw, port, cmd);
5445 	default:
5446 		return -EOPNOTSUPP;
5447 	}
5448 }
5449 
5450 /**
5451  * ice_ptp_one_port_cmd - Program one PHY port for a timer command
5452  * @hw: pointer to HW struct
5453  * @configured_port: the port that should execute the command
5454  * @configured_cmd: the command to be executed on the configured port
5455  *
5456  * Prepare one port for executing a timer command, while preparing all other
5457  * ports to ICE_PTP_NOP. This allows executing a command on a single port
5458  * while ensuring all other ports do not execute stale commands.
5459  *
5460  * Return:
5461  * * %0     - success
5462  * * %other - failed to write port command
5463  */
ice_ptp_one_port_cmd(struct ice_hw * hw,u8 configured_port,enum ice_ptp_tmr_cmd configured_cmd)5464 int ice_ptp_one_port_cmd(struct ice_hw *hw, u8 configured_port,
5465 			 enum ice_ptp_tmr_cmd configured_cmd)
5466 {
5467 	u32 port;
5468 
5469 	for (port = 0; port < hw->ptp.num_lports; port++) {
5470 		int err;
5471 
5472 		/* Program the configured port with the configured command,
5473 		 * program all other ports with ICE_PTP_NOP.
5474 		 */
5475 		if (port == configured_port)
5476 			err = ice_ptp_write_port_cmd(hw, port, configured_cmd);
5477 		else
5478 			err = ice_ptp_write_port_cmd(hw, port, ICE_PTP_NOP);
5479 
5480 		if (err)
5481 			return err;
5482 	}
5483 
5484 	return 0;
5485 }
5486 
5487 /**
5488  * ice_ptp_port_cmd - Prepare PHY ports for a timer sync command
5489  * @hw: pointer to HW struct
5490  * @cmd: the timer command to setup
5491  *
5492  * Prepare all PHY ports on this device for the requested timer command. For
5493  * some families this can be done in one shot, but for other families each
5494  * port must be configured individually.
5495  *
5496  * Return:
5497  * * %0     - success
5498  * * %other - failed to write port command
5499  */
ice_ptp_port_cmd(struct ice_hw * hw,enum ice_ptp_tmr_cmd cmd)5500 static int ice_ptp_port_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
5501 {
5502 	u32 port;
5503 
5504 	/* PHY models which can program all ports simultaneously */
5505 	switch (ice_get_phy_model(hw)) {
5506 	case ICE_PHY_E810:
5507 		return ice_ptp_port_cmd_e810(hw, cmd);
5508 	default:
5509 		break;
5510 	}
5511 
5512 	/* PHY models which require programming each port separately */
5513 	for (port = 0; port < hw->ptp.num_lports; port++) {
5514 		int err;
5515 
5516 		err = ice_ptp_write_port_cmd(hw, port, cmd);
5517 		if (err)
5518 			return err;
5519 	}
5520 
5521 	return 0;
5522 }
5523 
5524 /**
5525  * ice_ptp_tmr_cmd - Prepare and trigger a timer sync command
5526  * @hw: pointer to HW struct
5527  * @cmd: the command to issue
5528  *
5529  * Prepare the source timer and PHY timers and then trigger the requested
5530  * command. This causes the shadow registers previously written in preparation
5531  * for the command to be synchronously applied to both the source and PHY
5532  * timers.
5533  */
ice_ptp_tmr_cmd(struct ice_hw * hw,enum ice_ptp_tmr_cmd cmd)5534 static int ice_ptp_tmr_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
5535 {
5536 	int err;
5537 
5538 	/* First, prepare the source timer */
5539 	ice_ptp_src_cmd(hw, cmd);
5540 
5541 	/* Next, prepare the ports */
5542 	err = ice_ptp_port_cmd(hw, cmd);
5543 	if (err) {
5544 		ice_debug(hw, ICE_DBG_PTP, "Failed to prepare PHY ports for timer command %u, err %d\n",
5545 			  cmd, err);
5546 		return err;
5547 	}
5548 
5549 	/* Write the sync command register to drive both source and PHY timer
5550 	 * commands synchronously
5551 	 */
5552 	ice_ptp_exec_tmr_cmd(hw);
5553 
5554 	return 0;
5555 }
5556 
5557 /**
5558  * ice_ptp_init_time - Initialize device time to provided value
5559  * @hw: pointer to HW struct
5560  * @time: 64bits of time (GLTSYN_TIME_L and GLTSYN_TIME_H)
5561  *
5562  * Initialize the device to the specified time provided. This requires a three
5563  * step process:
5564  *
5565  * 1) write the new init time to the source timer shadow registers
5566  * 2) write the new init time to the PHY timer shadow registers
5567  * 3) issue an init_time timer command to synchronously switch both the source
5568  *    and port timers to the new init time value at the next clock cycle.
5569  */
ice_ptp_init_time(struct ice_hw * hw,u64 time)5570 int ice_ptp_init_time(struct ice_hw *hw, u64 time)
5571 {
5572 	u8 tmr_idx;
5573 	int err;
5574 
5575 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5576 
5577 	/* Source timers */
5578 	wr32(hw, GLTSYN_SHTIME_L(tmr_idx), lower_32_bits(time));
5579 	wr32(hw, GLTSYN_SHTIME_H(tmr_idx), upper_32_bits(time));
5580 	wr32(hw, GLTSYN_SHTIME_0(tmr_idx), 0);
5581 
5582 	/* PHY timers */
5583 	/* Fill Rx and Tx ports and send msg to PHY */
5584 	switch (ice_get_phy_model(hw)) {
5585 	case ICE_PHY_ETH56G:
5586 		err = ice_ptp_prep_phy_time_eth56g(hw,
5587 						   (u32)(time & 0xFFFFFFFF));
5588 		break;
5589 	case ICE_PHY_E810:
5590 		err = ice_ptp_prep_phy_time_e810(hw, time & 0xFFFFFFFF);
5591 		break;
5592 	case ICE_PHY_E82X:
5593 		err = ice_ptp_prep_phy_time_e82x(hw, time & 0xFFFFFFFF);
5594 		break;
5595 	default:
5596 		err = -EOPNOTSUPP;
5597 	}
5598 
5599 	if (err)
5600 		return err;
5601 
5602 	return ice_ptp_tmr_cmd(hw, ICE_PTP_INIT_TIME);
5603 }
5604 
5605 /**
5606  * ice_ptp_write_incval - Program PHC with new increment value
5607  * @hw: pointer to HW struct
5608  * @incval: Source timer increment value per clock cycle
5609  *
5610  * Program the PHC with a new increment value. This requires a three-step
5611  * process:
5612  *
5613  * 1) Write the increment value to the source timer shadow registers
5614  * 2) Write the increment value to the PHY timer shadow registers
5615  * 3) Issue an ICE_PTP_INIT_INCVAL timer command to synchronously switch both
5616  *    the source and port timers to the new increment value at the next clock
5617  *    cycle.
5618  */
ice_ptp_write_incval(struct ice_hw * hw,u64 incval)5619 int ice_ptp_write_incval(struct ice_hw *hw, u64 incval)
5620 {
5621 	u8 tmr_idx;
5622 	int err;
5623 
5624 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5625 
5626 	/* Shadow Adjust */
5627 	wr32(hw, GLTSYN_SHADJ_L(tmr_idx), lower_32_bits(incval));
5628 	wr32(hw, GLTSYN_SHADJ_H(tmr_idx), upper_32_bits(incval));
5629 
5630 	switch (ice_get_phy_model(hw)) {
5631 	case ICE_PHY_ETH56G:
5632 		err = ice_ptp_prep_phy_incval_eth56g(hw, incval);
5633 		break;
5634 	case ICE_PHY_E810:
5635 		err = ice_ptp_prep_phy_incval_e810(hw, incval);
5636 		break;
5637 	case ICE_PHY_E82X:
5638 		err = ice_ptp_prep_phy_incval_e82x(hw, incval);
5639 		break;
5640 	default:
5641 		err = -EOPNOTSUPP;
5642 	}
5643 
5644 	if (err)
5645 		return err;
5646 
5647 	return ice_ptp_tmr_cmd(hw, ICE_PTP_INIT_INCVAL);
5648 }
5649 
5650 /**
5651  * ice_ptp_write_incval_locked - Program new incval while holding semaphore
5652  * @hw: pointer to HW struct
5653  * @incval: Source timer increment value per clock cycle
5654  *
5655  * Program a new PHC incval while holding the PTP semaphore.
5656  */
ice_ptp_write_incval_locked(struct ice_hw * hw,u64 incval)5657 int ice_ptp_write_incval_locked(struct ice_hw *hw, u64 incval)
5658 {
5659 	int err;
5660 
5661 	if (!ice_ptp_lock(hw))
5662 		return -EBUSY;
5663 
5664 	err = ice_ptp_write_incval(hw, incval);
5665 
5666 	ice_ptp_unlock(hw);
5667 
5668 	return err;
5669 }
5670 
5671 /**
5672  * ice_ptp_adj_clock - Adjust PHC clock time atomically
5673  * @hw: pointer to HW struct
5674  * @adj: Adjustment in nanoseconds
5675  *
5676  * Perform an atomic adjustment of the PHC time by the specified number of
5677  * nanoseconds. This requires a three-step process:
5678  *
5679  * 1) Write the adjustment to the source timer shadow registers
5680  * 2) Write the adjustment to the PHY timer shadow registers
5681  * 3) Issue an ICE_PTP_ADJ_TIME timer command to synchronously apply the
5682  *    adjustment to both the source and port timers at the next clock cycle.
5683  */
ice_ptp_adj_clock(struct ice_hw * hw,s32 adj)5684 int ice_ptp_adj_clock(struct ice_hw *hw, s32 adj)
5685 {
5686 	u8 tmr_idx;
5687 	int err;
5688 
5689 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5690 
5691 	/* Write the desired clock adjustment into the GLTSYN_SHADJ register.
5692 	 * For an ICE_PTP_ADJ_TIME command, this set of registers represents
5693 	 * the value to add to the clock time. It supports subtraction by
5694 	 * interpreting the value as a 2's complement integer.
5695 	 */
5696 	wr32(hw, GLTSYN_SHADJ_L(tmr_idx), 0);
5697 	wr32(hw, GLTSYN_SHADJ_H(tmr_idx), adj);
5698 
5699 	switch (ice_get_phy_model(hw)) {
5700 	case ICE_PHY_ETH56G:
5701 		err = ice_ptp_prep_phy_adj_eth56g(hw, adj);
5702 		break;
5703 	case ICE_PHY_E810:
5704 		err = ice_ptp_prep_phy_adj_e810(hw, adj);
5705 		break;
5706 	case ICE_PHY_E82X:
5707 		err = ice_ptp_prep_phy_adj_e82x(hw, adj);
5708 		break;
5709 	default:
5710 		err = -EOPNOTSUPP;
5711 	}
5712 
5713 	if (err)
5714 		return err;
5715 
5716 	return ice_ptp_tmr_cmd(hw, ICE_PTP_ADJ_TIME);
5717 }
5718 
5719 /**
5720  * ice_read_phy_tstamp - Read a PHY timestamp from the timestamo block
5721  * @hw: pointer to the HW struct
5722  * @block: the block to read from
5723  * @idx: the timestamp index to read
5724  * @tstamp: on return, the 40bit timestamp value
5725  *
5726  * Read a 40bit timestamp value out of the timestamp block. For E822 devices,
5727  * the block is the quad to read from. For E810 devices, the block is the
5728  * logical port to read from.
5729  */
ice_read_phy_tstamp(struct ice_hw * hw,u8 block,u8 idx,u64 * tstamp)5730 int ice_read_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx, u64 *tstamp)
5731 {
5732 	switch (ice_get_phy_model(hw)) {
5733 	case ICE_PHY_ETH56G:
5734 		return ice_read_ptp_tstamp_eth56g(hw, block, idx, tstamp);
5735 	case ICE_PHY_E810:
5736 		return ice_read_phy_tstamp_e810(hw, block, idx, tstamp);
5737 	case ICE_PHY_E82X:
5738 		return ice_read_phy_tstamp_e82x(hw, block, idx, tstamp);
5739 	default:
5740 		return -EOPNOTSUPP;
5741 	}
5742 }
5743 
5744 /**
5745  * ice_clear_phy_tstamp - Clear a timestamp from the timestamp block
5746  * @hw: pointer to the HW struct
5747  * @block: the block to read from
5748  * @idx: the timestamp index to reset
5749  *
5750  * Clear a timestamp from the timestamp block, discarding its value without
5751  * returning it. This resets the memory status bit for the timestamp index
5752  * allowing it to be reused for another timestamp in the future.
5753  *
5754  * For E822 devices, the block number is the PHY quad to clear from. For E810
5755  * devices, the block number is the logical port to clear from.
5756  *
5757  * This function must only be called on a timestamp index whose valid bit is
5758  * set according to ice_get_phy_tx_tstamp_ready().
5759  */
ice_clear_phy_tstamp(struct ice_hw * hw,u8 block,u8 idx)5760 int ice_clear_phy_tstamp(struct ice_hw *hw, u8 block, u8 idx)
5761 {
5762 	switch (ice_get_phy_model(hw)) {
5763 	case ICE_PHY_ETH56G:
5764 		return ice_clear_ptp_tstamp_eth56g(hw, block, idx);
5765 	case ICE_PHY_E810:
5766 		return ice_clear_phy_tstamp_e810(hw, block, idx);
5767 	case ICE_PHY_E82X:
5768 		return ice_clear_phy_tstamp_e82x(hw, block, idx);
5769 	default:
5770 		return -EOPNOTSUPP;
5771 	}
5772 }
5773 
5774 /**
5775  * ice_get_pf_c827_idx - find and return the C827 index for the current pf
5776  * @hw: pointer to the hw struct
5777  * @idx: index of the found C827 PHY
5778  * Return:
5779  * * 0 - success
5780  * * negative - failure
5781  */
ice_get_pf_c827_idx(struct ice_hw * hw,u8 * idx)5782 static int ice_get_pf_c827_idx(struct ice_hw *hw, u8 *idx)
5783 {
5784 	struct ice_aqc_get_link_topo cmd;
5785 	u8 node_part_number;
5786 	u16 node_handle;
5787 	int status;
5788 	u8 ctx;
5789 
5790 	if (hw->mac_type != ICE_MAC_E810)
5791 		return -ENODEV;
5792 
5793 	if (hw->device_id != ICE_DEV_ID_E810C_QSFP) {
5794 		*idx = C827_0;
5795 		return 0;
5796 	}
5797 
5798 	memset(&cmd, 0, sizeof(cmd));
5799 
5800 	ctx = ICE_AQC_LINK_TOPO_NODE_TYPE_PHY << ICE_AQC_LINK_TOPO_NODE_TYPE_S;
5801 	ctx |= ICE_AQC_LINK_TOPO_NODE_CTX_PORT << ICE_AQC_LINK_TOPO_NODE_CTX_S;
5802 	cmd.addr.topo_params.node_type_ctx = ctx;
5803 
5804 	status = ice_aq_get_netlist_node(hw, &cmd, &node_part_number,
5805 					 &node_handle);
5806 	if (status || node_part_number != ICE_AQC_GET_LINK_TOPO_NODE_NR_C827)
5807 		return -ENOENT;
5808 
5809 	if (node_handle == E810C_QSFP_C827_0_HANDLE)
5810 		*idx = C827_0;
5811 	else if (node_handle == E810C_QSFP_C827_1_HANDLE)
5812 		*idx = C827_1;
5813 	else
5814 		return -EIO;
5815 
5816 	return 0;
5817 }
5818 
5819 /**
5820  * ice_ptp_reset_ts_memory - Reset timestamp memory for all blocks
5821  * @hw: pointer to the HW struct
5822  */
ice_ptp_reset_ts_memory(struct ice_hw * hw)5823 void ice_ptp_reset_ts_memory(struct ice_hw *hw)
5824 {
5825 	switch (ice_get_phy_model(hw)) {
5826 	case ICE_PHY_ETH56G:
5827 		ice_ptp_reset_ts_memory_eth56g(hw);
5828 		break;
5829 	case ICE_PHY_E82X:
5830 		ice_ptp_reset_ts_memory_e82x(hw);
5831 		break;
5832 	case ICE_PHY_E810:
5833 	default:
5834 		return;
5835 	}
5836 }
5837 
5838 /**
5839  * ice_ptp_init_phc - Initialize PTP hardware clock
5840  * @hw: pointer to the HW struct
5841  *
5842  * Perform the steps required to initialize the PTP hardware clock.
5843  */
ice_ptp_init_phc(struct ice_hw * hw)5844 int ice_ptp_init_phc(struct ice_hw *hw)
5845 {
5846 	u8 src_idx = hw->func_caps.ts_func_info.tmr_index_owned;
5847 
5848 	/* Enable source clocks */
5849 	wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M);
5850 
5851 	/* Clear event err indications for auxiliary pins */
5852 	(void)rd32(hw, GLTSYN_STAT(src_idx));
5853 
5854 	switch (ice_get_phy_model(hw)) {
5855 	case ICE_PHY_ETH56G:
5856 		return ice_ptp_init_phc_eth56g(hw);
5857 	case ICE_PHY_E810:
5858 		return ice_ptp_init_phc_e810(hw);
5859 	case ICE_PHY_E82X:
5860 		return ice_ptp_init_phc_e82x(hw);
5861 	default:
5862 		return -EOPNOTSUPP;
5863 	}
5864 }
5865 
5866 /**
5867  * ice_get_phy_tx_tstamp_ready - Read PHY Tx memory status indication
5868  * @hw: pointer to the HW struct
5869  * @block: the timestamp block to check
5870  * @tstamp_ready: storage for the PHY Tx memory status information
5871  *
5872  * Check the PHY for Tx timestamp memory status. This reports a 64 bit value
5873  * which indicates which timestamps in the block may be captured. A set bit
5874  * means the timestamp can be read. An unset bit means the timestamp is not
5875  * ready and software should avoid reading the register.
5876  */
ice_get_phy_tx_tstamp_ready(struct ice_hw * hw,u8 block,u64 * tstamp_ready)5877 int ice_get_phy_tx_tstamp_ready(struct ice_hw *hw, u8 block, u64 *tstamp_ready)
5878 {
5879 	switch (ice_get_phy_model(hw)) {
5880 	case ICE_PHY_ETH56G:
5881 		return ice_get_phy_tx_tstamp_ready_eth56g(hw, block,
5882 							  tstamp_ready);
5883 	case ICE_PHY_E810:
5884 		return ice_get_phy_tx_tstamp_ready_e810(hw, block,
5885 							tstamp_ready);
5886 	case ICE_PHY_E82X:
5887 		return ice_get_phy_tx_tstamp_ready_e82x(hw, block,
5888 							tstamp_ready);
5889 		break;
5890 	default:
5891 		return -EOPNOTSUPP;
5892 	}
5893 }
5894 
5895 /**
5896  * ice_cgu_get_pin_desc_e823 - get pin description array
5897  * @hw: pointer to the hw struct
5898  * @input: if request is done against input or output pin
5899  * @size: number of inputs/outputs
5900  *
5901  * Return: pointer to pin description array associated to given hw.
5902  */
5903 static const struct ice_cgu_pin_desc *
ice_cgu_get_pin_desc_e823(struct ice_hw * hw,bool input,int * size)5904 ice_cgu_get_pin_desc_e823(struct ice_hw *hw, bool input, int *size)
5905 {
5906 	static const struct ice_cgu_pin_desc *t;
5907 
5908 	if (hw->cgu_part_number ==
5909 	    ICE_AQC_GET_LINK_TOPO_NODE_NR_ZL30632_80032) {
5910 		if (input) {
5911 			t = ice_e823_zl_cgu_inputs;
5912 			*size = ARRAY_SIZE(ice_e823_zl_cgu_inputs);
5913 		} else {
5914 			t = ice_e823_zl_cgu_outputs;
5915 			*size = ARRAY_SIZE(ice_e823_zl_cgu_outputs);
5916 		}
5917 	} else if (hw->cgu_part_number ==
5918 		   ICE_AQC_GET_LINK_TOPO_NODE_NR_SI5383_5384) {
5919 		if (input) {
5920 			t = ice_e823_si_cgu_inputs;
5921 			*size = ARRAY_SIZE(ice_e823_si_cgu_inputs);
5922 		} else {
5923 			t = ice_e823_si_cgu_outputs;
5924 			*size = ARRAY_SIZE(ice_e823_si_cgu_outputs);
5925 		}
5926 	} else {
5927 		t = NULL;
5928 		*size = 0;
5929 	}
5930 
5931 	return t;
5932 }
5933 
5934 /**
5935  * ice_cgu_get_pin_desc - get pin description array
5936  * @hw: pointer to the hw struct
5937  * @input: if request is done against input or output pins
5938  * @size: size of array returned by function
5939  *
5940  * Return: pointer to pin description array associated to given hw.
5941  */
5942 static const struct ice_cgu_pin_desc *
ice_cgu_get_pin_desc(struct ice_hw * hw,bool input,int * size)5943 ice_cgu_get_pin_desc(struct ice_hw *hw, bool input, int *size)
5944 {
5945 	const struct ice_cgu_pin_desc *t = NULL;
5946 
5947 	switch (hw->device_id) {
5948 	case ICE_DEV_ID_E810C_SFP:
5949 		if (input) {
5950 			t = ice_e810t_sfp_cgu_inputs;
5951 			*size = ARRAY_SIZE(ice_e810t_sfp_cgu_inputs);
5952 		} else {
5953 			t = ice_e810t_sfp_cgu_outputs;
5954 			*size = ARRAY_SIZE(ice_e810t_sfp_cgu_outputs);
5955 		}
5956 		break;
5957 	case ICE_DEV_ID_E810C_QSFP:
5958 		if (input) {
5959 			t = ice_e810t_qsfp_cgu_inputs;
5960 			*size = ARRAY_SIZE(ice_e810t_qsfp_cgu_inputs);
5961 		} else {
5962 			t = ice_e810t_qsfp_cgu_outputs;
5963 			*size = ARRAY_SIZE(ice_e810t_qsfp_cgu_outputs);
5964 		}
5965 		break;
5966 	case ICE_DEV_ID_E823L_10G_BASE_T:
5967 	case ICE_DEV_ID_E823L_1GBE:
5968 	case ICE_DEV_ID_E823L_BACKPLANE:
5969 	case ICE_DEV_ID_E823L_QSFP:
5970 	case ICE_DEV_ID_E823L_SFP:
5971 	case ICE_DEV_ID_E823C_10G_BASE_T:
5972 	case ICE_DEV_ID_E823C_BACKPLANE:
5973 	case ICE_DEV_ID_E823C_QSFP:
5974 	case ICE_DEV_ID_E823C_SFP:
5975 	case ICE_DEV_ID_E823C_SGMII:
5976 		t = ice_cgu_get_pin_desc_e823(hw, input, size);
5977 		break;
5978 	default:
5979 		break;
5980 	}
5981 
5982 	return t;
5983 }
5984 
5985 /**
5986  * ice_cgu_get_num_pins - get pin description array size
5987  * @hw: pointer to the hw struct
5988  * @input: if request is done against input or output pins
5989  *
5990  * Return: size of pin description array for given hw.
5991  */
ice_cgu_get_num_pins(struct ice_hw * hw,bool input)5992 int ice_cgu_get_num_pins(struct ice_hw *hw, bool input)
5993 {
5994 	const struct ice_cgu_pin_desc *t;
5995 	int size;
5996 
5997 	t = ice_cgu_get_pin_desc(hw, input, &size);
5998 	if (t)
5999 		return size;
6000 
6001 	return 0;
6002 }
6003 
6004 /**
6005  * ice_cgu_get_pin_type - get pin's type
6006  * @hw: pointer to the hw struct
6007  * @pin: pin index
6008  * @input: if request is done against input or output pin
6009  *
6010  * Return: type of a pin.
6011  */
ice_cgu_get_pin_type(struct ice_hw * hw,u8 pin,bool input)6012 enum dpll_pin_type ice_cgu_get_pin_type(struct ice_hw *hw, u8 pin, bool input)
6013 {
6014 	const struct ice_cgu_pin_desc *t;
6015 	int t_size;
6016 
6017 	t = ice_cgu_get_pin_desc(hw, input, &t_size);
6018 
6019 	if (!t)
6020 		return 0;
6021 
6022 	if (pin >= t_size)
6023 		return 0;
6024 
6025 	return t[pin].type;
6026 }
6027 
6028 /**
6029  * ice_cgu_get_pin_freq_supp - get pin's supported frequency
6030  * @hw: pointer to the hw struct
6031  * @pin: pin index
6032  * @input: if request is done against input or output pin
6033  * @num: output number of supported frequencies
6034  *
6035  * Get frequency supported number and array of supported frequencies.
6036  *
6037  * Return: array of supported frequencies for given pin.
6038  */
6039 struct dpll_pin_frequency *
ice_cgu_get_pin_freq_supp(struct ice_hw * hw,u8 pin,bool input,u8 * num)6040 ice_cgu_get_pin_freq_supp(struct ice_hw *hw, u8 pin, bool input, u8 *num)
6041 {
6042 	const struct ice_cgu_pin_desc *t;
6043 	int t_size;
6044 
6045 	*num = 0;
6046 	t = ice_cgu_get_pin_desc(hw, input, &t_size);
6047 	if (!t)
6048 		return NULL;
6049 	if (pin >= t_size)
6050 		return NULL;
6051 	*num = t[pin].freq_supp_num;
6052 
6053 	return t[pin].freq_supp;
6054 }
6055 
6056 /**
6057  * ice_cgu_get_pin_name - get pin's name
6058  * @hw: pointer to the hw struct
6059  * @pin: pin index
6060  * @input: if request is done against input or output pin
6061  *
6062  * Return:
6063  * * null terminated char array with name
6064  * * NULL in case of failure
6065  */
ice_cgu_get_pin_name(struct ice_hw * hw,u8 pin,bool input)6066 const char *ice_cgu_get_pin_name(struct ice_hw *hw, u8 pin, bool input)
6067 {
6068 	const struct ice_cgu_pin_desc *t;
6069 	int t_size;
6070 
6071 	t = ice_cgu_get_pin_desc(hw, input, &t_size);
6072 
6073 	if (!t)
6074 		return NULL;
6075 
6076 	if (pin >= t_size)
6077 		return NULL;
6078 
6079 	return t[pin].name;
6080 }
6081 
6082 /**
6083  * ice_get_cgu_state - get the state of the DPLL
6084  * @hw: pointer to the hw struct
6085  * @dpll_idx: Index of internal DPLL unit
6086  * @last_dpll_state: last known state of DPLL
6087  * @pin: pointer to a buffer for returning currently active pin
6088  * @ref_state: reference clock state
6089  * @eec_mode: eec mode of the DPLL
6090  * @phase_offset: pointer to a buffer for returning phase offset
6091  * @dpll_state: state of the DPLL (output)
6092  *
6093  * This function will read the state of the DPLL(dpll_idx). Non-null
6094  * 'pin', 'ref_state', 'eec_mode' and 'phase_offset' parameters are used to
6095  * retrieve currently active pin, state, mode and phase_offset respectively.
6096  *
6097  * Return: state of the DPLL
6098  */
ice_get_cgu_state(struct ice_hw * hw,u8 dpll_idx,enum dpll_lock_status last_dpll_state,u8 * pin,u8 * ref_state,u8 * eec_mode,s64 * phase_offset,enum dpll_lock_status * dpll_state)6099 int ice_get_cgu_state(struct ice_hw *hw, u8 dpll_idx,
6100 		      enum dpll_lock_status last_dpll_state, u8 *pin,
6101 		      u8 *ref_state, u8 *eec_mode, s64 *phase_offset,
6102 		      enum dpll_lock_status *dpll_state)
6103 {
6104 	u8 hw_ref_state, hw_dpll_state, hw_eec_mode, hw_config;
6105 	s64 hw_phase_offset;
6106 	int status;
6107 
6108 	status = ice_aq_get_cgu_dpll_status(hw, dpll_idx, &hw_ref_state,
6109 					    &hw_dpll_state, &hw_config,
6110 					    &hw_phase_offset, &hw_eec_mode);
6111 	if (status)
6112 		return status;
6113 
6114 	if (pin)
6115 		/* current ref pin in dpll_state_refsel_status_X register */
6116 		*pin = hw_config & ICE_AQC_GET_CGU_DPLL_CONFIG_CLK_REF_SEL;
6117 	if (phase_offset)
6118 		*phase_offset = hw_phase_offset;
6119 	if (ref_state)
6120 		*ref_state = hw_ref_state;
6121 	if (eec_mode)
6122 		*eec_mode = hw_eec_mode;
6123 	if (!dpll_state)
6124 		return 0;
6125 
6126 	/* According to ZL DPLL documentation, once state reach LOCKED_HO_ACQ
6127 	 * it would never return to FREERUN. This aligns to ITU-T G.781
6128 	 * Recommendation. We cannot report HOLDOVER as HO memory is cleared
6129 	 * while switching to another reference.
6130 	 * Only for situations where previous state was either: "LOCKED without
6131 	 * HO_ACQ" or "HOLDOVER" we actually back to FREERUN.
6132 	 */
6133 	if (hw_dpll_state & ICE_AQC_GET_CGU_DPLL_STATUS_STATE_LOCK) {
6134 		if (hw_dpll_state & ICE_AQC_GET_CGU_DPLL_STATUS_STATE_HO_READY)
6135 			*dpll_state = DPLL_LOCK_STATUS_LOCKED_HO_ACQ;
6136 		else
6137 			*dpll_state = DPLL_LOCK_STATUS_LOCKED;
6138 	} else if (last_dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ ||
6139 		   last_dpll_state == DPLL_LOCK_STATUS_HOLDOVER) {
6140 		*dpll_state = DPLL_LOCK_STATUS_HOLDOVER;
6141 	} else {
6142 		*dpll_state = DPLL_LOCK_STATUS_UNLOCKED;
6143 	}
6144 
6145 	return 0;
6146 }
6147 
6148 /**
6149  * ice_get_cgu_rclk_pin_info - get info on available recovered clock pins
6150  * @hw: pointer to the hw struct
6151  * @base_idx: returns index of first recovered clock pin on device
6152  * @pin_num: returns number of recovered clock pins available on device
6153  *
6154  * Based on hw provide caller info about recovery clock pins available on the
6155  * board.
6156  *
6157  * Return:
6158  * * 0 - success, information is valid
6159  * * negative - failure, information is not valid
6160  */
ice_get_cgu_rclk_pin_info(struct ice_hw * hw,u8 * base_idx,u8 * pin_num)6161 int ice_get_cgu_rclk_pin_info(struct ice_hw *hw, u8 *base_idx, u8 *pin_num)
6162 {
6163 	u8 phy_idx;
6164 	int ret;
6165 
6166 	switch (hw->device_id) {
6167 	case ICE_DEV_ID_E810C_SFP:
6168 	case ICE_DEV_ID_E810C_QSFP:
6169 
6170 		ret = ice_get_pf_c827_idx(hw, &phy_idx);
6171 		if (ret)
6172 			return ret;
6173 		*base_idx = E810T_CGU_INPUT_C827(phy_idx, ICE_RCLKA_PIN);
6174 		*pin_num = ICE_E810_RCLK_PINS_NUM;
6175 		ret = 0;
6176 		break;
6177 	case ICE_DEV_ID_E823L_10G_BASE_T:
6178 	case ICE_DEV_ID_E823L_1GBE:
6179 	case ICE_DEV_ID_E823L_BACKPLANE:
6180 	case ICE_DEV_ID_E823L_QSFP:
6181 	case ICE_DEV_ID_E823L_SFP:
6182 	case ICE_DEV_ID_E823C_10G_BASE_T:
6183 	case ICE_DEV_ID_E823C_BACKPLANE:
6184 	case ICE_DEV_ID_E823C_QSFP:
6185 	case ICE_DEV_ID_E823C_SFP:
6186 	case ICE_DEV_ID_E823C_SGMII:
6187 		*pin_num = ICE_E82X_RCLK_PINS_NUM;
6188 		ret = 0;
6189 		if (hw->cgu_part_number ==
6190 		    ICE_AQC_GET_LINK_TOPO_NODE_NR_ZL30632_80032)
6191 			*base_idx = ZL_REF1P;
6192 		else if (hw->cgu_part_number ==
6193 			 ICE_AQC_GET_LINK_TOPO_NODE_NR_SI5383_5384)
6194 			*base_idx = SI_REF1P;
6195 		else
6196 			ret = -ENODEV;
6197 
6198 		break;
6199 	default:
6200 		ret = -ENODEV;
6201 		break;
6202 	}
6203 
6204 	return ret;
6205 }
6206 
6207 /**
6208  * ice_cgu_get_output_pin_state_caps - get output pin state capabilities
6209  * @hw: pointer to the hw struct
6210  * @pin_id: id of a pin
6211  * @caps: capabilities to modify
6212  *
6213  * Return:
6214  * * 0 - success, state capabilities were modified
6215  * * negative - failure, capabilities were not modified
6216  */
ice_cgu_get_output_pin_state_caps(struct ice_hw * hw,u8 pin_id,unsigned long * caps)6217 int ice_cgu_get_output_pin_state_caps(struct ice_hw *hw, u8 pin_id,
6218 				      unsigned long *caps)
6219 {
6220 	bool can_change = true;
6221 
6222 	switch (hw->device_id) {
6223 	case ICE_DEV_ID_E810C_SFP:
6224 		if (pin_id == ZL_OUT2 || pin_id == ZL_OUT3)
6225 			can_change = false;
6226 		break;
6227 	case ICE_DEV_ID_E810C_QSFP:
6228 		if (pin_id == ZL_OUT2 || pin_id == ZL_OUT3 || pin_id == ZL_OUT4)
6229 			can_change = false;
6230 		break;
6231 	case ICE_DEV_ID_E823L_10G_BASE_T:
6232 	case ICE_DEV_ID_E823L_1GBE:
6233 	case ICE_DEV_ID_E823L_BACKPLANE:
6234 	case ICE_DEV_ID_E823L_QSFP:
6235 	case ICE_DEV_ID_E823L_SFP:
6236 	case ICE_DEV_ID_E823C_10G_BASE_T:
6237 	case ICE_DEV_ID_E823C_BACKPLANE:
6238 	case ICE_DEV_ID_E823C_QSFP:
6239 	case ICE_DEV_ID_E823C_SFP:
6240 	case ICE_DEV_ID_E823C_SGMII:
6241 		if (hw->cgu_part_number ==
6242 		    ICE_AQC_GET_LINK_TOPO_NODE_NR_ZL30632_80032 &&
6243 		    pin_id == ZL_OUT2)
6244 			can_change = false;
6245 		else if (hw->cgu_part_number ==
6246 			 ICE_AQC_GET_LINK_TOPO_NODE_NR_SI5383_5384 &&
6247 			 pin_id == SI_OUT1)
6248 			can_change = false;
6249 		break;
6250 	default:
6251 		return -EINVAL;
6252 	}
6253 	if (can_change)
6254 		*caps |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
6255 	else
6256 		*caps &= ~DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
6257 
6258 	return 0;
6259 }
6260