treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / ethernet / microchip / lan743x_ptp.c
blob9399f6a987487b934223431a096f0373dbc66ee3
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Copyright (C) 2018 Microchip Technology Inc. */
4 #include <linux/netdevice.h>
5 #include "lan743x_main.h"
7 #include <linux/ptp_clock_kernel.h>
8 #include <linux/module.h>
9 #include <linux/pci.h>
10 #include <linux/net_tstamp.h>
12 #include "lan743x_ptp.h"
14 #define LAN743X_LED0_ENABLE 20 /* LED0 offset in HW_CFG */
15 #define LAN743X_LED_ENABLE(pin) BIT(LAN743X_LED0_ENABLE + (pin))
17 #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB (31249999)
18 #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM (2047999934)
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);
28 int lan743x_gpio_init(struct lan743x_adapter *adapter)
30 struct lan743x_gpio *gpio = &adapter->gpio;
32 spin_lock_init(&gpio->gpio_lock);
34 gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
35 gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
36 gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
37 gpio->gpio_cfg3 = 0;/* disable all 1588 output */
38 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
39 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
40 lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
41 lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
43 return 0;
46 static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
47 u32 bit_mask)
49 int timeout = 1000;
50 u32 data = 0;
52 while (timeout &&
53 (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
54 bit_mask))) {
55 usleep_range(1000, 20000);
56 timeout--;
58 if (data) {
59 netif_err(adapter, drv, adapter->netdev,
60 "timeout waiting for cmd to be done, cmd = 0x%08X\n",
61 bit_mask);
65 static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
66 u32 seconds, u32 nano_seconds,
67 u32 header)
69 struct lan743x_ptp *ptp = &adapter->ptp;
71 spin_lock_bh(&ptp->tx_ts_lock);
72 if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
73 ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
74 ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
75 ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
76 ptp->tx_ts_queue_size++;
77 } else {
78 netif_err(adapter, drv, adapter->netdev,
79 "tx ts queue overflow\n");
81 spin_unlock_bh(&ptp->tx_ts_lock);
84 static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
86 struct lan743x_ptp *ptp = &adapter->ptp;
87 struct skb_shared_hwtstamps tstamps;
88 u32 header, nseconds, seconds;
89 bool ignore_sync = false;
90 struct sk_buff *skb;
91 int c, i;
93 spin_lock_bh(&ptp->tx_ts_lock);
94 c = ptp->tx_ts_skb_queue_size;
96 if (c > ptp->tx_ts_queue_size)
97 c = ptp->tx_ts_queue_size;
98 if (c <= 0)
99 goto done;
101 for (i = 0; i < c; i++) {
102 ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
103 BIT(i)) != 0);
104 skb = ptp->tx_ts_skb_queue[i];
105 nseconds = ptp->tx_ts_nseconds_queue[i];
106 seconds = ptp->tx_ts_seconds_queue[i];
107 header = ptp->tx_ts_header_queue[i];
109 memset(&tstamps, 0, sizeof(tstamps));
110 tstamps.hwtstamp = ktime_set(seconds, nseconds);
111 if (!ignore_sync ||
112 ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
113 PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
114 skb_tstamp_tx(skb, &tstamps);
116 dev_kfree_skb(skb);
118 ptp->tx_ts_skb_queue[i] = NULL;
119 ptp->tx_ts_seconds_queue[i] = 0;
120 ptp->tx_ts_nseconds_queue[i] = 0;
121 ptp->tx_ts_header_queue[i] = 0;
124 /* shift queue */
125 ptp->tx_ts_ignore_sync_queue >>= c;
126 for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
127 ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
128 ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
129 ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
130 ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];
132 ptp->tx_ts_skb_queue[i] = NULL;
133 ptp->tx_ts_seconds_queue[i] = 0;
134 ptp->tx_ts_nseconds_queue[i] = 0;
135 ptp->tx_ts_header_queue[i] = 0;
137 ptp->tx_ts_skb_queue_size -= c;
138 ptp->tx_ts_queue_size -= c;
139 done:
140 ptp->pending_tx_timestamps -= c;
141 spin_unlock_bh(&ptp->tx_ts_lock);
144 static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter,
145 int event_channel)
147 struct lan743x_ptp *ptp = &adapter->ptp;
148 int result = -ENODEV;
150 mutex_lock(&ptp->command_lock);
151 if (!(test_bit(event_channel, &ptp->used_event_ch))) {
152 ptp->used_event_ch |= BIT(event_channel);
153 result = event_channel;
154 } else {
155 netif_warn(adapter, drv, adapter->netdev,
156 "attempted to reserved a used event_channel = %d\n",
157 event_channel);
159 mutex_unlock(&ptp->command_lock);
160 return result;
163 static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
164 int event_channel)
166 struct lan743x_ptp *ptp = &adapter->ptp;
168 mutex_lock(&ptp->command_lock);
169 if (test_bit(event_channel, &ptp->used_event_ch)) {
170 ptp->used_event_ch &= ~BIT(event_channel);
171 } else {
172 netif_warn(adapter, drv, adapter->netdev,
173 "attempted release on a not used event_channel = %d\n",
174 event_channel);
176 mutex_unlock(&ptp->command_lock);
179 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
180 u32 *seconds, u32 *nano_seconds,
181 u32 *sub_nano_seconds);
182 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
183 s64 time_step_ns);
185 static void lan743x_led_mux_enable(struct lan743x_adapter *adapter,
186 int pin, bool enable)
188 struct lan743x_ptp *ptp = &adapter->ptp;
190 if (ptp->leds_multiplexed &&
191 ptp->led_enabled[pin]) {
192 u32 val = lan743x_csr_read(adapter, HW_CFG);
194 if (enable)
195 val |= LAN743X_LED_ENABLE(pin);
196 else
197 val &= ~LAN743X_LED_ENABLE(pin);
199 lan743x_csr_write(adapter, HW_CFG, val);
203 static void lan743x_led_mux_save(struct lan743x_adapter *adapter)
205 struct lan743x_ptp *ptp = &adapter->ptp;
206 u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
208 if (id_rev == ID_REV_ID_LAN7430_) {
209 int i;
210 u32 val = lan743x_csr_read(adapter, HW_CFG);
212 for (i = 0; i < LAN7430_N_LED; i++) {
213 bool led_enabled = (val & LAN743X_LED_ENABLE(i)) != 0;
215 ptp->led_enabled[i] = led_enabled;
217 ptp->leds_multiplexed = true;
218 } else {
219 ptp->leds_multiplexed = false;
223 static void lan743x_led_mux_restore(struct lan743x_adapter *adapter)
225 u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
227 if (id_rev == ID_REV_ID_LAN7430_) {
228 int i;
230 for (i = 0; i < LAN7430_N_LED; i++)
231 lan743x_led_mux_enable(adapter, i, true);
235 static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
236 int pin, int event_channel)
238 struct lan743x_gpio *gpio = &adapter->gpio;
239 unsigned long irq_flags = 0;
240 int bit_mask = BIT(pin);
241 int ret = -EBUSY;
243 spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
245 if (!(gpio->used_bits & bit_mask)) {
246 gpio->used_bits |= bit_mask;
247 gpio->output_bits |= bit_mask;
248 gpio->ptp_bits |= bit_mask;
250 /* assign pin to GPIO function */
251 lan743x_led_mux_enable(adapter, pin, false);
253 /* set as output, and zero initial value */
254 gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(pin);
255 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
256 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
258 /* enable gpio, and set buffer type to push pull */
259 gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(pin);
260 gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(pin);
261 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
263 /* set 1588 polarity to high */
264 gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(pin);
265 lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
267 if (event_channel == 0) {
268 /* use channel A */
269 gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(pin);
270 } else {
271 /* use channel B */
272 gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(pin);
274 gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(pin);
275 lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
277 ret = pin;
279 spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
280 return ret;
283 static void lan743x_gpio_release(struct lan743x_adapter *adapter, int pin)
285 struct lan743x_gpio *gpio = &adapter->gpio;
286 unsigned long irq_flags = 0;
287 int bit_mask = BIT(pin);
289 spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
290 if (gpio->used_bits & bit_mask) {
291 gpio->used_bits &= ~bit_mask;
292 if (gpio->output_bits & bit_mask) {
293 gpio->output_bits &= ~bit_mask;
295 if (gpio->ptp_bits & bit_mask) {
296 gpio->ptp_bits &= ~bit_mask;
297 /* disable ptp output */
298 gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(pin);
299 lan743x_csr_write(adapter, GPIO_CFG3,
300 gpio->gpio_cfg3);
302 /* release gpio output */
304 /* disable gpio */
305 gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(pin);
306 gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(pin);
307 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
309 /* reset back to input */
310 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(pin);
311 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
312 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
314 /* assign pin to original function */
315 lan743x_led_mux_enable(adapter, pin, true);
318 spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
321 static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
323 struct lan743x_ptp *ptp =
324 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
325 struct lan743x_adapter *adapter =
326 container_of(ptp, struct lan743x_adapter, ptp);
327 u32 lan743x_rate_adj = 0;
328 bool positive = true;
329 u64 u64_delta = 0;
331 if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
332 scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
333 return -EINVAL;
335 if (scaled_ppm > 0) {
336 u64_delta = (u64)scaled_ppm;
337 positive = true;
338 } else {
339 u64_delta = (u64)(-scaled_ppm);
340 positive = false;
342 u64_delta = (u64_delta << 19);
343 lan743x_rate_adj = div_u64(u64_delta, 1000000);
345 if (positive)
346 lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
348 lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
349 lan743x_rate_adj);
351 return 0;
354 static int lan743x_ptpci_adjfreq(struct ptp_clock_info *ptpci, s32 delta_ppb)
356 struct lan743x_ptp *ptp =
357 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
358 struct lan743x_adapter *adapter =
359 container_of(ptp, struct lan743x_adapter, ptp);
360 u32 lan743x_rate_adj = 0;
361 bool positive = true;
362 u32 u32_delta = 0;
363 u64 u64_delta = 0;
365 if ((delta_ppb < (-LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB)) ||
366 delta_ppb > LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB) {
367 return -EINVAL;
369 if (delta_ppb > 0) {
370 u32_delta = (u32)delta_ppb;
371 positive = true;
372 } else {
373 u32_delta = (u32)(-delta_ppb);
374 positive = false;
376 u64_delta = (((u64)u32_delta) << 35);
377 lan743x_rate_adj = div_u64(u64_delta, 1000000000);
379 if (positive)
380 lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
382 lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
383 lan743x_rate_adj);
385 return 0;
388 static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
390 struct lan743x_ptp *ptp =
391 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
392 struct lan743x_adapter *adapter =
393 container_of(ptp, struct lan743x_adapter, ptp);
395 lan743x_ptp_clock_step(adapter, delta);
397 return 0;
400 static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
401 struct timespec64 *ts)
403 struct lan743x_ptp *ptp =
404 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
405 struct lan743x_adapter *adapter =
406 container_of(ptp, struct lan743x_adapter, ptp);
407 u32 nano_seconds = 0;
408 u32 seconds = 0;
410 lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
411 ts->tv_sec = seconds;
412 ts->tv_nsec = nano_seconds;
414 return 0;
417 static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
418 const struct timespec64 *ts)
420 struct lan743x_ptp *ptp =
421 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
422 struct lan743x_adapter *adapter =
423 container_of(ptp, struct lan743x_adapter, ptp);
424 u32 nano_seconds = 0;
425 u32 seconds = 0;
427 if (ts) {
428 if (ts->tv_sec > 0xFFFFFFFFLL ||
429 ts->tv_sec < 0) {
430 netif_warn(adapter, drv, adapter->netdev,
431 "ts->tv_sec out of range, %lld\n",
432 ts->tv_sec);
433 return -ERANGE;
435 if (ts->tv_nsec >= 1000000000L ||
436 ts->tv_nsec < 0) {
437 netif_warn(adapter, drv, adapter->netdev,
438 "ts->tv_nsec out of range, %ld\n",
439 ts->tv_nsec);
440 return -ERANGE;
442 seconds = ts->tv_sec;
443 nano_seconds = ts->tv_nsec;
444 lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
445 } else {
446 netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
447 return -EINVAL;
450 return 0;
453 static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter,
454 unsigned int index)
456 struct lan743x_ptp *ptp = &adapter->ptp;
457 u32 general_config = 0;
458 struct lan743x_ptp_perout *perout = &ptp->perout[index];
460 if (perout->gpio_pin >= 0) {
461 lan743x_gpio_release(adapter, perout->gpio_pin);
462 perout->gpio_pin = -1;
465 if (perout->event_ch >= 0) {
466 /* set target to far in the future, effectively disabling it */
467 lan743x_csr_write(adapter,
468 PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
469 0xFFFF0000);
470 lan743x_csr_write(adapter,
471 PTP_CLOCK_TARGET_NS_X(perout->event_ch),
474 general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
475 general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
476 (perout->event_ch);
477 lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
478 lan743x_ptp_release_event_ch(adapter, perout->event_ch);
479 perout->event_ch = -1;
483 static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
484 struct ptp_perout_request *perout_request)
486 struct lan743x_ptp *ptp = &adapter->ptp;
487 u32 period_sec = 0, period_nsec = 0;
488 u32 start_sec = 0, start_nsec = 0;
489 u32 general_config = 0;
490 int pulse_width = 0;
491 int perout_pin = 0;
492 unsigned int index = perout_request->index;
493 struct lan743x_ptp_perout *perout = &ptp->perout[index];
495 /* Reject requests with unsupported flags */
496 if (perout_request->flags)
497 return -EOPNOTSUPP;
499 if (on) {
500 perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
501 perout_request->index);
502 if (perout_pin < 0)
503 return -EBUSY;
504 } else {
505 lan743x_ptp_perout_off(adapter, index);
506 return 0;
509 if (perout->event_ch >= 0 ||
510 perout->gpio_pin >= 0) {
511 /* already on, turn off first */
512 lan743x_ptp_perout_off(adapter, index);
515 perout->event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
517 if (perout->event_ch < 0) {
518 netif_warn(adapter, drv, adapter->netdev,
519 "Failed to reserve event channel %d for PEROUT\n",
520 index);
521 goto failed;
524 perout->gpio_pin = lan743x_gpio_rsrv_ptp_out(adapter,
525 perout_pin,
526 perout->event_ch);
528 if (perout->gpio_pin < 0) {
529 netif_warn(adapter, drv, adapter->netdev,
530 "Failed to reserve gpio %d for PEROUT\n",
531 perout_pin);
532 goto failed;
535 start_sec = perout_request->start.sec;
536 start_sec += perout_request->start.nsec / 1000000000;
537 start_nsec = perout_request->start.nsec % 1000000000;
539 period_sec = perout_request->period.sec;
540 period_sec += perout_request->period.nsec / 1000000000;
541 period_nsec = perout_request->period.nsec % 1000000000;
543 if (period_sec == 0) {
544 if (period_nsec >= 400000000) {
545 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
546 } else if (period_nsec >= 20000000) {
547 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
548 } else if (period_nsec >= 2000000) {
549 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
550 } else if (period_nsec >= 200000) {
551 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
552 } else if (period_nsec >= 20000) {
553 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
554 } else if (period_nsec >= 200) {
555 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
556 } else {
557 netif_warn(adapter, drv, adapter->netdev,
558 "perout period too small, minimum is 200nS\n");
559 goto failed;
561 } else {
562 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
565 /* turn off by setting target far in future */
566 lan743x_csr_write(adapter,
567 PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
568 0xFFFF0000);
569 lan743x_csr_write(adapter,
570 PTP_CLOCK_TARGET_NS_X(perout->event_ch), 0);
572 /* Configure to pulse every period */
573 general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
574 general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
575 (perout->event_ch));
576 general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
577 (perout->event_ch, pulse_width);
578 general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
579 (perout->event_ch);
580 lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
582 /* set the reload to one toggle cycle */
583 lan743x_csr_write(adapter,
584 PTP_CLOCK_TARGET_RELOAD_SEC_X(perout->event_ch),
585 period_sec);
586 lan743x_csr_write(adapter,
587 PTP_CLOCK_TARGET_RELOAD_NS_X(perout->event_ch),
588 period_nsec);
590 /* set the start time */
591 lan743x_csr_write(adapter,
592 PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
593 start_sec);
594 lan743x_csr_write(adapter,
595 PTP_CLOCK_TARGET_NS_X(perout->event_ch),
596 start_nsec);
598 return 0;
600 failed:
601 lan743x_ptp_perout_off(adapter, index);
602 return -ENODEV;
605 static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
606 struct ptp_clock_request *request, int on)
608 struct lan743x_ptp *ptp =
609 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
610 struct lan743x_adapter *adapter =
611 container_of(ptp, struct lan743x_adapter, ptp);
613 if (request) {
614 switch (request->type) {
615 case PTP_CLK_REQ_EXTTS:
616 return -EINVAL;
617 case PTP_CLK_REQ_PEROUT:
618 if (request->perout.index < ptpci->n_per_out)
619 return lan743x_ptp_perout(adapter, on,
620 &request->perout);
621 return -EINVAL;
622 case PTP_CLK_REQ_PPS:
623 return -EINVAL;
624 default:
625 netif_err(adapter, drv, adapter->netdev,
626 "request->type == %d, Unknown\n",
627 request->type);
628 break;
630 } else {
631 netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
633 return 0;
636 static int lan743x_ptpci_verify_pin_config(struct ptp_clock_info *ptp,
637 unsigned int pin,
638 enum ptp_pin_function func,
639 unsigned int chan)
641 int result = 0;
643 /* Confirm the requested function is supported. Parameter
644 * validation is done by the caller.
646 switch (func) {
647 case PTP_PF_NONE:
648 case PTP_PF_PEROUT:
649 break;
650 case PTP_PF_EXTTS:
651 case PTP_PF_PHYSYNC:
652 default:
653 result = -1;
654 break;
656 return result;
659 static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
661 struct lan743x_ptp *ptp =
662 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
663 struct lan743x_adapter *adapter =
664 container_of(ptp, struct lan743x_adapter, ptp);
665 u32 cap_info, cause, header, nsec, seconds;
666 bool new_timestamp_available = false;
667 int count = 0;
669 while ((count < 100) &&
670 (lan743x_csr_read(adapter, PTP_INT_STS) & PTP_INT_BIT_TX_TS_)) {
671 count++;
672 cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
674 if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
675 seconds = lan743x_csr_read(adapter,
676 PTP_TX_EGRESS_SEC);
677 nsec = lan743x_csr_read(adapter, PTP_TX_EGRESS_NS);
678 cause = (nsec &
679 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
680 header = lan743x_csr_read(adapter,
681 PTP_TX_MSG_HEADER);
683 if (cause == PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
684 nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
685 lan743x_ptp_tx_ts_enqueue_ts(adapter,
686 seconds, nsec,
687 header);
688 new_timestamp_available = true;
689 } else if (cause ==
690 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
691 netif_err(adapter, drv, adapter->netdev,
692 "Auto capture cause not supported\n");
693 } else {
694 netif_warn(adapter, drv, adapter->netdev,
695 "unknown tx timestamp capture cause\n");
697 } else {
698 netif_warn(adapter, drv, adapter->netdev,
699 "TX TS INT but no TX TS CNT\n");
701 lan743x_csr_write(adapter, PTP_INT_STS, PTP_INT_BIT_TX_TS_);
704 if (new_timestamp_available)
705 lan743x_ptp_tx_ts_complete(adapter);
707 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
709 return -1;
712 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
713 u32 *seconds, u32 *nano_seconds,
714 u32 *sub_nano_seconds)
716 struct lan743x_ptp *ptp = &adapter->ptp;
718 mutex_lock(&ptp->command_lock);
720 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
721 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
723 if (seconds)
724 (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
726 if (nano_seconds)
727 (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
729 if (sub_nano_seconds)
730 (*sub_nano_seconds) =
731 lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
733 mutex_unlock(&ptp->command_lock);
736 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
737 s64 time_step_ns)
739 struct lan743x_ptp *ptp = &adapter->ptp;
740 u32 nano_seconds_step = 0;
741 u64 abs_time_step_ns = 0;
742 u32 unsigned_seconds = 0;
743 u32 nano_seconds = 0;
744 u32 remainder = 0;
745 s32 seconds = 0;
747 if (time_step_ns > 15000000000LL) {
748 /* convert to clock set */
749 lan743x_ptp_clock_get(adapter, &unsigned_seconds,
750 &nano_seconds, NULL);
751 unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
752 &remainder);
753 nano_seconds += remainder;
754 if (nano_seconds >= 1000000000) {
755 unsigned_seconds++;
756 nano_seconds -= 1000000000;
758 lan743x_ptp_clock_set(adapter, unsigned_seconds,
759 nano_seconds, 0);
760 return;
761 } else if (time_step_ns < -15000000000LL) {
762 /* convert to clock set */
763 time_step_ns = -time_step_ns;
765 lan743x_ptp_clock_get(adapter, &unsigned_seconds,
766 &nano_seconds, NULL);
767 unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
768 &remainder);
769 nano_seconds_step = remainder;
770 if (nano_seconds < nano_seconds_step) {
771 unsigned_seconds--;
772 nano_seconds += 1000000000;
774 nano_seconds -= nano_seconds_step;
775 lan743x_ptp_clock_set(adapter, unsigned_seconds,
776 nano_seconds, 0);
777 return;
780 /* do clock step */
781 if (time_step_ns >= 0) {
782 abs_time_step_ns = (u64)(time_step_ns);
783 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
784 &remainder);
785 nano_seconds = (u32)remainder;
786 } else {
787 abs_time_step_ns = (u64)(-time_step_ns);
788 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
789 &remainder));
790 nano_seconds = (u32)remainder;
791 if (nano_seconds > 0) {
792 /* subtracting nano seconds is not allowed
793 * convert to subtracting from seconds,
794 * and adding to nanoseconds
796 seconds--;
797 nano_seconds = (1000000000 - nano_seconds);
801 if (nano_seconds > 0) {
802 /* add 8 ns to cover the likely normal increment */
803 nano_seconds += 8;
806 if (nano_seconds >= 1000000000) {
807 /* carry into seconds */
808 seconds++;
809 nano_seconds -= 1000000000;
812 while (seconds) {
813 mutex_lock(&ptp->command_lock);
814 if (seconds > 0) {
815 u32 adjustment_value = (u32)seconds;
817 if (adjustment_value > 0xF)
818 adjustment_value = 0xF;
819 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
820 PTP_CLOCK_STEP_ADJ_DIR_ |
821 adjustment_value);
822 seconds -= ((s32)adjustment_value);
823 } else {
824 u32 adjustment_value = (u32)(-seconds);
826 if (adjustment_value > 0xF)
827 adjustment_value = 0xF;
828 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
829 adjustment_value);
830 seconds += ((s32)adjustment_value);
832 lan743x_csr_write(adapter, PTP_CMD_CTL,
833 PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
834 lan743x_ptp_wait_till_cmd_done(adapter,
835 PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
836 mutex_unlock(&ptp->command_lock);
838 if (nano_seconds) {
839 mutex_lock(&ptp->command_lock);
840 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
841 PTP_CLOCK_STEP_ADJ_DIR_ |
842 (nano_seconds &
843 PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
844 lan743x_csr_write(adapter, PTP_CMD_CTL,
845 PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
846 lan743x_ptp_wait_till_cmd_done(adapter,
847 PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
848 mutex_unlock(&ptp->command_lock);
852 void lan743x_ptp_isr(void *context)
854 struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
855 struct lan743x_ptp *ptp = NULL;
856 int enable_flag = 1;
857 u32 ptp_int_sts = 0;
859 ptp = &adapter->ptp;
861 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
863 ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
864 ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
866 if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
867 ptp_schedule_worker(ptp->ptp_clock, 0);
868 enable_flag = 0;/* tasklet will re-enable later */
870 if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
871 netif_err(adapter, drv, adapter->netdev,
872 "PTP TX Software Timestamp Error\n");
873 /* clear int status bit */
874 lan743x_csr_write(adapter, PTP_INT_STS,
875 PTP_INT_BIT_TX_SWTS_ERR_);
877 if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
878 /* clear int status bit */
879 lan743x_csr_write(adapter, PTP_INT_STS,
880 PTP_INT_BIT_TIMER_B_);
882 if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
883 /* clear int status bit */
884 lan743x_csr_write(adapter, PTP_INT_STS,
885 PTP_INT_BIT_TIMER_A_);
888 if (enable_flag) {
889 /* re-enable isr */
890 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
894 static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
895 struct sk_buff *skb, bool ignore_sync)
897 struct lan743x_ptp *ptp = &adapter->ptp;
899 spin_lock_bh(&ptp->tx_ts_lock);
900 if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
901 ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
902 if (ignore_sync)
903 ptp->tx_ts_ignore_sync_queue |=
904 BIT(ptp->tx_ts_skb_queue_size);
905 ptp->tx_ts_skb_queue_size++;
906 } else {
907 /* this should never happen, so long as the tx channel
908 * calls and honors the result from
909 * lan743x_ptp_request_tx_timestamp
911 netif_err(adapter, drv, adapter->netdev,
912 "tx ts skb queue overflow\n");
913 dev_kfree_skb(skb);
915 spin_unlock_bh(&ptp->tx_ts_lock);
918 static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
920 struct timespec64 ts;
922 ktime_get_clocktai_ts64(&ts);
924 lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
927 void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
928 u32 link_speed)
930 switch (link_speed) {
931 case 10:
932 lan743x_csr_write(adapter, PTP_LATENCY,
933 PTP_LATENCY_TX_SET_(0) |
934 PTP_LATENCY_RX_SET_(0));
935 break;
936 case 100:
937 lan743x_csr_write(adapter, PTP_LATENCY,
938 PTP_LATENCY_TX_SET_(181) |
939 PTP_LATENCY_RX_SET_(594));
940 break;
941 case 1000:
942 lan743x_csr_write(adapter, PTP_LATENCY,
943 PTP_LATENCY_TX_SET_(30) |
944 PTP_LATENCY_RX_SET_(525));
945 break;
949 int lan743x_ptp_init(struct lan743x_adapter *adapter)
951 struct lan743x_ptp *ptp = &adapter->ptp;
952 int i;
954 mutex_init(&ptp->command_lock);
955 spin_lock_init(&ptp->tx_ts_lock);
956 ptp->used_event_ch = 0;
958 for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) {
959 ptp->perout[i].event_ch = -1;
960 ptp->perout[i].gpio_pin = -1;
963 lan743x_led_mux_save(adapter);
965 return 0;
968 int lan743x_ptp_open(struct lan743x_adapter *adapter)
970 struct lan743x_ptp *ptp = &adapter->ptp;
971 int ret = -ENODEV;
972 u32 temp;
973 int i;
974 int n_pins;
976 lan743x_ptp_reset(adapter);
977 lan743x_ptp_sync_to_system_clock(adapter);
978 temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
979 temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
980 lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
981 lan743x_ptp_enable(adapter);
982 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
983 lan743x_csr_write(adapter, PTP_INT_EN_SET,
984 PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
985 ptp->flags |= PTP_FLAG_ISR_ENABLED;
987 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
988 return 0;
990 switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
991 case ID_REV_ID_LAN7430_:
992 n_pins = LAN7430_N_GPIO;
993 break;
994 case ID_REV_ID_LAN7431_:
995 n_pins = LAN7431_N_GPIO;
996 break;
997 default:
998 netif_warn(adapter, drv, adapter->netdev,
999 "Unknown LAN743x (%08x). Assuming no GPIO\n",
1000 adapter->csr.id_rev);
1001 n_pins = 0;
1002 break;
1005 if (n_pins > LAN743X_PTP_N_GPIO)
1006 n_pins = LAN743X_PTP_N_GPIO;
1008 for (i = 0; i < n_pins; i++) {
1009 struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i];
1011 snprintf(ptp_pin->name,
1012 sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i);
1013 ptp_pin->index = i;
1014 ptp_pin->func = PTP_PF_NONE;
1017 ptp->ptp_clock_info.owner = THIS_MODULE;
1018 snprintf(ptp->ptp_clock_info.name, 16, "%pm",
1019 adapter->netdev->dev_addr);
1020 ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
1021 ptp->ptp_clock_info.n_alarm = 0;
1022 ptp->ptp_clock_info.n_ext_ts = 0;
1023 ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
1024 ptp->ptp_clock_info.n_pins = n_pins;
1025 ptp->ptp_clock_info.pps = 0;
1026 ptp->ptp_clock_info.pin_config = ptp->pin_config;
1027 ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
1028 ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
1029 ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
1030 ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
1031 ptp->ptp_clock_info.getcrosststamp = NULL;
1032 ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
1033 ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
1034 ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
1035 ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config;
1037 ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
1038 &adapter->pdev->dev);
1040 if (IS_ERR(ptp->ptp_clock)) {
1041 netif_err(adapter, ifup, adapter->netdev,
1042 "ptp_clock_register failed\n");
1043 goto done;
1045 ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
1046 netif_info(adapter, ifup, adapter->netdev,
1047 "successfully registered ptp clock\n");
1049 return 0;
1050 done:
1051 lan743x_ptp_close(adapter);
1052 return ret;
1055 void lan743x_ptp_close(struct lan743x_adapter *adapter)
1057 struct lan743x_ptp *ptp = &adapter->ptp;
1058 int index;
1060 if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
1061 (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) {
1062 ptp_clock_unregister(ptp->ptp_clock);
1063 ptp->ptp_clock = NULL;
1064 ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
1065 netif_info(adapter, drv, adapter->netdev,
1066 "ptp clock unregister\n");
1069 if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
1070 lan743x_csr_write(adapter, PTP_INT_EN_CLR,
1071 PTP_INT_BIT_TX_SWTS_ERR_ |
1072 PTP_INT_BIT_TX_TS_);
1073 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1074 ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
1077 /* clean up pending timestamp requests */
1078 lan743x_ptp_tx_ts_complete(adapter);
1079 spin_lock_bh(&ptp->tx_ts_lock);
1080 for (index = 0;
1081 index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
1082 index++) {
1083 struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
1085 dev_kfree_skb(skb);
1086 ptp->tx_ts_skb_queue[index] = NULL;
1087 ptp->tx_ts_seconds_queue[index] = 0;
1088 ptp->tx_ts_nseconds_queue[index] = 0;
1090 ptp->tx_ts_skb_queue_size = 0;
1091 ptp->tx_ts_queue_size = 0;
1092 ptp->pending_tx_timestamps = 0;
1093 spin_unlock_bh(&ptp->tx_ts_lock);
1095 lan743x_led_mux_restore(adapter);
1097 lan743x_ptp_disable(adapter);
1100 static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
1101 bool ts_insert_enable)
1103 u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
1105 if (ts_insert_enable)
1106 ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1107 else
1108 ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1110 lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
1113 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
1115 if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
1116 return true;
1117 return false;
1120 static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
1122 struct lan743x_ptp *ptp = &adapter->ptp;
1124 mutex_lock(&ptp->command_lock);
1126 if (lan743x_ptp_is_enabled(adapter)) {
1127 netif_warn(adapter, drv, adapter->netdev,
1128 "PTP already enabled\n");
1129 goto done;
1131 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
1132 done:
1133 mutex_unlock(&ptp->command_lock);
1136 static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
1138 struct lan743x_ptp *ptp = &adapter->ptp;
1140 mutex_lock(&ptp->command_lock);
1141 if (!lan743x_ptp_is_enabled(adapter)) {
1142 netif_warn(adapter, drv, adapter->netdev,
1143 "PTP already disabled\n");
1144 goto done;
1146 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
1147 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
1148 done:
1149 mutex_unlock(&ptp->command_lock);
1152 static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
1154 struct lan743x_ptp *ptp = &adapter->ptp;
1156 mutex_lock(&ptp->command_lock);
1158 if (lan743x_ptp_is_enabled(adapter)) {
1159 netif_err(adapter, drv, adapter->netdev,
1160 "Attempting reset while enabled\n");
1161 goto done;
1164 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
1165 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
1166 done:
1167 mutex_unlock(&ptp->command_lock);
1170 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
1171 u32 seconds, u32 nano_seconds,
1172 u32 sub_nano_seconds)
1174 struct lan743x_ptp *ptp = &adapter->ptp;
1176 mutex_lock(&ptp->command_lock);
1178 lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
1179 lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
1180 lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
1182 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1183 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1184 mutex_unlock(&ptp->command_lock);
1187 bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
1189 struct lan743x_ptp *ptp = &adapter->ptp;
1190 bool result = false;
1192 spin_lock_bh(&ptp->tx_ts_lock);
1193 if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1194 /* request granted */
1195 ptp->pending_tx_timestamps++;
1196 result = true;
1198 spin_unlock_bh(&ptp->tx_ts_lock);
1199 return result;
1202 void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
1204 struct lan743x_ptp *ptp = &adapter->ptp;
1206 spin_lock_bh(&ptp->tx_ts_lock);
1207 if (ptp->pending_tx_timestamps > 0)
1208 ptp->pending_tx_timestamps--;
1209 else
1210 netif_err(adapter, drv, adapter->netdev,
1211 "unrequest failed, pending_tx_timestamps==0\n");
1212 spin_unlock_bh(&ptp->tx_ts_lock);
1215 void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
1216 struct sk_buff *skb, bool ignore_sync)
1218 lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
1220 lan743x_ptp_tx_ts_complete(adapter);
1223 int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1225 struct lan743x_adapter *adapter = netdev_priv(netdev);
1226 struct hwtstamp_config config;
1227 int ret = 0;
1228 int index;
1230 if (!ifr) {
1231 netif_err(adapter, drv, adapter->netdev,
1232 "SIOCSHWTSTAMP, ifr == NULL\n");
1233 return -EINVAL;
1236 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1237 return -EFAULT;
1239 if (config.flags) {
1240 netif_warn(adapter, drv, adapter->netdev,
1241 "ignoring hwtstamp_config.flags == 0x%08X, expected 0\n",
1242 config.flags);
1245 switch (config.tx_type) {
1246 case HWTSTAMP_TX_OFF:
1247 for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
1248 index++)
1249 lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1250 false, false);
1251 lan743x_ptp_set_sync_ts_insert(adapter, false);
1252 break;
1253 case HWTSTAMP_TX_ON:
1254 for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
1255 index++)
1256 lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1257 true, false);
1258 lan743x_ptp_set_sync_ts_insert(adapter, false);
1259 break;
1260 case HWTSTAMP_TX_ONESTEP_SYNC:
1261 for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
1262 index++)
1263 lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1264 true, true);
1266 lan743x_ptp_set_sync_ts_insert(adapter, true);
1267 break;
1268 case HWTSTAMP_TX_ONESTEP_P2P:
1269 ret = -ERANGE;
1270 break;
1271 default:
1272 netif_warn(adapter, drv, adapter->netdev,
1273 " tx_type = %d, UNKNOWN\n", config.tx_type);
1274 ret = -EINVAL;
1275 break;
1278 if (!ret)
1279 return copy_to_user(ifr->ifr_data, &config,
1280 sizeof(config)) ? -EFAULT : 0;
1281 return ret;