All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonas Jensen <jonas.jensen@gmail.com>
To: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org, arm@kernel.org,
	john.stultz@linaro.org, tglx@linutronix.de,
	u.kleine-koenig@pengutronix.de, tomasz.figa@gmail.com,
	linus.walleij@linaro.org, thomas.petazzoni@free-electrons.com,
	arnd@arndb.de, Jonas Jensen <jonas.jensen@gmail.com>
Subject: [PATCH v3] ARM: clocksource: add support for MOXA ART SoCs
Date: Thu, 27 Jun 2013 13:23:23 +0200	[thread overview]
Message-ID: <1372332203-30228-1-git-send-email-jonas.jensen@gmail.com> (raw)
In-Reply-To: <1372258383-24524-1-git-send-email-jonas.jensen@gmail.com>

This patch adds an clocksource driver for the main timer(s)
found on MOXA ART SoCs.

Signed-off-by: Jonas Jensen <jonas.jensen@gmail.com>
---

Notes:
    Applies to next-20130619
    
    Changes since v2:
    
    1. remove unused defines: MAX_TIMER, USED_TIMER, APB_CLK
    2. split register defines so each timer has a base
    3. use standard multiline comments
    4. replace whitespaces with tabs
    5. rename "timer_base" "base"
    6. rename "clock_frequency" "clock_count_per_tick"
    7. disable TIMER1 in CLOCK_EVT_MODE_ONESHOT
    8. remove pr_debug:s
    9. in CLOCK_EVT_MODE_PERIODIC, switch order, write clock_count_per_tick
       to TIMER1 LOAD and enable it
    10. enable TIMER1 in moxart_clkevt_next_event
    11. remove IRQF_DISABLED, IRQF_IRQPOLL from irqaction flags
    12. set clock_frequency exclusively from system clock
    13. bail if of_clk_get fails
    14. assign clock_count_per_tick to DIV_ROUND_CLOSEST(pclk, HZ)
    15. align continuation lines with matching opening brace

 drivers/clocksource/Makefile       |   1 +
 drivers/clocksource/moxart_timer.c | 166 +++++++++++++++++++++++++++++++++++++
 2 files changed, 167 insertions(+)
 create mode 100644 drivers/clocksource/moxart_timer.c

diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
index 8d979c7..c93e1a8 100644
--- a/drivers/clocksource/Makefile
+++ b/drivers/clocksource/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_CLKSRC_SAMSUNG_PWM)	+= samsung_pwm_timer.o
 
 obj-$(CONFIG_ARM_ARCH_TIMER)		+= arm_arch_timer.o
 obj-$(CONFIG_CLKSRC_METAG_GENERIC)	+= metag_generic.o
