Skip to contentSkip to conversation

Convert MediaTek GPT timers to OF probing

PATCH Applied Luca Leonardo Scorcia 4 participants 9 messages Oct 2 – Nov 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Convert MediaTek GPT timers to OF probing

While chasing a kernel freeze on the Lenovo Tab E7 TB-7104F (mt8167), I stumbled on the MediaTek GPT timer driver. This driver is currently early loaded via TIMER_OF_DECLARE. I noticed that on mt8167 (and I guess mt8516 too), this driver actually fails to probe because its device tree declaration depends on two HW clocks, CLK_TOP_CLK26M_D2 (13 MHz) and CLK_TOP_APXGPT (bus clock). However the driver providing those clocks is loaded later in the boot process, leading TIMER_OF_DECLARE to fail with -EDEFER.

Patch #2 converts this driver to OF probe, fixing this initial issue.

I noticed that other devices avoid this by connecting the timer to fixed clocks instead of referring to an actual clock. This approach may work on mt8167 too, but I think referring to the actual clock is cleaner.

Once the driver probed successfully I noticed that even if it was enabled by the boot loader, the kernel would quickly turn off its bus clock as the driver wasn't actually requesting it.

Patch #3 enables it and attaches it to the timer device. I wondered how other platforms made it actually work, and noticed that mt6735 declares it as a critical clock. This approach might work for mt8167 too, but same reasoning as before applies.

After those fixes the driver probes and works correctly, but I would occasionally notice a crash at boot inside the timer interrupt handler. It turned out to be a side effect of the bootloader enabling the GPT timer at boot: an interrupt may arrive and be handled while the driver is not completely initialized.

Patch #1 fixes it by requesting the IRQ but keeping it disabled and enabling it after the clock source initialization completes. I sorted this patch before the others because it's actually independent of the latter two and may fix (rare) random crashes on other devices.

Please note that all the changes apply both to GPT timers and the SYST timers, but I have no device to test the SYST ones.

The series was tested on the Sony PlayStation Classic Mini (mt8167).

Re: patch #1, credits for issue discovery and suggested fix go to matemiller992 on postmarketOS forums. I am submitting it upstream with permission. Not adding Reported-by or Suggested-by tags because it was requested not to share real name.

Unfortunately, this series did not fix the tablet randomly freezing during boot. Any idea on how to better troubleshoot this is more than welcome!

Luca Leonardo Scorcia (3):
clocksource/drivers/timer-mediatek: Fix random crash at boot clocksource/drivers/timer-mediatek: Convert to OF probing clocksource/drivers/timer-mediatek: Request and enable the bus clock

 drivers/clocksource/timer-mediatek.c | 47 +++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 clocksource/drivers/timer-mediatek: Fix random crash at boot

MediaTek bootloaders generally enable APXGPT timers at boot. This means that a timer IRQ can sometimes happen between the IRQ line request and the configuration of the clock event handler. This leads to a hard crash with an instruction abort, branch to pc 0x0 in mtk_gpt_interrupt handler as clkevt is null.

To avoid this issue request the IRQ with IRQF_NO_AUTOEN and enable it explicitly after the clock event is configured.

Issue was reported and fix tested on the Lenovo Tab E7 (TB-7104F, mt8167).

Fixes a0858f937960 ("clocksource/drivers/timer-mediatek: Convert the driver to timer-of")

---
Credits for issue discovery and suggested fix go to matemiller992 on postmarketOS forums. I am submitting it upstream with permission. Not adding Reported-by or Suggested-by tags because it was requested not to share real name.

 drivers/clocksource/timer-mediatek.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/clocksource/timer-mediatek.c +3 -1
index 7bcb4a3f26fb..e20075b09fab 100644
@@ -277,7 +277,7 @@ static struct timer_of to = {
 	},
 
 	.of_irq = {
-		.flags = IRQF_TIMER | IRQF_IRQPOLL,
+		.flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_NO_AUTOEN,
 	},
 };
 
