Skip to contentSkip to conversation

Doc: bindings: Add binding doc for nxp system counter timer

PATCH Superseded Jacky Bai 4 participants 8 messages Dec 12, 2018 – Jan 15, 2019

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jacky Bai

author
#0 lore raw

PATCH v2 1/2 Doc: bindings: Add binding doc for nxp system counter timer

Add the binding doc for nxp system counter timer module.

---
change v1->v2
- remove the blank line at EOF

---
 .../devicetree/bindings/timer/nxp,sysctr_timer.txt | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt
Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt new file +26 -0
new file mode 100644
index 0000000..21f1527
@@ -0,0 +1,26 @@
+NXP System Counter Module(sys_ctr)
+
+The system counter(sys_ctr) is a programmable system counter which provides
+a shared time base to Cortex A15, A7, A53, A73, etc. it is intended for use in
+applications where the counter is always powered and support multiple,
+unrelated clocks.
+
+Required properties:
+
+- compatible :      should be "nxp,sysctr-timer"
+- reg :             Specifies the base physical address and size of the comapre
+                    frame and the counter control, read & compare.
+- interrupts :      should be the compare frames' interrupt
+- clock-frequency : Specifies the counter clock frequency.
+
+Example:
+
+	system_counter: timer@306a0000 {
+		compatible = "nxp,sysctr-timer";
+		reg = <0x0 0x306a0000 0x0 0x10000>, /* system-counter-rd base */
+		      <0x0 0x306b0000 0x0 0x10000>, /* system-counter-cmp base */
+		      <0x0 0x306c0000 0x0 0x10000>; /* system-counter-ctrl base */
+		clock-frequency = <8000000>;
+		interrupts = <GIC_SPI 47 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 48 IRQ_TYPE_LEVEL_HIGH>;
+	};
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v2 2/2 driver: clocksource: Add nxp system counter timer driver support

The system counter (sys_ctr) is a programmable system counter which provides a shared time base to the Cortex A15, A7, A53 etc cores. It is intended for use in applications where the counter is always powered on and supports multiple, unrelated clocks. The sys_ctr hardware supports:
- 56-bit counter width (roll-over time greater than 40 years)
- compare frame(64-bit compare value) contains programmable interrupt generation

---
change v1->v2:
- no change

---
 drivers/clocksource/Kconfig            |   8 ++
 drivers/clocksource/Makefile           |   1 +
 drivers/clocksource/timer-imx-sysctr.c | 204 +++++++++++++++++++++++++++++++++
 3 files changed, 213 insertions(+)
 create mode 100644 drivers/clocksource/timer-imx-sysctr.c
drivers/clocksource/Kconfig +8 -0
index c57b156..7e5f2de 100644
@@ -579,6 +579,14 @@ config CLKSRC_IMX_TPM
 	  Enable this option to use IMX Timer/PWM Module (TPM) timer as
 	  clocksource.
 
+config CLKSRC_IMX_SYS_CTR
+	bool "Clocksource using i.MX system counter" if COMPILE_TEST
+	depends on (ARM || ARM64) && CLKDEV_LOOKUP
+	select CLKSRC_MMIO
+	help
+	  Enable this option to use IMX system counter timer as
+	  clocksource.
+
 config CLKSRC_ST_LPC
 	bool "Low power clocksource found in the LPC" if COMPILE_TEST
 	select TIMER_OF if OF
drivers/clocksource/Makefile +1 -0
index dd91381..372bf4e 100644
@@ -71,6 +71,7 @@ obj-$(CONFIG_CLKSRC_MIPS_GIC)		+= mips-gic-timer.o
 obj-$(CONFIG_CLKSRC_TANGO_XTAL)		+= tango_xtal.o
 obj-$(CONFIG_CLKSRC_IMX_GPT)		+= timer-imx-gpt.o
 obj-$(CONFIG_CLKSRC_IMX_TPM)		+= timer-imx-tpm.o
+obj-$(CONFIG_CLKSRC_IMX_SYS_CTR)	+= timer-imx-sysctr.o
 obj-$(CONFIG_ASM9260_TIMER)		+= asm9260_timer.o
 obj-$(CONFIG_H8300_TMR8)		+= h8300_timer8.o
 obj-$(CONFIG_H8300_TMR16)		+= h8300_timer16.o
drivers/clocksource/timer-imx-sysctr.c new file +204 -0

204 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

Hi,

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 583

Quoted 10 lines · Jacky Bai
drivers/clocksource/Kconfig +2 -0
index c57b156..7e5f2de 100644
@@ -579,6 +579,14 @@ config CLKSRC_IMX_TPM
 	  Enable this option to use IMX Timer/PWM Module (TPM) timer as
 	  clocksource.
 
+config CLKSRC_IMX_SYS_CTR
+	bool "Clocksource using i.MX system counter" if COMPILE_TEST

Looks like a "make *config" user only gets this prompt if COMPILE_TEST is set.

If COMPILE_TEST is not set, how does this driver become enabled/set?

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 592

Quoted 9 lines
drivers/clocksource/Kconfig +6 -0
+	depends on (ARM || ARM64) && CLKDEV_LOOKUP
+	select CLKSRC_MMIO
+	help
+	  Enable this option to use IMX system counter timer as
+	  clocksource.
+
 config CLKSRC_ST_LPC
 	bool "Low power clocksource found in the LPC" if COMPILE_TEST
 	select TIMER_OF if OF

thanks,

Signature · 1 line
~Randy
author
#3 lore raw

RE: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

Quoted 14 lines

Subject: Re: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

