Skip to content

Commit

Permalink
net: stmmac: ensure PTP time register reads are consistent
Browse files Browse the repository at this point in the history
Even if protected from preemption and interrupts, a small time window
remains when the 2 register reads could return inconsistent values,
each time the "seconds" register changes. This could lead to an about
1-second error in the reported time.

Add logic to ensure the "seconds" and "nanoseconds" values are consistent.

Fixes: 92ba688 ("stmmac: add the support for PTP hw clock driver")
Signed-off-by: Yannick Vignon <[email protected]>
Reviewed-by: Russell King (Oracle) <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jakub Kicinski <[email protected]>
  • Loading branch information
Yackou authored and kuba-moo committed Feb 3, 2022
1 parent 77b1b8b commit 80d4609
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,20 @@ static int adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,

static void get_systime(void __iomem *ioaddr, u64 *systime)
{
u64 ns;

/* Get the TSSS value */
ns = readl(ioaddr + PTP_STNSR);
/* Get the TSS and convert sec time value to nanosecond */
ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
u64 ns, sec0, sec1;

/* Get the TSS value */
sec1 = readl_relaxed(ioaddr + PTP_STSR);
do {
sec0 = sec1;
/* Get the TSSS value */
ns = readl_relaxed(ioaddr + PTP_STNSR);
/* Get the TSS value */
sec1 = readl_relaxed(ioaddr + PTP_STSR);
} while (sec0 != sec1);

if (systime)
*systime = ns;
*systime = ns + (sec1 * 1000000000ULL);
}

static void get_ptptime(void __iomem *ptpaddr, u64 *ptp_time)
Expand Down

0 comments on commit 80d4609

Please sign in to comment.