@@ -298,6 +298,7 @@ static int __init mtk_syst_init(struct device_node *node)
 
 	clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
 					TIMER_SYNC_TICKS, 0xffffffff);
+	enable_irq(timer_of_irq(&to));
 
 	return 0;
 }
@@ -334,6 +335,7 @@ static int __init mtk_gpt_init(struct device_node *node)
 					TIMER_SYNC_TICKS, 0xffffffff);
 
 	mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
+	enable_irq(timer_of_irq(&to));
 
 	return 0;
 }
Signature · 1 line
2.43.0
#2 lore raw

[clocksource: timers/drivers/next] clocksource/drivers/timer-mediatek: Fix random crash at boot

The following commit has been merged into the timers/drivers/next branch of clocksource:

The following commit has been merged into the timers/drivers/next branch of clocksource:

Commit-ID:     8496a1d234e8cf9cb15c4282cb3a11c6550bd242
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/daniel.lezcano/linux.git//8496a1d234e8cf9cb15c4282cb3a11c6550bd242
Author:        Luca Leonardo Scorcia <[email protected]>
AuthorDate:    Fri, 02 Oct 2026 09:19:26 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 13 Oct 2026 13:17:13 +02:00

clocksource/drivers/timer-mediatek: Fix random crash at boot

MediaTek bootloaders generally enable APXGPT timers at boot. This means that a timer IRQ can sometimes happen between the IRQ line request and the configuration of the clock event handler. This leads to a hard crash with an instruction abort, branch to pc 0x0 in mtk_gpt_interrupt handler as clkevt is null.

To avoid this issue request the IRQ with IRQF_NO_AUTOEN and enable it explicitly after the clock event is configured.

Issue was reported and fix tested on the Lenovo Tab E7 (TB-7104F, mt8167).

Fixes a0858f937960 ("clocksource/drivers/timer-mediatek: Convert the driver to timer-of")
---
 drivers/clocksource/timer-mediatek.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/clocksource/timer-mediatek.c +3 -1
index 7bcb4a3..e20075b 100644
@@ -277,7 +277,7 @@ static struct timer_of to = {
 	},
 
 	.of_irq = {
-		.flags = IRQF_TIMER | IRQF_IRQPOLL,
+		.flags = IRQF_TIMER | IRQF_IRQPOLL | IRQF_NO_AUTOEN,
 	},
 };
 
@@ -298,6 +298,7 @@ static int __init mtk_syst_init(struct device_node *node)
 
 	clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
 					TIMER_SYNC_TICKS, 0xffffffff);
+	enable_irq(timer_of_irq(&to));
 
 	return 0;
 }
@@ -334,6 +335,7 @@ static int __init mtk_gpt_init(struct device_node *node)
 					TIMER_SYNC_TICKS, 0xffffffff);
 
 	mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
+	enable_irq(timer_of_irq(&to));
 
 	return 0;
 }
#3 lore raw

PATCH 2/3 clocksource/drivers/timer-mediatek: Convert to OF probing

In reply to Luca Leonardo Scorcia

MediaTek APXGPT general purpose timer driver is registered via TIMER_OF_DECLARE to be loaded early in the boot process. It depends on a reference 13 MHz clock that is assigned via the clocks property of its DT node.
On platforms like mt8516/mt8167 this clock is provided by a driver that is loaded using OF probing and becomes available only later during boot. This leads to a silent EDEFER error during timer init, which is not recovered by TIMER_OF_DECLARE and GPT clocks are not registered at all.

Since those clocks are not critical for boot, convert the timer-mediatek to OF probing and drop the TIMER_OF_DECLARE instructions.

---
 drivers/clocksource/timer-mediatek.c | 36 ++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/clocksource/timer-mediatek.c +31 -5
index e20075b09fab..a9e7ff6908a3 100644
@@ -13,6 +13,9 @@
 #include <linux/clocksource.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
 #include <linux/sched_clock.h>
 #include <linux/slab.h>
 #include "timer-of.h"