Hi,

Quoted 8 lines · Jacky Bai
drivers/clocksource/Kconfig +0 -0
index c57b156..7e5f2de 100644
@@ -579,6 +579,14 @@ config CLKSRC_IMX_TPM
 	  Enable this option to use IMX Timer/PWM Module (TPM) timer as
 	  clocksource.
 
Quoted 8 lines

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 583

Quoted 2 lines
drivers/clocksource/Kconfig +2 -0
+config CLKSRC_IMX_SYS_CTR
+	bool "Clocksource using i.MX system counter" if COMPILE_TEST

Looks like a "make *config" user only gets this prompt if COMPILE_TEST is set.

If COMPILE_TEST is not set, how does this driver become enabled/set?

Sorry for reply to you so later. this driver need to be enable explicitly when IMX soc is built.

BR

Quoted 13 lines

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 592

Quoted 9 lines
drivers/clocksource/Kconfig +6 -0
+	depends on (ARM || ARM64) && CLKDEV_LOOKUP
+	select CLKSRC_MMIO
+	help
+	  Enable this option to use IMX system counter timer as
+	  clocksource.
+
 config CLKSRC_ST_LPC
 	bool "Low power clocksource found in the LPC" if COMPILE_TEST
 	select TIMER_OF if OF

thanks,
--
~Randy

author
#4 lore raw

RE: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

In reply to Jacky Bai

Ping...

BR
Jacky Bai

Quoted 264 lines

-----Original Message-----
From: Jacky Bai
Sent: 2018年12月12日 13:28
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Aisheng Dong <[email protected]>
Cc: [email protected]; [email protected]; dl-linux-imx <[email protected]>
Subject: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

The system counter (sys_ctr) is a programmable system counter which provides a shared time base to the Cortex A15, A7, A53 etc cores. It is intended for use in applications where the counter is always powered on and supports multiple, unrelated clocks. The sys_ctr hardware supports:
- 56-bit counter width (roll-over time greater than 40 years)
- compare frame(64-bit compare value) contains programmable interrupt generation

---
change v1->v2:
- no change

---
 drivers/clocksource/Kconfig            |   8 ++
 drivers/clocksource/Makefile           |   1 +
 drivers/clocksource/timer-imx-sysctr.c | 204
+++++++++++++++++++++++++++++++++
 3 files changed, 213 insertions(+)
 create mode 100644 drivers/clocksource/timer-imx-sysctr.c
drivers/clocksource/Kconfig index renamed from drivers/clocksource/Kconfig +0 -0

c57b156..7e5f2de 100644

drivers/clocksource/Kconfig +8 -0
@@ -579,6 +579,14 @@ config CLKSRC_IMX_TPM
 	  Enable this option to use IMX Timer/PWM Module (TPM) timer as
 	  clocksource.
 
+config CLKSRC_IMX_SYS_CTR
+	bool "Clocksource using i.MX system counter" if COMPILE_TEST
+	depends on (ARM || ARM64) && CLKDEV_LOOKUP
+	select CLKSRC_MMIO
+	help
+	  Enable this option to use IMX system counter timer as
+	  clocksource.
+
 config CLKSRC_ST_LPC
 	bool "Low power clocksource found in the LPC" if COMPILE_TEST
 	select TIMER_OF if OF
drivers/clocksource/Makefile +0 -0
index dd91381..372bf4e 100644
@@ -71,6 +71,7 @@ obj-$(CONFIG_CLKSRC_MIPS_GIC)		+=

mips-gic-timer.o
obj-$(CONFIG_CLKSRC_TANGO_XTAL) += tango_xtal.o
obj-$(CONFIG_CLKSRC_IMX_GPT) += timer-imx-gpt.o
obj-$(CONFIG_CLKSRC_IMX_TPM) += timer-imx-tpm.o

drivers/clocksource/Makefile +1 -0
+obj-$(CONFIG_CLKSRC_IMX_SYS_CTR)	+= timer-imx-sysctr.o
 obj-$(CONFIG_ASM9260_TIMER)		+= asm9260_timer.o
 obj-$(CONFIG_H8300_TMR8)		+= h8300_timer8.o
 obj-$(CONFIG_H8300_TMR16)		+= h8300_timer16.o
