blob: 7ab47fee1be8fdf24ae76dd3a0787221890eaa10 [file] [log] [blame]
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +00001/*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 platform time support
4 *
5 * This program is free software; you can redistribute it and/or modify it
Ralf Baechle70342282013-01-22 12:59:30 +01006 * under the terms of the GNU General Public License as published by the
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +00007 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
Paul Burtone06b86a2015-05-24 16:11:33 +010016#include <linux/clk.h>
Paul Burtonff1930c2015-05-24 16:11:36 +010017#include <linux/clk-provider.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000018#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/time.h>
21
22#include <linux/clockchips.h>
Deng-Cheng Zhu944081a2015-03-07 10:30:31 -080023#include <linux/sched_clock.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000024
Paul Burton1f4b8402015-05-24 16:11:32 +010025#include <asm/mach-jz4740/clock.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000026#include <asm/mach-jz4740/irq.h>
Thierry Reding46a98762012-09-02 11:20:34 +020027#include <asm/mach-jz4740/timer.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000028#include <asm/time.h>
29
30#include "clock.h"
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000031
32#define TIMER_CLOCKEVENT 0
33#define TIMER_CLOCKSOURCE 1
34
35static uint16_t jz4740_jiffies_per_tick;
36
37static cycle_t jz4740_clocksource_read(struct clocksource *cs)
38{
39 return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
40}
41
42static struct clocksource jz4740_clocksource = {
43 .name = "jz4740-timer",
44 .rating = 200,
45 .read = jz4740_clocksource_read,
46 .mask = CLOCKSOURCE_MASK(16),
47 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
48};
49
Deng-Cheng Zhu944081a2015-03-07 10:30:31 -080050static u64 notrace jz4740_read_sched_clock(void)
51{
52 return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
53}
54
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000055static irqreturn_t jz4740_clockevent_irq(int irq, void *devid)
56{
57 struct clock_event_device *cd = devid;
58
59 jz4740_timer_ack_full(TIMER_CLOCKEVENT);
60
61 if (cd->mode != CLOCK_EVT_MODE_PERIODIC)
62 jz4740_timer_disable(TIMER_CLOCKEVENT);
63
64 cd->event_handler(cd);
65
66 return IRQ_HANDLED;
67}
68
69static void jz4740_clockevent_set_mode(enum clock_event_mode mode,
70 struct clock_event_device *cd)
71{
72 switch (mode) {
73 case CLOCK_EVT_MODE_PERIODIC:
74 jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
75 jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
76 case CLOCK_EVT_MODE_RESUME:
77 jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
78 jz4740_timer_enable(TIMER_CLOCKEVENT);
79 break;
80 case CLOCK_EVT_MODE_ONESHOT:
81 case CLOCK_EVT_MODE_SHUTDOWN:
82 jz4740_timer_disable(TIMER_CLOCKEVENT);
83 break;
84 default:
85 break;
86 }
87}
88
89static int jz4740_clockevent_set_next(unsigned long evt,
90 struct clock_event_device *cd)
91{
92 jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
93 jz4740_timer_set_period(TIMER_CLOCKEVENT, evt);
94 jz4740_timer_enable(TIMER_CLOCKEVENT);
95
96 return 0;
97}
98
99static struct clock_event_device jz4740_clockevent = {
100 .name = "jz4740-timer",
Lars-Peter Clausen1e2bbde2011-03-31 20:52:20 +0200101 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000102 .set_next_event = jz4740_clockevent_set_next,
103 .set_mode = jz4740_clockevent_set_mode,
104 .rating = 200,
Paul Burton5b9cdd22015-05-24 16:11:46 +0100105#ifdef CONFIG_MACH_JZ4740
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000106 .irq = JZ4740_IRQ_TCU0,
Paul Burton5b9cdd22015-05-24 16:11:46 +0100107#endif
108#ifdef CONFIG_MACH_JZ4780
109 .irq = JZ4780_IRQ_TCU2,
110#endif
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000111};
112
113static struct irqaction timer_irqaction = {
114 .handler = jz4740_clockevent_irq,
115 .flags = IRQF_PERCPU | IRQF_TIMER,
116 .name = "jz4740-timerirq",
117 .dev_id = &jz4740_clockevent,
118};
119
120void __init plat_time_init(void)
121{
122 int ret;
123 uint32_t clk_rate;
124 uint16_t ctrl;
Paul Burtone06b86a2015-05-24 16:11:33 +0100125 struct clk *ext_clk;
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000126
Paul Burtonff1930c2015-05-24 16:11:36 +0100127 of_clk_init(NULL);
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000128 jz4740_timer_init();
129
Paul Burtone06b86a2015-05-24 16:11:33 +0100130 ext_clk = clk_get(NULL, "ext");
131 if (IS_ERR(ext_clk))
132 panic("unable to get ext clock");
133 clk_rate = clk_get_rate(ext_clk) >> 4;
134 clk_put(ext_clk);
135
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000136 jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
137
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);
142
143 clockevents_register_device(&jz4740_clockevent);
144
John Stultz75c4fd82010-04-26 20:23:11 -0700145 ret = clocksource_register_hz(&jz4740_clocksource, clk_rate);
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000146
147 if (ret)
148 printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
149
Deng-Cheng Zhu944081a2015-03-07 10:30:31 -0800150 sched_clock_register(jz4740_read_sched_clock, 16, clk_rate);
151
Paul Burton5b9cdd22015-05-24 16:11:46 +0100152 setup_irq(jz4740_clockevent.irq, &timer_irqaction);
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000153
154 ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
155
156 jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
157 jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
158
159 jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
160 jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
161
162 jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
163
164 jz4740_timer_enable(TIMER_CLOCKEVENT);
165 jz4740_timer_enable(TIMER_CLOCKSOURCE);
166}