1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Copyright (C) 2018 Microchip Technology Inc. */
3 
4 #include <linux/netdevice.h>
5 
6 #include <linux/ptp_clock_kernel.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 #include <linux/net_tstamp.h>
10 #include "lan743x_main.h"
11 
12 #include "lan743x_ptp.h"
13 
14 #define LAN743X_LED0_ENABLE		20	/* LED0 offset in HW_CFG */
15 #define LAN743X_LED_ENABLE(pin)		BIT(LAN743X_LED0_ENABLE + (pin))
16 
17 #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB		(31249999)
18 #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM	(2047999934)
19 
20 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
21 static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
22 static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
23 static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
24 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
25 				  u32 seconds, u32 nano_seconds,
26 				  u32 sub_nano_seconds);
27 
28 static int lan743x_get_channel(u32 ch_map)
29 {
30 	int idx;
31 
32 	for (idx = 0; idx < 32; idx++) {
33 		if (ch_map & (0x1 << idx))
34 			return idx;
35 	}
36 
37 	return -EINVAL;
38 }
39 
40 int lan743x_gpio_init(struct lan743x_adapter *adapter)
41 {
42 	struct lan743x_gpio *gpio = &adapter->gpio;
43 
44 	spin_lock_init(&gpio->gpio_lock);
45 
46 	gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
47 	gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
48 	gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
49 	gpio->gpio_cfg3 = 0;/* disable all 1588 output */
50 	lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
51 	lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
52 	lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
53 	lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
54 
55 	return 0;
56 }
57 
58 static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
59 					   u32 bit_mask)
60 {
61 	int timeout = 1000;
62 	u32 data = 0;
63 
64 	while (timeout &&
65 	       (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
66 	       bit_mask))) {
67 		usleep_range(1000, 20000);
68 		timeout--;
69 	}
70 	if (data) {
71 		netif_err(adapter, drv, adapter->netdev,
72 			  "timeout waiting for cmd to be done, cmd = 0x%08X\n",
73 			  bit_mask);
74 	}
75 }
76 
77 static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
78 					 u32 seconds, u32 nano_seconds,
79 					 u32 header)
80 {
81 	struct lan743x_ptp *ptp = &adapter->ptp;
82 
83 	spin_lock_bh(&ptp->tx_ts_lock);
84 	if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
85 		ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
86 		ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
87 		ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
88 		ptp->tx_ts_queue_size++;
89 	} else {
90 		netif_err(adapter, drv, adapter->netdev,
91 			  "tx ts queue overflow\n");
92 	}
93 	spin_unlock_bh(&ptp->tx_ts_lock);
94 }
95 
96 static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
97 {
98 	struct lan743x_ptp *ptp = &adapter->ptp;
99 	struct skb_shared_hwtstamps tstamps;
100 	u32 header, nseconds, seconds;
101 	bool ignore_sync = false;
102 	struct sk_buff *skb;
103 	int c, i;
104 
105 	spin_lock_bh(&ptp->tx_ts_lock);
106 	c = ptp->tx_ts_skb_queue_size;
107 
108 	if (c > ptp->tx_ts_queue_size)
109 		c = ptp->tx_ts_queue_size;
110 	if (c <= 0)
111 		goto done;
112 
113 	for (i = 0; i < c; i++) {
114 		ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
115 				BIT(i)) != 0);
116 		skb = ptp->tx_ts_skb_queue[i];
117 		nseconds = ptp->tx_ts_nseconds_queue[i];
118 		seconds = ptp->tx_ts_seconds_queue[i];
119 		header = ptp->tx_ts_header_queue[i];
120 
121 		memset(&tstamps, 0, sizeof(tstamps));
122 		tstamps.hwtstamp = ktime_set(seconds, nseconds);
123 		if (!ignore_sync ||
124 		    ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
125 		    PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
126 			skb_tstamp_tx(skb, &tstamps);
127 
128 		dev_kfree_skb(skb);
129 
130 		ptp->tx_ts_skb_queue[i] = NULL;
131 		ptp->tx_ts_seconds_queue[i] = 0;
132 		ptp->tx_ts_nseconds_queue[i] = 0;
133 		ptp->tx_ts_header_queue[i] = 0;
134 	}
135 
136 	/* shift queue */
137 	ptp->tx_ts_ignore_sync_queue >>= c;
138 	for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
139 		ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
140 		ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
141 		ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
142 		ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];
143 
144 		ptp->tx_ts_skb_queue[i] = NULL;
145 		ptp->tx_ts_seconds_queue[i] = 0;
146 		ptp->tx_ts_nseconds_queue[i] = 0;
147 		ptp->tx_ts_header_queue[i] = 0;
148 	}
149 	ptp->tx_ts_skb_queue_size -= c;
150 	ptp->tx_ts_queue_size -= c;
151 done:
152 	ptp->pending_tx_timestamps -= c;
153 	spin_unlock_bh(&ptp->tx_ts_lock);
154 }
155 
156 static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter,
157 					int event_channel)
158 {
159 	struct lan743x_ptp *ptp = &adapter->ptp;
160 	int result = -ENODEV;
161 
162 	mutex_lock(&ptp->command_lock);
163 	if (!(test_bit(event_channel, &ptp->used_event_ch))) {
164 		ptp->used_event_ch |= BIT(event_channel);
165 		result = event_channel;
166 	} else {
167 		netif_warn(adapter, drv, adapter->netdev,
168 			   "attempted to reserved a used event_channel = %d\n",
169 			   event_channel);
170 	}
171 	mutex_unlock(&ptp->command_lock);
172 	return result;
173 }
174 
175 static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
176 					 int event_channel)
177 {
178 	struct lan743x_ptp *ptp = &adapter->ptp;
179 
180 	mutex_lock(&ptp->command_lock);
181 	if (test_bit(event_channel, &ptp->used_event_ch)) {
182 		ptp->used_event_ch &= ~BIT(event_channel);
183 	} else {
184 		netif_warn(adapter, drv, adapter->netdev,
185 			   "attempted release on a not used event_channel = %d\n",
186 			   event_channel);
187 	}
188 	mutex_unlock(&ptp->command_lock);
189 }
190 
191 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
192 				  u32 *seconds, u32 *nano_seconds,
193 				  u32 *sub_nano_seconds);
194 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
195 				     u32 *sec, u32 *nsec, u32 *sub_nsec);
196 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
197 				   s64 time_step_ns);
198 
199 static void lan743x_led_mux_enable(struct lan743x_adapter *adapter,
200 				   int pin, bool enable)
201 {
202 	struct lan743x_ptp *ptp = &adapter->ptp;
203 
204 	if (ptp->leds_multiplexed &&
205 	    ptp->led_enabled[pin]) {
206 		u32 val = lan743x_csr_read(adapter, HW_CFG);
207 
208 		if (enable)
209 			val |= LAN743X_LED_ENABLE(pin);
210 		else
211 			val &= ~LAN743X_LED_ENABLE(pin);
212 
213 		lan743x_csr_write(adapter, HW_CFG, val);
214 	}
215 }
216 
217 static void lan743x_led_mux_save(struct lan743x_adapter *adapter)
218 {
219 	struct lan743x_ptp *ptp = &adapter->ptp;
220 	u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
221 
222 	if (id_rev == ID_REV_ID_LAN7430_) {
223 		int i;
224 		u32 val = lan743x_csr_read(adapter, HW_CFG);
225 
226 		for (i = 0; i < LAN7430_N_LED; i++) {
227 			bool led_enabled = (val & LAN743X_LED_ENABLE(i)) != 0;
228 
229 			ptp->led_enabled[i] = led_enabled;
230 		}
231 		ptp->leds_multiplexed = true;
232 	} else {
233 		ptp->leds_multiplexed = false;
234 	}
235 }
236 
237 static void lan743x_led_mux_restore(struct lan743x_adapter *adapter)
238 {
239 	u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
240 
241 	if (id_rev == ID_REV_ID_LAN7430_) {
242 		int i;
243 
244 		for (i = 0; i < LAN7430_N_LED; i++)
245 			lan743x_led_mux_enable(adapter, i, true);
246 	}
247 }
248 
249 static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
250 				     int pin, int event_channel)
251 {
252 	struct lan743x_gpio *gpio = &adapter->gpio;
253 	unsigned long irq_flags = 0;
254 	int bit_mask = BIT(pin);
255 	int ret = -EBUSY;
256 
257 	spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
258 
259 	if (!(gpio->used_bits & bit_mask)) {
260 		gpio->used_bits |= bit_mask;
261 		gpio->output_bits |= bit_mask;
262 		gpio->ptp_bits |= bit_mask;
263 
264 		/* assign pin to GPIO function */
265 		lan743x_led_mux_enable(adapter, pin, false);
266 
267 		/* set as output, and zero initial value */
268 		gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(pin);
269 		gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
270 		lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
271 
272 		/* enable gpio, and set buffer type to push pull */
273 		gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(pin);
274 		gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(pin);
275 		lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
276 
277 		/* set 1588 polarity to high */
278 		gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(pin);
279 		lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
280 
281 		if (event_channel == 0) {
282 			/* use channel A */
283 			gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(pin);
284 		} else {
285 			/* use channel B */
286 			gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(pin);
287 		}
288 		gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(pin);
289 		lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
290 
291 		ret = pin;
292 	}
293 	spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
294 	return ret;
295 }
296 
297 static void lan743x_gpio_release(struct lan743x_adapter *adapter, int pin)
298 {
299 	struct lan743x_gpio *gpio = &adapter->gpio;
300 	unsigned long irq_flags = 0;
301 	int bit_mask = BIT(pin);
302 
303 	spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
304 	if (gpio->used_bits & bit_mask) {
305 		gpio->used_bits &= ~bit_mask;
306 		if (gpio->output_bits & bit_mask) {
307 			gpio->output_bits &= ~bit_mask;
308 
309 			if (gpio->ptp_bits & bit_mask) {
310 				gpio->ptp_bits &= ~bit_mask;
311 				/* disable ptp output */
312 				gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(pin);
313 				lan743x_csr_write(adapter, GPIO_CFG3,
314 						  gpio->gpio_cfg3);
315 			}
316 			/* release gpio output */
317 
318 			/* disable gpio */
319 			gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(pin);
320 			gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(pin);
321 			lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
322 
323 			/* reset back to input */
324 			gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(pin);
325 			gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
326 			lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
327 
328 			/* assign pin to original function */
329 			lan743x_led_mux_enable(adapter, pin, true);
330 		}
331 	}
332 	spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
333 }
334 
335 static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
336 {
337 	struct lan743x_ptp *ptp =
338 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
339 	struct lan743x_adapter *adapter =
340 		container_of(ptp, struct lan743x_adapter, ptp);
341 	u32 lan743x_rate_adj = 0;
342 	bool positive = true;
343 	u64 u64_delta = 0;
344 
345 	if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
346 	    scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
347 		return -EINVAL;
348 	}
349 	if (scaled_ppm > 0) {
350 		u64_delta = (u64)scaled_ppm;
351 		positive = true;
352 	} else {
353 		u64_delta = (u64)(-scaled_ppm);
354 		positive = false;
355 	}
356 	u64_delta = (u64_delta << 19);
357 	lan743x_rate_adj = div_u64(u64_delta, 1000000);
358 
359 	if (positive)
360 		lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
361 
362 	lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
363 			  lan743x_rate_adj);
364 
365 	return 0;
366 }
367 
368 static int lan743x_ptpci_adjfreq(struct ptp_clock_info *ptpci, s32 delta_ppb)
369 {
370 	struct lan743x_ptp *ptp =
371 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
372 	struct lan743x_adapter *adapter =
373 		container_of(ptp, struct lan743x_adapter, ptp);
374 	u32 lan743x_rate_adj = 0;
375 	bool positive = true;
376 	u32 u32_delta = 0;
377 	u64 u64_delta = 0;
378 
379 	if ((delta_ppb < (-LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB)) ||
380 	    delta_ppb > LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB) {
381 		return -EINVAL;
382 	}
383 	if (delta_ppb > 0) {
384 		u32_delta = (u32)delta_ppb;
385 		positive = true;
386 	} else {
387 		u32_delta = (u32)(-delta_ppb);
388 		positive = false;
389 	}
390 	u64_delta = (((u64)u32_delta) << 35);
391 	lan743x_rate_adj = div_u64(u64_delta, 1000000000);
392 
393 	if (positive)
394 		lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
395 
396 	lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
397 			  lan743x_rate_adj);
398 
399 	return 0;
400 }
401 
402 static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
403 {
404 	struct lan743x_ptp *ptp =
405 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
406 	struct lan743x_adapter *adapter =
407 		container_of(ptp, struct lan743x_adapter, ptp);
408 
409 	lan743x_ptp_clock_step(adapter, delta);
410 
411 	return 0;
412 }
413 
414 static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
415 				   struct timespec64 *ts)
416 {
417 	struct lan743x_ptp *ptp =
418 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
419 	struct lan743x_adapter *adapter =
420 		container_of(ptp, struct lan743x_adapter, ptp);
421 	u32 nano_seconds = 0;
422 	u32 seconds = 0;
423 
424 	if (adapter->is_pci11x1x)
425 		lan743x_ptp_io_clock_get(adapter, &seconds, &nano_seconds,
426 					 NULL);
427 	else
428 		lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
429 	ts->tv_sec = seconds;
430 	ts->tv_nsec = nano_seconds;
431 
432 	return 0;
433 }
434 
435 static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
436 				   const struct timespec64 *ts)
437 {
438 	struct lan743x_ptp *ptp =
439 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
440 	struct lan743x_adapter *adapter =
441 		container_of(ptp, struct lan743x_adapter, ptp);
442 	u32 nano_seconds = 0;
443 	u32 seconds = 0;
444 
445 	if (ts) {
446 		if (ts->tv_sec > 0xFFFFFFFFLL ||
447 		    ts->tv_sec < 0) {
448 			netif_warn(adapter, drv, adapter->netdev,
449 				   "ts->tv_sec out of range, %lld\n",
450 				   ts->tv_sec);
451 			return -ERANGE;
452 		}
453 		if (ts->tv_nsec >= 1000000000L ||
454 		    ts->tv_nsec < 0) {
455 			netif_warn(adapter, drv, adapter->netdev,
456 				   "ts->tv_nsec out of range, %ld\n",
457 				   ts->tv_nsec);
458 			return -ERANGE;
459 		}
460 		seconds = ts->tv_sec;
461 		nano_seconds = ts->tv_nsec;
462 		lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
463 	} else {
464 		netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
465 		return -EINVAL;
466 	}
467 
468 	return 0;
469 }
470 
471 static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter,
472 				   unsigned int index)
473 {
474 	struct lan743x_ptp *ptp = &adapter->ptp;
475 	u32 general_config = 0;
476 	struct lan743x_ptp_perout *perout = &ptp->perout[index];
477 
478 	if (perout->gpio_pin >= 0) {
479 		lan743x_gpio_release(adapter, perout->gpio_pin);
480 		perout->gpio_pin = -1;
481 	}
482 
483 	if (perout->event_ch >= 0) {
484 		/* set target to far in the future, effectively disabling it */
485 		lan743x_csr_write(adapter,
486 				  PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
487 				  0xFFFF0000);
488 		lan743x_csr_write(adapter,
489 				  PTP_CLOCK_TARGET_NS_X(perout->event_ch),
490 				  0);
491 
492 		general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
493 		general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
494 				  (perout->event_ch);
495 		lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
496 		lan743x_ptp_release_event_ch(adapter, perout->event_ch);
497 		perout->event_ch = -1;
498 	}
499 }
500 
501 static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
502 			      struct ptp_perout_request *perout_request)
503 {
504 	struct lan743x_ptp *ptp = &adapter->ptp;
505 	u32 period_sec = 0, period_nsec = 0;
506 	u32 start_sec = 0, start_nsec = 0;
507 	u32 general_config = 0;
508 	int pulse_width = 0;
509 	int perout_pin = 0;
510 	unsigned int index = perout_request->index;
511 	struct lan743x_ptp_perout *perout = &ptp->perout[index];
512 	int ret = 0;
513 
514 	/* Reject requests with unsupported flags */
515 	if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
516 		return -EOPNOTSUPP;
517 
518 	if (on) {
519 		perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
520 					  perout_request->index);
521 		if (perout_pin < 0)
522 			return -EBUSY;
523 	} else {
524 		lan743x_ptp_perout_off(adapter, index);
525 		return 0;
526 	}
527 
528 	if (perout->event_ch >= 0 ||
529 	    perout->gpio_pin >= 0) {
530 		/* already on, turn off first */
531 		lan743x_ptp_perout_off(adapter, index);
532 	}
533 
534 	perout->event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
535 
536 	if (perout->event_ch < 0) {
537 		netif_warn(adapter, drv, adapter->netdev,
538 			   "Failed to reserve event channel %d for PEROUT\n",
539 			   index);
540 		ret = -EBUSY;
541 		goto failed;
542 	}
543 
544 	perout->gpio_pin = lan743x_gpio_rsrv_ptp_out(adapter,
545 						     perout_pin,
546 						     perout->event_ch);
547 
548 	if (perout->gpio_pin < 0) {
549 		netif_warn(adapter, drv, adapter->netdev,
550 			   "Failed to reserve gpio %d for PEROUT\n",
551 			   perout_pin);
552 		ret = -EBUSY;
553 		goto failed;
554 	}
555 
556 	start_sec = perout_request->start.sec;
557 	start_sec += perout_request->start.nsec / 1000000000;
558 	start_nsec = perout_request->start.nsec % 1000000000;
559 
560 	period_sec = perout_request->period.sec;
561 	period_sec += perout_request->period.nsec / 1000000000;
562 	period_nsec = perout_request->period.nsec % 1000000000;
563 
564 	if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
565 		struct timespec64 ts_on, ts_period;
566 		s64 wf_high, period64, half;
567 		s32 reminder;
568 
569 		ts_on.tv_sec = perout_request->on.sec;
570 		ts_on.tv_nsec = perout_request->on.nsec;
571 		wf_high = timespec64_to_ns(&ts_on);
572 		ts_period.tv_sec = perout_request->period.sec;
573 		ts_period.tv_nsec = perout_request->period.nsec;
574 		period64 = timespec64_to_ns(&ts_period);
575 
576 		if (period64 < 200) {
577 			netif_warn(adapter, drv, adapter->netdev,
578 				   "perout period too small, minimum is 200nS\n");
579 			ret = -EOPNOTSUPP;
580 			goto failed;
581 		}
582 		if (wf_high >= period64) {
583 			netif_warn(adapter, drv, adapter->netdev,
584 				   "pulse width must be smaller than period\n");
585 			ret = -EINVAL;
586 			goto failed;
587 		}
588 
589 		/* Check if we can do 50% toggle on an even value of period.
590 		 * If the period number is odd, then check if the requested
591 		 * pulse width is the same as one of pre-defined width values.
592 		 * Otherwise, return failure.
593 		 */
594 		half = div_s64_rem(period64, 2, &reminder);
595 		if (!reminder) {
596 			if (half == wf_high) {
597 				/* It's 50% match. Use the toggle option */
598 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
599 				/* In this case, devide period value by 2 */
600 				ts_period = ns_to_timespec64(div_s64(period64, 2));
601 				period_sec = ts_period.tv_sec;
602 				period_nsec = ts_period.tv_nsec;
603 
604 				goto program;
605 			}
606 		}
607 		/* if we can't do toggle, then the width option needs to be the exact match */
608 		if (wf_high == 200000000) {
609 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
610 		} else if (wf_high == 10000000) {
611 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
612 		} else if (wf_high == 1000000) {
613 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
614 		} else if (wf_high == 100000) {
615 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
616 		} else if (wf_high == 10000) {
617 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
618 		} else if (wf_high == 100) {
619 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
620 		} else {
621 			netif_warn(adapter, drv, adapter->netdev,
622 				   "duty cycle specified is not supported\n");
623 			ret = -EOPNOTSUPP;
624 			goto failed;
625 		}
626 	} else {
627 		if (period_sec == 0) {
628 			if (period_nsec >= 400000000) {
629 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
630 			} else if (period_nsec >= 20000000) {
631 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
632 			} else if (period_nsec >= 2000000) {
633 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
634 			} else if (period_nsec >= 200000) {
635 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
636 			} else if (period_nsec >= 20000) {
637 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
638 			} else if (period_nsec >= 200) {
639 				pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
640 			} else {
641 				netif_warn(adapter, drv, adapter->netdev,
642 					   "perout period too small, minimum is 200nS\n");
643 				ret = -EOPNOTSUPP;
644 				goto failed;
645 			}
646 		} else {
647 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
648 		}
649 	}
650 program:
651 
652 	/* turn off by setting target far in future */
653 	lan743x_csr_write(adapter,
654 			  PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
655 			  0xFFFF0000);
656 	lan743x_csr_write(adapter,
657 			  PTP_CLOCK_TARGET_NS_X(perout->event_ch), 0);
658 
659 	/* Configure to pulse every period */
660 	general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
661 	general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
662 			  (perout->event_ch));
663 	general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
664 			  (perout->event_ch, pulse_width);
665 	general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
666 			  (perout->event_ch);
667 	lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
668 
669 	/* set the reload to one toggle cycle */
670 	lan743x_csr_write(adapter,
671 			  PTP_CLOCK_TARGET_RELOAD_SEC_X(perout->event_ch),
672 			  period_sec);
673 	lan743x_csr_write(adapter,
674 			  PTP_CLOCK_TARGET_RELOAD_NS_X(perout->event_ch),
675 			  period_nsec);
676 
677 	/* set the start time */
678 	lan743x_csr_write(adapter,
679 			  PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
680 			  start_sec);
681 	lan743x_csr_write(adapter,
682 			  PTP_CLOCK_TARGET_NS_X(perout->event_ch),
683 			  start_nsec);
684 
685 	return 0;
686 
687 failed:
688 	lan743x_ptp_perout_off(adapter, index);
689 	return ret;
690 }
691 
692 static void lan743x_ptp_io_perout_off(struct lan743x_adapter *adapter,
693 				      u32 index)
694 {
695 	struct lan743x_ptp *ptp = &adapter->ptp;
696 	int perout_pin;
697 	int event_ch;
698 	u32 gen_cfg;
699 	int val;
700 
701 	event_ch = ptp->ptp_io_perout[index];
702 	if (event_ch >= 0) {
703 		/* set target to far in the future, effectively disabling it */
704 		lan743x_csr_write(adapter,
705 				  PTP_CLOCK_TARGET_SEC_X(event_ch),
706 				  0xFFFF0000);
707 		lan743x_csr_write(adapter,
708 				  PTP_CLOCK_TARGET_NS_X(event_ch),
709 				  0);
710 
711 		gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
712 		gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
713 				    (event_ch));
714 		gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch));
715 		gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch);
716 		lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
717 		if (event_ch)
718 			lan743x_csr_write(adapter, PTP_INT_STS,
719 					  PTP_INT_TIMER_INT_B_);
720 		else
721 			lan743x_csr_write(adapter, PTP_INT_STS,
722 					  PTP_INT_TIMER_INT_A_);
723 		lan743x_ptp_release_event_ch(adapter, event_ch);
724 		ptp->ptp_io_perout[index] = -1;
725 	}
726 
727 	perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
728 
729 	/* Deselect Event output */
730 	val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
731 
732 	/* Disables the output of Local Time Target compare events */
733 	val &= ~PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
734 	lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);
735 
736 	/* Configured as an opendrain driver*/
737 	val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
738 	val &= ~PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
739 	lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
740 	/* Dummy read to make sure write operation success */
741 	val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
742 }
743 
744 static int lan743x_ptp_io_perout(struct lan743x_adapter *adapter, int on,
745 				 struct ptp_perout_request *perout_request)
746 {
747 	struct lan743x_ptp *ptp = &adapter->ptp;
748 	u32 period_sec, period_nsec;
749 	u32 start_sec, start_nsec;
750 	u32 pulse_sec, pulse_nsec;
751 	int pulse_width;
752 	int perout_pin;
753 	int event_ch;
754 	u32 gen_cfg;
755 	u32 index;
756 	int val;
757 
758 	index = perout_request->index;
759 	event_ch = ptp->ptp_io_perout[index];
760 
761 	if (on) {
762 		perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
763 		if (perout_pin < 0)
764 			return -EBUSY;
765 	} else {
766 		lan743x_ptp_io_perout_off(adapter, index);
767 		return 0;
768 	}
769 
770 	if (event_ch >= LAN743X_PTP_N_EVENT_CHAN) {
771 		/* already on, turn off first */
772 		lan743x_ptp_io_perout_off(adapter, index);
773 	}
774 
775 	event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
776 	if (event_ch < 0) {
777 		netif_warn(adapter, drv, adapter->netdev,
778 			   "Failed to reserve event channel %d for PEROUT\n",
779 			   index);
780 		goto failed;
781 	}
782 	ptp->ptp_io_perout[index] = event_ch;
783 
784 	if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
785 		pulse_sec = perout_request->on.sec;
786 		pulse_sec += perout_request->on.nsec / 1000000000;
787 		pulse_nsec = perout_request->on.nsec % 1000000000;
788 	} else {
789 		pulse_sec = perout_request->period.sec;
790 		pulse_sec += perout_request->period.nsec / 1000000000;
791 		pulse_nsec = perout_request->period.nsec % 1000000000;
792 	}
793 
794 	if (pulse_sec == 0) {
795 		if (pulse_nsec >= 400000000) {
796 			pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
797 		} else if (pulse_nsec >= 200000000) {
798 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100MS_;
799 		} else if (pulse_nsec >= 100000000) {
800 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50MS_;
801 		} else if (pulse_nsec >= 20000000) {
802 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
803 		} else if (pulse_nsec >= 10000000) {
804 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5MS_;
805 		} else if (pulse_nsec >= 2000000) {
806 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
807 		} else if (pulse_nsec >= 1000000) {
808 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500US_;
809 		} else if (pulse_nsec >= 200000) {
810 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
811 		} else if (pulse_nsec >= 100000) {
812 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50US_;
813 		} else if (pulse_nsec >= 20000) {
814 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
815 		} else if (pulse_nsec >= 10000) {
816 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5US_;
817 		} else if (pulse_nsec >= 2000) {
818 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1US_;
819 		} else if (pulse_nsec >= 1000) {
820 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500NS_;
821 		} else if (pulse_nsec >= 200) {
822 			pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
823 		} else {
824 			netif_warn(adapter, drv, adapter->netdev,
825 				   "perout period too small, min is 200nS\n");
826 			goto failed;
827 		}
828 	} else {
829 		pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
830 	}
831 
832 	/* turn off by setting target far in future */
833 	lan743x_csr_write(adapter,
834 			  PTP_CLOCK_TARGET_SEC_X(event_ch),
835 			  0xFFFF0000);
836 	lan743x_csr_write(adapter,
837 			  PTP_CLOCK_TARGET_NS_X(event_ch), 0);
838 
839 	/* Configure to pulse every period */
840 	gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
841 	gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_(event_ch));
842 	gen_cfg |= HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
843 			  (event_ch, pulse_width);
844 	gen_cfg |= HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch);
845 	gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch));
846 	lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
847 
848 	/* set the reload to one toggle cycle */
849 	period_sec = perout_request->period.sec;
850 	period_sec += perout_request->period.nsec / 1000000000;
851 	period_nsec = perout_request->period.nsec % 1000000000;
852 	lan743x_csr_write(adapter,
853 			  PTP_CLOCK_TARGET_RELOAD_SEC_X(event_ch),
854 			  period_sec);
855 	lan743x_csr_write(adapter,
856 			  PTP_CLOCK_TARGET_RELOAD_NS_X(event_ch),
857 			  period_nsec);
858 
859 	start_sec = perout_request->start.sec;
860 	start_sec += perout_request->start.nsec / 1000000000;
861 	start_nsec = perout_request->start.nsec % 1000000000;
862 
863 	/* set the start time */
864 	lan743x_csr_write(adapter,
865 			  PTP_CLOCK_TARGET_SEC_X(event_ch),
866 			  start_sec);
867 	lan743x_csr_write(adapter,
868 			  PTP_CLOCK_TARGET_NS_X(event_ch),
869 			  start_nsec);
870 
871 	/* Enable LTC Target Read */
872 	val = lan743x_csr_read(adapter, PTP_CMD_CTL);
873 	val |= PTP_CMD_CTL_PTP_LTC_TARGET_READ_;
874 	lan743x_csr_write(adapter, PTP_CMD_CTL, val);
875 
876 	/* Configure as an push/pull driver */
877 	val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
878 	val |= PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
879 	lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
880 
881 	/* Select Event output */
882 	val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
883 	if (event_ch)
884 		/* Channel B as the output */
885 		val |= PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
886 	else
887 		/* Channel A as the output */
888 		val &= ~PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
889 
890 	/* Enables the output of Local Time Target compare events */
891 	val |= PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
892 	lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);
893 
894 	return 0;
895 
896 failed:
897 	lan743x_ptp_io_perout_off(adapter, index);
898 	return -ENODEV;
899 }
900 
901 static void lan743x_ptp_io_extts_off(struct lan743x_adapter *adapter,
902 				     u32 index)
903 {
904 	struct lan743x_ptp *ptp = &adapter->ptp;
905 	struct lan743x_extts *extts;
906 	int val;
907 
908 	extts = &ptp->extts[index];
909 	/* PTP Interrupt Enable Clear Register */
910 	if (extts->flags & PTP_FALLING_EDGE)
911 		val = PTP_INT_EN_FE_EN_CLR_(index);
912 	else
913 		val = PTP_INT_EN_RE_EN_CLR_(index);
914 	lan743x_csr_write(adapter, PTP_INT_EN_CLR, val);
915 
916 	/* Disables PTP-IO edge lock */
917 	val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
918 	if (extts->flags & PTP_FALLING_EDGE) {
919 		val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(index);
920 		val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(index);
921 	} else {
922 		val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(index);
923 		val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(index);
924 	}
925 	lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);
926 
927 	/* PTP-IO De-select register */
928 	val = lan743x_csr_read(adapter, PTP_IO_SEL);
929 	val &= ~PTP_IO_SEL_MASK_;
930 	lan743x_csr_write(adapter, PTP_IO_SEL, val);
931 
932 	/* Clear timestamp */
933 	memset(&extts->ts, 0, sizeof(struct timespec64));
934 	extts->flags = 0;
935 }
936 
937 static int lan743x_ptp_io_event_cap_en(struct lan743x_adapter *adapter,
938 				       u32 flags, u32 channel)
939 {
940 	struct lan743x_ptp *ptp = &adapter->ptp;
941 	int val;
942 
943 	if ((flags & PTP_EXTTS_EDGES) ==  PTP_EXTTS_EDGES)
944 		return -EOPNOTSUPP;
945 
946 	mutex_lock(&ptp->command_lock);
947 	/* PTP-IO Event Capture Enable */
948 	val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
949 	if (flags & PTP_FALLING_EDGE) {
950 		val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
951 		val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
952 		val |= PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
953 		val |= PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
954 	} else {
955 		/* Rising eventing as Default */
956 		val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
957 		val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
958 		val |= PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
959 		val |= PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
960 	}
961 	lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);
962 
963 	/* PTP-IO Select */
964 	val = lan743x_csr_read(adapter, PTP_IO_SEL);
965 	val &= ~PTP_IO_SEL_MASK_;
966 	val |= channel << PTP_IO_SEL_SHIFT_;
967 	lan743x_csr_write(adapter, PTP_IO_SEL, val);
968 
969 	/* PTP Interrupt Enable Register */
970 	if (flags & PTP_FALLING_EDGE)
971 		val = PTP_INT_EN_FE_EN_SET_(channel);
972 	else
973 		val = PTP_INT_EN_RE_EN_SET_(channel);
974 	lan743x_csr_write(adapter, PTP_INT_EN_SET, val);
975 
976 	mutex_unlock(&ptp->command_lock);
977 
978 	return 0;
979 }
980 
981 static int lan743x_ptp_io_extts(struct lan743x_adapter *adapter, int on,
982 				struct ptp_extts_request *extts_request)
983 {
984 	struct lan743x_ptp *ptp = &adapter->ptp;
985 	u32 flags = extts_request->flags;
986 	u32 index = extts_request->index;
987 	struct lan743x_extts *extts;
988 	int extts_pin;
989 	int ret = 0;
990 
991 	extts = &ptp->extts[index];
992 
993 	if (on) {
994 		extts_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, index);
995 		if (extts_pin < 0)
996 			return -EBUSY;
997 
998 		ret = lan743x_ptp_io_event_cap_en(adapter, flags, index);
999 		if (!ret)
1000 			extts->flags = flags;
1001 	} else {
1002 		lan743x_ptp_io_extts_off(adapter, index);
1003 	}
1004 
1005 	return ret;
1006 }
1007 
1008 static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
1009 				struct ptp_clock_request *request, int on)
1010 {
1011 	struct lan743x_ptp *ptp =
1012 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
1013 	struct lan743x_adapter *adapter =
1014 		container_of(ptp, struct lan743x_adapter, ptp);
1015 
1016 	if (request) {
1017 		switch (request->type) {
1018 		case PTP_CLK_REQ_EXTTS:
1019 			if (request->extts.index < ptpci->n_ext_ts)
1020 				return lan743x_ptp_io_extts(adapter, on,
1021 							 &request->extts);
1022 			return -EINVAL;
1023 		case PTP_CLK_REQ_PEROUT:
1024 			if (request->perout.index < ptpci->n_per_out) {
1025 				if (adapter->is_pci11x1x)
1026 					return lan743x_ptp_io_perout(adapter, on,
1027 							     &request->perout);
1028 				else
1029 					return lan743x_ptp_perout(adapter, on,
1030 							  &request->perout);
1031 			}
1032 			return -EINVAL;
1033 		case PTP_CLK_REQ_PPS:
1034 			return -EINVAL;
1035 		default:
1036 			netif_err(adapter, drv, adapter->netdev,
1037 				  "request->type == %d, Unknown\n",
1038 				  request->type);
1039 			break;
1040 		}
1041 	} else {
1042 		netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
1043 	}
1044 	return 0;
1045 }
1046 
1047 static int lan743x_ptpci_verify_pin_config(struct ptp_clock_info *ptp,
1048 					   unsigned int pin,
1049 					   enum ptp_pin_function func,
1050 					   unsigned int chan)
1051 {
1052 	struct lan743x_ptp *lan_ptp =
1053 		container_of(ptp, struct lan743x_ptp, ptp_clock_info);
1054 	struct lan743x_adapter *adapter =
1055 		container_of(lan_ptp, struct lan743x_adapter, ptp);
1056 	int result = 0;
1057 
1058 	/* Confirm the requested function is supported. Parameter
1059 	 * validation is done by the caller.
1060 	 */
1061 	switch (func) {
1062 	case PTP_PF_NONE:
1063 	case PTP_PF_PEROUT:
1064 		break;
1065 	case PTP_PF_EXTTS:
1066 		if (!adapter->is_pci11x1x)
1067 			result = -1;
1068 		break;
1069 	case PTP_PF_PHYSYNC:
1070 	default:
1071 		result = -1;
1072 		break;
1073 	}
1074 	return result;
1075 }
1076 
1077 static void lan743x_ptp_io_event_clock_get(struct lan743x_adapter *adapter,
1078 					   bool fe, u8 channel,
1079 					   struct timespec64 *ts)
1080 {
1081 	struct lan743x_ptp *ptp = &adapter->ptp;
1082 	struct lan743x_extts *extts;
1083 	u32 sec, nsec;
1084 
1085 	mutex_lock(&ptp->command_lock);
1086 	if (fe) {
1087 		sec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_SEC_CAP_X);
1088 		nsec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_NS_CAP_X);
1089 	} else {
1090 		sec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_SEC_CAP_X);
1091 		nsec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_NS_CAP_X);
1092 	}
1093 
1094 	mutex_unlock(&ptp->command_lock);
1095 
1096 	/* Update Local timestamp */
1097 	extts = &ptp->extts[channel];
1098 	extts->ts.tv_sec = sec;
1099 	extts->ts.tv_nsec = nsec;
1100 	ts->tv_sec = sec;
1101 	ts->tv_nsec = nsec;
1102 }
1103 
1104 static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
1105 {
1106 	struct lan743x_ptp *ptp =
1107 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
1108 	struct lan743x_adapter *adapter =
1109 		container_of(ptp, struct lan743x_adapter, ptp);
1110 	u32 cap_info, cause, header, nsec, seconds;
1111 	bool new_timestamp_available = false;
1112 	struct ptp_clock_event ptp_event;
1113 	struct timespec64 ts;
1114 	int ptp_int_sts;
1115 	int count = 0;
1116 	int channel;
1117 	s64 ns;
1118 
1119 	ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1120 	while ((count < 100) && ptp_int_sts) {
1121 		count++;
1122 
1123 		if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
1124 			cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
1125 
1126 			if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
1127 				seconds = lan743x_csr_read(adapter,
1128 							   PTP_TX_EGRESS_SEC);
1129 				nsec = lan743x_csr_read(adapter,
1130 							PTP_TX_EGRESS_NS);
1131 				cause = (nsec &
1132 					 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
1133 				header = lan743x_csr_read(adapter,
1134 							  PTP_TX_MSG_HEADER);
1135 
1136 				if (cause ==
1137 				    PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
1138 					nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
1139 					lan743x_ptp_tx_ts_enqueue_ts(adapter,
1140 								     seconds,
1141 								     nsec,
1142 								     header);
1143 					new_timestamp_available = true;
1144 				} else if (cause ==
1145 					   PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
1146 					netif_err(adapter, drv, adapter->netdev,
1147 						  "Auto capture cause not supported\n");
1148 				} else {
1149 					netif_warn(adapter, drv, adapter->netdev,
1150 						   "unknown tx timestamp capture cause\n");
1151 				}
1152 			} else {
1153 				netif_warn(adapter, drv, adapter->netdev,
1154 					   "TX TS INT but no TX TS CNT\n");
1155 			}
1156 			lan743x_csr_write(adapter, PTP_INT_STS,
1157 					  PTP_INT_BIT_TX_TS_);
1158 		}
1159 
1160 		if (ptp_int_sts & PTP_INT_IO_FE_MASK_) {
1161 			do {
1162 				channel = lan743x_get_channel((ptp_int_sts &
1163 							PTP_INT_IO_FE_MASK_) >>
1164 							PTP_INT_IO_FE_SHIFT_);
1165 				if (channel >= 0 &&
1166 				    channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
1167 					lan743x_ptp_io_event_clock_get(adapter,
1168 								       true,
1169 								       channel,
1170 								       &ts);
1171 					/* PTP Falling Event post */
1172 					ns = timespec64_to_ns(&ts);
1173 					ptp_event.timestamp = ns;
1174 					ptp_event.index = channel;
1175 					ptp_event.type = PTP_CLOCK_EXTTS;
1176 					ptp_clock_event(ptp->ptp_clock,
1177 							&ptp_event);
1178 					lan743x_csr_write(adapter, PTP_INT_STS,
1179 							  PTP_INT_IO_FE_SET_
1180 							  (channel));
1181 					ptp_int_sts &= ~(1 <<
1182 							 (PTP_INT_IO_FE_SHIFT_ +
1183 							  channel));
1184 				} else {
1185 					/* Clear falling event interrupts */
1186 					lan743x_csr_write(adapter, PTP_INT_STS,
1187 							  PTP_INT_IO_FE_MASK_);
1188 					ptp_int_sts &= ~PTP_INT_IO_FE_MASK_;
1189 				}
1190 			} while (ptp_int_sts & PTP_INT_IO_FE_MASK_);
1191 		}
1192 
1193 		if (ptp_int_sts & PTP_INT_IO_RE_MASK_) {
1194 			do {
1195 				channel = lan743x_get_channel((ptp_int_sts &
1196 						       PTP_INT_IO_RE_MASK_) >>
1197 						       PTP_INT_IO_RE_SHIFT_);
1198 				if (channel >= 0 &&
1199 				    channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
1200 					lan743x_ptp_io_event_clock_get(adapter,
1201 								       false,
1202 								       channel,
1203 								       &ts);
1204 					/* PTP Rising Event post */
1205 					ns = timespec64_to_ns(&ts);
1206 					ptp_event.timestamp = ns;
1207 					ptp_event.index = channel;
1208 					ptp_event.type = PTP_CLOCK_EXTTS;
1209 					ptp_clock_event(ptp->ptp_clock,
1210 							&ptp_event);
1211 					lan743x_csr_write(adapter, PTP_INT_STS,
1212 							  PTP_INT_IO_RE_SET_
1213 							  (channel));
1214 					ptp_int_sts &= ~(1 <<
1215 							 (PTP_INT_IO_RE_SHIFT_ +
1216 							  channel));
1217 				} else {
1218 					/* Clear Rising event interrupt */
1219 					lan743x_csr_write(adapter, PTP_INT_STS,
1220 							  PTP_INT_IO_RE_MASK_);
1221 					ptp_int_sts &= ~PTP_INT_IO_RE_MASK_;
1222 				}
1223 			} while (ptp_int_sts & PTP_INT_IO_RE_MASK_);
1224 		}
1225 
1226 		ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1227 	}
1228 
1229 	if (new_timestamp_available)
1230 		lan743x_ptp_tx_ts_complete(adapter);
1231 
1232 	lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1233 
1234 	return -1;
1235 }
1236 
1237 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
1238 				  u32 *seconds, u32 *nano_seconds,
1239 				  u32 *sub_nano_seconds)
1240 {
1241 	struct lan743x_ptp *ptp = &adapter->ptp;
1242 
1243 	mutex_lock(&ptp->command_lock);
1244 
1245 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
1246 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
1247 
1248 	if (seconds)
1249 		(*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
1250 
1251 	if (nano_seconds)
1252 		(*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
1253 
1254 	if (sub_nano_seconds)
1255 		(*sub_nano_seconds) =
1256 		lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
1257 
1258 	mutex_unlock(&ptp->command_lock);
1259 }
1260 
1261 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
1262 				     u32 *sec, u32 *nsec, u32 *sub_nsec)
1263 {
1264 	struct lan743x_ptp *ptp = &adapter->ptp;
1265 
1266 	mutex_lock(&ptp->command_lock);
1267 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
1268 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
1269 
1270 	if (sec)
1271 		(*sec) = lan743x_csr_read(adapter, PTP_LTC_RD_SEC_LO);
1272 
1273 	if (nsec)
1274 		(*nsec) = lan743x_csr_read(adapter, PTP_LTC_RD_NS);
1275 
1276 	if (sub_nsec)
1277 		(*sub_nsec) =
1278 		lan743x_csr_read(adapter, PTP_LTC_RD_SUBNS);
1279 
1280 	mutex_unlock(&ptp->command_lock);
1281 }
1282 
1283 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
1284 				   s64 time_step_ns)
1285 {
1286 	struct lan743x_ptp *ptp = &adapter->ptp;
1287 	u32 nano_seconds_step = 0;
1288 	u64 abs_time_step_ns = 0;
1289 	u32 unsigned_seconds = 0;
1290 	u32 nano_seconds = 0;
1291 	u32 remainder = 0;
1292 	s32 seconds = 0;
1293 
1294 	if (time_step_ns >  15000000000LL) {
1295 		/* convert to clock set */
1296 		if (adapter->is_pci11x1x)
1297 			lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
1298 						 &nano_seconds, NULL);
1299 		else
1300 			lan743x_ptp_clock_get(adapter, &unsigned_seconds,
1301 					      &nano_seconds, NULL);
1302 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
1303 						&remainder);
1304 		nano_seconds += remainder;
1305 		if (nano_seconds >= 1000000000) {
1306 			unsigned_seconds++;
1307 			nano_seconds -= 1000000000;
1308 		}
1309 		lan743x_ptp_clock_set(adapter, unsigned_seconds,
1310 				      nano_seconds, 0);
1311 		return;
1312 	} else if (time_step_ns < -15000000000LL) {
1313 		/* convert to clock set */
1314 		time_step_ns = -time_step_ns;
1315 
1316 		if (adapter->is_pci11x1x) {
1317 			lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
1318 						 &nano_seconds, NULL);
1319 		} else {
1320 			lan743x_ptp_clock_get(adapter, &unsigned_seconds,
1321 					      &nano_seconds, NULL);
1322 		}
1323 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
1324 						&remainder);
1325 		nano_seconds_step = remainder;
1326 		if (nano_seconds < nano_seconds_step) {
1327 			unsigned_seconds--;
1328 			nano_seconds += 1000000000;
1329 		}
1330 		nano_seconds -= nano_seconds_step;
1331 		lan743x_ptp_clock_set(adapter, unsigned_seconds,
1332 				      nano_seconds, 0);
1333 		return;
1334 	}
1335 
1336 	/* do clock step */
1337 	if (time_step_ns >= 0) {
1338 		abs_time_step_ns = (u64)(time_step_ns);
1339 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
1340 					   &remainder);
1341 		nano_seconds = (u32)remainder;
1342 	} else {
1343 		abs_time_step_ns = (u64)(-time_step_ns);
1344 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
1345 					     &remainder));
1346 		nano_seconds = (u32)remainder;
1347 		if (nano_seconds > 0) {
1348 			/* subtracting nano seconds is not allowed
1349 			 * convert to subtracting from seconds,
1350 			 * and adding to nanoseconds
1351 			 */
1352 			seconds--;
1353 			nano_seconds = (1000000000 - nano_seconds);
1354 		}
1355 	}
1356 
1357 	if (nano_seconds > 0) {
1358 		/* add 8 ns to cover the likely normal increment */
1359 		nano_seconds += 8;
1360 	}
1361 
1362 	if (nano_seconds >= 1000000000) {
1363 		/* carry into seconds */
1364 		seconds++;
1365 		nano_seconds -= 1000000000;
1366 	}
1367 
1368 	while (seconds) {
1369 		mutex_lock(&ptp->command_lock);
1370 		if (seconds > 0) {
1371 			u32 adjustment_value = (u32)seconds;
1372 
1373 			if (adjustment_value > 0xF)
1374 				adjustment_value = 0xF;
1375 			lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1376 					  PTP_CLOCK_STEP_ADJ_DIR_ |
1377 					  adjustment_value);
1378 			seconds -= ((s32)adjustment_value);
1379 		} else {
1380 			u32 adjustment_value = (u32)(-seconds);
1381 
1382 			if (adjustment_value > 0xF)
1383 				adjustment_value = 0xF;
1384 			lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1385 					  adjustment_value);
1386 			seconds += ((s32)adjustment_value);
1387 		}
1388 		lan743x_csr_write(adapter, PTP_CMD_CTL,
1389 				  PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
1390 		lan743x_ptp_wait_till_cmd_done(adapter,
1391 					       PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
1392 		mutex_unlock(&ptp->command_lock);
1393 	}
1394 	if (nano_seconds) {
1395 		mutex_lock(&ptp->command_lock);
1396 		lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1397 				  PTP_CLOCK_STEP_ADJ_DIR_ |
1398 				  (nano_seconds &
1399 				  PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
1400 		lan743x_csr_write(adapter, PTP_CMD_CTL,
1401 				  PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
1402 		lan743x_ptp_wait_till_cmd_done(adapter,
1403 					       PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
1404 		mutex_unlock(&ptp->command_lock);
1405 	}
1406 }
1407 
1408 void lan743x_ptp_isr(void *context)
1409 {
1410 	struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
1411 	struct lan743x_ptp *ptp = NULL;
1412 	int enable_flag = 1;
1413 	u32 ptp_int_sts = 0;
1414 
1415 	ptp = &adapter->ptp;
1416 
1417 	lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1418 
1419 	ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1420 	ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
1421 
1422 	if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
1423 		ptp_schedule_worker(ptp->ptp_clock, 0);
1424 		enable_flag = 0;/* tasklet will re-enable later */
1425 	}
1426 	if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
1427 		netif_err(adapter, drv, adapter->netdev,
1428 			  "PTP TX Software Timestamp Error\n");
1429 		/* clear int status bit */
1430 		lan743x_csr_write(adapter, PTP_INT_STS,
1431 				  PTP_INT_BIT_TX_SWTS_ERR_);
1432 	}
1433 	if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
1434 		/* clear int status bit */
1435 		lan743x_csr_write(adapter, PTP_INT_STS,
1436 				  PTP_INT_BIT_TIMER_B_);
1437 	}
1438 	if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
1439 		/* clear int status bit */
1440 		lan743x_csr_write(adapter, PTP_INT_STS,
1441 				  PTP_INT_BIT_TIMER_A_);
1442 	}
1443 
1444 	if (enable_flag) {
1445 		/* re-enable isr */
1446 		lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1447 	}
1448 }
1449 
1450 static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
1451 					  struct sk_buff *skb, bool ignore_sync)
1452 {
1453 	struct lan743x_ptp *ptp = &adapter->ptp;
1454 
1455 	spin_lock_bh(&ptp->tx_ts_lock);
1456 	if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1457 		ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
1458 		if (ignore_sync)
1459 			ptp->tx_ts_ignore_sync_queue |=
1460 				BIT(ptp->tx_ts_skb_queue_size);
1461 		ptp->tx_ts_skb_queue_size++;
1462 	} else {
1463 		/* this should never happen, so long as the tx channel
1464 		 * calls and honors the result from
1465 		 * lan743x_ptp_request_tx_timestamp
1466 		 */
1467 		netif_err(adapter, drv, adapter->netdev,
1468 			  "tx ts skb queue overflow\n");
1469 		dev_kfree_skb(skb);
1470 	}
1471 	spin_unlock_bh(&ptp->tx_ts_lock);
1472 }
1473 
1474 static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
1475 {
1476 	struct timespec64 ts;
1477 
1478 	ktime_get_clocktai_ts64(&ts);
1479 
1480 	lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
1481 }
1482 
1483 void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
1484 				u32 link_speed)
1485 {
1486 	switch (link_speed) {
1487 	case 10:
1488 		lan743x_csr_write(adapter, PTP_LATENCY,
1489 				  PTP_LATENCY_TX_SET_(0) |
1490 				  PTP_LATENCY_RX_SET_(0));
1491 		break;
1492 	case 100:
1493 		lan743x_csr_write(adapter, PTP_LATENCY,
1494 				  PTP_LATENCY_TX_SET_(181) |
1495 				  PTP_LATENCY_RX_SET_(594));
1496 		break;
1497 	case 1000:
1498 		lan743x_csr_write(adapter, PTP_LATENCY,
1499 				  PTP_LATENCY_TX_SET_(30) |
1500 				  PTP_LATENCY_RX_SET_(525));
1501 		break;
1502 	}
1503 }
1504 
1505 int lan743x_ptp_init(struct lan743x_adapter *adapter)
1506 {
1507 	struct lan743x_ptp *ptp = &adapter->ptp;
1508 	int i;
1509 
1510 	mutex_init(&ptp->command_lock);
1511 	spin_lock_init(&ptp->tx_ts_lock);
1512 	ptp->used_event_ch = 0;
1513 
1514 	for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) {
1515 		ptp->perout[i].event_ch = -1;
1516 		ptp->perout[i].gpio_pin = -1;
1517 	}
1518 
1519 	lan743x_led_mux_save(adapter);
1520 
1521 	return 0;
1522 }
1523 
1524 int lan743x_ptp_open(struct lan743x_adapter *adapter)
1525 {
1526 	struct lan743x_ptp *ptp = &adapter->ptp;
1527 	int ret = -ENODEV;
1528 	u32 temp;
1529 	int i;
1530 	int n_pins;
1531 
1532 	lan743x_ptp_reset(adapter);
1533 	lan743x_ptp_sync_to_system_clock(adapter);
1534 	temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
1535 	temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
1536 	lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
1537 	lan743x_ptp_enable(adapter);
1538 	lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1539 	lan743x_csr_write(adapter, PTP_INT_EN_SET,
1540 			  PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
1541 	ptp->flags |= PTP_FLAG_ISR_ENABLED;
1542 
1543 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
1544 		return 0;
1545 
1546 	switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
1547 	case ID_REV_ID_LAN7430_:
1548 		n_pins = LAN7430_N_GPIO;
1549 		break;
1550 	case ID_REV_ID_LAN7431_:
1551 	case ID_REV_ID_A011_:
1552 	case ID_REV_ID_A041_:
1553 		n_pins = LAN7431_N_GPIO;
1554 		break;
1555 	default:
1556 		netif_warn(adapter, drv, adapter->netdev,
1557 			   "Unknown LAN743x (%08x). Assuming no GPIO\n",
1558 			   adapter->csr.id_rev);
1559 		n_pins = 0;
1560 		break;
1561 	}
1562 
1563 	if (n_pins > LAN743X_PTP_N_GPIO)
1564 		n_pins = LAN743X_PTP_N_GPIO;
1565 
1566 	for (i = 0; i < n_pins; i++) {
1567 		struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i];
1568 
1569 		snprintf(ptp_pin->name,
1570 			 sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i);
1571 		ptp_pin->index = i;
1572 		ptp_pin->func = PTP_PF_NONE;
1573 	}
1574 
1575 	ptp->ptp_clock_info.owner = THIS_MODULE;
1576 	snprintf(ptp->ptp_clock_info.name, 16, "%pm",
1577 		 adapter->netdev->dev_addr);
1578 	ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
1579 	ptp->ptp_clock_info.n_alarm = 0;
1580 	ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS;
1581 	ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
1582 	ptp->ptp_clock_info.n_pins = n_pins;
1583 	ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
1584 	ptp->ptp_clock_info.pin_config = ptp->pin_config;
1585 	ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
1586 	ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
1587 	ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
1588 	ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
1589 	ptp->ptp_clock_info.getcrosststamp = NULL;
1590 	ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
1591 	ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
1592 	ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
1593 	ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config;
1594 
1595 	ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
1596 					    &adapter->pdev->dev);
1597 
1598 	if (IS_ERR(ptp->ptp_clock)) {
1599 		netif_err(adapter, ifup, adapter->netdev,
1600 			  "ptp_clock_register failed\n");
1601 		goto done;
1602 	}
1603 	ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
1604 	netif_info(adapter, ifup, adapter->netdev,
1605 		   "successfully registered ptp clock\n");
1606 
1607 	return 0;
1608 done:
1609 	lan743x_ptp_close(adapter);
1610 	return ret;
1611 }
1612 
1613 void lan743x_ptp_close(struct lan743x_adapter *adapter)
1614 {
1615 	struct lan743x_ptp *ptp = &adapter->ptp;
1616 	int index;
1617 
1618 	if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
1619 	    (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) {
1620 		ptp_clock_unregister(ptp->ptp_clock);
1621 		ptp->ptp_clock = NULL;
1622 		ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
1623 		netif_info(adapter, drv, adapter->netdev,
1624 			   "ptp clock unregister\n");
1625 	}
1626 
1627 	if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
1628 		lan743x_csr_write(adapter, PTP_INT_EN_CLR,
1629 				  PTP_INT_BIT_TX_SWTS_ERR_ |
1630 				  PTP_INT_BIT_TX_TS_);
1631 		lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1632 		ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
1633 	}
1634 
1635 	/* clean up pending timestamp requests */
1636 	lan743x_ptp_tx_ts_complete(adapter);
1637 	spin_lock_bh(&ptp->tx_ts_lock);
1638 	for (index = 0;
1639 		index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
1640 		index++) {
1641 		struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
1642 
1643 		dev_kfree_skb(skb);
1644 		ptp->tx_ts_skb_queue[index] = NULL;
1645 		ptp->tx_ts_seconds_queue[index] = 0;
1646 		ptp->tx_ts_nseconds_queue[index] = 0;
1647 	}
1648 	ptp->tx_ts_skb_queue_size = 0;
1649 	ptp->tx_ts_queue_size = 0;
1650 	ptp->pending_tx_timestamps = 0;
1651 	spin_unlock_bh(&ptp->tx_ts_lock);
1652 
1653 	lan743x_led_mux_restore(adapter);
1654 
1655 	lan743x_ptp_disable(adapter);
1656 }
1657 
1658 static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
1659 					   bool ts_insert_enable)
1660 {
1661 	u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
1662 
1663 	if (ts_insert_enable)
1664 		ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1665 	else
1666 		ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1667 
1668 	lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
1669 }
1670 
1671 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
1672 {
1673 	if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
1674 		return true;
1675 	return false;
1676 }
1677 
1678 static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
1679 {
1680 	struct lan743x_ptp *ptp = &adapter->ptp;
1681 
1682 	mutex_lock(&ptp->command_lock);
1683 
1684 	if (lan743x_ptp_is_enabled(adapter)) {
1685 		netif_warn(adapter, drv, adapter->netdev,
1686 			   "PTP already enabled\n");
1687 		goto done;
1688 	}
1689 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
1690 done:
1691 	mutex_unlock(&ptp->command_lock);
1692 }
1693 
1694 static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
1695 {
1696 	struct lan743x_ptp *ptp = &adapter->ptp;
1697 
1698 	mutex_lock(&ptp->command_lock);
1699 	if (!lan743x_ptp_is_enabled(adapter)) {
1700 		netif_warn(adapter, drv, adapter->netdev,
1701 			   "PTP already disabled\n");
1702 		goto done;
1703 	}
1704 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
1705 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
1706 done:
1707 	mutex_unlock(&ptp->command_lock);
1708 }
1709 
1710 static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
1711 {
1712 	struct lan743x_ptp *ptp = &adapter->ptp;
1713 
1714 	mutex_lock(&ptp->command_lock);
1715 
1716 	if (lan743x_ptp_is_enabled(adapter)) {
1717 		netif_err(adapter, drv, adapter->netdev,
1718 			  "Attempting reset while enabled\n");
1719 		goto done;
1720 	}
1721 
1722 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
1723 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
1724 done:
1725 	mutex_unlock(&ptp->command_lock);
1726 }
1727 
1728 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
1729 				  u32 seconds, u32 nano_seconds,
1730 				  u32 sub_nano_seconds)
1731 {
1732 	struct lan743x_ptp *ptp = &adapter->ptp;
1733 
1734 	mutex_lock(&ptp->command_lock);
1735 
1736 	lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
1737 	lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
1738 	lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
1739 
1740 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1741 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1742 	mutex_unlock(&ptp->command_lock);
1743 }
1744 
1745 bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
1746 {
1747 	struct lan743x_ptp *ptp = &adapter->ptp;
1748 	bool result = false;
1749 
1750 	spin_lock_bh(&ptp->tx_ts_lock);
1751 	if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1752 		/* request granted */
1753 		ptp->pending_tx_timestamps++;
1754 		result = true;
1755 	}
1756 	spin_unlock_bh(&ptp->tx_ts_lock);
1757 	return result;
1758 }
1759 
1760 void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
1761 {
1762 	struct lan743x_ptp *ptp = &adapter->ptp;
1763 
1764 	spin_lock_bh(&ptp->tx_ts_lock);
1765 	if (ptp->pending_tx_timestamps > 0)
1766 		ptp->pending_tx_timestamps--;
1767 	else
1768 		netif_err(adapter, drv, adapter->netdev,
1769 			  "unrequest failed, pending_tx_timestamps==0\n");
1770 	spin_unlock_bh(&ptp->tx_ts_lock);
1771 }
1772 
1773 void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
1774 				  struct sk_buff *skb, bool ignore_sync)
1775 {
1776 	lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
1777 
1778 	lan743x_ptp_tx_ts_complete(adapter);
1779 }
1780 
1781 int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1782 {
1783 	struct lan743x_adapter *adapter = netdev_priv(netdev);
1784 	struct hwtstamp_config config;
1785 	int ret = 0;
1786 	int index;
1787 
1788 	if (!ifr) {
1789 		netif_err(adapter, drv, adapter->netdev,
1790 			  "SIOCSHWTSTAMP, ifr == NULL\n");
1791 		return -EINVAL;
1792 	}
1793 
1794 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1795 		return -EFAULT;
1796 
1797 	switch (config.tx_type) {
1798 	case HWTSTAMP_TX_OFF:
1799 		for (index = 0; index < adapter->used_tx_channels;
1800 		     index++)
1801 			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1802 							 false, false);
1803 		lan743x_ptp_set_sync_ts_insert(adapter, false);
1804 		break;
1805 	case HWTSTAMP_TX_ON:
1806 		for (index = 0; index < adapter->used_tx_channels;
1807 			index++)
1808 			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1809 							 true, false);
1810 		lan743x_ptp_set_sync_ts_insert(adapter, false);
1811 		break;
1812 	case HWTSTAMP_TX_ONESTEP_SYNC:
1813 		for (index = 0; index < adapter->used_tx_channels;
1814 			index++)
1815 			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1816 							 true, true);
1817 
1818 		lan743x_ptp_set_sync_ts_insert(adapter, true);
1819 		break;
1820 	case HWTSTAMP_TX_ONESTEP_P2P:
1821 		ret = -ERANGE;
1822 		break;
1823 	default:
1824 		netif_warn(adapter, drv, adapter->netdev,
1825 			   "  tx_type = %d, UNKNOWN\n", config.tx_type);
1826 		ret = -EINVAL;
1827 		break;
1828 	}
1829 
1830 	if (!ret)
1831 		return copy_to_user(ifr->ifr_data, &config,
1832 			sizeof(config)) ? -EFAULT : 0;
1833 	return ret;
1834 }
1835