drivers/clocksource/timer-imx-sysctr.c new file +117 -0
new file mode 100644
index 0000000..ad3c27f
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// Copyright 2017-2018 NXP
+
+#include <linux/interrupt.h>
+#include <linux/clockchips.h>
+#include <linux/clocksource.h>
+#include <linux/delay.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/sched_clock.h>
+
+#define CNTCV_LO	0x8
+#define CNTCV_HI	0xc
+#define CMPCV_LO	0x20
+#define CMPCV_HI	0x24
+#define CMPCR		0x2c
+
+#define SYS_CTR_EN		0x1
+#define SYS_CTR_IRQ_MASK	0x2
+
+static void __iomem *sys_ctr_rd_base;
+static void __iomem *sys_ctr_cmp_base;
+static struct clock_event_device clockevent_sysctr;
+
+static inline void sysctr_timer_enable(bool enable) {
+	u32 val;
+
+	val = readl(sys_ctr_cmp_base + CMPCR);
+	val &= ~SYS_CTR_EN;
+	if (enable)
+		val |= SYS_CTR_EN;
+
+	writel(val, sys_ctr_cmp_base + CMPCR); }
+
+static void sysctr_irq_acknowledge(void) {
+	u32 val;
+
+	/* clear th enable bit(EN=0) to clear the ISTAT */
+	val = readl(sys_ctr_cmp_base + CMPCR);
+	val &= ~SYS_CTR_EN;
+	writel(val, sys_ctr_cmp_base + CMPCR); }
+
+static inline u64 sysctr_read_counter(void) {
+	u32 cnt_hi, tmp_hi, cnt_lo;
+
+	do {
+		cnt_hi = readl_relaxed(sys_ctr_rd_base + CNTCV_HI);
+		cnt_lo = readl_relaxed(sys_ctr_rd_base + CNTCV_LO);
+		tmp_hi = readl_relaxed(sys_ctr_rd_base + CNTCV_HI);
+	} while (tmp_hi != cnt_hi);
+
+	return  ((u64) cnt_hi << 32) | cnt_lo; }
+
+static u64 notrace sysctr_read_sched_clock(void) {
+	return sysctr_read_counter();
+}
+
+static u64 sysctr_clocksource_read(struct clocksource *cs) {
+	return sysctr_read_counter();
+}
+
+static int __init sysctr_clocksource_init(unsigned int rate) {
+	sched_clock_register(sysctr_read_sched_clock, 56, rate);
+	return clocksource_mmio_init(sys_ctr_rd_base, "i.MX sys_ctr",
+				     rate, 200, 56, sysctr_clocksource_read); }
+
+static int sysctr_set_next_event(unsigned long delta,
+				 struct clock_event_device *evt)
+{
+	u32 cmp_hi, cmp_lo;
+	u64 next;
+
+	sysctr_timer_enable(false);
+
+	next = sysctr_read_counter();
+
+	next += delta;
+
+	cmp_hi = (next >> 32) & 0x00fffff;
+	cmp_lo = next & 0xffffffff;
+
+	writel_relaxed(cmp_hi, sys_ctr_cmp_base + CMPCV_HI);
+	writel_relaxed(cmp_lo, sys_ctr_cmp_base + CMPCV_LO);
+
+	sysctr_timer_enable(true);
+
+	return 0;
+}
+
+static int sysctr_set_state_oneshot(struct clock_event_device *evt) {
+	/* enable timer */
+	sysctr_timer_enable(true);
+
+	return 0;
+}
+
+static int sysctr_set_state_shutdown(struct clock_event_device *evt) {
+	/* disable the timer */
+	sysctr_timer_enable(false);
+
+	return 0;
+}
+
+static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id) {
+	struct clock_event_device *evt = &clockevent_sysctr;
+
+	sysctr_irq_acknowledge();
+
+	evt->event_handler(evt);
+
+	return IRQ_HANDLED;
+}
+
+static struct clock_event_device clockevent_sysctr = {
+	.name			= "i.MX system counter timer",
+	.features		= CLOCK_EVT_FEAT_ONESHOT |

CLOCK_EVT_FEAT_DYNIRQ,

drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+	.set_state_oneshot	= sysctr_set_state_oneshot,
+	.set_next_event		= sysctr_set_next_event,
+	.set_state_shutdown	= sysctr_set_state_shutdown,
+	.rating			= 200,
+};
+
+static int __init sysctr_clockevent_init(unsigned long rate, int irq) {
+	int ret;
+
+	ret = request_irq(irq, sysctr_timer_interrupt, IRQF_TIMER |

IRQF_IRQPOLL,

drivers/clocksource/timer-imx-sysctr.c new file +62 -1
+		    "i.MX system counter timer", &clockevent_sysctr);
+	if (ret) {
+		pr_err("Failed to request i.MX sysctr timer irq\n");
+		return ret;
+	}
+
+	clockevent_sysctr.cpumask = cpumask_of(0);
+	clockevent_sysctr.irq = irq;
+	clockevents_config_and_register(&clockevent_sysctr,
+			rate, 0xff, 0x7fffffff);
+
+	return 0;
+}
+
+static int __init sysctr_timer_init(struct device_node *np) {
+	u32 rate;
+	int irq, ret = 0;
+
+	/* map the system counter's CNTreadbase */
+	sys_ctr_rd_base = of_iomap(np, 0);
+	if (!sys_ctr_rd_base) {
+		pr_err("Failed to map sys_ctr rd base%pOF\n", np);
+		return -ENXIO;
+	}
+
+	/* map the system counter's CNTcomparebase */
+	sys_ctr_cmp_base = of_iomap(np, 1);
+	if (!sys_ctr_cmp_base) {
+		pr_err("Failed to map sys_ctr compare base%pOF\n", np);
+		ret = -ENXIO;
+		goto out_free2;
+	}
+
+	/*
+	 * the purpose of this driver is to provide a global timer,
+	 * So only use one compare frame, request frame0's irq only.
+	 */
+	irq = irq_of_parse_and_map(np, 0);
+	if (!irq) {
+		pr_err("Failed to map interrupt for %pOF\n", np);
+		ret = -EINVAL;
+		goto out_free1;
+	}
+
+	if (of_property_read_u32(np, "clock-frequency", &rate)) {
+		pr_err("Failed to get clock frequency %pOF\n", np);
+		ret = -EINVAL;
+		goto out_free1;
+	}
+
+	sysctr_clocksource_init(rate);
+	sysctr_clockevent_init(rate, irq);
+
+	return 0;
+
+out_free1:
+	iounmap(sys_ctr_cmp_base);
+out_free2:
+	iounmap(sys_ctr_rd_base);
+	return ret;
+}
+TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer", sysctr_timer_init);
--

1.9.1

#5 lore raw

Re: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

In reply to Jacky Bai
Quoted 25 lines · Jacky Bai