+obj-$(CONFIG_ARCH_MOXART)	+= moxart_timer.o
diff --git a/drivers/clocksource/moxart_timer.c b/drivers/clocksource/moxart_timer.c
new file mode 100644
index 0000000..4678b30
--- /dev/null
+++ b/drivers/clocksource/moxart_timer.c
@@ -0,0 +1,166 @@
+/*
+ * MOXA ART SoCs timer handling.
+ *
+ * Copyright (C) 2013 Jonas Jensen
+ *
+ * Jonas Jensen <jonas.jensen@gmail.com>
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2.  This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/clk.h>
+#include <linux/clockchips.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqreturn.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/io.h>
+#include <linux/clocksource.h>
+
+#define TIMER1_BASE			0x00
+#define TIMER2_BASE			0x10
+#define TIMER3_BASE			0x20
+
+#define REG_COUNT			0x0 /* writable */
+#define REG_LOAD			0x4
+#define REG_MATCH1			0x8
+#define REG_MATCH2			0xC
+
+#define TIMER_CR			0x30
+#define TIMER_INTR_STATE	0x34
+#define TIMER_INTR_MASK		0x38
+
+/*
+ * TIMER_CR flags:
+ *
+ * TIMEREG_CR_*_CLOCK	0: PCLK, 1: EXT1CLK
+ * TIMEREG_CR_*_INT		overflow interrupt enable bit
+ */
+#define TIMEREG_CR_1_ENABLE		(1 << 0)
+#define TIMEREG_CR_1_CLOCK		(1 << 1)
+#define TIMEREG_CR_1_INT		(1 << 2)
+#define TIMEREG_CR_2_ENABLE		(1 << 3)
+#define TIMEREG_CR_2_CLOCK		(1 << 4)
+#define TIMEREG_CR_2_INT		(1 << 5)
+#define TIMEREG_CR_3_ENABLE		(1 << 6)
+#define TIMEREG_CR_3_CLOCK		(1 << 7)
+#define TIMEREG_CR_3_INT		(1 << 8)
+#define TIMEREG_CR_COUNT_UP		(1 << 9)
+#define TIMEREG_CR_COUNT_DOWN	(0 << 9)
+
+static void __iomem *base;
+static unsigned int clock_count_per_tick;
+
+static void moxart_clkevt_mode(enum clock_event_mode mode,
+	struct clock_event_device *clk)
+{
+	u32 u = readl(base + TIMER_CR);
+
+	switch (mode) {
+	case CLOCK_EVT_MODE_RESUME:
+	case CLOCK_EVT_MODE_ONESHOT:
+		u &= ~TIMEREG_CR_1_ENABLE;
+		writel(u, base + TIMER_CR);
+		writel(~0, base + TIMER1_BASE + REG_LOAD);
+		break;
+	case CLOCK_EVT_MODE_PERIODIC:
+		writel(clock_count_per_tick, base + TIMER1_BASE + REG_LOAD);
+		u |= TIMEREG_CR_1_ENABLE;
+		writel(u, base + TIMER_CR);
+		break;
+	case CLOCK_EVT_MODE_UNUSED:
+	case CLOCK_EVT_MODE_SHUTDOWN:
+	default:
+		u &= ~TIMEREG_CR_1_ENABLE;
+		writel(u, base + TIMER_CR);
+		break;
+	}
+}
+
+static int moxart_clkevt_next_event(unsigned long cycles,
+	struct clock_event_device *unused)
+{
+	u32 u;
+	u = readl(base + TIMER1_BASE + REG_COUNT) - cycles;
+	writel(u, base + TIMER1_BASE + REG_MATCH1);
+	u = readl(base + TIMER_CR) | TIMEREG_CR_1_ENABLE;
+	writel(u, base + TIMER_CR);
+	return 0;
+}
+
+static struct clock_event_device moxart_clockevent = {
+	.name = "moxart_timer",
+	.rating = 200,
+	.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
+	.set_mode = moxart_clkevt_mode,
+	.set_next_event = moxart_clkevt_next_event,
+};
+
+static irqreturn_t moxart_timer_interrupt(int irq, void *dev_id)
+{
+	struct clock_event_device *evt = dev_id;
+	evt->event_handler(evt);
+	return IRQ_HANDLED;
+}
+
+static struct irqaction moxart_timer_irq = {
+	.name = "moxart-timer",
+	.flags = IRQF_TIMER,
+	.handler = moxart_timer_interrupt,
+	.dev_id = &moxart_clockevent,
+};
+
+static void __init moxart_timer_init(struct device_node *node)
+{
+	int ret, irq;
+	unsigned long pclk;
+	struct clk *clk;
+
+	base = of_iomap(node, 0);
+	if (!base)
+		panic("%s: failed to map base\n", node->full_name);
+
+	irq = irq_of_parse_and_map(node, 0);
+	if (irq <= 0)
+		panic("%s: can't parse IRQ\n", node->full_name);
+
+	ret = setup_irq(irq, &moxart_timer_irq);
+	if (ret) {
+		pr_err("%s: failed to setup IRQ %d\n", node->full_name, irq);
+		return;
+	}
+
+	clk = of_clk_get(node, 0);
+	if (IS_ERR(clk)) {
+		pr_err("%s: of_clk_get failed\n", __func__);
+		return;
+	}
+
+	pclk = clk_get_rate(clk);
+	clock_count_per_tick = DIV_ROUND_CLOSEST(pclk, HZ);
+
+	writel(~0, base + TIMER2_BASE + REG_LOAD);
+
+	writel(TIMEREG_CR_2_ENABLE, base + TIMER_CR);
+
+	if (clocksource_mmio_init(base + TIMER2_BASE + REG_COUNT,
+					"moxart_timer", pclk, 200, 32,
+					clocksource_mmio_readl_down)) {
+		pr_err("%s: clocksource_mmio_init failed\n", __func__);
+		return;
+	}
+
+	moxart_clockevent.cpumask = cpumask_of(0);
+
+	clockevents_config_and_register(&moxart_clockevent, pclk,
+					0x4, 0xf0000000);
+
+	pr_info("%s: %s finished pclk=%lu HZ=%d IRQ=%d\n",
+			node->full_name, __func__, pclk, HZ, irq);
+}
+CLOCKSOURCE_OF_DECLARE(moxart, "moxa,moxart-timer", moxart_timer_init);
+
-- 
1.8.2.1


