blob: 78ed7652ef67a8016abb9c9217d9f3c02fb37dd0 [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
16#include <linux/interrupt.h>
17#include <linux/kernel.h>
18#include <linux/time.h>
19
20#include <linux/clockchips.h>
Deng-Cheng Zhu944081a2015-03-07 10:30:31 -080021#include <linux/sched_clock.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000022
Paul Burton1f4b8402015-05-24 16:11:32 +010023#include <asm/mach-jz4740/clock.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000024#include <asm/mach-jz4740/irq.h>
Thierry Reding46a98762012-09-02 11:20:34 +020025#include <asm/mach-jz4740/timer.h>
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000026#include <asm/time.h>
27
28#include "clock.h"
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000029
30#define TIMER_CLOCKEVENT 0
31#define TIMER_CLOCKSOURCE 1
32
33static uint16_t jz4740_jiffies_per_tick;
34
35static cycle_t jz4740_clocksource_read(struct clocksource *cs)
36{
37 return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
38}
39
40static struct clocksource jz4740_clocksource = {
41 .name = "jz4740-timer",
42 .rating = 200,
43 .read = jz4740_clocksource_read,
44 .mask = CLOCKSOURCE_MASK(16),
45 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
46};
47
Deng-Cheng Zhu944081a2015-03-07 10:30:31 -080048static u64 notrace jz4740_read_sched_clock(void)
49{
50 return jz4740_timer_get_count(TIMER_CLOCKSOURCE);
51}
52
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +000053static irqreturn_t jz4740_clockevent_irq(int irq, void *devid)
54{
55 struct clock_event_device *cd = devid;
56
57 jz4740_timer_ack_full(TIMER_CLOCKEVENT);
58
59 if (cd->mode != CLOCK_EVT_MODE_PERIODIC)
60 jz4740_timer_disable(TIMER_CLOCKEVENT);
61
62 cd->event_handler(cd);
63
64 return IRQ_HANDLED;
65}
66
67static void jz4740_clockevent_set_mode(enum clock_event_mode mode,
68 struct clock_event_device *cd)
69{
70 switch (mode) {
71 case CLOCK_EVT_MODE_PERIODIC:
72 jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
73 jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
74 case CLOCK_EVT_MODE_RESUME:
75 jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
76 jz4740_timer_enable(TIMER_CLOCKEVENT);
77 break;
78 case CLOCK_EVT_MODE_ONESHOT:
79 case CLOCK_EVT_MODE_SHUTDOWN:
80 jz4740_timer_disable(TIMER_CLOCKEVENT);
81 break;
82 default:
83 break;
84 }
85}
86
87static int jz4740_clockevent_set_next(unsigned long evt,
88 struct clock_event_device *cd)
89{
90 jz4740_timer_set_count(TIMER_CLOCKEVENT, 0);
91 jz4740_timer_set_period(TIMER_CLOCKEVENT, evt);
92 jz4740_timer_enable(TIMER_CLOCKEVENT);
93
94 return 0;
95}
96
97static struct clock_event_device jz4740_clockevent = {
98 .name = "jz4740-timer",
Lars-Peter Clausen1e2bbde2011-03-31 20:52:20 +020099 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000100 .set_next_event = jz4740_clockevent_set_next,
101 .set_mode = jz4740_clockevent_set_mode,
102 .rating = 200,
103 .irq = JZ4740_IRQ_TCU0,
104};
105
106static struct irqaction timer_irqaction = {
107 .handler = jz4740_clockevent_irq,
108 .flags = IRQF_PERCPU | IRQF_TIMER,
109 .name = "jz4740-timerirq",
110 .dev_id = &jz4740_clockevent,
111};
112
113void __init plat_time_init(void)
114{
115 int ret;
116 uint32_t clk_rate;
117 uint16_t ctrl;
118
Paul Burton1f4b8402015-05-24 16:11:32 +0100119 jz4740_clock_init();
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000120 jz4740_timer_init();
121
122 clk_rate = jz4740_clock_bdata.ext_rate >> 4;
123 jz4740_jiffies_per_tick = DIV_ROUND_CLOSEST(clk_rate, HZ);
124
125 clockevent_set_clock(&jz4740_clockevent, clk_rate);
126 jz4740_clockevent.min_delta_ns = clockevent_delta2ns(100, &jz4740_clockevent);
127 jz4740_clockevent.max_delta_ns = clockevent_delta2ns(0xffff, &jz4740_clockevent);
128 jz4740_clockevent.cpumask = cpumask_of(0);
129
130 clockevents_register_device(&jz4740_clockevent);
131
John Stultz75c4fd82010-04-26 20:23:11 -0700132 ret = clocksource_register_hz(&jz4740_clocksource, clk_rate);
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000133
134 if (ret)
135 printk(KERN_ERR "Failed to register clocksource: %d\n", ret);
136
Deng-Cheng Zhu944081a2015-03-07 10:30:31 -0800137 sched_clock_register(jz4740_read_sched_clock, 16, clk_rate);
138
Lars-Peter Clausenb200eff2010-06-19 04:08:10 +0000139 setup_irq(JZ4740_IRQ_TCU0, &timer_irqaction);
140
141 ctrl = JZ_TIMER_CTRL_PRESCALE_16 | JZ_TIMER_CTRL_SRC_EXT;
142
143 jz4740_timer_set_ctrl(TIMER_CLOCKEVENT, ctrl);
144 jz4740_timer_set_ctrl(TIMER_CLOCKSOURCE, ctrl);
145
146 jz4740_timer_set_period(TIMER_CLOCKEVENT, jz4740_jiffies_per_tick);
147 jz4740_timer_irq_full_enable(TIMER_CLOCKEVENT);
148
149 jz4740_timer_set_period(TIMER_CLOCKSOURCE, 0xffff);
150
151 jz4740_timer_enable(TIMER_CLOCKEVENT);
152 jz4740_timer_enable(TIMER_CLOCKSOURCE);
153}