The system counter (sys_ctr) is a programmable system counter which provides a shared time base to the Cortex A15, A7, A53 etc cores. It is intended for use in applications where the counter is always powered on and supports multiple, unrelated clocks. The sys_ctr hardware supports:
- 56-bit counter width (roll-over time greater than 40 years)
- compare frame(64-bit compare value) contains programmable interrupt generation

---
change v1->v2:
- no change

---
 drivers/clocksource/Kconfig            |   8 ++
 drivers/clocksource/Makefile           |   1 +
 drivers/clocksource/timer-imx-sysctr.c | 204 +++++++++++++++++++++++++++++++++
 3 files changed, 213 insertions(+)
 create mode 100644 drivers/clocksource/timer-imx-sysctr.c
drivers/clocksource/Kconfig +0 -0
index c57b156..7e5f2de 100644

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 582

Quoted 4 lines
drivers/clocksource/Kconfig +1 -0
@@ -579,6 +579,14 @@ config CLKSRC_IMX_TPM
 	  Enable this option to use IMX Timer/PWM Module (TPM) timer as
 	  clocksource.
 
+config CLKSRC_IMX_SYS_CTR

As the driver is a timer, please replace the CLKSRC by TIMER

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 584

Quoted 2 lines
drivers/clocksource/Kconfig +2 -0
+	bool "Clocksource using i.MX system counter" if COMPILE_TEST
+	depends on (ARM || ARM64) && CLKDEV_LOOKUP

CLKDEV_LOOKUP is not needed, it is selected with COMMON_CLK

Quoted 42 lines
drivers/clocksource/Kconfig +5 -0
+	select CLKSRC_MMIO
+	help
+	  Enable this option to use IMX system counter timer as
+	  clocksource.
+
 config CLKSRC_ST_LPC
 	bool "Low power clocksource found in the LPC" if COMPILE_TEST
 	select TIMER_OF if OF
drivers/clocksource/Makefile +1 -0
index dd91381..372bf4e 100644
@@ -71,6 +71,7 @@ obj-$(CONFIG_CLKSRC_MIPS_GIC)		+= mips-gic-timer.o
 obj-$(CONFIG_CLKSRC_TANGO_XTAL)		+= tango_xtal.o
 obj-$(CONFIG_CLKSRC_IMX_GPT)		+= timer-imx-gpt.o
 obj-$(CONFIG_CLKSRC_IMX_TPM)		+= timer-imx-tpm.o
+obj-$(CONFIG_CLKSRC_IMX_SYS_CTR)	+= timer-imx-sysctr.o
 obj-$(CONFIG_ASM9260_TIMER)		+= asm9260_timer.o
 obj-$(CONFIG_H8300_TMR8)		+= h8300_timer8.o
 obj-$(CONFIG_H8300_TMR16)		+= h8300_timer16.o
drivers/clocksource/timer-imx-sysctr.c new file +16 -0
new file mode 100644
index 0000000..ad3c27f
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// Copyright 2017-2018 NXP
+
+#include <linux/interrupt.h>
+#include <linux/clockchips.h>
+#include <linux/clocksource.h>
+#include <linux/delay.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/sched_clock.h>
+
+#define CNTCV_LO	0x8
+#define CNTCV_HI	0xc
+#define CMPCV_LO	0x20
+#define CMPCV_HI	0x24

drivers/clocksource/timer-imx-sysctr.c line 26

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+#define CMPCR		0x2c
+
+#define SYS_CTR_EN		0x1
+#define SYS_CTR_IRQ_MASK	0x2
+
+static void __iomem *sys_ctr_rd_base;
+static void __iomem *sys_ctr_cmp_base;
+static struct clock_event_device clockevent_sysctr;
+
+static inline void sysctr_timer_enable(bool enable)

Remove 'inline' tag

Quoted 6 lines
drivers/clocksource/timer-imx-sysctr.c new file +6 -0
+{
+	u32 val;
+
+	val = readl(sys_ctr_cmp_base + CMPCR);
+	val &= ~SYS_CTR_EN;
+	if (enable)

drivers/clocksource/timer-imx-sysctr.c line 42

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+		val |= SYS_CTR_EN;
+
+	writel(val, sys_ctr_cmp_base + CMPCR);
+}
+
+static void sysctr_irq_acknowledge(void)
+{
+	u32 val;
+
+	/* clear th enable bit(EN=0) to clear the ISTAT */

What is the ISTAT?

Thanks for adding comments, however the format is:

/*
*
*/

drivers/clocksource/timer-imx-sysctr.c line 35

Quoted 3 lines
drivers/clocksource/timer-imx-sysctr.c new file +3 -0
+	val = readl(sys_ctr_cmp_base + CMPCR);
+	val &= ~SYS_CTR_EN;
+	writel(val, sys_ctr_cmp_base + CMPCR);

This is duplicate with the sysctr_timer_enable() function, call it instead of duplicating code.

Quoted 4 lines
drivers/clocksource/timer-imx-sysctr.c new file +4 -0
+}
+
+static inline u64 sysctr_read_counter(void)
+{

drivers/clocksource/timer-imx-sysctr.c line 59

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+	u32 cnt_hi, tmp_hi, cnt_lo;
+
+	do {
+		cnt_hi = readl_relaxed(sys_ctr_rd_base + CNTCV_HI);
+		cnt_lo = readl_relaxed(sys_ctr_rd_base + CNTCV_LO);
+		tmp_hi = readl_relaxed(sys_ctr_rd_base + CNTCV_HI);
+	} while (tmp_hi != cnt_hi);
+
+	return  ((u64) cnt_hi << 32) | cnt_lo;
+}