@@ -215,8 +218,7 @@ static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-static void
-__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
+static void mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
 {
 	writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
 	       timer_of_base(to) + GPT_CTRL_REG(timer));
@@ -281,7 +283,7 @@ static struct timer_of to = {
 	},
 };
 
-static int __init mtk_syst_init(struct device_node *node)
+static int mtk_syst_init(struct device_node *node)
 {
 	int ret;
 
@@ -339,5 +341,29 @@ static int __init mtk_gpt_init(struct device_node *node)
 
 	return 0;
 }
-TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
-TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
+
+static int mtk_timer_probe(struct platform_device *pdev)
+{
+	int (*timer_init)(struct device_node *node);
+	struct device_node *np = pdev->dev.of_node;
+
+	timer_init = of_device_get_match_data(&pdev->dev);
+	return timer_init(np);
+}
+
+static const struct of_device_id mtk_timer_match_table[] = {
+	{ .compatible = "mediatek,mt6577-timer", .data = mtk_gpt_init },
+	{ .compatible = "mediatek,mt6765-timer", .data = mtk_syst_init },
+	{ /* sentinel */ }
+};
+
+static struct platform_driver mtk_timer_driver = {
+	.probe = mtk_timer_probe,
+	.driver = {
+		.name = "mediatek-timer",
+		.of_match_table = mtk_timer_match_table,
+	},
+};
+module_platform_driver(mtk_timer_driver);
+
+MODULE_DESCRIPTION("MediaTek Timer driver");
Signature · 1 line
2.43.0
#4 lore raw

[clocksource: timers/drivers/next] clocksource/drivers/timer-mediatek: Convert to OF probing

The following commit has been merged into the timers/drivers/next branch of clocksource:

The following commit has been merged into the timers/drivers/next branch of clocksource:

Commit-ID:     7a097aba725501c399b5e93d0199aa3a2f6e9c88
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/daniel.lezcano/linux.git//7a097aba725501c399b5e93d0199aa3a2f6e9c88
Author:        Luca Leonardo Scorcia <[email protected]>
AuthorDate:    Fri, 02 Oct 2026 09:19:27 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 13 Oct 2026 13:17:20 +02:00

clocksource/drivers/timer-mediatek: Convert to OF probing

MediaTek APXGPT general purpose timer driver is registered via TIMER_OF_DECLARE to be loaded early in the boot process. It depends on a reference 13 MHz clock that is assigned via the clocks property of its DT node.
On platforms like mt8516/mt8167 this clock is provided by a driver that is loaded using OF probing and becomes available only later during boot. This leads to a silent EDEFER error during timer init, which is not recovered by TIMER_OF_DECLARE and GPT clocks are not registered at all.

Since those clocks are not critical for boot, convert the timer-mediatek to OF probing and drop the TIMER_OF_DECLARE instructions.

---
 drivers/clocksource/timer-mediatek.c | 36 +++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/clocksource/timer-mediatek.c +31 -5
index e20075b..a9e7ff6 100644
@@ -13,6 +13,9 @@
 #include <linux/clocksource.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
 #include <linux/sched_clock.h>
 #include <linux/slab.h>
 #include "timer-of.h"
@@ -215,8 +218,7 @@ static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-static void
-__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
+static void mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
 {
 	writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
 	       timer_of_base(to) + GPT_CTRL_REG(timer));
@@ -281,7 +283,7 @@ static struct timer_of to = {
 	},
 };
 
-static int __init mtk_syst_init(struct device_node *node)
+static int mtk_syst_init(struct device_node *node)
 {
 	int ret;
 
@@ -339,5 +341,29 @@ static int __init mtk_gpt_init(struct device_node *node)
 
 	return 0;
 }
-TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
-TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
+
+static int mtk_timer_probe(struct platform_device *pdev)
+{
+	int (*timer_init)(struct device_node *node);
+	struct device_node *np = pdev->dev.of_node;
+
+	timer_init = of_device_get_match_data(&pdev->dev);
+	return timer_init(np);
+}
+
+static const struct of_device_id mtk_timer_match_table[] = {
+	{ .compatible = "mediatek,mt6577-timer", .data = mtk_gpt_init },
+	{ .compatible = "mediatek,mt6765-timer", .data = mtk_syst_init },
+	{ /* sentinel */ }
+};
+
+static struct platform_driver mtk_timer_driver = {
+	.probe = mtk_timer_probe,
+	.driver = {
+		.name = "mediatek-timer",
+		.of_match_table = mtk_timer_match_table,
+	},
+};
+module_platform_driver(mtk_timer_driver);
+
+MODULE_DESCRIPTION("MediaTek Timer driver");
#5 lore raw

Re: [PATCH 2/3] clocksource/drivers/timer-mediatek: Convert to OF probing

In reply to Luca Leonardo Scorcia
Quoted 11 lines · Luca Leonardo Scorcia

MediaTek APXGPT general purpose timer driver is registered via TIMER_OF_DECLARE to be loaded early in the boot process. It depends on a reference 13 MHz clock that is assigned via the clocks property of its DT node.
On platforms like mt8516/mt8167 this clock is provided by a driver that is loaded using OF probing and becomes available only later during boot. This leads to a silent EDEFER error during timer init, which is not recovered by TIMER_OF_DECLARE and GPT clocks are not registered at all.

Since those clocks are not critical for boot, convert the timer-mediatek to OF probing and drop the TIMER_OF_DECLARE instructions.

I think this is now triggering some lockdep warnings.

[  +0.000084] BUG: sleeping function called from invalid context at ./include/linux/percpu-rwsem.h:51
[  +0.000086] in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 1, name: swapper/0
[  +0.000079] preempt_count: 0, expected: 0
[  +0.000050] RCU nest depth: 0, expected: 0
[  +0.000051] locks held by swapper/0/1: 1, last CPU#3:
[  +0.000060]  #0: ffffff80c15d1138 (&dev->mutex){....}-{4:4}, at: __driver_attach (./include/linux/device.h:1109 drivers/base/dd.c:1046 drivers/base/dd.c:1299)
[  +0.000142] irq event stamp: 250728
[  +0.067364] hardirqs last  enabled at (250727): tick_oneshot_mode_active (kernel/time/tick-oneshot.c:128)
[  +0.009480] hardirqs last disabled at (250728): sched_clock_register (kernel/time/sched_clock.c:189)
[  +0.009301] softirqs last  enabled at (250642): __do_softirq (kernel/softirq.c:703)
[  +0.008427] softirqs last disabled at (250637): __do_softirq (kernel/softirq.c:703)
[  +0.008433] CPU: 3 UID: 0 PID: 1 Comm: swapper/0 Tainted: G        W           7.4.0-rc1-next-20261105-04243-gd86019357887 #373 PREEMPT>
[  +0.000018] Tainted: [W]=WARN
[  +0.000005] Hardware name: Google Hana (DT)
[  +0.000007] Call trace:
[  +0.000005]  show_stack (arch/arm64/kernel/stacktrace.c:499) (C)
[  +0.000018]  __dump_stack (lib/dump_stack.c:94)
[  +0.000015]  dump_stack_lvl (lib/dump_stack.c:120)
[  +0.000015]  dump_stack (lib/dump_stack.c:129)
[  +0.000013]  __might_resched (kernel/sched/core.c:9195)
[  +0.000018]  __might_sleep (kernel/sched/core.c:9124)
[  +0.000014]  cpus_read_lock (./include/linux/percpu-rwsem.h:51 ./include/linux/percpu-rwsem.h:77 kernel/cpu.c:490)
[  +0.000013]  static_key_enable (kernel/jump_label.c:222)
[  +0.000016]  enable_sched_clock_irqtime (kernel/sched/cputime.c:33)
[  +0.000014]  sched_clock_register (kernel/time/sched_clock.c:243)
[  +0.000014]  mtk_gpt_init (drivers/clocksource/timer-mediatek.c:338)
[  +0.000014]  mtk_timer_probe (drivers/clocksource/timer-mediatek.c:356)
[  +0.000014]  platform_probe (drivers/base/platform.c:1508)
[  +0.000012]  really_probe (drivers/base/dd.c:? drivers/base/dd.c:707)
[  +0.000014]  __driver_probe_device (drivers/base/dd.c:869)
[  +0.000014]  driver_probe_device (drivers/base/dd.c:899)
[  +0.000014]  __driver_attach (drivers/base/dd.c:1300)
[  +0.000014]  bus_for_each_dev (drivers/base/bus.c:383)
[  +0.000013]  driver_attach (drivers/base/dd.c:1318)
[  +0.000013]  bus_add_driver (drivers/base/bus.c:782)
[  +0.000013]  driver_register (drivers/base/driver.c:174)
[  +0.000014]  __platform_driver_register (drivers/base/platform.c:996)
[  +0.000016]  mtk_timer_driver_init (drivers/clocksource/timer-mediatek.c:372)
[  +0.000017]  do_one_initcall (init/main.c:1419)
[  +0.000015]  do_initcall_level (init/main.c:1481)
[  +0.000014]  do_initcalls (init/main.c:1497)
[  +0.000012]  do_basic_setup (init/main.c:1517)
[  +0.000013]  kernel_init_freeable (init/main.c:1732)
[  +0.000013]  kernel_init (init/main.c:1622)
[  +0.000014]  ret_from_fork (arch/arm64/kernel/entry.S:870)
Quoted 73 lines
---
 drivers/clocksource/timer-mediatek.c | 36 ++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/clocksource/timer-mediatek.c +31 -5
