2 * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2012-2013 Xilinx, Inc.
4 * Copyright (C) 2007-2009 PetaLogix
5 * Copyright (C) 2006 Atmark Techno, Inc.
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/sched.h>
15 #include <linux/sched_clock.h>
16 #include <linux/clk.h>
17 #include <linux/clockchips.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <asm/cpuinfo.h>
22 static void __iomem *timer_baseaddr;
24 static unsigned int freq_div_hz;
25 static unsigned int timer_clock_freq;
34 #define TCSR_MDT (1<<0)
35 #define TCSR_UDT (1<<1)
36 #define TCSR_GENT (1<<2)
37 #define TCSR_CAPT (1<<3)
38 #define TCSR_ARHT (1<<4)
39 #define TCSR_LOAD (1<<5)
40 #define TCSR_ENIT (1<<6)
41 #define TCSR_ENT (1<<7)
42 #define TCSR_TINT (1<<8)
43 #define TCSR_PWMA (1<<9)
44 #define TCSR_ENALL (1<<10)
46 static inline void xilinx_timer0_stop(void)
48 out_be32(timer_baseaddr + TCSR0,
49 in_be32(timer_baseaddr + TCSR0) & ~TCSR_ENT);
52 static inline void xilinx_timer0_start_periodic(unsigned long load_val)
56 /* loading value to timer reg */
57 out_be32(timer_baseaddr + TLR0, load_val);
59 /* load the initial value */
60 out_be32(timer_baseaddr + TCSR0, TCSR_LOAD);
62 /* see timer data sheet for detail
63 * !ENALL - don't enable 'em all
65 * TINT - clear interrupt status
66 * ENT- enable timer itself
67 * ENIT - enable interrupt
68 * !LOAD - clear the bit to let go
70 * !CAPT - no external trigger
71 * !GENT - no external signal
72 * UDT - set the timer as down counter
73 * !MDT0 - generate mode
75 out_be32(timer_baseaddr + TCSR0,
76 TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
79 static inline void xilinx_timer0_start_oneshot(unsigned long load_val)
83 /* loading value to timer reg */
84 out_be32(timer_baseaddr + TLR0, load_val);
86 /* load the initial value */
87 out_be32(timer_baseaddr + TCSR0, TCSR_LOAD);
89 out_be32(timer_baseaddr + TCSR0,
90 TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
93 static int xilinx_timer_set_next_event(unsigned long delta,
94 struct clock_event_device *dev)
96 pr_debug("%s: next event, delta %x\n", __func__, (u32)delta);
97 xilinx_timer0_start_oneshot(delta);
101 static void xilinx_timer_set_mode(enum clock_event_mode mode,
102 struct clock_event_device *evt)
105 case CLOCK_EVT_MODE_PERIODIC:
106 pr_info("%s: periodic\n", __func__);
107 xilinx_timer0_start_periodic(freq_div_hz);
109 case CLOCK_EVT_MODE_ONESHOT:
110 pr_info("%s: oneshot\n", __func__);
112 case CLOCK_EVT_MODE_UNUSED:
113 pr_info("%s: unused\n", __func__);
115 case CLOCK_EVT_MODE_SHUTDOWN:
116 pr_info("%s: shutdown\n", __func__);
117 xilinx_timer0_stop();
119 case CLOCK_EVT_MODE_RESUME:
120 pr_info("%s: resume\n", __func__);
125 static struct clock_event_device clockevent_xilinx_timer = {
126 .name = "xilinx_clockevent",
127 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
130 .set_next_event = xilinx_timer_set_next_event,
131 .set_mode = xilinx_timer_set_mode,
134 static inline void timer_ack(void)
136 out_be32(timer_baseaddr + TCSR0, in_be32(timer_baseaddr + TCSR0));
139 static irqreturn_t timer_interrupt(int irq, void *dev_id)
141 struct clock_event_device *evt = &clockevent_xilinx_timer;
142 #ifdef CONFIG_HEART_BEAT
146 evt->event_handler(evt);
150 static struct irqaction timer_irqaction = {
151 .handler = timer_interrupt,
154 .dev_id = &clockevent_xilinx_timer,
157 static __init void xilinx_clockevent_init(void)
159 clockevent_xilinx_timer.mult =
160 div_sc(timer_clock_freq, NSEC_PER_SEC,
161 clockevent_xilinx_timer.shift);
162 clockevent_xilinx_timer.max_delta_ns =
163 clockevent_delta2ns((u32)~0, &clockevent_xilinx_timer);
164 clockevent_xilinx_timer.min_delta_ns =
165 clockevent_delta2ns(1, &clockevent_xilinx_timer);
166 clockevent_xilinx_timer.cpumask = cpumask_of(0);
167 clockevents_register_device(&clockevent_xilinx_timer);
170 static u64 xilinx_clock_read(void)
172 return in_be32(timer_baseaddr + TCR1);
175 static cycle_t xilinx_read(struct clocksource *cs)
177 /* reading actual value of timer 1 */
178 return (cycle_t)xilinx_clock_read();
181 static struct timecounter xilinx_tc = {
185 static cycle_t xilinx_cc_read(const struct cyclecounter *cc)
187 return xilinx_read(NULL);
190 static struct cyclecounter xilinx_cc = {
191 .read = xilinx_cc_read,
192 .mask = CLOCKSOURCE_MASK(32),
196 static int __init init_xilinx_timecounter(void)
198 xilinx_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
201 timecounter_init(&xilinx_tc, &xilinx_cc, sched_clock());
206 static struct clocksource clocksource_microblaze = {
207 .name = "xilinx_clocksource",
210 .mask = CLOCKSOURCE_MASK(32),
211 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
214 static int __init xilinx_clocksource_init(void)
216 if (clocksource_register_hz(&clocksource_microblaze, timer_clock_freq))
217 panic("failed to register clocksource");
220 out_be32(timer_baseaddr + TCSR1,
221 in_be32(timer_baseaddr + TCSR1) & ~TCSR_ENT);
222 /* start timer1 - up counting without interrupt */
223 out_be32(timer_baseaddr + TCSR1, TCSR_TINT|TCSR_ENT|TCSR_ARHT);
225 /* register timecounter - for ftrace support */
226 init_xilinx_timecounter();
230 static void __init xilinx_timer_init(struct device_node *timer)
233 static int initialized;
242 timer_baseaddr = of_iomap(timer, 0);
243 if (!timer_baseaddr) {
244 pr_err("ERROR: invalid timer base address\n");
248 irq = irq_of_parse_and_map(timer, 0);
250 of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
252 pr_emerg("Please enable two timers in HW\n");
256 pr_info("%s: irq=%d\n", timer->full_name, irq);
258 clk = of_clk_get(timer, 0);
260 pr_err("ERROR: timer CCF input clock not found\n");
261 /* If there is clock-frequency property than use it */
262 of_property_read_u32(timer, "clock-frequency",
265 timer_clock_freq = clk_get_rate(clk);
268 if (!timer_clock_freq) {
269 pr_err("ERROR: Using CPU clock frequency\n");
270 timer_clock_freq = cpuinfo.cpu_clock_freq;
273 freq_div_hz = timer_clock_freq / HZ;
275 setup_irq(irq, &timer_irqaction);
276 #ifdef CONFIG_HEART_BEAT
279 xilinx_clocksource_init();
280 xilinx_clockevent_init();
282 sched_clock_register(xilinx_clock_read, 32, timer_clock_freq);
285 CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",