It has been showed a 64bit counter usage adds a non negligible overhead, especially on 32bits. Are you really sure you want this ?

Quoted 33 lines
drivers/clocksource/timer-imx-sysctr.c new file +33 -0
+static u64 notrace sysctr_read_sched_clock(void)
+{
+	return sysctr_read_counter();
+}
+
+static u64 sysctr_clocksource_read(struct clocksource *cs)
+{
+	return sysctr_read_counter();
+}
+
+static int __init sysctr_clocksource_init(unsigned int rate)
+{
+	sched_clock_register(sysctr_read_sched_clock, 56, rate);
+	return clocksource_mmio_init(sys_ctr_rd_base, "i.MX sys_ctr",
+				     rate, 200, 56, sysctr_clocksource_read);
+}
+
+static int sysctr_set_next_event(unsigned long delta,
+				 struct clock_event_device *evt)
+{
+	u32 cmp_hi, cmp_lo;
+	u64 next;
+
+	sysctr_timer_enable(false);
+
+	next = sysctr_read_counter();
+
+	next += delta;
+
+	cmp_hi = (next >> 32) & 0x00fffff;
+	cmp_lo = next & 0xffffffff;
+
+	writel_relaxed(cmp_hi, sys_ctr_cmp_base + CMPCV_HI);

drivers/clocksource/timer-imx-sysctr.c line 103

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+	writel_relaxed(cmp_lo, sys_ctr_cmp_base + CMPCV_LO);
+
+	sysctr_timer_enable(true);
+
+	return 0;
+}
+
+static int sysctr_set_state_oneshot(struct clock_event_device *evt)
+{
+	/* enable timer */

The function name is explicit enough, no need to add this comment

drivers/clocksource/timer-imx-sysctr.c line 111

Quoted 8 lines
drivers/clocksource/timer-imx-sysctr.c new file +8 -0
+	sysctr_timer_enable(true);
+
+	return 0;
+}
+
+static int sysctr_set_state_shutdown(struct clock_event_device *evt)
+{
+	/* disable the timer */

Ditto

Quoted 39 lines
drivers/clocksource/timer-imx-sysctr.c new file +39 -0
+	sysctr_timer_enable(false);
+
+	return 0;
+}
+
+static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id)
+{
+	struct clock_event_device *evt = &clockevent_sysctr;
+
+	sysctr_irq_acknowledge();
+
+	evt->event_handler(evt);
+
+	return IRQ_HANDLED;
+}
+
+static struct clock_event_device clockevent_sysctr = {
+	.name			= "i.MX system counter timer",
+	.features		= CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ,
+	.set_state_oneshot	= sysctr_set_state_oneshot,
+	.set_next_event		= sysctr_set_next_event,
+	.set_state_shutdown	= sysctr_set_state_shutdown,
+	.rating			= 200,
+};
+
+static int __init sysctr_clockevent_init(unsigned long rate, int irq)
+{
+	int ret;
+
+	ret = request_irq(irq, sysctr_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
+		    "i.MX system counter timer", &clockevent_sysctr);
+	if (ret) {
+		pr_err("Failed to request i.MX sysctr timer irq\n");
+		return ret;
+	}
+
+	clockevent_sysctr.cpumask = cpumask_of(0);
+	clockevent_sysctr.irq = irq;
+	clockevents_config_and_register(&clockevent_sysctr,

drivers/clocksource/timer-imx-sysctr.c line 161

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+			rate, 0xff, 0x7fffffff);
+	return 0;
+}
+
+static int __init sysctr_timer_init(struct device_node *np)
+{
+	u32 rate;
+	int irq, ret = 0;
+
+	/* map the system counter's CNTreadbase */

Comment not useful, remove it

drivers/clocksource/timer-imx-sysctr.c line 164

Quoted 3 lines
drivers/clocksource/timer-imx-sysctr.c new file +3 -0
+	sys_ctr_rd_base = of_iomap(np, 0);
+	if (!sys_ctr_rd_base) {
+		pr_err("Failed to map sys_ctr rd base%pOF\n", np);

nit: space missing after 'base'

drivers/clocksource/timer-imx-sysctr.c line 168

Quoted 4 lines
drivers/clocksource/timer-imx-sysctr.c new file +4 -0
+		return -ENXIO;
+	}
+
+	/* map the system counter's CNTcomparebase */

Remove comment

Quoted 13 lines
drivers/clocksource/timer-imx-sysctr.c new file +13 -0
+	sys_ctr_cmp_base = of_iomap(np, 1);
+	if (!sys_ctr_cmp_base) {
+		pr_err("Failed to map sys_ctr compare base%pOF\n", np);
+		ret = -ENXIO;
+		goto out_free2;
+	}
+
+	/*
+	 * the purpose of this driver is to provide a global timer,
+	 * So only use one compare frame, request frame0's irq only.
+	 */
+	irq = irq_of_parse_and_map(np, 0);
+	if (!irq) {

drivers/clocksource/timer-imx-sysctr.c line 191

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+		pr_err("Failed to map interrupt for %pOF\n", np);
+		ret = -EINVAL;
+		goto out_free1;
+	}
+
+	if (of_property_read_u32(np, "clock-frequency", &rate)) {
+		pr_err("Failed to get clock frequency %pOF\n", np);
+		ret = -EINVAL;
+		goto out_free1;
+	}

Please replace it with a 'fixed-clock' in the DT and use the clk API.

In addition you can use the timer-of API (cf timer-of.h)

drivers/clocksource/timer-imx-sysctr.c line 198

Quoted 7 lines
drivers/clocksource/timer-imx-sysctr.c new file +7 -0
+
+	sysctr_clocksource_init(rate);
+	sysctr_clockevent_init(rate, irq);
+
+	return 0;
+
+out_free1:

label names are inadequate, use out_iounmap_cmp: for example

Quoted 7 lines
drivers/clocksource/timer-imx-sysctr.c new file +6 -0
+	iounmap(sys_ctr_cmp_base);
+out_free2:
+	iounmap(sys_ctr_rd_base);
+	return ret;
+}
+TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer", sysctr_timer_init);
Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#6 lore raw