index e20075b09fab..a9e7ff6908a3 100644
@@ -13,6 +13,9 @@
 #include <linux/clocksource.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
 #include <linux/sched_clock.h>
 #include <linux/slab.h>
 #include "timer-of.h"
@@ -215,8 +218,7 @@ static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-static void
-__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
+static void mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
 {
 	writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
 	       timer_of_base(to) + GPT_CTRL_REG(timer));
@@ -281,7 +283,7 @@ static struct timer_of to = {
 	},
 };
 
-static int __init mtk_syst_init(struct device_node *node)
+static int mtk_syst_init(struct device_node *node)
 {
 	int ret;
 
@@ -339,5 +341,29 @@ static int __init mtk_gpt_init(struct device_node *node)
 
 	return 0;
 }
-TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
-TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
+
+static int mtk_timer_probe(struct platform_device *pdev)
+{
+	int (*timer_init)(struct device_node *node);
+	struct device_node *np = pdev->dev.of_node;
+
+	timer_init = of_device_get_match_data(&pdev->dev);
+	return timer_init(np);
+}
+
+static const struct of_device_id mtk_timer_match_table[] = {
+	{ .compatible = "mediatek,mt6577-timer", .data = mtk_gpt_init },
+	{ .compatible = "mediatek,mt6765-timer", .data = mtk_syst_init },
+	{ /* sentinel */ }
+};
+
+static struct platform_driver mtk_timer_driver = {
+	.probe = mtk_timer_probe,
+	.driver = {
+		.name = "mediatek-timer",
+		.of_match_table = mtk_timer_match_table,
+	},
+};
+module_platform_driver(mtk_timer_driver);
+
+MODULE_DESCRIPTION("MediaTek Timer driver");
Signature · 1 line
2.43.0
#6 lore raw

PATCH 3/3 clocksource/drivers/timer-mediatek: Request and enable the bus clock

In reply to Luca Leonardo Scorcia

MediaTek APXGPT general purpose timers may optionally depend on a bus clock. If this clock is not enabled the timers don't actually run. Boot loaders usually turn it on, but later the kernel turns it off since it's never requested by any driver.

