Skip to contentSkip to conversation

timer: dw-apb: read the counter until two reads agree

PATCH New Yalçın Can 1 participant 1 message Oct 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 timer: dw-apb: read the counter until two reads agree

On the RK3066 after a warm reset, a read of the running DW APB timer counter occasionally returns a wrong value (about one in a few thousand reads ran backwards). timer_conv_64() takes every such value for a wrap of the 32-bit counter, so the 64-bit time jumps ahead by 2^32 ticks and all pending timeouts expire at once (here: SD reads failing with "fs_devread read error" in about half of the boots).

Read twice and retry until both reads are close together; the counter runs down, so the second read is at most a few ticks below the first.

Tested on an MK808B (RK3066): 5 of 5 warm reboots load the kernel from SD; before, about half failed.

---
 drivers/timer/dw-apb-timer.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/timer/dw-apb-timer.c +13 -1
index 77ccb98c..ecabbe04 100644
@@ -34,13 +34,25 @@ struct dw_apb_timer_plat {
 static u64 dw_apb_timer_get_count(struct udevice *dev)
 {
 	struct dw_apb_timer_priv *priv = dev_get_priv(dev);
+	u32 a, b;
+
+	/*
+	 * A read of the running counter can come back wrong now and then (RK3066 after a warm
+	 * reset with the clocks Linux left: about one in every few thousand reads ran backwards,
+	 * timer_conv_64() counted each as a wrap, and all timeouts fired at once). Two reads of a
+	 * counter going down must be close together: retry until they are.
+	 */
+	do {
+		a = readl(priv->regs + DW_APB_CURR_VAL);
+		b = readl(priv->regs + DW_APB_CURR_VAL);
+	} while (a - b > 0x10000);
 
 	/*
 	 * The DW APB counter counts down, but this function
 	 * requires the count to be incrementing. Invert the
 	 * result.
 	 */
-	return timer_conv_64(~readl(priv->regs + DW_APB_CURR_VAL));
+	return timer_conv_64(~b);
 }
 
 static int dw_apb_timer_probe(struct udevice *dev)
Signature · 1 line
2.47.3