All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonas Bonn <jonas@southpole.se>
To: linux-kernel@vger.kernel.org
Cc: Jonas Bonn <jonas@southpole.se>
Subject: [PATCH 08/19] OpenRISC: Timekeeping
Date: Sun, 19 Jun 2011 13:43:34 +0200	[thread overview]
Message-ID: <1308483825-6023-9-git-send-email-jonas@southpole.se> (raw)
In-Reply-To: <1308483825-6023-1-git-send-email-jonas@southpole.se>


Signed-off-by: Jonas Bonn <jonas@southpole.se>
---
 arch/openrisc/include/asm/timex.h |   35 +++++++
 arch/openrisc/kernel/time.c       |  182 +++++++++++++++++++++++++++++++++++++
 2 files changed, 217 insertions(+), 0 deletions(-)
 create mode 100644 arch/openrisc/include/asm/timex.h
 create mode 100644 arch/openrisc/kernel/time.c

diff --git a/arch/openrisc/include/asm/timex.h b/arch/openrisc/include/asm/timex.h
new file mode 100644
index 0000000..20d5619
--- /dev/null
+++ b/arch/openrisc/include/asm/timex.h
@@ -0,0 +1,35 @@
+/*
+ * OpenRISC Linux
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * OpenRISC implementation:
+ * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
+ * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
+ * et al.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef __ASM_OPENRISC_TIMEX_H
+#define __ASM_OPENRISC_TIMEX_H
+
+#include <asm-generic/timex.h>
+
+#include <asm/param.h>
+#include <asm/cpuinfo.h>
+
+//#define CLOCK_TICK_RATE	(CONFIG_OPENRISC_SYS_CLK*1000000 / HZ)
+//#define CLOCK_TICK_RATE	(cpuinfo.clock_frequency / HZ)
+
+/* This isn't really used any more */
+#define CLOCK_TICK_RATE 1000
+
+#define ARCH_HAS_READ_CURRENT_TIMER
+
+#endif
diff --git a/arch/openrisc/kernel/time.c b/arch/openrisc/kernel/time.c
new file mode 100644
index 0000000..5b367cf
--- /dev/null
+++ b/arch/openrisc/kernel/time.c
@@ -0,0 +1,182 @@
+/*
+ * OpenRISC time.c
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * Modifications for the OpenRISC architecture:
+ * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
+ *
+ *      This program is free software; you can redistribute it and/or
+ *      modify it under the terms of the GNU General Public License
+ *      as published by the Free Software Foundation; either version
+ *      2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/time.h>
+#include <linux/timex.h>
+#include <linux/interrupt.h>
+#include <linux/ftrace.h>
+
+#include <linux/clocksource.h>
+#include <linux/clockchips.h>
+#include <linux/irq.h>
+#include <linux/io.h>
+
+#include <asm/cpuinfo.h>
+
+static int openrisc_timer_set_next_event(unsigned long delta,
+                                     struct clock_event_device *dev)
+{
+	u32 c;
+
+	/* Read 32-bit counter value, add delta, mask off the low 28 bits.
+	 * We're guaranteed delta won't be bigger than 28 bits because the
+	 * generic timekeeping code ensures that for us.
+	 */
+	c = mfspr(SPR_TTCR);
+	c += delta;
+	c &= SPR_TTMR_TP;
+
+	/* Set counter and enable interrupt.
+	 * Keep timer in continuous mode always.
+	 */
+	mtspr(SPR_TTMR, SPR_TTMR_CR | SPR_TTMR_IE | c);
+
+        return 0;
+}
+
+static void openrisc_timer_set_mode(enum clock_event_mode mode,
+                                struct clock_event_device *evt)
+{
+        switch (mode) {
+        case CLOCK_EVT_MODE_PERIODIC:
+                pr_debug(KERN_INFO "%s: periodic\n", __func__);
+		BUG();
+                break;
+        case CLOCK_EVT_MODE_ONESHOT:
+                pr_debug(KERN_INFO "%s: oneshot\n", __func__);
+                break;
+        case CLOCK_EVT_MODE_UNUSED:
+                pr_debug(KERN_INFO "%s: unused\n", __func__);
+                break;
+        case CLOCK_EVT_MODE_SHUTDOWN:
+                pr_debug(KERN_INFO "%s: shutdown\n", __func__);
+                break;
+        case CLOCK_EVT_MODE_RESUME:
+                pr_debug(KERN_INFO "%s: resume\n", __func__);
+                break;
+        }
+}
+
+/* This is the clock event device based on the OR1K tick timer.
+ * As the timer is being used as a continuous clock-source (required for HR
+ * timers) we cannot enable the PERIODIC feature.  The tick timer can run using
+ * one-shot events, so no problem.
+ */
+
+static struct clock_event_device clockevent_openrisc_timer = {
+        .name           = "openrisc_timer_clockevent",
+        .features       = CLOCK_EVT_FEAT_ONESHOT,
+        .rating         = 300,
+        .set_next_event = openrisc_timer_set_next_event,
+        .set_mode       = openrisc_timer_set_mode,
+};
+
+static inline void timer_ack(void)
+{
+	/* Clear the IP bit and disable further interrupts */
+	/* This can be done very simply... we just need to keep the timer
+	   running, so just maintain the CR bits while clearing the rest
+	   of the register
+	*/
+	mtspr(SPR_TTMR, SPR_TTMR_CR);
+}
+
+/*
+ * The timer interrupt is mostly handled in generic code nowadays... this
+ * function just acknowledges the interrupt and fires the event handler that
+ * has been set on the clockevent device by the generic time management code.
+ *
+ * This function needs to be called by the timer exception handler and that's
+ * all the exception handler needs to do.
+ */
+
+irqreturn_t __irq_entry timer_interrupt(struct pt_regs * regs)
+{
+        struct pt_regs *old_regs = set_irq_regs(regs);
+        struct clock_event_device *evt = &clockevent_openrisc_timer;
+
+        timer_ack();
+
+	/*
+	 * update_process_times() expects us to have called irq_enter().
+	 */
+	irq_enter();
+        evt->event_handler(evt);
+	irq_exit();
+
+        set_irq_regs(old_regs);
+
+        return IRQ_HANDLED;
+}
+
+static __init void openrisc_clockevent_init(void)
+{
+	clockevents_calc_mult_shift(&clockevent_openrisc_timer,
+				    cpuinfo.clock_frequency, 4);
+
+	/* We only have 28 bits */
+        clockevent_openrisc_timer.max_delta_ns =
+                clockevent_delta2ns((u32)0x0fffffff,
+				    &clockevent_openrisc_timer);
+        clockevent_openrisc_timer.min_delta_ns =
+                clockevent_delta2ns(1, &clockevent_openrisc_timer);
+        clockevent_openrisc_timer.cpumask = cpumask_of(0);
+        clockevents_register_device(&clockevent_openrisc_timer);
+}
+
+
+/**
+ * Clocksource: Based on OpenRISC timer/counter
+ *
+ * This sets up the OpenRISC Tick Timer as a clock source.  The tick timer
+ * is 32 bits wide and runs at the CPU clock frequency.
+ */
+
+static cycle_t openrisc_timer_read(struct clocksource* cs) {
+	return (cycle_t) mfspr(SPR_TTCR);
+}
+
+static struct clocksource openrisc_timer = {
+	.name		= "openrisc_timer",
+        .rating         = 200,
+	.read		= openrisc_timer_read,
+        .mask           = CLOCKSOURCE_MASK(32),
+        .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
+};
+
+static int __init openrisc_timer_init(void)
+{
+        if (clocksource_register_hz(&openrisc_timer, cpuinfo.clock_frequency))
+                panic("failed to register clocksource");
+
+	/* Enable the incrementer: 'continuous' mode with interrupt disabled */
+        mtspr(SPR_TTMR, SPR_TTMR_CR);
+
+        return 0;
+}
+
+void __init time_init(void)
+{
+	u32 upr;
+
+	upr = mfspr(SPR_UPR);
+	if (!(upr & SPR_UPR_TTP))
+		panic("Linux not supported on devices without tick timer");
+
+	openrisc_timer_init();
+	openrisc_clockevent_init();
+}
-- 
1.7.4.1


  parent reply	other threads:[~2011-06-19 11:47 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-19 11:43 OpenRISC Architecture: Request for review Jonas Bonn
2011-06-19 11:43 ` [PATCH 01/19] OpenRISC: Boot code Jonas Bonn
2011-06-19 17:14   ` Arnd Bergmann
2011-06-19 11:43 ` [PATCH 02/19] OpenRISC: Device tree Jonas Bonn
2011-06-19 17:19   ` Arnd Bergmann
2011-06-19 17:19     ` Arnd Bergmann
2011-06-19 11:43 ` [PATCH 03/19] OpenRISC: Memory management Jonas Bonn
2011-06-19 18:35   ` Arnd Bergmann
2011-06-19 11:43 ` [PATCH 04/19] OpenRISC: Signal handling Jonas Bonn
2011-06-19 11:43 ` [PATCH 05/19] OpenRISC: Build infrastructure Jonas Bonn
2011-06-19 18:57   ` Arnd Bergmann
2011-06-19 11:43 ` [PATCH 06/19] OpenRISC: PTrace Jonas Bonn
2011-06-19 11:43 ` [PATCH 07/19] OpenRISC: DMA Jonas Bonn
2011-06-19 19:02   ` Arnd Bergmann
2011-06-19 11:43 ` Jonas Bonn [this message]
2011-06-19 19:06   ` [PATCH 08/19] OpenRISC: Timekeeping Arnd Bergmann
2011-06-19 11:43 ` [PATCH 09/19] OpenRISC: IRQ Jonas Bonn
2011-06-19 19:09   ` Arnd Bergmann
2011-06-19 11:43 ` [PATCH 10/19] OpenRISC: System calls Jonas Bonn
2011-06-19 15:09   ` richard -rw- weinberger
2011-06-19 15:51     ` Jonas Bonn
2011-06-19 21:11       ` Andi Kleen
2011-06-19 11:43 ` [PATCH 11/19] OpenRISC: Idle/Power management Jonas Bonn
2011-06-20  8:20   ` Arnd Bergmann
2011-06-19 11:43 ` [PATCH 12/19] OpenRISC: Scheduling/Process management Jonas Bonn
2011-06-19 19:12   ` Arnd Bergmann
2011-06-19 21:17   ` Andi Kleen
2011-06-19 11:43 ` [PATCH 13/19] OpenRISC: GPIO Jonas Bonn
2011-06-19 11:43 ` [PATCH 14/19] OpenRISC: Module support Jonas Bonn
2011-06-21 20:03   ` Valdis.Kletnieks
2011-06-22 14:26     ` Arnd Bergmann
2011-06-22 19:08       ` [PATCH 1/1] Add default implementations for moduleloader hooks Jonas Bonn
2011-06-22 19:14       ` [PATCH 14/19] OpenRISC: Module support Jonas Bonn
2011-06-22 19:58         ` Arnd Bergmann
2011-06-22 20:05           ` Jonas Bonn
2011-06-22 20:46             ` Arnd Bergmann
2011-06-24  8:52       ` Jonas Bonn
2011-06-24 10:05         ` Arnd Bergmann
2011-06-24 11:06           ` Rusty Russell
2011-06-19 11:43 ` [PATCH 15/19] OpenRISC: Traps Jonas Bonn
2011-06-19 11:43 ` [PATCH 16/19] OpenRISC: Headers Jonas Bonn
2011-06-19 19:39   ` Arnd Bergmann
2011-06-19 19:43     ` Arnd Bergmann
2011-06-19 20:19   ` Geert Uytterhoeven
2011-06-19 11:43 ` [PATCH 17/19] OpenRISC: Library routines Jonas Bonn
2011-06-19 11:43 ` [PATCH 18/19] OpenRISC: Miscellaneous Jonas Bonn
2011-06-19 11:43 ` [PATCH 19/19] OpenRISC: Add MAINTAINERS entry Jonas Bonn
2011-06-19 17:06 ` OpenRISC Architecture: Request for review Arnd Bergmann
2011-06-22 21:23 ` H. Peter Anvin
2011-06-23  9:10   ` Jonas Bonn
2011-06-23  9:54     ` Julius Baxter

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=1308483825-6023-9-git-send-email-jonas@southpole.se \
    --to=jonas@southpole.se \
    --cc=linux-kernel@vger.kernel.org \
    /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.