RE: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

Quoted 34 lines

Subject: Re: [PATCH v2 2/2] driver: clocksource: Add nxp system counter timer driver support

Quoted 30 lines · Jacky Bai

The system counter (sys_ctr) is a programmable system counter which provides a shared time base to the Cortex A15, A7, A53 etc cores. It is intended for use in applications where the counter is always powered on and supports multiple, unrelated clocks. The sys_ctr hardware
supports:
- 56-bit counter width (roll-over time greater than 40 years)
- compare frame(64-bit compare value) contains programmable interrupt generation

---
change v1->v2:
- no change

---
 drivers/clocksource/Kconfig            |   8 ++
 drivers/clocksource/Makefile           |   1 +
 drivers/clocksource/timer-imx-sysctr.c | 204
+++++++++++++++++++++++++++++++++
 3 files changed, 213 insertions(+)
 create mode 100644 drivers/clocksource/timer-imx-sysctr.c
drivers/clocksource/Kconfig +0 -0
index c57b156..7e5f2de 100644
@@ -579,6 +579,14 @@ config CLKSRC_IMX_TPM
 	  Enable this option to use IMX Timer/PWM Module (TPM) timer as
 	  clocksource.
 
Quoted 3 lines

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 582

Quoted 1 line
drivers/clocksource/Kconfig +1 -0
+config CLKSRC_IMX_SYS_CTR

As the driver is a timer, please replace the CLKSRC by TIMER

Ok, will fix it.

Quoted 54 lines

drivers/clocksource/Kconfig config CLKSRC_IMX_TPM · line 584

Quoted 3 lines
drivers/clocksource/Kconfig +2 -0
+	bool "Clocksource using i.MX system counter" if COMPILE_TEST
+	depends on (ARM || ARM64) && CLKDEV_LOOKUP

CLKDEV_LOOKUP is not needed, it is selected with COMMON_CLK

drivers/clocksource/Makefile obj-$(CONFIG_CLKSRC_MIPS_GIC) +=

Quoted 13 lines
drivers/clocksource/Kconfig +5 -0
+	select CLKSRC_MMIO
+	help
+	  Enable this option to use IMX system counter timer as
+	  clocksource.
+
 config CLKSRC_ST_LPC
 	bool "Low power clocksource found in the LPC" if COMPILE_TEST
 	select TIMER_OF if OF
(quoted diff) +0 -0

b/drivers/clocksource/Makefile index dd91381..372bf4e 100644

drivers/clocksource/Makefile +0 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_CLKSRC_MIPS_GIC)		+=

mips-gic-timer.o

Quoted 34 lines
drivers/clocksource/Makefile +1 -0
 obj-$(CONFIG_CLKSRC_TANGO_XTAL)		+= tango_xtal.o
 obj-$(CONFIG_CLKSRC_IMX_GPT)		+= timer-imx-gpt.o
 obj-$(CONFIG_CLKSRC_IMX_TPM)		+= timer-imx-tpm.o
+obj-$(CONFIG_CLKSRC_IMX_SYS_CTR)	+= timer-imx-sysctr.o
 obj-$(CONFIG_ASM9260_TIMER)		+= asm9260_timer.o
 obj-$(CONFIG_H8300_TMR8)		+= h8300_timer8.o
 obj-$(CONFIG_H8300_TMR16)		+= h8300_timer16.o
drivers/clocksource/timer-imx-sysctr.c new file +20 -0
new file mode 100644
index 0000000..ad3c27f
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// Copyright 2017-2018 NXP
+
+#include <linux/interrupt.h>
+#include <linux/clockchips.h>
+#include <linux/clocksource.h>
+#include <linux/delay.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/sched_clock.h>
+
+#define CNTCV_LO	0x8
+#define CNTCV_HI	0xc
+#define CMPCV_LO	0x20
+#define CMPCV_HI	0x24
+#define CMPCR		0x2c
+
+#define SYS_CTR_EN		0x1
+#define SYS_CTR_IRQ_MASK	0x2
Quoted 9 lines

drivers/clocksource/timer-imx-sysctr.c line 26

Quoted 6 lines
drivers/clocksource/timer-imx-sysctr.c new file +6 -0
+
+static void __iomem *sys_ctr_rd_base; static void __iomem
+*sys_ctr_cmp_base; static struct clock_event_device
+clockevent_sysctr;
+
+static inline void sysctr_timer_enable(bool enable)

Remove 'inline' tag

OK.

