* [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle)
@ 2021-10-12 13:49 yuiko.oshino
2021-10-12 21:53 ` Jakub Kicinski
2021-10-15 2:45 ` Jakub Kicinski
0 siblings, 2 replies; 5+ messages in thread
From: yuiko.oshino @ 2021-10-12 13:49 UTC (permalink / raw)
To: davem; +Cc: netdev, UNGLinuxDriver, Yuiko Oshino
From: Yuiko Oshino <yuiko.oshino@microchip.com>
If the PTP_PEROUT_DUTY_CYCLE flag is set, then check if the
request_on value in ptp_perout_request matches the pre-defined
values or a toggle option.
Return a failure if the value is not supported.
Preserve the old behaviors if the PTP_PEROUT_DUTY_CYCLE flag is not
set.
Tested with an oscilloscope on EVB-LAN7430:
e.g., to output PPS 1sec period 500mS on (high) to GPIO 2.
./testptp -L 2,2
./testptp -p 1000000000 -w 500000000
Signed-off-by: Yuiko Oshino <yuiko.oshino@microchip.com>
---
drivers/net/ethernet/microchip/lan743x_main.h | 1 +
drivers/net/ethernet/microchip/lan743x_ptp.c | 91 ++++++++++++++++---
2 files changed, 81 insertions(+), 11 deletions(-)
diff --git a/drivers/net/ethernet/microchip/lan743x_main.h b/drivers/net/ethernet/microchip/lan743x_main.h
index 6080028c1df2..34c22eea0124 100644
--- a/drivers/net/ethernet/microchip/lan743x_main.h
+++ b/drivers/net/ethernet/microchip/lan743x_main.h
@@ -279,6 +279,7 @@
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_ (3)
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_ (4)
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_ (5)
+#define PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_ (6)
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_(channel, value) \
(((value) & 0x7) << (1 + ((channel) << 2)))
#define PTP_GENERAL_CONFIG_RELOAD_ADD_X_(channel) (BIT((channel) << 2))
diff --git a/drivers/net/ethernet/microchip/lan743x_ptp.c b/drivers/net/ethernet/microchip/lan743x_ptp.c
index ab6d719d40f0..9380e396f648 100644
--- a/drivers/net/ethernet/microchip/lan743x_ptp.c
+++ b/drivers/net/ethernet/microchip/lan743x_ptp.c
@@ -491,9 +491,10 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
int perout_pin = 0;
unsigned int index = perout_request->index;
struct lan743x_ptp_perout *perout = &ptp->perout[index];
+ int ret = 0;
/* Reject requests with unsupported flags */
- if (perout_request->flags)
+ if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
return -EOPNOTSUPP;
if (on) {
@@ -518,6 +519,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
netif_warn(adapter, drv, adapter->netdev,
"Failed to reserve event channel %d for PEROUT\n",
index);
+ ret = -EBUSY;
goto failed;
}
@@ -529,6 +531,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
netif_warn(adapter, drv, adapter->netdev,
"Failed to reserve gpio %d for PEROUT\n",
perout_pin);
+ ret = -EBUSY;
goto failed;
}
@@ -540,27 +543,93 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
period_sec += perout_request->period.nsec / 1000000000;
period_nsec = perout_request->period.nsec % 1000000000;
- if (period_sec == 0) {
- if (period_nsec >= 400000000) {
+ if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
+ struct timespec64 ts_on, ts_period;
+ s64 wf_high, period64, half;
+ s32 reminder;
+
+ ts_on.tv_sec = perout_request->on.sec;
+ ts_on.tv_nsec = perout_request->on.nsec;
+ wf_high = timespec64_to_ns(&ts_on);
+ ts_period.tv_sec = perout_request->period.sec;
+ ts_period.tv_nsec = perout_request->period.nsec;
+ period64 = timespec64_to_ns(&ts_period);
+
+ if (period64 < 200) {
+ netif_warn(adapter, drv, adapter->netdev,
+ "perout period too small, minimum is 200nS\n");
+ ret = -EOPNOTSUPP;
+ goto failed;
+ }
+ if (wf_high >= period64) {
+ netif_warn(adapter, drv, adapter->netdev,
+ "pulse width must be smaller than period\n");
+ ret = -EINVAL;
+ goto failed;
+ }
+
+ /* Check if we can do 50% toggle on an even value of period.
+ * If the period number is odd, then check if the requested
+ * pulse width is the same as one of pre-defined width values.
+ * Otherwise, return failure.
+ */
+ half = div_s64_rem(period64, 2, &reminder);
+ if (!reminder) {
+ if (half == wf_high) {
+ /* It's 50% match. Use the toggle option */
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
+ /* In this case, devide period value by 2 */
+ ts_period = ns_to_timespec64(div_s64(period64, 2));
+ period_sec = ts_period.tv_sec;
+ period_nsec = ts_period.tv_nsec;
+
+ goto program;
+ }
+ }
+ /* if we can't do toggle, then the width option needs to be the exact match */
+ if (wf_high == 200000000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
- } else if (period_nsec >= 20000000) {
+ } else if (wf_high == 10000000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
- } else if (period_nsec >= 2000000) {
+ } else if (wf_high == 1000000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
- } else if (period_nsec >= 200000) {
+ } else if (wf_high == 100000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
- } else if (period_nsec >= 20000) {
+ } else if (wf_high == 10000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
- } else if (period_nsec >= 200) {
+ } else if (wf_high == 100) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
} else {
netif_warn(adapter, drv, adapter->netdev,
- "perout period too small, minimum is 200nS\n");
+ "duty cycle specified is not supported\n");
+ ret = -EOPNOTSUPP;
goto failed;
}
} else {
- pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
+ if (period_sec == 0) {
+ if (period_nsec >= 400000000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
+ } else if (period_nsec >= 20000000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
+ } else if (period_nsec >= 2000000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
+ } else if (period_nsec >= 200000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
+ } else if (period_nsec >= 20000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
+ } else if (period_nsec >= 200) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
+ } else {
+ netif_warn(adapter, drv, adapter->netdev,
+ "perout period too small, minimum is 200nS\n");
+ ret = -EOPNOTSUPP;
+ goto failed;
+ }
+ } else {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
+ }
}
+program:
/* turn off by setting target far in future */
lan743x_csr_write(adapter,
@@ -599,7 +668,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
failed:
lan743x_ptp_perout_off(adapter, index);
- return -ENODEV;
+ return ret;
}
static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle)
2021-10-12 13:49 [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle) yuiko.oshino
@ 2021-10-12 21:53 ` Jakub Kicinski
2021-10-13 12:44 ` Yuiko.Oshino
2021-10-15 2:45 ` Jakub Kicinski
1 sibling, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2021-10-12 21:53 UTC (permalink / raw)
To: yuiko.oshino; +Cc: davem, netdev, UNGLinuxDriver, Richard Cochran
On Tue, 12 Oct 2021 09:49:53 -0400 yuiko.oshino@microchip.com wrote:
> From: Yuiko Oshino <yuiko.oshino@microchip.com>
>
> If the PTP_PEROUT_DUTY_CYCLE flag is set, then check if the
> request_on value in ptp_perout_request matches the pre-defined
> values or a toggle option.
> Return a failure if the value is not supported.
>
> Preserve the old behaviors if the PTP_PEROUT_DUTY_CYCLE flag is not
> set.
>
> Tested with an oscilloscope on EVB-LAN7430:
> e.g., to output PPS 1sec period 500mS on (high) to GPIO 2.
> ./testptp -L 2,2
> ./testptp -p 1000000000 -w 500000000
>
> Signed-off-by: Yuiko Oshino <yuiko.oshino@microchip.com>
Please make sure to CC Richard on PTP-related changes.
> diff --git a/drivers/net/ethernet/microchip/lan743x_main.h b/drivers/net/ethernet/microchip/lan743x_main.h
> index 6080028c1df2..34c22eea0124 100644
> --- a/drivers/net/ethernet/microchip/lan743x_main.h
> +++ b/drivers/net/ethernet/microchip/lan743x_main.h
> @@ -279,6 +279,7 @@
> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_ (3)
> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_ (4)
> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_ (5)
> +#define PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_ (6)
> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_(channel, value) \
> (((value) & 0x7) << (1 + ((channel) << 2)))
> #define PTP_GENERAL_CONFIG_RELOAD_ADD_X_(channel) (BIT((channel) << 2))
> diff --git a/drivers/net/ethernet/microchip/lan743x_ptp.c b/drivers/net/ethernet/microchip/lan743x_ptp.c
> index ab6d719d40f0..9380e396f648 100644
> --- a/drivers/net/ethernet/microchip/lan743x_ptp.c
> +++ b/drivers/net/ethernet/microchip/lan743x_ptp.c
> @@ -491,9 +491,10 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
> int perout_pin = 0;
> unsigned int index = perout_request->index;
> struct lan743x_ptp_perout *perout = &ptp->perout[index];
> + int ret = 0;
>
> /* Reject requests with unsupported flags */
> - if (perout_request->flags)
> + if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
> return -EOPNOTSUPP;
>
> if (on) {
> @@ -518,6 +519,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
> netif_warn(adapter, drv, adapter->netdev,
> "Failed to reserve event channel %d for PEROUT\n",
> index);
> + ret = -EBUSY;
> goto failed;
> }
>
> @@ -529,6 +531,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
> netif_warn(adapter, drv, adapter->netdev,
> "Failed to reserve gpio %d for PEROUT\n",
> perout_pin);
> + ret = -EBUSY;
> goto failed;
> }
>
> @@ -540,27 +543,93 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
> period_sec += perout_request->period.nsec / 1000000000;
> period_nsec = perout_request->period.nsec % 1000000000;
>
> - if (period_sec == 0) {
> - if (period_nsec >= 400000000) {
> + if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
> + struct timespec64 ts_on, ts_period;
> + s64 wf_high, period64, half;
> + s32 reminder;
> +
> + ts_on.tv_sec = perout_request->on.sec;
> + ts_on.tv_nsec = perout_request->on.nsec;
> + wf_high = timespec64_to_ns(&ts_on);
> + ts_period.tv_sec = perout_request->period.sec;
> + ts_period.tv_nsec = perout_request->period.nsec;
> + period64 = timespec64_to_ns(&ts_period);
> +
> + if (period64 < 200) {
> + netif_warn(adapter, drv, adapter->netdev,
> + "perout period too small, minimum is 200nS\n");
> + ret = -EOPNOTSUPP;
> + goto failed;
> + }
> + if (wf_high >= period64) {
> + netif_warn(adapter, drv, adapter->netdev,
> + "pulse width must be smaller than period\n");
> + ret = -EINVAL;
> + goto failed;
> + }
> +
> + /* Check if we can do 50% toggle on an even value of period.
> + * If the period number is odd, then check if the requested
> + * pulse width is the same as one of pre-defined width values.
> + * Otherwise, return failure.
> + */
> + half = div_s64_rem(period64, 2, &reminder);
> + if (!reminder) {
> + if (half == wf_high) {
> + /* It's 50% match. Use the toggle option */
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
> + /* In this case, devide period value by 2 */
> + ts_period = ns_to_timespec64(div_s64(period64, 2));
> + period_sec = ts_period.tv_sec;
> + period_nsec = ts_period.tv_nsec;
> +
> + goto program;
> + }
> + }
> + /* if we can't do toggle, then the width option needs to be the exact match */
> + if (wf_high == 200000000) {
> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
> - } else if (period_nsec >= 20000000) {
> + } else if (wf_high == 10000000) {
> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
> - } else if (period_nsec >= 2000000) {
> + } else if (wf_high == 1000000) {
> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
> - } else if (period_nsec >= 200000) {
> + } else if (wf_high == 100000) {
> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
> - } else if (period_nsec >= 20000) {
> + } else if (wf_high == 10000) {
> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
> - } else if (period_nsec >= 200) {
> + } else if (wf_high == 100) {
> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
> } else {
> netif_warn(adapter, drv, adapter->netdev,
> - "perout period too small, minimum is 200nS\n");
> + "duty cycle specified is not supported\n");
> + ret = -EOPNOTSUPP;
> goto failed;
> }
> } else {
> - pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
> + if (period_sec == 0) {
> + if (period_nsec >= 400000000) {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
> + } else if (period_nsec >= 20000000) {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
> + } else if (period_nsec >= 2000000) {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
> + } else if (period_nsec >= 200000) {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
> + } else if (period_nsec >= 20000) {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
> + } else if (period_nsec >= 200) {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
> + } else {
> + netif_warn(adapter, drv, adapter->netdev,
> + "perout period too small, minimum is 200nS\n");
> + ret = -EOPNOTSUPP;
> + goto failed;
> + }
> + } else {
> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
> + }
> }
> +program:
>
> /* turn off by setting target far in future */
> lan743x_csr_write(adapter,
> @@ -599,7 +668,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
>
> failed:
> lan743x_ptp_perout_off(adapter, index);
> - return -ENODEV;
> + return ret;
> }
>
> static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle)
2021-10-12 21:53 ` Jakub Kicinski
@ 2021-10-13 12:44 ` Yuiko.Oshino
0 siblings, 0 replies; 5+ messages in thread
From: Yuiko.Oshino @ 2021-10-13 12:44 UTC (permalink / raw)
To: kuba; +Cc: davem, netdev, UNGLinuxDriver, richardcochran
>-----Original Message-----
>From: Jakub Kicinski <kuba@kernel.org>
>Sent: Tuesday, October 12, 2021 5:54 PM
>To: Yuiko Oshino - C18177 <Yuiko.Oshino@microchip.com>
>Cc: davem@davemloft.net; netdev@vger.kernel.org; UNGLinuxDriver
><UNGLinuxDriver@microchip.com>; Richard Cochran
><richardcochran@gmail.com>
>Subject: Re: [PATCH net-next] net: microchip: lan743x: add support for PTP pulse
>width (duty cycle)
>
>EXTERNAL EMAIL: Do not click links or open attachments unless you know the
>content is safe
>
>On Tue, 12 Oct 2021 09:49:53 -0400 yuiko.oshino@microchip.com wrote:
>> From: Yuiko Oshino <yuiko.oshino@microchip.com>
>>
>> If the PTP_PEROUT_DUTY_CYCLE flag is set, then check if the request_on
>> value in ptp_perout_request matches the pre-defined values or a toggle
>> option.
>> Return a failure if the value is not supported.
>>
>> Preserve the old behaviors if the PTP_PEROUT_DUTY_CYCLE flag is not
>> set.
>>
>> Tested with an oscilloscope on EVB-LAN7430:
>> e.g., to output PPS 1sec period 500mS on (high) to GPIO 2.
>> ./testptp -L 2,2
>> ./testptp -p 1000000000 -w 500000000
>>
>> Signed-off-by: Yuiko Oshino <yuiko.oshino@microchip.com>
>
>Please make sure to CC Richard on PTP-related changes.
I did not know. Thank you!
>
>> diff --git a/drivers/net/ethernet/microchip/lan743x_main.h
>> b/drivers/net/ethernet/microchip/lan743x_main.h
>> index 6080028c1df2..34c22eea0124 100644
>> --- a/drivers/net/ethernet/microchip/lan743x_main.h
>> +++ b/drivers/net/ethernet/microchip/lan743x_main.h
>> @@ -279,6 +279,7 @@
>> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_ (3) #define
>> PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_ (4)
>> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_ (5)
>> +#define PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_ (6)
>> #define PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_(channel, value) \
>> (((value) & 0x7) << (1 + ((channel) << 2)))
>> #define PTP_GENERAL_CONFIG_RELOAD_ADD_X_(channel) (BIT((channel) <<
>2))
>> diff --git a/drivers/net/ethernet/microchip/lan743x_ptp.c
>> b/drivers/net/ethernet/microchip/lan743x_ptp.c
>> index ab6d719d40f0..9380e396f648 100644
>> --- a/drivers/net/ethernet/microchip/lan743x_ptp.c
>> +++ b/drivers/net/ethernet/microchip/lan743x_ptp.c
>> @@ -491,9 +491,10 @@ static int lan743x_ptp_perout(struct lan743x_adapter
>*adapter, int on,
>> int perout_pin = 0;
>> unsigned int index = perout_request->index;
>> struct lan743x_ptp_perout *perout = &ptp->perout[index];
>> + int ret = 0;
>>
>> /* Reject requests with unsupported flags */
>> - if (perout_request->flags)
>> + if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
>> return -EOPNOTSUPP;
>>
>> if (on) {
>> @@ -518,6 +519,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter
>*adapter, int on,
>> netif_warn(adapter, drv, adapter->netdev,
>> "Failed to reserve event channel %d for PEROUT\n",
>> index);
>> + ret = -EBUSY;
>> goto failed;
>> }
>>
>> @@ -529,6 +531,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter
>*adapter, int on,
>> netif_warn(adapter, drv, adapter->netdev,
>> "Failed to reserve gpio %d for PEROUT\n",
>> perout_pin);
>> + ret = -EBUSY;
>> goto failed;
>> }
>>
>> @@ -540,27 +543,93 @@ static int lan743x_ptp_perout(struct
>lan743x_adapter *adapter, int on,
>> period_sec += perout_request->period.nsec / 1000000000;
>> period_nsec = perout_request->period.nsec % 1000000000;
>>
>> - if (period_sec == 0) {
>> - if (period_nsec >= 400000000) {
>> + if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
>> + struct timespec64 ts_on, ts_period;
>> + s64 wf_high, period64, half;
>> + s32 reminder;
>> +
>> + ts_on.tv_sec = perout_request->on.sec;
>> + ts_on.tv_nsec = perout_request->on.nsec;
>> + wf_high = timespec64_to_ns(&ts_on);
>> + ts_period.tv_sec = perout_request->period.sec;
>> + ts_period.tv_nsec = perout_request->period.nsec;
>> + period64 = timespec64_to_ns(&ts_period);
>> +
>> + if (period64 < 200) {
>> + netif_warn(adapter, drv, adapter->netdev,
>> + "perout period too small, minimum is 200nS\n");
>> + ret = -EOPNOTSUPP;
>> + goto failed;
>> + }
>> + if (wf_high >= period64) {
>> + netif_warn(adapter, drv, adapter->netdev,
>> + "pulse width must be smaller than period\n");
>> + ret = -EINVAL;
>> + goto failed;
>> + }
>> +
>> + /* Check if we can do 50% toggle on an even value of period.
>> + * If the period number is odd, then check if the requested
>> + * pulse width is the same as one of pre-defined width values.
>> + * Otherwise, return failure.
>> + */
>> + half = div_s64_rem(period64, 2, &reminder);
>> + if (!reminder) {
>> + if (half == wf_high) {
>> + /* It's 50% match. Use the toggle option */
>> + pulse_width =
>PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
>> + /* In this case, devide period value by 2 */
>> + ts_period = ns_to_timespec64(div_s64(period64, 2));
>> + period_sec = ts_period.tv_sec;
>> + period_nsec = ts_period.tv_nsec;
>> +
>> + goto program;
>> + }
>> + }
>> + /* if we can't do toggle, then the width option needs to be the exact
>match */
>> + if (wf_high == 200000000) {
>> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
>> - } else if (period_nsec >= 20000000) {
>> + } else if (wf_high == 10000000) {
>> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
>> - } else if (period_nsec >= 2000000) {
>> + } else if (wf_high == 1000000) {
>> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
>> - } else if (period_nsec >= 200000) {
>> + } else if (wf_high == 100000) {
>> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
>> - } else if (period_nsec >= 20000) {
>> + } else if (wf_high == 10000) {
>> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
>> - } else if (period_nsec >= 200) {
>> + } else if (wf_high == 100) {
>> pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
>> } else {
>> netif_warn(adapter, drv, adapter->netdev,
>> - "perout period too small, minimum is 200nS\n");
>> + "duty cycle specified is not supported\n");
>> + ret = -EOPNOTSUPP;
>> goto failed;
>> }
>> } else {
>> - pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
>> + if (period_sec == 0) {
>> + if (period_nsec >= 400000000) {
>> + pulse_width =
>PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
>> + } else if (period_nsec >= 20000000) {
>> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
>> + } else if (period_nsec >= 2000000) {
>> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
>> + } else if (period_nsec >= 200000) {
>> + pulse_width =
>PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
>> + } else if (period_nsec >= 20000) {
>> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
>> + } else if (period_nsec >= 200) {
>> + pulse_width =
>PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
>> + } else {
>> + netif_warn(adapter, drv, adapter->netdev,
>> + "perout period too small, minimum is 200nS\n");
>> + ret = -EOPNOTSUPP;
>> + goto failed;
>> + }
>> + } else {
>> + pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
>> + }
>> }
>> +program:
>>
>> /* turn off by setting target far in future */
>> lan743x_csr_write(adapter,
>> @@ -599,7 +668,7 @@ static int lan743x_ptp_perout(struct
>> lan743x_adapter *adapter, int on,
>>
>> failed:
>> lan743x_ptp_perout_off(adapter, index);
>> - return -ENODEV;
>> + return ret;
>> }
>>
>> static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle)
2021-10-12 13:49 [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle) yuiko.oshino
2021-10-12 21:53 ` Jakub Kicinski
@ 2021-10-15 2:45 ` Jakub Kicinski
1 sibling, 0 replies; 5+ messages in thread
From: Jakub Kicinski @ 2021-10-15 2:45 UTC (permalink / raw)
To: yuiko.oshino; +Cc: davem, netdev, UNGLinuxDriver
On Tue, 12 Oct 2021 09:49:53 -0400 yuiko.oshino@microchip.com wrote:
> From: Yuiko Oshino <yuiko.oshino@microchip.com>
>
> If the PTP_PEROUT_DUTY_CYCLE flag is set, then check if the
> request_on value in ptp_perout_request matches the pre-defined
> values or a toggle option.
> Return a failure if the value is not supported.
>
> Preserve the old behaviors if the PTP_PEROUT_DUTY_CYCLE flag is not
> set.
>
> Tested with an oscilloscope on EVB-LAN7430:
> e.g., to output PPS 1sec period 500mS on (high) to GPIO 2.
> ./testptp -L 2,2
> ./testptp -p 1000000000 -w 500000000
>
> Signed-off-by: Yuiko Oshino <yuiko.oshino@microchip.com>
Applied, thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle)
@ 2021-10-12 13:44 yuiko.oshino
0 siblings, 0 replies; 5+ messages in thread
From: yuiko.oshino @ 2021-10-12 13:44 UTC (permalink / raw)
To: davem; +Cc: netdev, UNGLinuxDriver, Yuiko Oshino
From: Yuiko Oshino <yuiko.oshino@microchip.com>
If the PTP_PEROUT_DUTY_CYCLE flag is set, then check if the
request_on value in ptp_perout_request matches the pre-defined
values or a toggle option.
Return a failure if the value is not supported.
Preserve the old behaviors if the PTP_PEROUT_DUTY_CYCLE flag is not
set.
Tested with an oscilloscope on EVB-LAN7430:
e.g., to output PPS 1sec period 500mS on (high) to GPIO 2.
./testptp -L 2,2
./testptp -p 1000000000 -w 500000000
Signed-off-by: Yuiko Oshino <yuiko.oshino@microchip.com>
---
drivers/net/ethernet/microchip/lan743x_main.h | 1 +
drivers/net/ethernet/microchip/lan743x_ptp.c | 91 ++++++++++++++++---
2 files changed, 81 insertions(+), 11 deletions(-)
diff --git a/drivers/net/ethernet/microchip/lan743x_main.h b/drivers/net/ethernet/microchip/lan743x_main.h
index 6080028c1df2..34c22eea0124 100644
--- a/drivers/net/ethernet/microchip/lan743x_main.h
+++ b/drivers/net/ethernet/microchip/lan743x_main.h
@@ -279,6 +279,7 @@
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_ (3)
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_ (4)
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_ (5)
+#define PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_ (6)
#define PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_(channel, value) \
(((value) & 0x7) << (1 + ((channel) << 2)))
#define PTP_GENERAL_CONFIG_RELOAD_ADD_X_(channel) (BIT((channel) << 2))
diff --git a/drivers/net/ethernet/microchip/lan743x_ptp.c b/drivers/net/ethernet/microchip/lan743x_ptp.c
index ab6d719d40f0..9380e396f648 100644
--- a/drivers/net/ethernet/microchip/lan743x_ptp.c
+++ b/drivers/net/ethernet/microchip/lan743x_ptp.c
@@ -491,9 +491,10 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
int perout_pin = 0;
unsigned int index = perout_request->index;
struct lan743x_ptp_perout *perout = &ptp->perout[index];
+ int ret = 0;
/* Reject requests with unsupported flags */
- if (perout_request->flags)
+ if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
return -EOPNOTSUPP;
if (on) {
@@ -518,6 +519,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
netif_warn(adapter, drv, adapter->netdev,
"Failed to reserve event channel %d for PEROUT\n",
index);
+ ret = -EBUSY;
goto failed;
}
@@ -529,6 +531,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
netif_warn(adapter, drv, adapter->netdev,
"Failed to reserve gpio %d for PEROUT\n",
perout_pin);
+ ret = -EBUSY;
goto failed;
}
@@ -540,27 +543,93 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
period_sec += perout_request->period.nsec / 1000000000;
period_nsec = perout_request->period.nsec % 1000000000;
- if (period_sec == 0) {
- if (period_nsec >= 400000000) {
+ if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
+ struct timespec64 ts_on, ts_period;
+ s64 wf_high, period64, half;
+ s32 reminder;
+
+ ts_on.tv_sec = perout_request->on.sec;
+ ts_on.tv_nsec = perout_request->on.nsec;
+ wf_high = timespec64_to_ns(&ts_on);
+ ts_period.tv_sec = perout_request->period.sec;
+ ts_period.tv_nsec = perout_request->period.nsec;
+ period64 = timespec64_to_ns(&ts_period);
+
+ if (period64 < 200) {
+ netif_warn(adapter, drv, adapter->netdev,
+ "perout period too small, minimum is 200nS\n");
+ ret = -EOPNOTSUPP;
+ goto failed;
+ }
+ if (wf_high >= period64) {
+ netif_warn(adapter, drv, adapter->netdev,
+ "pulse width must be smaller than period\n");
+ ret = -EINVAL;
+ goto failed;
+ }
+
+ /* Check if we can do 50% toggle on an even value of period.
+ * If the period number is odd, then check if the requested
+ * pulse width is the same as one of pre-defined width values.
+ * Otherwise, return failure.
+ */
+ half = div_s64_rem(period64, 2, &reminder);
+ if (!reminder) {
+ if (half == wf_high) {
+ /* It's 50% match. Use the toggle option */
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
+ /* In this case, devide period value by 2 */
+ ts_period = ns_to_timespec64(div_s64(period64, 2));
+ period_sec = ts_period.tv_sec;
+ period_nsec = ts_period.tv_nsec;
+
+ goto program;
+ }
+ }
+ /* if we can't do toggle, then the width option needs to be the exact match */
+ if (wf_high == 200000000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
- } else if (period_nsec >= 20000000) {
+ } else if (wf_high == 10000000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
- } else if (period_nsec >= 2000000) {
+ } else if (wf_high == 1000000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
- } else if (period_nsec >= 200000) {
+ } else if (wf_high == 100000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
- } else if (period_nsec >= 20000) {
+ } else if (wf_high == 10000) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
- } else if (period_nsec >= 200) {
+ } else if (wf_high == 100) {
pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
} else {
netif_warn(adapter, drv, adapter->netdev,
- "perout period too small, minimum is 200nS\n");
+ "duty cycle specified is not supported\n");
+ ret = -EOPNOTSUPP;
goto failed;
}
} else {
- pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
+ if (period_sec == 0) {
+ if (period_nsec >= 400000000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
+ } else if (period_nsec >= 20000000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
+ } else if (period_nsec >= 2000000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
+ } else if (period_nsec >= 200000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
+ } else if (period_nsec >= 20000) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
+ } else if (period_nsec >= 200) {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
+ } else {
+ netif_warn(adapter, drv, adapter->netdev,
+ "perout period too small, minimum is 200nS\n");
+ ret = -EOPNOTSUPP;
+ goto failed;
+ }
+ } else {
+ pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
+ }
}
+program:
/* turn off by setting target far in future */
lan743x_csr_write(adapter,
@@ -599,7 +668,7 @@ static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
failed:
lan743x_ptp_perout_off(adapter, index);
- return -ENODEV;
+ return ret;
}
static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-10-15 2:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-12 13:49 [PATCH net-next] net: microchip: lan743x: add support for PTP pulse width (duty cycle) yuiko.oshino
2021-10-12 21:53 ` Jakub Kicinski
2021-10-13 12:44 ` Yuiko.Oshino
2021-10-15 2:45 ` Jakub Kicinski
-- strict thread matches above, loose matches on Subject: below --
2021-10-12 13:44 yuiko.oshino
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.