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 	int result = 0;
1053 
1054 	/* Confirm the requested function is supported. Parameter
1055 	 * validation is done by the caller.
1056 	 */
1057 	switch (func) {
1058 	case PTP_PF_NONE:
1059 	case PTP_PF_PEROUT:
1060 	case PTP_PF_EXTTS:
1061 		break;
1062 	case PTP_PF_PHYSYNC:
1063 	default:
1064 		result = -1;
1065 		break;
1066 	}
1067 	return result;
1068 }
1069 
1070 static void lan743x_ptp_io_event_clock_get(struct lan743x_adapter *adapter,
1071 					   bool fe, u8 channel,
1072 					   struct timespec64 *ts)
1073 {
1074 	struct lan743x_ptp *ptp = &adapter->ptp;
1075 	struct lan743x_extts *extts;
1076 	u32 sec, nsec;
1077 
1078 	mutex_lock(&ptp->command_lock);
1079 	if (fe) {
1080 		sec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_SEC_CAP_X);
1081 		nsec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_NS_CAP_X);
1082 	} else {
1083 		sec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_SEC_CAP_X);
1084 		nsec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_NS_CAP_X);
1085 	}
1086 
1087 	mutex_unlock(&ptp->command_lock);
1088 
1089 	/* Update Local timestamp */
1090 	extts = &ptp->extts[channel];
1091 	extts->ts.tv_sec = sec;
1092 	extts->ts.tv_nsec = nsec;
1093 	ts->tv_sec = sec;
1094 	ts->tv_nsec = nsec;
1095 }
1096 
1097 static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
1098 {
1099 	struct lan743x_ptp *ptp =
1100 		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
1101 	struct lan743x_adapter *adapter =
1102 		container_of(ptp, struct lan743x_adapter, ptp);
1103 	u32 cap_info, cause, header, nsec, seconds;
1104 	bool new_timestamp_available = false;
1105 	struct ptp_clock_event ptp_event;
1106 	struct timespec64 ts;
1107 	int ptp_int_sts;
1108 	int count = 0;
1109 	int channel;
1110 	s64 ns;
1111 
1112 	ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1113 	while ((count < 100) && ptp_int_sts) {
1114 		count++;
1115 
1116 		if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
1117 			cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
1118 
1119 			if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
1120 				seconds = lan743x_csr_read(adapter,
1121 							   PTP_TX_EGRESS_SEC);
1122 				nsec = lan743x_csr_read(adapter,
1123 							PTP_TX_EGRESS_NS);
1124 				cause = (nsec &
1125 					 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
1126 				header = lan743x_csr_read(adapter,
1127 							  PTP_TX_MSG_HEADER);
1128 
1129 				if (cause ==
1130 				    PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
1131 					nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
1132 					lan743x_ptp_tx_ts_enqueue_ts(adapter,
1133 								     seconds,
1134 								     nsec,
1135 								     header);
1136 					new_timestamp_available = true;
1137 				} else if (cause ==
1138 					   PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
1139 					netif_err(adapter, drv, adapter->netdev,
1140 						  "Auto capture cause not supported\n");
1141 				} else {
1142 					netif_warn(adapter, drv, adapter->netdev,
1143 						   "unknown tx timestamp capture cause\n");
1144 				}
1145 			} else {
1146 				netif_warn(adapter, drv, adapter->netdev,
1147 					   "TX TS INT but no TX TS CNT\n");
1148 			}
1149 			lan743x_csr_write(adapter, PTP_INT_STS,
1150 					  PTP_INT_BIT_TX_TS_);
1151 		}
1152 
1153 		if (ptp_int_sts & PTP_INT_IO_FE_MASK_) {
1154 			do {
1155 				channel = lan743x_get_channel((ptp_int_sts &
1156 							PTP_INT_IO_FE_MASK_) >>
1157 							PTP_INT_IO_FE_SHIFT_);
1158 				if (channel >= 0 &&
1159 				    channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
1160 					lan743x_ptp_io_event_clock_get(adapter,
1161 								       true,
1162 								       channel,
1163 								       &ts);
1164 					/* PTP Falling Event post */
1165 					ns = timespec64_to_ns(&ts);
1166 					ptp_event.timestamp = ns;
1167 					ptp_event.index = channel;
1168 					ptp_event.type = PTP_CLOCK_EXTTS;
1169 					ptp_clock_event(ptp->ptp_clock,
1170 							&ptp_event);
1171 					lan743x_csr_write(adapter, PTP_INT_STS,
1172 							  PTP_INT_IO_FE_SET_
1173 							  (channel));
1174 					ptp_int_sts &= ~(1 <<
1175 							 (PTP_INT_IO_FE_SHIFT_ +
1176 							  channel));
1177 				} else {
1178 					/* Clear falling event interrupts */
1179 					lan743x_csr_write(adapter, PTP_INT_STS,
1180 							  PTP_INT_IO_FE_MASK_);
1181 					ptp_int_sts &= ~PTP_INT_IO_FE_MASK_;
1182 				}
1183 			} while (ptp_int_sts & PTP_INT_IO_FE_MASK_);
1184 		}
1185 
1186 		if (ptp_int_sts & PTP_INT_IO_RE_MASK_) {
1187 			do {
1188 				channel = lan743x_get_channel((ptp_int_sts &
1189 						       PTP_INT_IO_RE_MASK_) >>
1190 						       PTP_INT_IO_RE_SHIFT_);
1191 				if (channel >= 0 &&
1192 				    channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
1193 					lan743x_ptp_io_event_clock_get(adapter,
1194 								       false,
1195 								       channel,
1196 								       &ts);
1197 					/* PTP Rising Event post */
1198 					ns = timespec64_to_ns(&ts);
1199 					ptp_event.timestamp = ns;
1200 					ptp_event.index = channel;
1201 					ptp_event.type = PTP_CLOCK_EXTTS;
1202 					ptp_clock_event(ptp->ptp_clock,
1203 							&ptp_event);
1204 					lan743x_csr_write(adapter, PTP_INT_STS,
1205 							  PTP_INT_IO_RE_SET_
1206 							  (channel));
1207 					ptp_int_sts &= ~(1 <<
1208 							 (PTP_INT_IO_RE_SHIFT_ +
1209 							  channel));
1210 				} else {
1211 					/* Clear Rising event interrupt */
1212 					lan743x_csr_write(adapter, PTP_INT_STS,
1213 							  PTP_INT_IO_RE_MASK_);
1214 					ptp_int_sts &= ~PTP_INT_IO_RE_MASK_;
1215 				}
1216 			} while (ptp_int_sts & PTP_INT_IO_RE_MASK_);
1217 		}
1218 
1219 		ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1220 	}
1221 
1222 	if (new_timestamp_available)
1223 		lan743x_ptp_tx_ts_complete(adapter);
1224 
1225 	lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1226 
1227 	return -1;
1228 }
1229 
1230 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
1231 				  u32 *seconds, u32 *nano_seconds,
1232 				  u32 *sub_nano_seconds)
1233 {
1234 	struct lan743x_ptp *ptp = &adapter->ptp;
1235 
1236 	mutex_lock(&ptp->command_lock);
1237 
1238 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
1239 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
1240 
1241 	if (seconds)
1242 		(*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
1243 
1244 	if (nano_seconds)
1245 		(*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
1246 
1247 	if (sub_nano_seconds)
1248 		(*sub_nano_seconds) =
1249 		lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
1250 
1251 	mutex_unlock(&ptp->command_lock);
1252 }
1253 
1254 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
1255 				     u32 *sec, u32 *nsec, u32 *sub_nsec)
1256 {
1257 	struct lan743x_ptp *ptp = &adapter->ptp;
1258 
1259 	mutex_lock(&ptp->command_lock);
1260 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
1261 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
1262 
1263 	if (sec)
1264 		(*sec) = lan743x_csr_read(adapter, PTP_LTC_RD_SEC_LO);
1265 
1266 	if (nsec)
1267 		(*nsec) = lan743x_csr_read(adapter, PTP_LTC_RD_NS);
1268 
1269 	if (sub_nsec)
1270 		(*sub_nsec) =
1271 		lan743x_csr_read(adapter, PTP_LTC_RD_SUBNS);
1272 
1273 	mutex_unlock(&ptp->command_lock);
1274 }
1275 
1276 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
1277 				   s64 time_step_ns)
1278 {
1279 	struct lan743x_ptp *ptp = &adapter->ptp;
1280 	u32 nano_seconds_step = 0;
1281 	u64 abs_time_step_ns = 0;
1282 	u32 unsigned_seconds = 0;
1283 	u32 nano_seconds = 0;
1284 	u32 remainder = 0;
1285 	s32 seconds = 0;
1286 
1287 	if (time_step_ns >  15000000000LL) {
1288 		/* convert to clock set */
1289 		if (adapter->is_pci11x1x)
1290 			lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
1291 						 &nano_seconds, NULL);
1292 		else
1293 			lan743x_ptp_clock_get(adapter, &unsigned_seconds,
1294 					      &nano_seconds, NULL);
1295 		unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
1296 						&remainder);
1297 		nano_seconds += remainder;
1298 		if (nano_seconds >= 1000000000) {
1299 			unsigned_seconds++;
1300 			nano_seconds -= 1000000000;
1301 		}
1302 		lan743x_ptp_clock_set(adapter, unsigned_seconds,
1303 				      nano_seconds, 0);
1304 		return;
1305 	} else if (time_step_ns < -15000000000LL) {
1306 		/* convert to clock set */
1307 		time_step_ns = -time_step_ns;
1308 
1309 		if (adapter->is_pci11x1x) {
1310 			lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
1311 						 &nano_seconds, NULL);
1312 		} else {
1313 			lan743x_ptp_clock_get(adapter, &unsigned_seconds,
1314 					      &nano_seconds, NULL);
1315 		}
1316 		unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
1317 						&remainder);
1318 		nano_seconds_step = remainder;
1319 		if (nano_seconds < nano_seconds_step) {
1320 			unsigned_seconds--;
1321 			nano_seconds += 1000000000;
1322 		}
1323 		nano_seconds -= nano_seconds_step;
1324 		lan743x_ptp_clock_set(adapter, unsigned_seconds,
1325 				      nano_seconds, 0);
1326 		return;
1327 	}
1328 
1329 	/* do clock step */
1330 	if (time_step_ns >= 0) {
1331 		abs_time_step_ns = (u64)(time_step_ns);
1332 		seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
1333 					   &remainder);
1334 		nano_seconds = (u32)remainder;
1335 	} else {
1336 		abs_time_step_ns = (u64)(-time_step_ns);
1337 		seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
1338 					     &remainder));
1339 		nano_seconds = (u32)remainder;
1340 		if (nano_seconds > 0) {
1341 			/* subtracting nano seconds is not allowed
1342 			 * convert to subtracting from seconds,
1343 			 * and adding to nanoseconds
1344 			 */
1345 			seconds--;
1346 			nano_seconds = (1000000000 - nano_seconds);
1347 		}
1348 	}
1349 
1350 	if (nano_seconds > 0) {
1351 		/* add 8 ns to cover the likely normal increment */
1352 		nano_seconds += 8;
1353 	}
1354 
1355 	if (nano_seconds >= 1000000000) {
1356 		/* carry into seconds */
1357 		seconds++;
1358 		nano_seconds -= 1000000000;
1359 	}
1360 
1361 	while (seconds) {
1362 		mutex_lock(&ptp->command_lock);
1363 		if (seconds > 0) {
1364 			u32 adjustment_value = (u32)seconds;
1365 
1366 			if (adjustment_value > 0xF)
1367 				adjustment_value = 0xF;
1368 			lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1369 					  PTP_CLOCK_STEP_ADJ_DIR_ |
1370 					  adjustment_value);
1371 			seconds -= ((s32)adjustment_value);
1372 		} else {
1373 			u32 adjustment_value = (u32)(-seconds);
1374 
1375 			if (adjustment_value > 0xF)
1376 				adjustment_value = 0xF;
1377 			lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1378 					  adjustment_value);
1379 			seconds += ((s32)adjustment_value);
1380 		}
1381 		lan743x_csr_write(adapter, PTP_CMD_CTL,
1382 				  PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
1383 		lan743x_ptp_wait_till_cmd_done(adapter,
1384 					       PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
1385 		mutex_unlock(&ptp->command_lock);
1386 	}
1387 	if (nano_seconds) {
1388 		mutex_lock(&ptp->command_lock);
1389 		lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1390 				  PTP_CLOCK_STEP_ADJ_DIR_ |
1391 				  (nano_seconds &
1392 				  PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
1393 		lan743x_csr_write(adapter, PTP_CMD_CTL,
1394 				  PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
1395 		lan743x_ptp_wait_till_cmd_done(adapter,
1396 					       PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
1397 		mutex_unlock(&ptp->command_lock);
1398 	}
1399 }
1400 
1401 void lan743x_ptp_isr(void *context)
1402 {
1403 	struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
1404 	struct lan743x_ptp *ptp = NULL;
1405 	int enable_flag = 1;
1406 	u32 ptp_int_sts = 0;
1407 
1408 	ptp = &adapter->ptp;
1409 
1410 	lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1411 
1412 	ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1413 	ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
1414 
1415 	if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
1416 		ptp_schedule_worker(ptp->ptp_clock, 0);
1417 		enable_flag = 0;/* tasklet will re-enable later */
1418 	}
1419 	if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
1420 		netif_err(adapter, drv, adapter->netdev,
1421 			  "PTP TX Software Timestamp Error\n");
1422 		/* clear int status bit */
1423 		lan743x_csr_write(adapter, PTP_INT_STS,
1424 				  PTP_INT_BIT_TX_SWTS_ERR_);
1425 	}
1426 	if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
1427 		/* clear int status bit */
1428 		lan743x_csr_write(adapter, PTP_INT_STS,
1429 				  PTP_INT_BIT_TIMER_B_);
1430 	}
1431 	if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
1432 		/* clear int status bit */
1433 		lan743x_csr_write(adapter, PTP_INT_STS,
1434 				  PTP_INT_BIT_TIMER_A_);
1435 	}
1436 
1437 	if (enable_flag) {
1438 		/* re-enable isr */
1439 		lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1440 	}
1441 }
1442 
1443 static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
1444 					  struct sk_buff *skb, bool ignore_sync)
1445 {
1446 	struct lan743x_ptp *ptp = &adapter->ptp;
1447 
1448 	spin_lock_bh(&ptp->tx_ts_lock);
1449 	if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1450 		ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
1451 		if (ignore_sync)
1452 			ptp->tx_ts_ignore_sync_queue |=
1453 				BIT(ptp->tx_ts_skb_queue_size);
1454 		ptp->tx_ts_skb_queue_size++;
1455 	} else {
1456 		/* this should never happen, so long as the tx channel
1457 		 * calls and honors the result from
1458 		 * lan743x_ptp_request_tx_timestamp
1459 		 */
1460 		netif_err(adapter, drv, adapter->netdev,
1461 			  "tx ts skb queue overflow\n");
1462 		dev_kfree_skb(skb);
1463 	}
1464 	spin_unlock_bh(&ptp->tx_ts_lock);
1465 }
1466 
1467 static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
1468 {
1469 	struct timespec64 ts;
1470 
1471 	ktime_get_clocktai_ts64(&ts);
1472 
1473 	lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
1474 }
1475 
1476 void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
1477 				u32 link_speed)
1478 {
1479 	switch (link_speed) {
1480 	case 10:
1481 		lan743x_csr_write(adapter, PTP_LATENCY,
1482 				  PTP_LATENCY_TX_SET_(0) |
1483 				  PTP_LATENCY_RX_SET_(0));
1484 		break;
1485 	case 100:
1486 		lan743x_csr_write(adapter, PTP_LATENCY,
1487 				  PTP_LATENCY_TX_SET_(181) |
1488 				  PTP_LATENCY_RX_SET_(594));
1489 		break;
1490 	case 1000:
1491 		lan743x_csr_write(adapter, PTP_LATENCY,
1492 				  PTP_LATENCY_TX_SET_(30) |
1493 				  PTP_LATENCY_RX_SET_(525));
1494 		break;
1495 	}
1496 }
1497 
1498 int lan743x_ptp_init(struct lan743x_adapter *adapter)
1499 {
1500 	struct lan743x_ptp *ptp = &adapter->ptp;
1501 	int i;
1502 
1503 	mutex_init(&ptp->command_lock);
1504 	spin_lock_init(&ptp->tx_ts_lock);
1505 	ptp->used_event_ch = 0;
1506 
1507 	for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) {
1508 		ptp->perout[i].event_ch = -1;
1509 		ptp->perout[i].gpio_pin = -1;
1510 	}
1511 
1512 	lan743x_led_mux_save(adapter);
1513 
1514 	return 0;
1515 }
1516 
1517 int lan743x_ptp_open(struct lan743x_adapter *adapter)
1518 {
1519 	struct lan743x_ptp *ptp = &adapter->ptp;
1520 	int ret = -ENODEV;
1521 	u32 temp;
1522 	int i;
1523 	int n_pins;
1524 
1525 	lan743x_ptp_reset(adapter);
1526 	lan743x_ptp_sync_to_system_clock(adapter);
1527 	temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
1528 	temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
1529 	lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
1530 	lan743x_ptp_enable(adapter);
1531 	lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1532 	lan743x_csr_write(adapter, PTP_INT_EN_SET,
1533 			  PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
1534 	ptp->flags |= PTP_FLAG_ISR_ENABLED;
1535 
1536 	if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
1537 		return 0;
1538 
1539 	switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
1540 	case ID_REV_ID_LAN7430_:
1541 		n_pins = LAN7430_N_GPIO;
1542 		break;
1543 	case ID_REV_ID_LAN7431_:
1544 	case ID_REV_ID_A011_:
1545 	case ID_REV_ID_A041_:
1546 		n_pins = LAN7431_N_GPIO;
1547 		break;
1548 	default:
1549 		netif_warn(adapter, drv, adapter->netdev,
1550 			   "Unknown LAN743x (%08x). Assuming no GPIO\n",
1551 			   adapter->csr.id_rev);
1552 		n_pins = 0;
1553 		break;
1554 	}
1555 
1556 	if (n_pins > LAN743X_PTP_N_GPIO)
1557 		n_pins = LAN743X_PTP_N_GPIO;
1558 
1559 	for (i = 0; i < n_pins; i++) {
1560 		struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i];
1561 
1562 		snprintf(ptp_pin->name,
1563 			 sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i);
1564 		ptp_pin->index = i;
1565 		ptp_pin->func = PTP_PF_NONE;
1566 	}
1567 
1568 	ptp->ptp_clock_info.owner = THIS_MODULE;
1569 	snprintf(ptp->ptp_clock_info.name, 16, "%pm",
1570 		 adapter->netdev->dev_addr);
1571 	ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
1572 	ptp->ptp_clock_info.n_alarm = 0;
1573 	ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS;
1574 	ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
1575 	ptp->ptp_clock_info.n_pins = n_pins;
1576 	ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
1577 	ptp->ptp_clock_info.pin_config = ptp->pin_config;
1578 	ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
1579 	ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
1580 	ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
1581 	ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
1582 	ptp->ptp_clock_info.getcrosststamp = NULL;
1583 	ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
1584 	ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
1585 	ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
1586 	ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config;
1587 
1588 	ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
1589 					    &adapter->pdev->dev);
1590 
1591 	if (IS_ERR(ptp->ptp_clock)) {
1592 		netif_err(adapter, ifup, adapter->netdev,
1593 			  "ptp_clock_register failed\n");
1594 		goto done;
1595 	}
1596 	ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
1597 	netif_info(adapter, ifup, adapter->netdev,
1598 		   "successfully registered ptp clock\n");
1599 
1600 	return 0;
1601 done:
1602 	lan743x_ptp_close(adapter);
1603 	return ret;
1604 }
1605 
1606 void lan743x_ptp_close(struct lan743x_adapter *adapter)
1607 {
1608 	struct lan743x_ptp *ptp = &adapter->ptp;
1609 	int index;
1610 
1611 	if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
1612 	    (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) {
1613 		ptp_clock_unregister(ptp->ptp_clock);
1614 		ptp->ptp_clock = NULL;
1615 		ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
1616 		netif_info(adapter, drv, adapter->netdev,
1617 			   "ptp clock unregister\n");
1618 	}
1619 
1620 	if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
1621 		lan743x_csr_write(adapter, PTP_INT_EN_CLR,
1622 				  PTP_INT_BIT_TX_SWTS_ERR_ |
1623 				  PTP_INT_BIT_TX_TS_);
1624 		lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1625 		ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
1626 	}
1627 
1628 	/* clean up pending timestamp requests */
1629 	lan743x_ptp_tx_ts_complete(adapter);
1630 	spin_lock_bh(&ptp->tx_ts_lock);
1631 	for (index = 0;
1632 		index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
1633 		index++) {
1634 		struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
1635 
1636 		dev_kfree_skb(skb);
1637 		ptp->tx_ts_skb_queue[index] = NULL;
1638 		ptp->tx_ts_seconds_queue[index] = 0;
1639 		ptp->tx_ts_nseconds_queue[index] = 0;
1640 	}
1641 	ptp->tx_ts_skb_queue_size = 0;
1642 	ptp->tx_ts_queue_size = 0;
1643 	ptp->pending_tx_timestamps = 0;
1644 	spin_unlock_bh(&ptp->tx_ts_lock);
1645 
1646 	lan743x_led_mux_restore(adapter);
1647 
1648 	lan743x_ptp_disable(adapter);
1649 }
1650 
1651 static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
1652 					   bool ts_insert_enable)
1653 {
1654 	u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
1655 
1656 	if (ts_insert_enable)
1657 		ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1658 	else
1659 		ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1660 
1661 	lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
1662 }
1663 
1664 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
1665 {
1666 	if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
1667 		return true;
1668 	return false;
1669 }
1670 
1671 static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
1672 {
1673 	struct lan743x_ptp *ptp = &adapter->ptp;
1674 
1675 	mutex_lock(&ptp->command_lock);
1676 
1677 	if (lan743x_ptp_is_enabled(adapter)) {
1678 		netif_warn(adapter, drv, adapter->netdev,
1679 			   "PTP already enabled\n");
1680 		goto done;
1681 	}
1682 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
1683 done:
1684 	mutex_unlock(&ptp->command_lock);
1685 }
1686 
1687 static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
1688 {
1689 	struct lan743x_ptp *ptp = &adapter->ptp;
1690 
1691 	mutex_lock(&ptp->command_lock);
1692 	if (!lan743x_ptp_is_enabled(adapter)) {
1693 		netif_warn(adapter, drv, adapter->netdev,
1694 			   "PTP already disabled\n");
1695 		goto done;
1696 	}
1697 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
1698 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
1699 done:
1700 	mutex_unlock(&ptp->command_lock);
1701 }
1702 
1703 static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
1704 {
1705 	struct lan743x_ptp *ptp = &adapter->ptp;
1706 
1707 	mutex_lock(&ptp->command_lock);
1708 
1709 	if (lan743x_ptp_is_enabled(adapter)) {
1710 		netif_err(adapter, drv, adapter->netdev,
1711 			  "Attempting reset while enabled\n");
1712 		goto done;
1713 	}
1714 
1715 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
1716 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
1717 done:
1718 	mutex_unlock(&ptp->command_lock);
1719 }
1720 
1721 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
1722 				  u32 seconds, u32 nano_seconds,
1723 				  u32 sub_nano_seconds)
1724 {
1725 	struct lan743x_ptp *ptp = &adapter->ptp;
1726 
1727 	mutex_lock(&ptp->command_lock);
1728 
1729 	lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
1730 	lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
1731 	lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
1732 
1733 	lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1734 	lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1735 	mutex_unlock(&ptp->command_lock);
1736 }
1737 
1738 bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
1739 {
1740 	struct lan743x_ptp *ptp = &adapter->ptp;
1741 	bool result = false;
1742 
1743 	spin_lock_bh(&ptp->tx_ts_lock);
1744 	if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1745 		/* request granted */
1746 		ptp->pending_tx_timestamps++;
1747 		result = true;
1748 	}
1749 	spin_unlock_bh(&ptp->tx_ts_lock);
1750 	return result;
1751 }
1752 
1753 void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
1754 {
1755 	struct lan743x_ptp *ptp = &adapter->ptp;
1756 
1757 	spin_lock_bh(&ptp->tx_ts_lock);
1758 	if (ptp->pending_tx_timestamps > 0)
1759 		ptp->pending_tx_timestamps--;
1760 	else
1761 		netif_err(adapter, drv, adapter->netdev,
1762 			  "unrequest failed, pending_tx_timestamps==0\n");
1763 	spin_unlock_bh(&ptp->tx_ts_lock);
1764 }
1765 
1766 void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
1767 				  struct sk_buff *skb, bool ignore_sync)
1768 {
1769 	lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
1770 
1771 	lan743x_ptp_tx_ts_complete(adapter);
1772 }
1773 
1774 int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1775 {
1776 	struct lan743x_adapter *adapter = netdev_priv(netdev);
1777 	struct hwtstamp_config config;
1778 	int ret = 0;
1779 	int index;
1780 
1781 	if (!ifr) {
1782 		netif_err(adapter, drv, adapter->netdev,
1783 			  "SIOCSHWTSTAMP, ifr == NULL\n");
1784 		return -EINVAL;
1785 	}
1786 
1787 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1788 		return -EFAULT;
1789 
1790 	switch (config.tx_type) {
1791 	case HWTSTAMP_TX_OFF:
1792 		for (index = 0; index < adapter->used_tx_channels;
1793 		     index++)
1794 			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1795 							 false, false);
1796 		lan743x_ptp_set_sync_ts_insert(adapter, false);
1797 		break;
1798 	case HWTSTAMP_TX_ON:
1799 		for (index = 0; index < adapter->used_tx_channels;
1800 			index++)
1801 			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1802 							 true, false);
1803 		lan743x_ptp_set_sync_ts_insert(adapter, false);
1804 		break;
1805 	case HWTSTAMP_TX_ONESTEP_SYNC:
1806 		for (index = 0; index < adapter->used_tx_channels;
1807 			index++)
1808 			lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1809 							 true, true);
1810 
1811 		lan743x_ptp_set_sync_ts_insert(adapter, true);
1812 		break;
1813 	case HWTSTAMP_TX_ONESTEP_P2P:
1814 		ret = -ERANGE;
1815 		break;
1816 	default:
1817 		netif_warn(adapter, drv, adapter->netdev,
1818 			   "  tx_type = %d, UNKNOWN\n", config.tx_type);
1819 		ret = -EINVAL;
1820 		break;
1821 	}
1822 
1823 	if (!ret)
1824 		return copy_to_user(ifr->ifr_data, &config,
1825 			sizeof(config)) ? -EFAULT : 0;
1826 	return ret;
1827 }
1828