Skip to content

Commit da1827a

Browse files
GseoCNipaLocal
authored andcommitted
drivers: net: stmmac: handle start time set in the past for flexible PPS
In case the time arguments used for flexible PPS signal generation are in the past, consider the arguments to be a time offset relative to the MAC system time. This way, past time use case is handled and it avoids the tedious work of passing an absolute time value for the flexible PPS signal generation while not breaking existing scripts that may rely on this behavior. Signed-off-by: Gatien Chevallier <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 1f51ab1 commit da1827a

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

drivers/net/ethernet/stmicro/stmmac/stmmac_ptp.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "stmmac.h"
1111
#include "stmmac_ptp.h"
1212

13+
#define PTP_SAFE_TIME_OFFSET_NS 500000
14+
1315
/**
1416
* stmmac_adjust_freq
1517
*
@@ -171,7 +173,11 @@ static int stmmac_enable(struct ptp_clock_info *ptp,
171173
u32 acr_value;
172174

173175
switch (rq->type) {
174-
case PTP_CLK_REQ_PEROUT:
176+
case PTP_CLK_REQ_PEROUT: {
177+
struct timespec64 curr_time;
178+
u64 target_ns = 0;
179+
u64 ns = 0;
180+
175181
/* Reject requests with unsupported flags */
176182
if (rq->perout.flags)
177183
return -EOPNOTSUPP;
@@ -180,6 +186,31 @@ static int stmmac_enable(struct ptp_clock_info *ptp,
180186

181187
cfg->start.tv_sec = rq->perout.start.sec;
182188
cfg->start.tv_nsec = rq->perout.start.nsec;
189+
190+
/* A time set in the past won't trigger the start of the flexible PPS generation for
191+
* the GMAC5. For some reason it does for the GMAC4 but setting a time in the past
192+
* should be addressed anyway. Therefore, any value set it the past is considered as
193+
* an offset compared to the current MAC system time.
194+
* Be aware that an offset too low may not trigger flexible PPS generation
195+
* if time spent in this configuration makes the targeted time already outdated.
196+
* To address this, add a safe time offset.
197+
*/
198+
if (!cfg->start.tv_sec && cfg->start.tv_nsec < PTP_SAFE_TIME_OFFSET_NS)
199+
cfg->start.tv_nsec += PTP_SAFE_TIME_OFFSET_NS;
200+
201+
target_ns = cfg->start.tv_nsec + ((u64)cfg->start.tv_sec * NSEC_PER_SEC);
202+
203+
stmmac_get_systime(priv, priv->ptpaddr, &ns);
204+
if (ns > TIME64_MAX - PTP_SAFE_TIME_OFFSET_NS)
205+
return -EINVAL;
206+
207+
curr_time = ns_to_timespec64(ns);
208+
if (target_ns < ns + PTP_SAFE_TIME_OFFSET_NS) {
209+
cfg->start = timespec64_add_safe(cfg->start, curr_time);
210+
if (cfg->start.tv_sec == TIME64_MAX)
211+
return -EINVAL;
212+
}
213+
183214
cfg->period.tv_sec = rq->perout.period.sec;
184215
cfg->period.tv_nsec = rq->perout.period.nsec;
185216

@@ -190,6 +221,7 @@ static int stmmac_enable(struct ptp_clock_info *ptp,
190221
priv->systime_flags);
191222
write_unlock_irqrestore(&priv->ptp_lock, flags);
192223
break;
224+
}
193225
case PTP_CLK_REQ_EXTTS: {
194226
u8 channel;
195227

0 commit comments

Comments
 (0)