Skip to contentSkip to conversation

timer: mtk_timer: initialize the timer before use

PATCH New Weijie Gao 3 participants 7 messages Jan 12, 2021 – Jan 19, 2021

Subsystems (2)
lore
Share & tools
lore

Weijie Gao

author
#0 lore raw

PATCH 1/2 timer: mtk_timer: initialize the timer before use

The timer being used by this driver may have already been used by first stage bootloader (e.g. ATF/preloader), and it's settings may differ from what this driver is going to use.

This may cause issues, such as inaccurate timer frequency due to incorrect clock divider.

This patch adds the initialization code to avoid them.

---
 drivers/timer/mtk_timer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/timer/mtk_timer.c +10 -0
index 448a76a7e1..f6b97f868c 100644
@@ -61,6 +61,16 @@ static int mtk_timer_probe(struct udevice *dev)
 	if (!uc_priv->clock_rate)
 		return -EINVAL;
 
+	/*
+	 * Initialize the timer:
+	 * 1. set clock source to system clock with clock divider setting to 1
+	 * 2. set timer mode to free running
+	 * 3. reset timer counter to 0 then enable the timer
+	 */
+	writel(GPT4_CLK_SYS | GPT4_CLK_DIV1, priv->base + MTK_GPT4_CLK);
+	writel(GPT4_FREERUN | GPT4_CLEAR | GPT4_ENABLE,
+	       priv->base + MTK_GPT4_CTRL);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 dts: mt7622: use accurate clock source fot mtk_timer

The input system clock for mt7622 timer is 10MHz and can be retrieved through the clk driver.

---
 arch/arm/dts/mt7622.dtsi | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
arch/arm/dts/mt7622.dtsi +1 -7
index d888545809..5c2e0251de 100644
@@ -71,16 +71,10 @@
 		compatible = "mediatek,timer";
 		reg = <0x10004000 0x80>;
 		interrupts = <GIC_SPI 152 IRQ_TYPE_LEVEL_LOW>;
-		clocks = <&system_clk>;
+		clocks = <&infracfg CLK_INFRA_APXGPT_PD>;
 		clock-names = "system-clk";
 	};
 