Quoted 17 lines
Quoted 4 lines
drivers/clocksource/timer-imx-sysctr.c new file +4 -0
+{
+	u32 val;
+
+	val = readl(sys_ctr_cmp_base + CMPCR);

drivers/clocksource/timer-imx-sysctr.c line 42

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+	val &= ~SYS_CTR_EN;
+	if (enable)
+		val |= SYS_CTR_EN;
+
+	writel(val, sys_ctr_cmp_base + CMPCR); }
+
+static void sysctr_irq_acknowledge(void) {
+	u32 val;
+
+	/* clear th enable bit(EN=0) to clear the ISTAT */

What is the ISTAT?

Quoted 6 lines

Thanks for adding comments, however the format is:

/*
*
*/

Th ISTAT is interrupt status bit. I will fix it.

Quoted 7 lines

drivers/clocksource/timer-imx-sysctr.c line 35

Quoted 3 lines
drivers/clocksource/timer-imx-sysctr.c new file +3 -0
+	val = readl(sys_ctr_cmp_base + CMPCR);
+	val &= ~SYS_CTR_EN;
+	writel(val, sys_ctr_cmp_base + CMPCR);

This is duplicate with the sysctr_timer_enable() function, call it instead of duplicating code.

Sure.

Quoted 7 lines
Quoted 7 lines
drivers/clocksource/timer-imx-sysctr.c new file +7 -0
+}
+
+static inline u64 sysctr_read_counter(void) {
+	u32 cnt_hi, tmp_hi, cnt_lo;
+
+	do {
+		cnt_hi = readl_relaxed(sys_ctr_rd_base + CNTCV_HI);
Quoted 9 lines

drivers/clocksource/timer-imx-sysctr.c

Quoted 5 lines
drivers/clocksource/timer-imx-sysctr.c new file +5 -0
+		cnt_lo = readl_relaxed(sys_ctr_rd_base + CNTCV_LO);
+		tmp_hi = readl_relaxed(sys_ctr_rd_base + CNTCV_HI);
+	} while (tmp_hi != cnt_hi);
+
+	return  ((u64) cnt_hi << 32) | cnt_lo; }

It has been showed a 64bit counter usage adds a non negligible overhead, especially on 32bits. Are you really sure you want this ?

This HW is designed as 56 bit counter & timer. when doing timer 'set_next_event', The 56bit counter value must be read out, and then adding the delta value to set the next event. Can you give me some suggestion on how to handle this?

