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