-	system_clk: dummy13m {
-		compatible = "fixed-clock";
-		clock-frequency = <13000000>;
-		#clock-cells = <0>;
-	};
-
 	infracfg: infracfg at 10000000 {
 		compatible = "mediatek,mt7622-infracfg",
 			     "syscon";
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/2 dts: mt7622: use accurate clock source fot mtk_timer

Quoted 4 lines · Weijie Gao

The input system clock for mt7622 timer is 10MHz and can be retrieved through the clk driver.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210119/ef7666bd/attachment.sig>
#3 lore raw

PATCH 1/2 timer: mtk_timer: initialize the timer before use

In reply to Weijie Gao
Quoted 20 lines · Weijie Gao

The timer being used by this driver may have already been used by first stage bootloader (e.g. ATF/preloader), and it's settings may differ from what this driver is going to use.

This may cause issues, such as inaccurate timer frequency due to incorrect clock divider.

This patch adds the initialization code to avoid them.

---
 drivers/timer/mtk_timer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/timer/mtk_timer.c +0 -0
index 448a76a7e1..f6b97f868c 100644
@@ -61,6 +61,16 @@ static int mtk_timer_probe(struct udevice *dev)
 	if (!uc_priv->clock_rate)

drivers/timer/mtk_timer.c static int mtk_timer_probe(struct udevice *dev) · line 71

Quoted 10 lines
drivers/timer/mtk_timer.c +8 -0
 		return -EINVAL;
 
+	/*
+	 * Initialize the timer:
+	 * 1. set clock source to system clock with clock divider setting to 1
+	 * 2. set timer mode to free running
+	 * 3. reset timer counter to 0 then enable the timer
+	 */
+	writel(GPT4_CLK_SYS | GPT4_CLK_DIV1, priv->base + MTK_GPT4_CLK);
+	writel(GPT4_FREERUN | GPT4_CLEAR | GPT4_ENABLE,

GPT4_FREERUN is defined as GENMASK(5,4) while in the Linux kernel it has the value of 3. Can you explain where this difference comes from?

Regards,
Matthias

Quoted 7 lines
drivers/timer/mtk_timer.c +2 -0
+	       priv->base + MTK_GPT4_CTRL);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH 1/2 timer: mtk_timer: initialize the timer before use

Quoted 23 lines · Matthias Brugger
Quoted 22 lines · Weijie Gao

The timer being used by this driver may have already been used by first stage bootloader (e.g. ATF/preloader), and it's settings may differ from what this driver is going to use.

This may cause issues, such as inaccurate timer frequency due to incorrect clock divider.

This patch adds the initialization code to avoid them.

---
 drivers/timer/mtk_timer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/timer/mtk_timer.c +0 -0
index 448a76a7e1..f6b97f868c 100644
@@ -61,6 +61,16 @@ static int mtk_timer_probe(struct udevice *dev)
 	if (!uc_priv->clock_rate)
 		return -EINVAL;
 
Quoted 11 lines

drivers/timer/mtk_timer.c static int mtk_timer_probe(struct udevice *dev) · line 71

Quoted 8 lines
drivers/timer/mtk_timer.c +8 -0
+	/*
+	 * Initialize the timer:
+	 * 1. set clock source to system clock with clock divider setting to 1
+	 * 2. set timer mode to free running
+	 * 3. reset timer counter to 0 then enable the timer
+	 */
+	writel(GPT4_CLK_SYS | GPT4_CLK_DIV1, priv->base + MTK_GPT4_CLK);
+	writel(GPT4_FREERUN | GPT4_CLEAR | GPT4_ENABLE,

GPT4_FREERUN is defined as GENMASK(5,4) while in the Linux kernel it has the value of 3. Can you explain where this difference comes from?

GENMASK(5,4) == 3 << 4, which is exactly the same as what's being used in kernel:

#define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
#define GPT_CTRL_OP_FREERUN (3)

Quoted 11 lines

Regards,
Matthias

Quoted 7 lines
drivers/timer/mtk_timer.c +2 -0
+	       priv->base + MTK_GPT4_CTRL);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH 1/2 timer: mtk_timer: initialize the timer before use

Quoted 36 lines · Weijie Gao
Quoted 23 lines · Matthias Brugger
Quoted 22 lines · Weijie Gao

The timer being used by this driver may have already been used by first stage bootloader (e.g. ATF/preloader), and it's settings may differ from what this driver is going to use.

This may cause issues, such as inaccurate timer frequency due to incorrect clock divider.

This patch adds the initialization code to avoid them.

---
 drivers/timer/mtk_timer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/timer/mtk_timer.c +0 -0
index 448a76a7e1..f6b97f868c 100644
@@ -61,6 +61,16 @@ static int mtk_timer_probe(struct udevice *dev)
 	if (!uc_priv->clock_rate)
 		return -EINVAL;
 
Quoted 11 lines

drivers/timer/mtk_timer.c static int mtk_timer_probe(struct udevice *dev) · line 71

Quoted 8 lines
drivers/timer/mtk_timer.c +8 -0
+	/*
+	 * Initialize the timer:
+	 * 1. set clock source to system clock with clock divider setting to 1
+	 * 2. set timer mode to free running
+	 * 3. reset timer counter to 0 then enable the timer
+	 */
+	writel(GPT4_CLK_SYS | GPT4_CLK_DIV1, priv->base + MTK_GPT4_CLK);
+	writel(GPT4_FREERUN | GPT4_CLEAR | GPT4_ENABLE,

GPT4_FREERUN is defined as GENMASK(5,4) while in the Linux kernel it has the value of 3. Can you explain where this difference comes from?

Quoted 6 lines

GENMASK(5,4) == 3 << 4, which is exactly the same as what's being used in kernel:

#define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
#define GPT_CTRL_OP_FREERUN (3)

You are right, sorry my fault:

Quoted 12 lines
Quoted 11 lines

Regards,
Matthias

Quoted 7 lines
drivers/timer/mtk_timer.c +2 -0
+	       priv->base + MTK_GPT4_CTRL);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#6 lore raw

PATCH 1/2 timer: mtk_timer: initialize the timer before use

In reply to Weijie Gao
Quoted 10 lines · Weijie Gao

The timer being used by this driver may have already been used by first stage bootloader (e.g. ATF/preloader), and it's settings may differ from what this driver is going to use.

This may cause issues, such as inaccurate timer frequency due to incorrect clock divider.

This patch adds the initialization code to avoid them.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210119/1d04380c/attachment.sig>