Skip to contentSkip to conversation

armv7m: Add SysTick timer driver

PATCH New Phil Edworthy 4 participants 10 messages Feb 3, 2017 – Feb 13, 2017 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91 +++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
arch/arm/cpu/armv7m/Makefile +2 -0
index aff60e8..e1a6c40 100644
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
arch/arm/cpu/armv7m/systick-timer.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Quoted 107 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91 +++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
arch/arm/cpu/armv7m/Makefile +2 -0
index aff60e8..e1a6c40 100644
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
arch/arm/cpu/armv7m/systick-timer.c new file +81 -0
new file mode 100644
index 0000000..6ccc2fb
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter <[email protected]>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE		0xE000E010
+
+struct cm3_systick {
+	uint32_t ctrl;
+	uint32_t reload;
+	uint32_t val;
+	uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL		0x00FFFFFF
+#define SYSTICK_CTRL_EN		BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
+#define SYSTICK_CTRL_CPU_CLK	BIT(2)
+
+/* read the 24-bit timer */
+static ulong read_timer(void)
+{
+	struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+	/* The timer counts down, therefore convert to an incrementing timer */
+	return TIMER_LOAD_VAL - readl(&systick->val);
+}
+
+int timer_init(void)
+{
+	struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+	writel(TIMER_LOAD_VAL, &systick->reload);
+	writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+	/* Use CPU clock, no interrupts */
+	writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+	/* Use external clock, no interrupts */
+	writel(SYSTICK_CTRL_EN, &systick->ctrl);
+#endif
+
+	gd->arch.tbl = 0;
+	gd->arch.tbu = 0;
+	gd->arch.lastinc = read_timer();
+
+	return 0;
+}
+
+/* return milli-seconds timer value */
+ulong get_timer(ulong base)
+{
+	unsigned long long t = get_ticks() * 1000;
+
+	return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base;
+}
+
+unsigned long long get_ticks(void)
+{
+	u32 now = read_timer();
+
+	if (now >= gd->arch.lastinc)
+		gd->arch.tbl += (now - gd->arch.lastinc);
+	else
+		gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+
+	gd->arch.lastinc = now;
+
+	return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+	return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170205/2200d8e5/attachment.sig>
#2 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Quoted 124 lines · Tom Rini
Quoted 107 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91 +++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
arch/arm/cpu/armv7m/Makefile +2 -0
index aff60e8..e1a6c40 100644
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
arch/arm/cpu/armv7m/systick-timer.c new file +81 -0
new file mode 100644
index 0000000..6ccc2fb
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter <[email protected]>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE         0xE000E010
+
+struct cm3_systick {
+     uint32_t ctrl;
+     uint32_t reload;
+     uint32_t val;
+     uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL               0x00FFFFFF
+#define SYSTICK_CTRL_EN              BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
+#define SYSTICK_CTRL_CPU_CLK BIT(2)
+
+/* read the 24-bit timer */
+static ulong read_timer(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     /* The timer counts down, therefore convert to an incrementing timer */
+     return TIMER_LOAD_VAL - readl(&systick->val);
+}
+
+int timer_init(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     writel(TIMER_LOAD_VAL, &systick->reload);
+     writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+     /* Use CPU clock, no interrupts */
+     writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+     /* Use external clock, no interrupts */
+     writel(SYSTICK_CTRL_EN, &systick->ctrl);
+#endif
+
+     gd->arch.tbl = 0;
+     gd->arch.tbu = 0;
+     gd->arch.lastinc = read_timer();
+
+     return 0;
+}
+
+/* return milli-seconds timer value */
+ulong get_timer(ulong base)
+{
+     unsigned long long t = get_ticks() * 1000;
+
+     return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base;
+}
+
+unsigned long long get_ticks(void)
+{
+     u32 now = read_timer();
+
+     if (now >= gd->arch.lastinc)
+             gd->arch.tbl += (now - gd->arch.lastinc);
+     else
+             gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+
+     gd->arch.lastinc = now;
+
+     return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+     return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

Quoted 2 lines

--
Tom

Yes, however as far as I remember the systick is only 24-bit and usually runs on CPU clock so it will roll over too fast to be useful without using interrupts. Maybe I have missed something?

/Kamil

#3 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Hi Kamil,

Quoted 127 lines · Kamil Lulko
Quoted 9 lines · Tom Rini
Quoted 7 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91

+++++++++++++++++++++++++++++++++++++

Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/systick-timer.c

Quoted 100 lines
Quoted 86 lines

new file mode 100644
index 0000000..6ccc2fb

arch/arm/cpu/armv7m/systick-timer.c new file +81 -0
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter <[email protected]>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE         0xE000E010
+
+struct cm3_systick {
+     uint32_t ctrl;
+     uint32_t reload;
+     uint32_t val;
+     uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL               0x00FFFFFF
+#define SYSTICK_CTRL_EN              BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
+#define SYSTICK_CTRL_CPU_CLK BIT(2)
+
+/* read the 24-bit timer */
+static ulong read_timer(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     /* The timer counts down, therefore convert to an incrementing timer */
+     return TIMER_LOAD_VAL - readl(&systick->val);
+}
+
+int timer_init(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     writel(TIMER_LOAD_VAL, &systick->reload);
+     writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+     /* Use CPU clock, no interrupts */
+     writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+     /* Use external clock, no interrupts */
+     writel(SYSTICK_CTRL_EN, &systick->ctrl);
+#endif
+
+     gd->arch.tbl = 0;
+     gd->arch.tbu = 0;
+     gd->arch.lastinc = read_timer();
+
+     return 0;
+}
+
+/* return milli-seconds timer value */
+ulong get_timer(ulong base)
+{
+     unsigned long long t = get_ticks() * 1000;
+
+     return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base;
+}
+
+unsigned long long get_ticks(void)
+{
+     u32 now = read_timer();
+
+     if (now >= gd->arch.lastinc)
+             gd->arch.tbl += (now - gd->arch.lastinc);
+     else
+             gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+
+     gd->arch.lastinc = now;
+
+     return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+     return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

Quoted 6 lines
Quoted 2 lines

--
Tom

Yes, however as far as I remember the systick is only 24-bit and usually runs on CPU clock so it will roll over too fast to be useful without using interrupts. Maybe I have missed something?

Yes, it is only a 24-bit counter. On the device I am using, the clock is not the cpu clock, but a low speed clock at 6.25MHz. Even at that low clock it will still wrap every ~2.6 seconds.

Quoted 1 line

/Kamil

Thanks
Phil

#4 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Quoted 133 lines · Phil Edworthy

Hi Kamil,

Quoted 130 lines · Kamil Lulko
Quoted 9 lines · Tom Rini
Quoted 7 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91

+++++++++++++++++++++++++++++++++++++

Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/systick-timer.c

Quoted 102 lines
Quoted 86 lines

new file mode 100644
index 0000000..6ccc2fb

arch/arm/cpu/armv7m/systick-timer.c new file +81 -0
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter <[email protected]>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE         0xE000E010
+
+struct cm3_systick {
+     uint32_t ctrl;
+     uint32_t reload;
+     uint32_t val;
+     uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL               0x00FFFFFF
+#define SYSTICK_CTRL_EN              BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
+#define SYSTICK_CTRL_CPU_CLK BIT(2)
+
+/* read the 24-bit timer */
+static ulong read_timer(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     /* The timer counts down, therefore convert to an incrementing timer */
+     return TIMER_LOAD_VAL - readl(&systick->val);
+}
+
+int timer_init(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     writel(TIMER_LOAD_VAL, &systick->reload);
+     writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+     /* Use CPU clock, no interrupts */
+     writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+     /* Use external clock, no interrupts */
+     writel(SYSTICK_CTRL_EN, &systick->ctrl);
+#endif
+
+     gd->arch.tbl = 0;
+     gd->arch.tbu = 0;
+     gd->arch.lastinc = read_timer();
+
+     return 0;
+}
+
+/* return milli-seconds timer value */
+ulong get_timer(ulong base)
+{
+     unsigned long long t = get_ticks() * 1000;
+
+     return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base;
+}
+
+unsigned long long get_ticks(void)
+{
+     u32 now = read_timer();
+
+     if (now >= gd->arch.lastinc)
+             gd->arch.tbl += (now - gd->arch.lastinc);
+     else
+             gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+
+     gd->arch.lastinc = now;
+
+     return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+     return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

--
Tom

Quoted 7 lines
Quoted 3 lines

Yes, however as far as I remember the systick is only 24-bit and usually runs on CPU clock so it will roll over too fast to be useful without using interrupts. Maybe I have missed something?

Yes, it is only a 24-bit counter. On the device I am using, the clock is not the cpu clock, but a low speed clock at 6.25MHz. Even at that low clock it will still wrap every ~2.6 seconds.

So are you able to use the timer then, given the quick overflow?

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170207/83a2e91c/attachment.sig>
#5 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Hi Tom,

Quoted 140 lines · Tom Rini
Quoted 30 lines · Phil Edworthy

Hi Kamil,

Quoted 27 lines · Kamil Lulko
Quoted 9 lines · Tom Rini
Quoted 7 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91

+++++++++++++++++++++++++++++++++++++

Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/systick-timer.c

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

new file mode 100644
index 0000000..6ccc2fb

arch/arm/cpu/armv7m/systick-timer.c new file +2 -0
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
Quoted 8 lines
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter

<[email protected]>

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
arch/arm/cpu/armv7m/systick-timer.c new file +22 -0
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE         0xE000E010
+
+struct cm3_systick {
+     uint32_t ctrl;
+     uint32_t reload;
+     uint32_t val;
+     uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL               0x00FFFFFF
+#define SYSTICK_CTRL_EN              BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+#define SYSTICK_CTRL_CPU_CLK BIT(2)
+
+/* read the 24-bit timer */
+static ulong read_timer(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     /* The timer counts down, therefore convert to an incrementing timer

*/

Quoted 62 lines
Quoted 61 lines
Quoted 55 lines
Quoted 41 lines
arch/arm/cpu/armv7m/systick-timer.c new file +41 -0
+     return TIMER_LOAD_VAL - readl(&systick->val);
+}
+
+int timer_init(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+     writel(TIMER_LOAD_VAL, &systick->reload);
+     writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+     /* Use CPU clock, no interrupts */
+     writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+     /* Use external clock, no interrupts */
+     writel(SYSTICK_CTRL_EN, &systick->ctrl);
+#endif
+
+     gd->arch.tbl = 0;
+     gd->arch.tbu = 0;
+     gd->arch.lastinc = read_timer();
+
+     return 0;
+}
+
+/* return milli-seconds timer value */
+ulong get_timer(ulong base)
+{
+     unsigned long long t = get_ticks() * 1000;
+
+     return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base;
+}
+
+unsigned long long get_ticks(void)
+{
+     u32 now = read_timer();
+
+     if (now >= gd->arch.lastinc)
+             gd->arch.tbl += (now - gd->arch.lastinc);
+     else
+             gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+
+     gd->arch.lastinc = now;
+
+     return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+     return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

Quoted 2 lines

--
Tom

Yes, however as far as I remember the systick is only 24-bit and usually runs on CPU clock so it will roll over too fast to be useful without using interrupts. Maybe I have missed something?

Quoted 5 lines
Quoted 3 lines

Yes, it is only a 24-bit counter. On the device I am using, the clock is not the cpu clock, but a low speed clock at 6.25MHz. Even at that low clock it will still wrap every ~2.6 seconds.

So are you able to use the timer then, given the quick overflow?

Yes, I didn't see any problems. All of the udelay/mdelay calls should work because they resolve to constantly calling get_ticks.

I guess it will not work if you attempt to time something that is longer than 2.6 seconds though.

Thanks
Phil

#6 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Hi Phil,

Quoted 148 lines

-----Original Message-----
From: Phil Edworthy [mailto:phil.edworthy at renesas.com]
Sent: Tuesday, February 07, 2017 6:34 AM
To: Tom Rini <[email protected]>
Cc: Kamil Lulko <[email protected]>; Vikas MANOCHA <[email protected]>; Michael Kurz <[email protected]>; Albert Aribaud <[email protected]>; U-Boot Mailing List <[email protected]>
Subject: RE: [U-Boot] [PATCH] armv7m: Add SysTick timer driver

Hi Tom,

Quoted 137 lines · Tom Rini
Quoted 30 lines · Phil Edworthy

Hi Kamil,

Quoted 27 lines · Kamil Lulko
Quoted 9 lines · Tom Rini
Quoted 7 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91

+++++++++++++++++++++++++++++++++++++

Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 93 insertions(+)  create mode 100644

arch/arm/cpu/armv7m/systick-timer.c

(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/systick-timer.c

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

new file mode 100644
index 0000000..6ccc2fb

arch/arm/cpu/armv7m/systick-timer.c new file +2 -0
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
Quoted 8 lines
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter

<[email protected]>

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
arch/arm/cpu/armv7m/systick-timer.c new file +23 -0
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE         0xE000E010
+
+struct cm3_systick {
+     uint32_t ctrl;
+     uint32_t reload;
+     uint32_t val;
+     uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL               0x00FFFFFF
+#define SYSTICK_CTRL_EN              BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */ #define
+SYSTICK_CTRL_CPU_CLK BIT(2)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c line 41

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+
+/* read the 24-bit timer */
+static ulong read_timer(void) {
+     struct cm3_systick *systick = (struct cm3_systick
+*)SYSTICK_BASE;
+
+     /* The timer counts down, therefore convert to an
+ incrementing timer

*/

Quoted 58 lines
Quoted 57 lines
Quoted 51 lines
Quoted 37 lines
arch/arm/cpu/armv7m/systick-timer.c new file +37 -0
+     return TIMER_LOAD_VAL - readl(&systick->val); }
+
+int timer_init(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick
+*)SYSTICK_BASE;
+
+     writel(TIMER_LOAD_VAL, &systick->reload);
+     writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+     /* Use CPU clock, no interrupts */
+     writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK,
+&systick->ctrl); #else
+     /* Use external clock, no interrupts */
+     writel(SYSTICK_CTRL_EN, &systick->ctrl); #endif
+
+     gd->arch.tbl = 0;
+     gd->arch.tbu = 0;
+     gd->arch.lastinc = read_timer();
+
+     return 0;
+}
+
+/* return milli-seconds timer value */ ulong get_timer(ulong
+base) {
+     unsigned long long t = get_ticks() * 1000;
+
+     return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base; }
+
+unsigned long long get_ticks(void) {
+     u32 now = read_timer();
+
+     if (now >= gd->arch.lastinc)
+             gd->arch.tbl += (now - gd->arch.lastinc);
+     else
+             gd->arch.tbl += (TIMER_LOAD_VAL -

arch/arm/cpu/armv7m/systick-timer.c

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+ gd->arch.lastinc) + now;
+
+     gd->arch.lastinc = now;
+
+     return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+     return CONFIG_SYS_HZ_CLOCK; }

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

Quoted 2 lines

--
Tom

Yes, however as far as I remember the systick is only 24-bit and usually runs on CPU clock so it will roll over too fast to be useful without using interrupts. Maybe I have missed something?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Yes, it is only a 24-bit counter. On the device I am using, the clock is not the cpu clock, but a low speed clock at 6.25MHz. Even at that low clock it will still wrap every ~2.6 seconds.

So are you able to use the timer then, given the quick overflow?

Yes, I didn't see any problems. All of the udelay/mdelay calls should work because they resolve to constantly calling get_ticks.

I guess it will not work if you attempt to time something that is longer than 2.6 seconds though.

Hmm, I wish it had some way to increase overflow time...it doesn't have the pre-scalar to divide the clock..

As this timer is same for all cortex-Mx, It makes sense to use this timer for stm32 and other cortexMx. I didn't check calibration register : could it be useful here ?

Cheers,
Vikas

Quoted 3 lines

Thanks
Phil

#7 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Hi Vikas,

Quoted 158 lines · Vikas MANOCHA

Hi Phil,

Quoted 5 lines

-----Original Message-----
From: Phil Edworthy [mailto:phil.edworthy at renesas.com]
Sent: Tuesday, February 07, 2017 6:34 AM
To: Tom Rini <[email protected]>
Cc: Kamil Lulko <[email protected]>; Vikas MANOCHA

<[email protected]>; Michael Kurz <[email protected]>;

Quoted 1 line

Albert Aribaud <[email protected]>; U-Boot Mailing List <u-

boot at lists.denx.de>

Quoted 52 lines

Subject: RE: [U-Boot] [PATCH] armv7m: Add SysTick timer driver

Hi Tom,

Quoted 47 lines · Tom Rini
Quoted 30 lines · Phil Edworthy

Hi Kamil,

Quoted 27 lines · Kamil Lulko
Quoted 9 lines · Tom Rini
Quoted 7 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91

+++++++++++++++++++++++++++++++++++++

Quoted 4 lines

Review

Quoted 4 lines
 2 files changed, 93 insertions(+)  create mode 100644

arch/arm/cpu/armv7m/systick-timer.c

(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile

Quoted 10 lines

Review

Quoted 10 lines

index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/systick-timer.c

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

new file mode 100644
index 0000000..6ccc2fb

arch/arm/cpu/armv7m/systick-timer.c new file +2 -0
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
Quoted 8 lines
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c line 10

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter

<[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

arch/arm/cpu/armv7m/systick-timer.c line 20

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices

*/

Quoted 85 lines
Quoted 85 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
arch/arm/cpu/armv7m/systick-timer.c new file +13 -0
+#define SYSTICK_BASE         0xE000E010
+
+struct cm3_systick {
+     uint32_t ctrl;
+     uint32_t reload;
+     uint32_t val;
+     uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL               0x00FFFFFF
+#define SYSTICK_CTRL_EN              BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */ #define
+SYSTICK_CTRL_CPU_CLK BIT(2)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c line 41

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+
+/* read the 24-bit timer */
+static ulong read_timer(void) {
+     struct cm3_systick *systick = (struct cm3_systick
+*)SYSTICK_BASE;
+
+     /* The timer counts down, therefore convert to an
+ incrementing timer

*/

Quoted 54 lines
Quoted 54 lines
Quoted 53 lines
Quoted 37 lines
arch/arm/cpu/armv7m/systick-timer.c new file +37 -0
+     return TIMER_LOAD_VAL - readl(&systick->val); }
+
+int timer_init(void)
+{
+     struct cm3_systick *systick = (struct cm3_systick
+*)SYSTICK_BASE;
+
+     writel(TIMER_LOAD_VAL, &systick->reload);
+     writel(TIMER_LOAD_VAL, &systick->val);
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU
+     /* Use CPU clock, no interrupts */
+     writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK,
+&systick->ctrl); #else
+     /* Use external clock, no interrupts */
+     writel(SYSTICK_CTRL_EN, &systick->ctrl); #endif
+
+     gd->arch.tbl = 0;
+     gd->arch.tbu = 0;
+     gd->arch.lastinc = read_timer();
+
+     return 0;
+}
+
+/* return milli-seconds timer value */ ulong get_timer(ulong
+base) {
+     unsigned long long t = get_ticks() * 1000;
+
+     return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base; }
+
+unsigned long long get_ticks(void) {
+     u32 now = read_timer();
+
+     if (now >= gd->arch.lastinc)
+             gd->arch.tbl += (now - gd->arch.lastinc);
+     else
+             gd->arch.tbl += (TIMER_LOAD_VAL -

arch/arm/cpu/armv7m/systick-timer.c line 88

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+ gd->arch.lastinc) + now;
+
+     gd->arch.lastinc = now;
+
+     return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+     return CONFIG_SYS_HZ_CLOCK; }

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

--
Tom

Quoted 7 lines
Quoted 3 lines

Yes, however as far as I remember the systick is only 24-bit and usually runs on CPU clock so it will roll over too fast to be useful without using interrupts. Maybe I have missed something?

Yes, it is only a 24-bit counter. On the device I am using, the clock is not the cpu clock, but a low speed clock at 6.25MHz. Even at that low clock it will still wrap every ~2.6 seconds.

So are you able to use the timer then, given the quick overflow?

Quoted 8 lines
Quoted 1 line

Yes, I didn't see any problems. All of the udelay/mdelay calls should work

because they resolve to constantly calling get_ticks.

Quoted 2 lines

I guess it will not work if you attempt to time something that is longer than 2.6

seconds though.

Hmm, I wish it had some way to increase overflow time...it doesn't have the pre- scalar to divide the clock..

None that I could find.

Quoted 3 lines

As this timer is same for all cortex-Mx, It makes sense to use this timer for stm32 and other cortexMx.
I didn't check calibration register : could it be useful here ?

It could be used to get the clock rate, though you would need a fall back in case the calibration reg is wrong or indicates an inexact 10ms value.

Thanks
Phil

Quoted 6 lines

Cheers,
Vikas

Quoted 3 lines

Thanks
Phil

#8 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

In reply to Tom Rini

Hi Phil,

Quoted 56 lines

-----Original Message-----
From: Tom Rini [mailto:trini at konsulko.com]
Sent: Sunday, February 05, 2017 3:17 PM
To: Phil Edworthy <[email protected]>; Vikas MANOCHA <[email protected]>; Michael Kurz <[email protected]>
Cc: Albert Aribaud <[email protected]>; u-boot at lists.denx.de; Kamil Lulko <[email protected]>
Subject: Re: [U-Boot] [PATCH] armv7m: Add SysTick timer driver

Quoted 46 lines · Phil Edworthy

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91
+++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
arch/arm/cpu/armv7m/systick-timer.c new file +18 -0
new file mode 100644
index 0000000..6ccc2fb
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter <[email protected]>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c line 26

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE		0xE000E010
+
+struct cm3_systick {
+	uint32_t ctrl;
+	uint32_t reload;
+	uint32_t val;

This register is current timer value, write of any value clears it. Rename to cur_val would avoid any confusion.

Quoted 7 lines
Quoted 7 lines
arch/arm/cpu/armv7m/systick-timer.c new file +7 -0
+	uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL		0x00FFFFFF
+#define SYSTICK_CTRL_EN		BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
+#define SYSTICK_CTRL_CPU_CLK	BIT(2)
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c

Quoted 8 lines
arch/arm/cpu/armv7m/systick-timer.c new file +8 -0
+
+/* read the 24-bit timer */
+static ulong read_timer(void)
+{
+	struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+	/* The timer counts down, therefore convert to an incrementing timer */
+	return TIMER_LOAD_VAL - readl(&systick->val); }

use mask to be sure of 24bit timer counter value.

Quoted 7 lines

arch/arm/cpu/armv7m/systick-timer.c line 49

Quoted 7 lines
arch/arm/cpu/armv7m/systick-timer.c new file +7 -0
+
+int timer_init(void)
+{
+	struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+	writel(TIMER_LOAD_VAL, &systick->reload);
+	writel(TIMER_LOAD_VAL, &systick->val);

Writing load value gives the impression that it is load register, in fact it is only clearing countflag.

Quoted 2 lines

arch/arm/cpu/armv7m/systick-timer.c line 50

Quoted 2 lines
arch/arm/cpu/armv7m/systick-timer.c new file +2 -0
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU

Cortex M3 systick has only cpu clock source ?

Cheers,
Vikas

Quoted 41 lines
Quoted 26 lines
arch/arm/cpu/armv7m/systick-timer.c new file +26 -0
+	/* Use CPU clock, no interrupts */
+	writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+	/* Use external clock, no interrupts */
+	writel(SYSTICK_CTRL_EN, &systick->ctrl); #endif
+
+	gd->arch.tbl = 0;
+	gd->arch.tbu = 0;
+	gd->arch.lastinc = read_timer();
+
+	return 0;
+}
+
+/* return milli-seconds timer value */ ulong get_timer(ulong base) {
+	unsigned long long t = get_ticks() * 1000;
+
+	return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base; }
+
+unsigned long long get_ticks(void)
+{
+	u32 now = read_timer();
+
+	if (now >= gd->arch.lastinc)
+		gd->arch.tbl += (now - gd->arch.lastinc);
+	else
+		gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 10 lines
arch/arm/cpu/armv7m/systick-timer.c new file +10 -0
+
+	gd->arch.lastinc = now;
+
+	return gd->arch.tbl;
+}
+
+ulong get_tbclk(void)
+{
+	return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-stm32/*/timer.c I assume, yes? Thanks!

--
Tom

#9 lore raw

PATCH 1/1 armv7m: Add SysTick timer driver

Hi Vikas,

Quoted 51 lines · Vikas MANOCHA
Quoted 51 lines · Phil Edworthy
Quoted 50 lines

The SysTick is a 24-bit down counter that is found on all ARM Cortex M3, M4, M7 devices and is always located at a fixed address.

---
 arch/arm/cpu/armv7m/Makefile        |  2 +
 arch/arm/cpu/armv7m/systick-timer.c | 91
+++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 arch/arm/cpu/armv7m/systick-timer.c
(quoted diff) +0 -0

b/arch/arm/cpu/armv7m/Makefile index aff60e8..e1a6c40 100644

arch/arm/cpu/armv7m/Makefile +2 -0
@@ -7,3 +7,5 @@
 
 extra-y := start.o
 obj-y += cpu.o
+
+obj-$(CONFIG_SYS_ARCH_TIMER) += systick-timer.o
arch/arm/cpu/armv7m/systick-timer.c new file +21 -0
new file mode 100644
index 0000000..6ccc2fb
@@ -0,0 +1,91 @@
+/*
+ * ARM Cortex M3/M4/M7 SysTick timer driver
+ * (C) Copyright 2017 Renesas Electronics Europe Ltd
+ *
+ * Based on arch/arm/mach-stm32/stm32f1/timer.c
+ * (C) Copyright 2015
+ * Kamil Lulko, <[email protected]>
+ *
+ * Copyright 2015 ATS Advanced Telematics Systems GmbH
+ * Copyright 2015 Konsulko Group, Matt Porter <[email protected]>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/io.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
+#define SYSTICK_BASE		0xE000E010
Quoted 8 lines
Quoted 5 lines

arch/arm/cpu/armv7m/systick-timer.c line 26

Quoted 5 lines
arch/arm/cpu/armv7m/systick-timer.c new file +5 -0
+
+struct cm3_systick {
+	uint32_t ctrl;
+	uint32_t reload;
+	uint32_t val;

This register is current timer value, write of any value clears it. Rename to cur_val would avoid any confusion.

Ok, though I'm not sure that cur_val is any clearer :)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/arm/cpu/armv7m/systick-timer.c new file +9 -0
+	uint32_t calibration;
+};
+
+#define TIMER_LOAD_VAL		0x00FFFFFF
+#define SYSTICK_CTRL_EN		BIT(0)
+/* Clock source: 0 = Ref clock, 1 = CPU clock */
+#define SYSTICK_CTRL_CPU_CLK	BIT(2)
+
+/* read the 24-bit timer */
Quoted 8 lines
Quoted 6 lines

arch/arm/cpu/armv7m/systick-timer.c

Quoted 6 lines
arch/arm/cpu/armv7m/systick-timer.c new file +6 -0
+static ulong read_timer(void)
+{
+	struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+	/* The timer counts down, therefore convert to an incrementing timer */
+	return TIMER_LOAD_VAL - readl(&systick->val); }

use mask to be sure of 24bit timer counter value.

Why? It's a 24-bit timer, the other bits are always zero.

Quoted 10 lines
Quoted 7 lines

arch/arm/cpu/armv7m/systick-timer.c line 49

Quoted 7 lines
arch/arm/cpu/armv7m/systick-timer.c new file +7 -0
+
+int timer_init(void)
+{
+	struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
+
+	writel(TIMER_LOAD_VAL, &systick->reload);
+	writel(TIMER_LOAD_VAL, &systick->val);

Writing load value gives the impression that it is load register, in fact it is only clearing countflag.

Ok

Quoted 4 lines
Quoted 2 lines

arch/arm/cpu/armv7m/systick-timer.c line 50

Quoted 2 lines
arch/arm/cpu/armv7m/systick-timer.c new file +2 -0
+
+#ifdef CONFIG_ARMCORTEXM3_SYSTICK_CPU

Cortex M3 systick has only cpu clock source ?

No, it can have cpu clock or another clock external to the M3 core. In our case we use the external clock because it is much slower than the cpu clock, making the timer usable without interrupts. I can't imagine anyone using the cpu clock unless they add code to handle the interrupt. I'll add a comment to that effect.

Thanks
Phil

Quoted 45 lines

Cheers,
Vikas

Quoted 30 lines
Quoted 30 lines
arch/arm/cpu/armv7m/systick-timer.c new file +30 -0
+	/* Use CPU clock, no interrupts */
+	writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
+#else
+	/* Use external clock, no interrupts */
+	writel(SYSTICK_CTRL_EN, &systick->ctrl); #endif
+
+	gd->arch.tbl = 0;
+	gd->arch.tbu = 0;
+	gd->arch.lastinc = read_timer();
+
+	return 0;
+}
+
+/* return milli-seconds timer value */ ulong get_timer(ulong base) {
+	unsigned long long t = get_ticks() * 1000;
+
+	return (ulong)((t / CONFIG_SYS_HZ_CLOCK)) - base; }
+
+unsigned long long get_ticks(void)
+{
+	u32 now = read_timer();
+
+	if (now >= gd->arch.lastinc)
+		gd->arch.tbl += (now - gd->arch.lastinc);
+	else
+		gd->arch.tbl += (TIMER_LOAD_VAL - gd->arch.lastinc) + now;
+
+	gd->arch.lastinc = now;
+
+	return gd->arch.tbl;
Quoted 8 lines

arch/arm/cpu/armv7m/systick-timer.c line 91

Quoted 6 lines
arch/arm/cpu/armv7m/systick-timer.c new file +6 -0
+}
+
+ulong get_tbclk(void)
+{
+	return CONFIG_SYS_HZ_CLOCK;
+}

And (cc'ing maintainers) we could use this in place of arch/arm/mach-

stm32/*/timer.c I assume, yes? Thanks!

Quoted 3 lines

--
Tom