1 From ebf176a38105200dca51a96ad1535c8d56235653 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 19 Jun 2010 04:08:10 +0000
4 Subject: [PATCH] MIPS: JZ4740: Add clocksource/clockevent support.
6 Add clocksource and clockevent support for the timer/counter unit on
9 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
10 Cc: linux-mips@linux-mips.org
11 Cc: linux-kernel@vger.kernel.org
12 Patchwork: https://patchwork.linux-mips.org/patch/1397/
13 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
15 arch/mips/jz4740/time.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++
16 1 files changed, 144 insertions(+), 0 deletions(-)
17 create mode 100644 arch/mips/jz4740/time.c
20 +++ b/arch/mips/jz4740/time.c
23 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
24 + * JZ4740 platform time support
26 + * This program is free software; you can redistribute it and/or modify it
27 + * under the terms of the GNU General Public License as published by the
28 + * Free Software Foundation; either version 2 of the License, or (at your
29 + * option) any later version.
31 + * You should have received a copy of the GNU General Public License along
32 + * with this program; if not, write to the Free Software Foundation, Inc.,
33 + * 675 Mass Ave, Cambridge, MA 02139, USA.
37 +#include <linux/interrupt.h>
38 +#include <linux/kernel.h>
39 +#include <linux/time.h>
41 +#include <linux/clockchips.h>
43 +#include <asm/mach-jz4740/irq.h>
44 +#include <asm/time.h>
49 +#define TIMER_CLOCKEVENT 0
50 +#define TIMER_CLOCKSOURCE 1
52 +static uint16_t jz4740_jiffies_per_tick;
54 +static cycle_t jz4740_clocksource_read(struct clocksource *cs)
56 + return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
59 +static struct clocksource jz4740_clocksource = {
60 + .name = "jz4740-timer",
62 + .read = jz4740_clocksource_read,
63 + .mask = CLOCKSOURCE_MASK(16),
64 + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
67 +static irqreturn_t jz4740_clockevent_irq(int irq, void *devid)
69 + struct clock_event_device *cd = devid;
71 + jz4740_timer_ack_full(TIMER_CLOCKEVENT);
73 + if (cd->mode != CLOCK_EVT_MODE_PERIODIC)
74 + jz4740_timer_disable(TIMER_CLOCKEVENT);
76 + cd->event_handler(cd);
81 +static void jz4740_clockevent_set_mode(enum clock_event_mode mode,
82 + struct clock_event_device *cd)
85 + case CLOCK_EVT_MODE_PERIODIC:
86 + jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
87 + jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
88 + case CLOCK_EVT_MODE_RESUME:
89 + jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
90 + jz4740_timer_enable(TIMER_CLOCKEVENT);
92 + case CLOCK_EVT_MODE_ONESHOT:
93 + case CLOCK_EVT_MODE_SHUTDOWN:
94 + jz4740_timer_disable(TIMER_CLOCKEVENT);
101 +static int jz4740_clockevent_set_next(unsigned long evt,
102 + struct clock_event_device *cd)
104 + jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
105 + jz4740_timer_set_period(TIMER_CLOCKEVENT, evt);
106 + jz4740_timer_enable(TIMER_CLOCKEVENT);
111 +static struct clock_event_device jz4740_clockevent = {
112 + .name = "jz4740-timer",
113 + .features = CLOCK_EVT_FEAT_PERIODIC,
114 + .set_next_event = jz4740_clockevent_set_next,
115 + .set_mode = jz4740_clockevent_set_mode,
117 + .irq = JZ4740_IRQ_TCU0,
120 +static struct irqaction timer_irqaction = {
121 + .handler = jz4740_clockevent_irq,
122 + .flags = IRQF_PERCPU | IRQF_TIMER,
123 + .name = "jz4740-timerirq",
124 + .dev_id = &jz4740_clockevent,
127 +void __init plat_time_init(void)
133 + jz4740_timer_init();
135 + clk_rate = jz4740_clock_bdata.ext_rate >> 4;
136 + jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
138 + clockevent_set_clock(&jz4740_clockevent, clk_rate);
139 + jz4740_clockevent.min_delta_ns = clockevent_delta2ns(100, &jz4740_clockevent);
140 + jz4740_clockevent.max_delta_ns = clockevent_delta2ns(0xffff, &jz4740_clockevent);
141 + jz4740_clockevent.cpumask = cpumask_of(0);
143 + clockevents_register_device(&jz4740_clockevent);
145 + clocksource_set_clock(&jz4740_clocksource, clk_rate);
146 + ret = clocksource_register(&jz4740_clocksource);
149 + printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
151 + setup_irq(JZ4740_IRQ_TCU0, &timer_irqaction);
153 + ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
155 + jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
156 + jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
158 + jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
159 + jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
161 + jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
163 + jz4740_timer_enable(TIMER_CLOCKEVENT);
164 + jz4740_timer_enable(TIMER_CLOCKSOURCE);