Commit be0d2179 authored by Andre Przywara's avatar Andre Przywara Committed by Jagan Teki

arm: timer: sunxi: add Allwinner timer erratum workaround

The Allwinner A64 SoCs suffers from an arch timer implementation erratum,
where sometimes the lower 11 bits of the counter value erroneously
become all 0's or all 1's [1]. This leads to sudden jumps, both forwards and
backwards, with the latter one often showing weird behaviour.
Port the workaround proposed for Linux to U-Boot and activate it for all
A64 boards.
This fixes crashes when accessing MMC devices (SD cards), caused by a
recent change to actually use the counter value for timeout checks.

Fixes: 5ff8e548 ("sunxi: improve throughput
in the sunxi_mmc driver")

[1] default avatarAndre Przywara <>
Reviewed-by: Philipp Tomsich's avatarPhilipp Tomsich <>
Tested-by: Jagan Teki's avatarJagan Teki <>
Tested-by: default avatarAndreas Färber <>
Tested-by: default avatarGuillaume Gardet <>
parent 38651588
......@@ -46,6 +46,30 @@ unsigned long timer_read_counter(void)
return cntpct;
* This erratum sometimes flips the lower 11 bits of the counter value
* to all 0's or all 1's, leading to jumps forwards or backwards.
* Backwards jumps might be interpreted all roll-overs and be treated as
* huge jumps forward.
* The workaround is to check whether the lower 11 bits of the counter are
* all 0 or all 1, then discard this value and read again.
* This occasionally discards valid values, but will catch all erroneous
* reads and fixes the problem reliably. Also this mostly requires only a
* single read, so does not have any significant overhead.
* The algorithm was conceived by Samuel Holland.
unsigned long timer_read_counter(void)
unsigned long cntpct;
do {
asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
} while (((cntpct + 1) & GENMASK(10, 0)) <= 1);
return cntpct;
* timer_read_counter() using the Arm Generic Timer (aka arch timer).
......@@ -84,6 +84,9 @@ config SUNXI_HIGH_SRAM
Chips using the latter setup are supposed to select this option to
adjust the addresses accordingly.
# Note only one of these may be selected at a time! But hidden choices are
# not supported by Kconfig
......@@ -270,6 +273,7 @@ config MACH_SUN50I
select FIT
config MACH_SUN50I_H5
bool "sun50i (Allwinner H5)"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment