mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/1 v2] arm: add generic smp twd timer
@ 2012-10-04 10:55 Jean-Christophe PLAGNIOL-VILLARD
  2012-10-04 11:06 ` Sascha Hauer
  0 siblings, 1 reply; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-10-04 10:55 UTC (permalink / raw)
  To: barebox

on Cortex A9 and Cortex A5 we have a generic timer which we can use as
clocksource

Limit the timer frequency to < 25Mhz

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 v2:
  move to clocksource
  drop header

Best Regards,
J.
 drivers/Kconfig                   |    1 +
 drivers/Makefile                  |    1 +
 drivers/clocksource/arm_smp_twd.c |  101 +++++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
 create mode 100644 drivers/clocksource/arm_smp_twd.c

diff --git a/drivers/Kconfig b/drivers/Kconfig
index d0b5e3a..6f78644 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -11,6 +11,7 @@ source "drivers/usb/Kconfig"
 source "drivers/video/Kconfig"
 source "drivers/mci/Kconfig"
 source "drivers/clk/Kconfig"
+source "drivers/clocksource/Kconfig"
 source "drivers/mfd/Kconfig"
 source "drivers/misc/Kconfig"
 source "drivers/led/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index 2a1f8b0..742a5bd 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -11,6 +11,7 @@ obj-$(CONFIG_I2C) += i2c/
 obj-$(CONFIG_MCI) += mci/
 obj-$(CONFIG_VIDEO) += video/
 obj-y	+= clk/
+obj-y	+= clocksource/
 obj-y	+= mfd/
 obj-$(CONFIG_LED) += led/
 obj-y	+= eeprom/
diff --git a/drivers/clocksource/arm_smp_twd.c b/drivers/clocksource/arm_smp_twd.c
new file mode 100644
index 0000000..746d566
--- /dev/null
+++ b/drivers/clocksource/arm_smp_twd.c
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnio@jcrosoft.com>
+ *
+ * Under GPL v2
+ */
+#include <common.h>
+#include <init.h>
+#include <clock.h>
+#include <io.h>
+#include <driver.h>
+#include <errno.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+
+#define TWD_TIMER_LOAD			0x00
+#define TWD_TIMER_COUNTER		0x04
+#define TWD_TIMER_CONTROL		0x08
+#define TWD_TIMER_INTSTAT		0x0C
+
+#define TWD_TIMER_CONTROL_ENABLE	(1 << 0)
+#define TWD_TIMER_CONTROL_ONESHOT	(0 << 1)
+#define TWD_TIMER_CONTROL_PERIODIC	(1 << 1)
+#define TWD_TIMER_CONTROL_IT_ENABLE	(1 << 2)
+
+#define TWD_TIMER_CONTROL_PRESC(x)	(((x) & 0xff) << 8)
+
+static __iomem void *twd_base;
+static struct clk *twd_clk;
+
+static uint64_t smp_twd_read(void)
+{
+	return ~readl(twd_base + TWD_TIMER_COUNTER);
+}
+
+static struct clocksource smp_twd_clksrc = {
+	.read	= smp_twd_read,
+	.shift	= 20,
+	.mask	= CLOCKSOURCE_MASK(32),
+};
+
+#define SMP_TWD_MAX_FREQ (25 *1000 * 1000)
+
+static int smp_twd_probe(struct device_d *dev)
+{
+	u32 tick_rate;
+	u32 val;
+	int ret;
+	u32 presc = 0;
+
+	twd_clk = clk_get(dev, NULL);
+	if (IS_ERR(twd_clk)) {
+		ret = PTR_ERR(twd_clk);
+		dev_err(dev, "clock not found: %d\n", ret);
+		return ret;
+	}
+
+	ret = clk_enable(twd_clk);
+	if (ret < 0) {
+		dev_err(dev, "clock failed to enable: %d\n", ret);
+		clk_put(twd_clk);
+		return ret;
+	}
+
+	twd_base = dev_request_mem_region(dev, 0);
+
+	tick_rate = clk_get_rate(twd_clk);
+	if (tick_rate > SMP_TWD_MAX_FREQ) {
+		presc = tick_rate / SMP_TWD_MAX_FREQ;
+		if (presc)
+			presc--;
+		presc = min((u32)0xff, presc);
+		tick_rate /= presc + 1;
+	}
+
+	val = TWD_TIMER_CONTROL_PRESC(presc) |
+	      TWD_TIMER_CONTROL_PERIODIC;
+	writel(val, twd_base + TWD_TIMER_CONTROL);
+
+	writel(0xffffffff, twd_base + TWD_TIMER_LOAD);
+
+	val = readl(twd_base + TWD_TIMER_CONTROL);
+	val |= TWD_TIMER_CONTROL_ENABLE;
+	writel(val, twd_base + TWD_TIMER_CONTROL);
+
+	smp_twd_clksrc.mult = clocksource_hz2mult(tick_rate, smp_twd_clksrc.shift);
+
+	init_clock(&smp_twd_clksrc);
+
+	return 0;
+}
+
+static struct driver_d smp_twd_driver = {
+	.name = "smp_twd",
+	.probe = smp_twd_probe,
+};
+
+static int smp_twd_init(void)
+{
+	return platform_driver_register(&smp_twd_driver);
+}
+coredevice_initcall(smp_twd_init);
-- 
1.7.10.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/1 v2] arm: add generic smp twd timer
  2012-10-04 10:55 [PATCH 1/1 v2] arm: add generic smp twd timer Jean-Christophe PLAGNIOL-VILLARD
@ 2012-10-04 11:06 ` Sascha Hauer
  2012-10-04 12:05   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 1 reply; 3+ messages in thread