Quoted 32 lines
Quoted 32 lines
drivers/clocksource/timer-imx-sysctr.c new file +32 -0
+static u64 notrace sysctr_read_sched_clock(void) {
+	return sysctr_read_counter();
+}
+
+static u64 sysctr_clocksource_read(struct clocksource *cs) {
+	return sysctr_read_counter();
+}
+
+static int __init sysctr_clocksource_init(unsigned int rate) {
+	sched_clock_register(sysctr_read_sched_clock, 56, rate);
+	return clocksource_mmio_init(sys_ctr_rd_base, "i.MX sys_ctr",
+				     rate, 200, 56, sysctr_clocksource_read); }
+
+static int sysctr_set_next_event(unsigned long delta,
+				 struct clock_event_device *evt)
+{
+	u32 cmp_hi, cmp_lo;
+	u64 next;
+
+	sysctr_timer_enable(false);
+
+	next = sysctr_read_counter();
+
+	next += delta;
+
+	cmp_hi = (next >> 32) & 0x00fffff;
+	cmp_lo = next & 0xffffffff;
+
+	writel_relaxed(cmp_hi, sys_ctr_cmp_base + CMPCV_HI);
+	writel_relaxed(cmp_lo, sys_ctr_cmp_base + CMPCV_LO);
+
+	sysctr_timer_enable(true);
Quoted 9 lines

drivers/clocksource/timer-imx-sysctr.c line 103

Quoted 6 lines
drivers/clocksource/timer-imx-sysctr.c new file +6 -0
+
+	return 0;
+}
+
+static int sysctr_set_state_oneshot(struct clock_event_device *evt) {
+	/* enable timer */

The function name is explicit enough, no need to add this comment

OK, will remove it in V3.

Quoted 102 lines

drivers/clocksource/timer-imx-sysctr.c line 111

Quoted 8 lines
drivers/clocksource/timer-imx-sysctr.c new file +8 -0
+	sysctr_timer_enable(true);
+
+	return 0;
+}
+
+static int sysctr_set_state_shutdown(struct clock_event_device *evt)
+{
+	/* disable the timer */

Ditto

Quoted 8 lines
drivers/clocksource/timer-imx-sysctr.c new file +8 -0
+	sysctr_timer_enable(false);
+
+	return 0;
+}
+
+static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id) {
+	struct clock_event_device *evt = &clockevent_sysctr;
+

drivers/clocksource/timer-imx-sysctr.c

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+	sysctr_irq_acknowledge();
+
+	evt->event_handler(evt);
+
+	return IRQ_HANDLED;
+}
+
+static struct clock_event_device clockevent_sysctr = {
+	.name			= "i.MX system counter timer",
+	.features		= CLOCK_EVT_FEAT_ONESHOT |

CLOCK_EVT_FEAT_DYNIRQ,

drivers/clocksource/timer-imx-sysctr.c

Quoted 11 lines
drivers/clocksource/timer-imx-sysctr.c new file +11 -0
+	.set_state_oneshot	= sysctr_set_state_oneshot,
+	.set_next_event		= sysctr_set_next_event,
+	.set_state_shutdown	= sysctr_set_state_shutdown,
+	.rating			= 200,
+};
+
+static int __init sysctr_clockevent_init(unsigned long rate, int irq)
+{
+	int ret;
+
+	ret = request_irq(irq, sysctr_timer_interrupt, IRQF_TIMER |

IRQF_IRQPOLL,

Quoted 8 lines
drivers/clocksource/timer-imx-sysctr.c new file +8 -0
+		    "i.MX system counter timer", &clockevent_sysctr);
+	if (ret) {
+		pr_err("Failed to request i.MX sysctr timer irq\n");
+		return ret;
+	}
+
+	clockevent_sysctr.cpumask = cpumask_of(0);
+	clockevent_sysctr.irq = irq;

drivers/clocksource/timer-imx-sysctr.c line 161

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+	clockevents_config_and_register(&clockevent_sysctr,
+			rate, 0xff, 0x7fffffff);
+	return 0;
+}
+
+static int __init sysctr_timer_init(struct device_node *np) {
+	u32 rate;
+	int irq, ret = 0;
+
+	/* map the system counter's CNTreadbase */

Comment not useful, remove it

drivers/clocksource/timer-imx-sysctr.c line 164

Quoted 3 lines
drivers/clocksource/timer-imx-sysctr.c new file +3 -0
+	sys_ctr_rd_base = of_iomap(np, 0);
+	if (!sys_ctr_rd_base) {
+		pr_err("Failed to map sys_ctr rd base%pOF\n", np);

nit: space missing after 'base'

drivers/clocksource/timer-imx-sysctr.c line 168

Quoted 4 lines
drivers/clocksource/timer-imx-sysctr.c new file +4 -0
+		return -ENXIO;
+	}
+
+	/* map the system counter's CNTcomparebase */

Remove comment

Quoted 13 lines
drivers/clocksource/timer-imx-sysctr.c new file +13 -0
+	sys_ctr_cmp_base = of_iomap(np, 1);
+	if (!sys_ctr_cmp_base) {
+		pr_err("Failed to map sys_ctr compare base%pOF\n", np);
+		ret = -ENXIO;
+		goto out_free2;
+	}
+
+	/*
+	 * the purpose of this driver is to provide a global timer,
+	 * So only use one compare frame, request frame0's irq only.
+	 */
+	irq = irq_of_parse_and_map(np, 0);
+	if (!irq) {

drivers/clocksource/timer-imx-sysctr.c line 191

Quoted 10 lines
drivers/clocksource/timer-imx-sysctr.c new file +10 -0
+		pr_err("Failed to map interrupt for %pOF\n", np);
+		ret = -EINVAL;
+		goto out_free1;
+	}
+
+	if (of_property_read_u32(np, "clock-frequency", &rate)) {
+		pr_err("Failed to get clock frequency %pOF\n", np);
+		ret = -EINVAL;
+		goto out_free1;
+	}

Please replace it with a 'fixed-clock' in the DT and use the clk API.

Quoted 2 lines

In addition you can use the timer-of API (cf timer-of.h)

Thanks, I will fix it in V3.

Quoted 10 lines

drivers/clocksource/timer-imx-sysctr.c line 198

Quoted 7 lines
drivers/clocksource/timer-imx-sysctr.c new file +7 -0
+
+	sysctr_clocksource_init(rate);
+	sysctr_clockevent_init(rate, irq);
+
+	return 0;
+
+out_free1:

label names are inadequate, use out_iounmap_cmp: for example

Will fix it in V3.

BR

Quoted 8 lines
Quoted 8 lines
drivers/clocksource/timer-imx-sysctr.c new file +7 -0
+	iounmap(sys_ctr_cmp_base);
+out_free2:
+	iounmap(sys_ctr_rd_base);
+	return ret;
+}
+TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer",
+sysctr_timer_init);
maintainer
#7 lore raw
In reply to Jacky Bai
Quoted 17 lines · Jacky Bai

Add the binding doc for nxp system counter timer module.

---
change v1->v2
- remove the blank line at EOF

---
 .../devicetree/bindings/timer/nxp,sysctr_timer.txt | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt
Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt new file +0 -0
new file mode 100644
index 0000000..21f1527

Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt line 10

Quoted 10 lines
Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt new file +10 -0
@@ -0,0 +1,26 @@
+NXP System Counter Module(sys_ctr)
+
+The system counter(sys_ctr) is a programmable system counter which provides
+a shared time base to Cortex A15, A7, A53, A73, etc. it is intended for use in
+applications where the counter is always powered and support multiple,
+unrelated clocks.
+
+Required properties:
+
+- compatible :      should be "nxp,sysctr-timer"

Only 1 version? Should be SoC specific.

Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt line 11

Quoted 1 line
Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt new file +1 -0
+- reg :             Specifies the base physical address and size of the comapre

s/comapre/compare/

Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt line 22

Quoted 11 lines
Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt new file +11 -0
+                    frame and the counter control, read & compare.
+- interrupts :      should be the compare frames' interrupt
+- clock-frequency : Specifies the counter clock frequency.
+
+Example:
+
+	system_counter: timer@306a0000 {
+		compatible = "nxp,sysctr-timer";
+		reg = <0x0 0x306a0000 0x0 0x10000>, /* system-counter-rd base */
+		      <0x0 0x306b0000 0x0 0x10000>, /* system-counter-cmp base */
+		      <0x0 0x306c0000 0x0 0x10000>; /* system-counter-ctrl base */

Are these really 64K in length? That wastes virtual space which can be important on 32-bit systems.

Quoted 7 lines
Documentation/devicetree/bindings/timer/nxp,sysctr_timer.txt new file +4 -0
+		clock-frequency = <8000000>;
+		interrupts = <GIC_SPI 47 IRQ_TYPE_LEVEL_HIGH>,
+			     <GIC_SPI 48 IRQ_TYPE_LEVEL_HIGH>;
+	};
Signature · 1 line
1.9.1