WARNING: multiple messages have this Message-ID (diff)
From: jonas.jensen@gmail.com (Jonas Jensen)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v3] ARM: clocksource: add support for MOXA ART SoCs
Date: Thu, 27 Jun 2013 13:23:23 +0200	[thread overview]
Message-ID: <1372332203-30228-1-git-send-email-jonas.jensen@gmail.com> (raw)
In-Reply-To: <1372258383-24524-1-git-send-email-jonas.jensen@gmail.com>

This patch adds an clocksource driver for the main timer(s)
found on MOXA ART SoCs.

Signed-off-by: Jonas Jensen <jonas.jensen@gmail.com>
---

Notes:
    Applies to next-20130619
    
    Changes since v2:
    
    1. remove unused defines: MAX_TIMER, USED_TIMER, APB_CLK
    2. split register defines so each timer has a base
    3. use standard multiline comments
    4. replace whitespaces with tabs
    5. rename "timer_base" "base"
    6. rename "clock_frequency" "clock_count_per_tick"
    7. disable TIMER1 in CLOCK_EVT_MODE_ONESHOT
    8. remove pr_debug:s
    9. in CLOCK_EVT_MODE_PERIODIC, switch order, write clock_count_per_tick
       to TIMER1 LOAD and enable it
    10. enable TIMER1 in moxart_clkevt_next_event
    11. remove IRQF_DISABLED, IRQF_IRQPOLL from irqaction flags
    12. set clock_frequency exclusively from system clock
    13. bail if of_clk_get fails
    14. assign clock_count_per_tick to DIV_ROUND_CLOSEST(pclk, HZ)
    15. align continuation lines with matching opening brace

 drivers/clocksource/Makefile       |   1 +
 drivers/clocksource/moxart_timer.c | 166 +++++++++++++++++++++++++++++++++++++
 2 files changed, 167 insertions(+)
 create mode 100644 drivers/clocksource/moxart_timer.c

diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
index 8d979c7..c93e1a8 100644
--- a/drivers/clocksource/Makefile
+++ b/drivers/clocksource/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_CLKSRC_SAMSUNG_PWM)	+= samsung_pwm_timer.o
 
 obj-$(CONFIG_ARM_ARCH_TIMER)		+= arm_arch_timer.o
 obj-$(CONFIG_CLKSRC_METAG_GENERIC)	+= metag_generic.o
