Skip to content

Commit 80d4609

Browse files
Yackoukuba-moo
authored andcommitted
net: stmmac: ensure PTP time register reads are consistent
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]>
1 parent 77b1b8b commit 80d4609

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,20 @@ static int adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
145145

146146
static void get_systime(void __iomem *ioaddr, u64 *systime)
147147
{
148-
u64 ns;
149-
150-
/* Get the TSSS value */
151-
ns = readl(ioaddr + PTP_STNSR);
152-
/* Get the TSS and convert sec time value to nanosecond */
153-
ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
148+
u64 ns, sec0, sec1;
149+
150+
/* Get the TSS value */
151+
sec1 = readl_relaxed(ioaddr + PTP_STSR);
152+
do {
153+
sec0 = sec1;
154+
/* Get the TSSS value */
155+
ns = readl_relaxed(ioaddr + PTP_STNSR);
156+
/* Get the TSS value */
157+
sec1 = readl_relaxed(ioaddr + PTP_STSR);
158+
} while (sec0 != sec1);
154159

155160
if (systime)
156-
*systime = ns;
161+
*systime = ns + (sec1 * 1000000000ULL);
157162
}
158163

159164
static void get_ptptime(void __iomem *ptpaddr, u64 *ptp_time)

0 commit comments

Comments
 (0)