Some platforms (like mt6735) declare it as a critical clock in their clock definitions to keep it running, but at least on mt8167 it's not actually required for a successful boot.

If a bus clock is declared in the device tree, make sure it is enabled by driver probe and associated to the device.

---
 drivers/clocksource/timer-mediatek.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/clocksource/timer-mediatek.c +10 -5
index a9e7ff6908a3..842d6ee4259e 100644
@@ -9,6 +9,7 @@
 
 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 
+#include <linux/clk.h>
 #include <linux/clockchips.h>
 #include <linux/clocksource.h>
 #include <linux/interrupt.h>
@@ -283,8 +284,9 @@ static struct timer_of to = {
 	},
 };
 
-static int mtk_syst_init(struct device_node *node)
+static int mtk_syst_init(struct platform_device *pdev)
 {
+	struct device_node *node = pdev->dev.of_node;
 	int ret;
 
 	to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
@@ -305,10 +307,14 @@ static int mtk_syst_init(struct device_node *node)
 	return 0;
 }
 
-static int __init mtk_gpt_init(struct device_node *node)
+static int mtk_gpt_init(struct platform_device *pdev)
 {
+	struct device_node *node = pdev->dev.of_node;
+	struct clk *clk_bus;
 	int ret;
 
+	clk_bus = devm_clk_get_optional_enabled(&pdev->dev, "bus");
+
 	to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
 	to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
 	to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
@@ -344,11 +350,10 @@ static int __init mtk_gpt_init(struct device_node *node)
 
 static int mtk_timer_probe(struct platform_device *pdev)
 {
-	int (*timer_init)(struct device_node *node);
-	struct device_node *np = pdev->dev.of_node;
+	int (*timer_init)(struct platform_device *pdev);
 
 	timer_init = of_device_get_match_data(&pdev->dev);
-	return timer_init(np);
+	return timer_init(pdev);
 }
 
 static const struct of_device_id mtk_timer_match_table[] = {
Signature · 1 line
2.43.0
#7 lore raw

[clocksource: timers/drivers/next] clocksource/drivers/timer-mediatek: Request and enable the bus clock

The following commit has been merged into the timers/drivers/next branch of clocksource:

The following commit has been merged into the timers/drivers/next branch of clocksource:

Commit-ID:     e234d40823808c1727e88a40d8c9ee960de1baed
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/daniel.lezcano/linux.git//e234d40823808c1727e88a40d8c9ee960de1baed
Author:        Luca Leonardo Scorcia <[email protected]>
AuthorDate:    Fri, 02 Oct 2026 09:19:28 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 13 Oct 2026 13:17:27 +02:00

clocksource/drivers/timer-mediatek: Request and enable the bus clock

MediaTek APXGPT general purpose timers may optionally depend on a bus clock. If this clock is not enabled the timers don't actually run. Boot loaders usually turn it on, but later the kernel turns it off since it's never requested by any driver.

Some platforms (like mt6735) declare it as a critical clock in their clock definitions to keep it running, but at least on mt8167 it's not actually required for a successful boot.

If a bus clock is declared in the device tree, make sure it is enabled by driver probe and associated to the device.

---
 drivers/clocksource/timer-mediatek.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/clocksource/timer-mediatek.c +10 -5
index a9e7ff6..842d6ee 100644
@@ -9,6 +9,7 @@
 
 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 
+#include <linux/clk.h>
 #include <linux/clockchips.h>
 #include <linux/clocksource.h>
 #include <linux/interrupt.h>
@@ -283,8 +284,9 @@ static struct timer_of to = {
 	},
 };
 
-static int mtk_syst_init(struct device_node *node)
+static int mtk_syst_init(struct platform_device *pdev)
 {
+	struct device_node *node = pdev->dev.of_node;
 	int ret;
 
 	to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
@@ -305,10 +307,14 @@ static int mtk_syst_init(struct device_node *node)
 	return 0;
 }
 
-static int __init mtk_gpt_init(struct device_node *node)
+static int mtk_gpt_init(struct platform_device *pdev)
 {
+	struct device_node *node = pdev->dev.of_node;
+	struct clk *clk_bus;
 	int ret;
 
+	clk_bus = devm_clk_get_optional_enabled(&pdev->dev, "bus");
+
 	to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
 	to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
 	to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
@@ -344,11 +350,10 @@ static int __init mtk_gpt_init(struct device_node *node)
 
 static int mtk_timer_probe(struct platform_device *pdev)
 {
-	int (*timer_init)(struct device_node *node);
-	struct device_node *np = pdev->dev.of_node;
+	int (*timer_init)(struct platform_device *pdev);
 
 	timer_init = of_device_get_match_data(&pdev->dev);
-	return timer_init(np);
+	return timer_init(pdev);
 }
 
 static const struct of_device_id mtk_timer_match_table[] = {
#8 lore raw
In reply to Luca Leonardo Scorcia

Hi,

Quoted 16 lines · Luca Leonardo Scorcia

While chasing a kernel freeze on the Lenovo Tab E7 TB-7104F (mt8167), I stumbled on the MediaTek GPT timer driver. This driver is currently early loaded via TIMER_OF_DECLARE. I noticed that on mt8167 (and I guess mt8516 too), this driver actually fails to probe because its device tree declaration depends on two HW clocks, CLK_TOP_CLK26M_D2 (13 MHz) and CLK_TOP_APXGPT (bus clock). However the driver providing those clocks is loaded later in the boot process, leading TIMER_OF_DECLARE to fail with -EDEFER.

Patch #2 converts this driver to OF probe, fixing this initial issue.

I noticed that other devices avoid this by connecting the timer to fixed clocks instead of referring to an actual clock. This approach may work on mt8167 too, but I think referring to the actual clock is cleaner.

Quoted 8 lines

Once the driver probed successfully I noticed that even if it was enabled by the boot loader, the kernel would quickly turn off its bus clock as the driver wasn't actually requesting it.

Patch #3 enables it and attaches it to the timer device. I wondered how other platforms made it actually work, and noticed that mt6735 declares it as a critical clock. This approach might work for mt8167 too, but same reasoning as before applies.

Most mt67xx and their 81xx siblings are feeding arch timer from cpuxgpt timers. These are usually turned on by the firmware at EL3, so in general you can disable both apxgpt and syst in the devicetree (unless 8167 feeds arch timer from the gpt6 like 65xx...)

Quoted 17 lines

After those fixes the driver probes and works correctly, but I would occasionally notice a crash at boot inside the timer interrupt handler. It turned out to be a side effect of the bootloader enabling the GPT timer at boot: an interrupt may arrive and be handled while the driver is not completely initialized.

Patch #1 fixes it by requesting the IRQ but keeping it disabled and enabling it after the clock source initialization completes. I sorted this patch before the others because it's actually independent of the latter two and may fix (rare) random crashes on other devices.

Please note that all the changes apply both to GPT timers and the SYST timers, but I have no device to test the SYST ones.

The series was tested on the Sony PlayStation Classic Mini (mt8167).

Quoted 7 lines

Re: patch #1, credits for issue discovery and suggested fix go to matemiller992 on postmarketOS forums. I am submitting it upstream with permission. Not adding Reported-by or Suggested-by tags because it was requested not to share real name.

Unfortunately, this series did not fix the tablet randomly freezing during boot. Any idea on how to better troubleshoot this is more than welcome!

Have you tried to add {clk,regulator,pd}_ignore_unused? If that doesn't help, try turning off cpufreq. On mt6595 I had to mark CPU regulator as always-on (although it should be safe to disable) to avoid system freeze at the boot. Otherwise start adding status = "disabled" to the devicetree nodes until it properly boots.

Best regards,
Roman