+obj-$(CONFIG_ARCH_MOXART)	+= moxart_timer.o
diff --git a/drivers/clocksource/moxart_timer.c b/drivers/clocksource/moxart_timer.c
new file mode 100644
index 0000000..4678b30
--- /dev/null
+++ b/drivers/clocksource/moxart_timer.c
@@ -0,0 +1,166 @@
+/*
+ * MOXA ART SoCs timer handling.
+ *
+ * Copyright (C) 2013 Jonas Jensen
+ *
+ * Jonas Jensen <jonas.jensen@gmail.com>
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2.  This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include <linux/clk.h>
+#include <linux/clockchips.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqreturn.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/io.h>
+#include <linux/clocksource.h>
+
+#define TIMER1_BASE			0x00
+#define TIMER2_BASE			0x10
+#define TIMER3_BASE			0x20
+
+#define REG_COUNT			0x0 /* writable */
+#define REG_LOAD			0x4
+#define REG_MATCH1			0x8
+#define REG_MATCH2			0xC
+
+#define TIMER_CR			0x30
+#define TIMER_INTR_STATE	0x34
+#define TIMER_INTR_MASK		0x38
+
+/*
+ * TIMER_CR flags:
+ *
+ * TIMEREG_CR_*_CLOCK	0: PCLK, 1: EXT1CLK
+ * TIMEREG_CR_*_INT		overflow interrupt enable bit
+ */
+#define TIMEREG_CR_1_ENABLE		(1 << 0)
+#define TIMEREG_CR_1_CLOCK		(1 << 1)
+#define TIMEREG_CR_1_INT		(1 << 2)
+#define TIMEREG_CR_2_ENABLE		(1 << 3)
+#define TIMEREG_CR_2_CLOCK		(1 << 4)
+#define TIMEREG_CR_2_INT		(1 << 5)
+#define TIMEREG_CR_3_ENABLE		(1 << 6)
+#define TIMEREG_CR_3_CLOCK		(1 << 7)
+#define TIMEREG_CR_3_INT		(1 << 8)
+#define TIMEREG_CR_COUNT_UP		(1 << 9)
+#define TIMEREG_CR_COUNT_DOWN	(0 << 9)
+
+static void __iomem *base;
+static unsigned int clock_count_per_tick;
+
+static void moxart_clkevt_mode(enum clock_event_mode mode,
+	struct clock_event_device *clk)
+{
+	u32 u = readl(base + TIMER_CR);
+
+	switch (mode) {
+	case CLOCK_EVT_MODE_RESUME:
+	case CLOCK_EVT_MODE_ONESHOT:
+		u &= ~TIMEREG_CR_1_ENABLE;
+		writel(u, base + TIMER_CR);
+		writel(~0, base + TIMER1_BASE + REG_LOAD);
+		break;
+	case CLOCK_EVT_MODE_PERIODIC:
+		writel(clock_count_per_tick, base + TIMER1_BASE + REG_LOAD);
+		u |= TIMEREG_CR_1_ENABLE;
+		writel(u, base + TIMER_CR);
+		break;
+	case CLOCK_EVT_MODE_UNUSED:
+	case CLOCK_EVT_MODE_SHUTDOWN:
+	default:
+		u &= ~TIMEREG_CR_1_ENABLE;
+		writel(u, base + TIMER_CR);
+		break;
+	}
+}
+
+static int moxart_clkevt_next_event(unsigned long cycles,
+	struct clock_event_device *unused)
+{
+	u32 u;
+	u = readl(base + TIMER1_BASE + REG_COUNT) - cycles;
+	writel(u, base + TIMER1_BASE + REG_MATCH1);
+	u = readl(base + TIMER_CR) | TIMEREG_CR_1_ENABLE;
+	writel(u, base + TIMER_CR);
+	return 0;
+}
+
+static struct clock_event_device moxart_clockevent = {
+	.name = "moxart_timer",
+	.rating = 200,
+	.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
+	.set_mode = moxart_clkevt_mode,
+	.set_next_event = moxart_clkevt_next_event,
+};
+
+static irqreturn_t moxart_timer_interrupt(int irq, void *dev_id)
+{
+	struct clock_event_device *evt = dev_id;
+	evt->event_handler(evt);
+	return IRQ_HANDLED;
+}
+
+static struct irqaction moxart_timer_irq = {
+	.name = "moxart-timer",
+	.flags = IRQF_TIMER,
+	.handler = moxart_timer_interrupt,
+	.dev_id = &moxart_clockevent,
+};
+
+static void __init moxart_timer_init(struct device_node *node)
+{
+	int ret, irq;
+	unsigned long pclk;
+	struct clk *clk;
+
+	base = of_iomap(node, 0);
+	if (!base)
+		panic("%s: failed to map base\n", node->full_name);
+
+	irq = irq_of_parse_and_map(node, 0);
+	if (irq <= 0)
+		panic("%s: can't parse IRQ\n", node->full_name);
+
+	ret = setup_irq(irq, &moxart_timer_irq);
+	if (ret) {
+		pr_err("%s: failed to setup IRQ %d\n", node->full_name, irq);
+		return;
+	}
+
+	clk = of_clk_get(node, 0);
+	if (IS_ERR(clk)) {
+		pr_err("%s: of_clk_get failed\n", __func__);
+		return;
+	}
+
+	pclk = clk_get_rate(clk);
+	clock_count_per_tick = DIV_ROUND_CLOSEST(pclk, HZ);
+
+	writel(~0, base + TIMER2_BASE + REG_LOAD);
+
+	writel(TIMEREG_CR_2_ENABLE, base + TIMER_CR);
+
+	if (clocksource_mmio_init(base + TIMER2_BASE + REG_COUNT,
+					"moxart_timer", pclk, 200, 32,
+					clocksource_mmio_readl_down)) {
+		pr_err("%s: clocksource_mmio_init failed\n", __func__);
+		return;
+	}
+
+	moxart_clockevent.cpumask = cpumask_of(0);
+
+	clockevents_config_and_register(&moxart_clockevent, pclk,
+					0x4, 0xf0000000);
+
+	pr_info("%s: %s finished pclk=%lu HZ=%d IRQ=%d\n",
+			node->full_name, __func__, pclk, HZ, irq);
+}
+CLOCKSOURCE_OF_DECLARE(moxart, "moxa,moxart-timer", moxart_timer_init);
+
-- 
1.8.2.1

  parent reply	other threads:[~2013-06-27 11:23 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-18 10:00 [PATCH] ARM: clocksource: add support for MOXA ART SoCs Jonas Jensen
2013-06-18 10:00 ` Jonas Jensen
2013-06-18 15:14 ` Thomas Petazzoni
2013-06-18 15:14   ` Thomas Petazzoni
2013-06-18 15:28 ` Arnd Bergmann
2013-06-18 15:28   ` Arnd Bergmann
2013-06-26 14:53 ` [PATCH v2] " Jonas Jensen
2013-06-26 14:53   ` Jonas Jensen
2013-06-26 14:59   ` Jonas Jensen
2013-06-26 14:59     ` Jonas Jensen
2013-06-26 16:10   ` Uwe Kleine-König
2013-06-26 16:10     ` Uwe Kleine-König
2013-06-26 19:15   ` Linus Walleij
2013-06-26 19:15     ` Linus Walleij
2013-06-27 11:23   ` Jonas Jensen [this message]
2013-06-27 11:23     ` [PATCH v3] " Jonas Jensen
2013-06-28 13:34     ` Thomas Gleixner
2013-06-28 13:34       ` Thomas Gleixner
2013-07-01 14:02     ` [PATCH v4] " Jonas Jensen
2013-07-01 14:02       ` Jonas Jensen
2013-07-01 17:55       ` Thomas Gleixner
2013-07-01 17:55         ` Thomas Gleixner
2013-07-02 20:19       ` Linus Walleij
2013-07-02 20:19         ` Linus Walleij
2013-07-04 12:19       ` [PATCH v5] " Jonas Jensen
2013-07-04 12:19         ` Jonas Jensen
2013-07-04 21:42         ` Thomas Gleixner
2013-07-04 21:42           ` Thomas Gleixner
2013-07-05 10:05           ` Jonas Jensen
2013-07-05 10:05             ` Jonas Jensen
2013-07-05 10:21             ` Thomas Gleixner
2013-07-05 10:21               ` Thomas Gleixner
2013-07-05 11:48               ` Jonas Jensen
2013-07-05 11:48                 ` Jonas Jensen
2013-07-05 10:04         ` [PATCH v6] " Jonas Jensen
2013-07-05 10:04           ` Jonas Jensen
2013-07-05 11:46           ` [PATCH v7] " Jonas Jensen
2013-07-05 11:46             ` Jonas Jensen
2013-07-16 13:52             ` Daniel Lezcano
2013-07-16 13:52               ` Daniel Lezcano
2013-07-16 14:44             ` [PATCH v8] " Jonas Jensen
2013-07-16 14:44               ` Jonas Jensen
2013-07-16 15:01               ` Daniel Lezcano
2013-07-16 15:01                 ` Daniel Lezcano
2013-07-17  8:14                 ` Jonas Jensen
2013-07-17  8:14                   ` Jonas Jensen
2013-07-17 12:13                   ` Daniel Lezcano
2013-07-17 12:13                     ` Daniel Lezcano
2013-07-17  8:04               ` [PATCH v9] " Jonas Jensen
2013-07-17  8:04                 ` Jonas Jensen
2013-07-19 11:12                 ` [PATCH] ARM: clocksource: moxart: documentation: update device tree bindings document Jonas Jensen
2013-07-19 11:12                   ` Jonas Jensen
2013-07-19 12:16                   ` Daniel Lezcano
2013-07-19 12:16                     ` Daniel Lezcano
2013-07-19 12:50                     ` Jonas Jensen
2013-07-19 12:50                       ` Jonas Jensen
2013-07-20 20:45                 ` [PATCH v9] ARM: clocksource: add support for MOXA ART SoCs Linus Walleij
2013-07-20 20:45                   ` Linus Walleij
2013-07-20 21:34                   ` Daniel Lezcano
2013-07-20 21:34                     ` Daniel Lezcano
2013-07-26 14:03                     ` [PATCH] ARM: clocksource: moxart: add bitops.h include Jonas Jensen
2013-07-26 14:03                       ` Jonas Jensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1372332203-30228-1-git-send-email-jonas.jensen@gmail.com \
    --to=jonas.jensen@gmail.com \
    --cc=arm@kernel.org \
    --cc=arnd@arndb.de \
    --cc=john.stultz@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=thomas.petazzoni@free-electrons.com \
    --cc=tomasz.figa@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.