From: Sascha Hauer @ 2012-10-04 11:06 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Thu, Oct 04, 2012 at 12:55:35PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> on Cortex A9 and Cortex A5 we have a generic timer which we can use as
> clocksource
> 
> Limit the timer frequency to < 25Mhz
> 
> +	return ~readl(twd_base + TWD_TIMER_COUNTER);
> +}
> +
> +static struct clocksource smp_twd_clksrc = {
> +	.read	= smp_twd_read,
> +	.shift	= 20,
> +	.mask	= CLOCKSOURCE_MASK(32),
> +};
> +
> +#define SMP_TWD_MAX_FREQ (25 *1000 * 1000)
> +
> +static int smp_twd_probe(struct device_d *dev)
> +{
> +	u32 tick_rate;
> +	u32 val;
> +	int ret;
> +	u32 presc = 0;
> +
> +	twd_clk = clk_get(dev, NULL);
> +	if (IS_ERR(twd_clk)) {
> +		ret = PTR_ERR(twd_clk);
> +		dev_err(dev, "clock not found: %d\n", ret);

Still do you really really want to bloat the binary with such strings?
This message will only ever be seen by developers. I would find it much
more useful to turn this into dev_dbg and instead add a dev_err in the
driver code when a device fails to probe. That would give a developer
enough hints to know where to increase the debug level.

Sascha


-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/1 v2] arm: add generic smp twd timer
  2012-10-04 11:06 ` Sascha Hauer
@ 2012-10-04 12:05   ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 0 replies; 3+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-10-04 12:05 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 13:06 Thu 04 Oct     , Sascha Hauer wrote:
> On Thu, Oct 04, 2012 at 12:55:35PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> > on Cortex A9 and Cortex A5 we have a generic timer which we can use as
> > clocksource
> > 
> > Limit the timer frequency to < 25Mhz
> > 
> > +	return ~readl(twd_base + TWD_TIMER_COUNTER);
> > +}
> > +
> > +static struct clocksource smp_twd_clksrc = {
> > +	.read	= smp_twd_read,
> > +	.shift	= 20,
> > +	.mask	= CLOCKSOURCE_MASK(32),
> > +};
> > +
> > +#define SMP_TWD_MAX_FREQ (25 *1000 * 1000)
> > +
> > +static int smp_twd_probe(struct device_d *dev)
> > +{
> > +	u32 tick_rate;
> > +	u32 val;
> > +	int ret;
> > +	u32 presc = 0;
> > +
> > +	twd_clk = clk_get(dev, NULL);
> > +	if (IS_ERR(twd_clk)) {
> > +		ret = PTR_ERR(twd_clk);
> > +		dev_err(dev, "clock not found: %d\n", ret);
> 
> Still do you really really want to bloat the binary with such strings?
> This message will only ever be seen by developers. I would find it much
> more useful to turn this into dev_dbg and instead add a dev_err in the
> driver code when a device fails to probe. That would give a developer
> enough hints to know where to increase the debug level.
I try it and I forget to udpate the clock and really anoyed to do not see the
error message

for other drivers maybe but the clocksource is critical so clear error message
are madatory

and honestly here we save few bytes on a critical driver

and with the pbl it's even less

Best Regards,
J.

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-10-04 12:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-04 10:55 [PATCH 1/1 v2] arm: add generic smp twd timer Jean-Christophe PLAGNIOL-VILLARD
2012-10-04 11:06 ` Sascha Hauer
2012-10-04 12:05   ` Jean-Christophe PLAGNIOL-VILLARD

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox