blob: 5bbf38b916ef36839396c01cfd5bc105245ae934 [file] [log] [blame]
Michal Simekeedbdab2009-03-27 14:25:49 +01001/*
Michal Simek1e529802013-08-27 12:02:54 +02002 * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2012-2013 Xilinx, Inc.
Michal Simekeedbdab2009-03-27 14:25:49 +01004 * Copyright (C) 2007-2009 PetaLogix
5 * Copyright (C) 2006 Atmark Techno, Inc.
6 *
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
9 * for more details.
10 */
11
Michal Simekeedbdab2009-03-27 14:25:49 +010012#include <linux/interrupt.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010013#include <linux/delay.h>
14#include <linux/sched.h>
Michal Simek839396a2013-12-20 10:16:40 +010015#include <linux/sched_clock.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010016#include <linux/clk.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010017#include <linux/clockchips.h>
Michal Simekcfd4eae2013-08-27 11:52:32 +020018#include <linux/of_address.h>
Rob Herring5c9f3032013-09-07 14:05:10 -050019#include <linux/of_irq.h>
Richard Cochran5ce07a52015-01-02 20:22:09 +010020#include <linux/timecounter.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010021#include <asm/cpuinfo.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010022
Michal Simekcfd4eae2013-08-27 11:52:32 +020023static void __iomem *timer_baseaddr;
Michal Simekeedbdab2009-03-27 14:25:49 +010024
Michal Simek29e3dbb2011-02-07 11:33:47 +010025static unsigned int freq_div_hz;
26static unsigned int timer_clock_freq;
Michal Simekccea0e62010-10-07 17:39:21 +100027
Michal Simekeedbdab2009-03-27 14:25:49 +010028#define TCSR0 (0x00)
29#define TLR0 (0x04)
30#define TCR0 (0x08)
31#define TCSR1 (0x10)
32#define TLR1 (0x14)
33#define TCR1 (0x18)
34
35#define TCSR_MDT (1<<0)
36#define TCSR_UDT (1<<1)
37#define TCSR_GENT (1<<2)
38#define TCSR_CAPT (1<<3)
39#define TCSR_ARHT (1<<4)
40#define TCSR_LOAD (1<<5)
41#define TCSR_ENIT (1<<6)
42#define TCSR_ENT (1<<7)
43#define TCSR_TINT (1<<8)
44#define TCSR_PWMA (1<<9)
45#define TCSR_ENALL (1<<10)
46
Michal Simeka1715bb2014-02-24 15:04:03 +010047static unsigned int (*read_fn)(void __iomem *);
48static void (*write_fn)(u32, void __iomem *);
49
50static void timer_write32(u32 val, void __iomem *addr)
51{
52 iowrite32(val, addr);
53}
54
55static unsigned int timer_read32(void __iomem *addr)
56{
57 return ioread32(addr);
58}
59
60static void timer_write32_be(u32 val, void __iomem *addr)
61{
62 iowrite32be(val, addr);
63}
64
65static unsigned int timer_read32_be(void __iomem *addr)
66{
67 return ioread32be(addr);
68}
69
Michal Simek5955563a2013-08-27 12:04:39 +020070static inline void xilinx_timer0_stop(void)
Michal Simekeedbdab2009-03-27 14:25:49 +010071{
Michal Simeka1715bb2014-02-24 15:04:03 +010072 write_fn(read_fn(timer_baseaddr + TCSR0) & ~TCSR_ENT,
73 timer_baseaddr + TCSR0);
Michal Simekeedbdab2009-03-27 14:25:49 +010074}
75
Michal Simek5955563a2013-08-27 12:04:39 +020076static inline void xilinx_timer0_start_periodic(unsigned long load_val)
Michal Simekeedbdab2009-03-27 14:25:49 +010077{
78 if (!load_val)
79 load_val = 1;
Michal Simek9e77dab2013-08-27 09:57:52 +020080 /* loading value to timer reg */
Michal Simeka1715bb2014-02-24 15:04:03 +010081 write_fn(load_val, timer_baseaddr + TLR0);
Michal Simekeedbdab2009-03-27 14:25:49 +010082
83 /* load the initial value */
Michal Simeka1715bb2014-02-24 15:04:03 +010084 write_fn(TCSR_LOAD, timer_baseaddr + TCSR0);
Michal Simekeedbdab2009-03-27 14:25:49 +010085
86 /* see timer data sheet for detail
87 * !ENALL - don't enable 'em all
88 * !PWMA - disable pwm
89 * TINT - clear interrupt status
90 * ENT- enable timer itself
Michal Simekf7f47862011-04-05 15:49:22 +020091 * ENIT - enable interrupt
Michal Simekeedbdab2009-03-27 14:25:49 +010092 * !LOAD - clear the bit to let go
93 * ARHT - auto reload
94 * !CAPT - no external trigger
95 * !GENT - no external signal
96 * UDT - set the timer as down counter
97 * !MDT0 - generate mode
98 */
Michal Simeka1715bb2014-02-24 15:04:03 +010099 write_fn(TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT,
100 timer_baseaddr + TCSR0);
Michal Simekeedbdab2009-03-27 14:25:49 +0100101}
102
Michal Simek5955563a2013-08-27 12:04:39 +0200103static inline void xilinx_timer0_start_oneshot(unsigned long load_val)
Michal Simekeedbdab2009-03-27 14:25:49 +0100104{
105 if (!load_val)
106 load_val = 1;
Michal Simek9e77dab2013-08-27 09:57:52 +0200107 /* loading value to timer reg */
Michal Simeka1715bb2014-02-24 15:04:03 +0100108 write_fn(load_val, timer_baseaddr + TLR0);
Michal Simekeedbdab2009-03-27 14:25:49 +0100109
110 /* load the initial value */
Michal Simeka1715bb2014-02-24 15:04:03 +0100111 write_fn(TCSR_LOAD, timer_baseaddr + TCSR0);
Michal Simekeedbdab2009-03-27 14:25:49 +0100112
Michal Simeka1715bb2014-02-24 15:04:03 +0100113 write_fn(TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT,
114 timer_baseaddr + TCSR0);
Michal Simekeedbdab2009-03-27 14:25:49 +0100115}
116
Michal Simek5955563a2013-08-27 12:04:39 +0200117static int xilinx_timer_set_next_event(unsigned long delta,
Michal Simekeedbdab2009-03-27 14:25:49 +0100118 struct clock_event_device *dev)
119{
120 pr_debug("%s: next event, delta %x\n", __func__, (u32)delta);
Michal Simek5955563a2013-08-27 12:04:39 +0200121 xilinx_timer0_start_oneshot(delta);
Michal Simekeedbdab2009-03-27 14:25:49 +0100122 return 0;
123}
124
Viresh Kumar97975292015-07-16 16:56:21 +0530125static int xilinx_timer_shutdown(struct clock_event_device *evt)
Michal Simekeedbdab2009-03-27 14:25:49 +0100126{
Viresh Kumar97975292015-07-16 16:56:21 +0530127 pr_info("%s\n", __func__);
128 xilinx_timer0_stop();
129 return 0;
130}
131
132static int xilinx_timer_set_periodic(struct clock_event_device *evt)
133{
134 pr_info("%s\n", __func__);
135 xilinx_timer0_start_periodic(freq_div_hz);
136 return 0;
Michal Simekeedbdab2009-03-27 14:25:49 +0100137}
138
Michal Simek5955563a2013-08-27 12:04:39 +0200139static struct clock_event_device clockevent_xilinx_timer = {
Viresh Kumar97975292015-07-16 16:56:21 +0530140 .name = "xilinx_clockevent",
141 .features = CLOCK_EVT_FEAT_ONESHOT |
142 CLOCK_EVT_FEAT_PERIODIC,
143 .shift = 8,
144 .rating = 300,
145 .set_next_event = xilinx_timer_set_next_event,
146 .set_state_shutdown = xilinx_timer_shutdown,
147 .set_state_periodic = xilinx_timer_set_periodic,
Michal Simekeedbdab2009-03-27 14:25:49 +0100148};
149
150static inline void timer_ack(void)
151{
Michal Simeka1715bb2014-02-24 15:04:03 +0100152 write_fn(read_fn(timer_baseaddr + TCSR0), timer_baseaddr + TCSR0);
Michal Simekeedbdab2009-03-27 14:25:49 +0100153}
154
155static irqreturn_t timer_interrupt(int irq, void *dev_id)
156{
Michal Simek5955563a2013-08-27 12:04:39 +0200157 struct clock_event_device *evt = &clockevent_xilinx_timer;
Michal Simekeedbdab2009-03-27 14:25:49 +0100158#ifdef CONFIG_HEART_BEAT
Guenter Roeck79c157a2014-02-17 22:46:54 -0800159 microblaze_heartbeat();
Michal Simekeedbdab2009-03-27 14:25:49 +0100160#endif
161 timer_ack();
162 evt->event_handler(evt);
163 return IRQ_HANDLED;
164}
165
166static struct irqaction timer_irqaction = {
167 .handler = timer_interrupt,
Michal Simekdb2a7df2013-08-20 16:45:36 +0200168 .flags = IRQF_TIMER,
Michal Simekeedbdab2009-03-27 14:25:49 +0100169 .name = "timer",
Michal Simek5955563a2013-08-27 12:04:39 +0200170 .dev_id = &clockevent_xilinx_timer,
Michal Simekeedbdab2009-03-27 14:25:49 +0100171};
172
Daniel Lezcano05864212016-06-07 00:03:34 +0200173static __init int xilinx_clockevent_init(void)
Michal Simekeedbdab2009-03-27 14:25:49 +0100174{
Michal Simek5955563a2013-08-27 12:04:39 +0200175 clockevent_xilinx_timer.mult =
Michal Simekccea0e62010-10-07 17:39:21 +1000176 div_sc(timer_clock_freq, NSEC_PER_SEC,
Michal Simek5955563a2013-08-27 12:04:39 +0200177 clockevent_xilinx_timer.shift);
178 clockevent_xilinx_timer.max_delta_ns =
179 clockevent_delta2ns((u32)~0, &clockevent_xilinx_timer);
180 clockevent_xilinx_timer.min_delta_ns =
181 clockevent_delta2ns(1, &clockevent_xilinx_timer);
182 clockevent_xilinx_timer.cpumask = cpumask_of(0);
183 clockevents_register_device(&clockevent_xilinx_timer);
Daniel Lezcano05864212016-06-07 00:03:34 +0200184
185 return 0;
Michal Simekeedbdab2009-03-27 14:25:49 +0100186}
187
Michal Simek839396a2013-12-20 10:16:40 +0100188static u64 xilinx_clock_read(void)
189{
Michal Simeka1715bb2014-02-24 15:04:03 +0100190 return read_fn(timer_baseaddr + TCR1);
Michal Simek839396a2013-12-20 10:16:40 +0100191}
192
Michal Simek5955563a2013-08-27 12:04:39 +0200193static cycle_t xilinx_read(struct clocksource *cs)
Michal Simekeedbdab2009-03-27 14:25:49 +0100194{
195 /* reading actual value of timer 1 */
Michal Simek839396a2013-12-20 10:16:40 +0100196 return (cycle_t)xilinx_clock_read();
Michal Simekeedbdab2009-03-27 14:25:49 +0100197}
198
Michal Simek5955563a2013-08-27 12:04:39 +0200199static struct timecounter xilinx_tc = {
Michal Simek519e9f42009-11-06 12:31:00 +0100200 .cc = NULL,
201};
202
Michal Simek5955563a2013-08-27 12:04:39 +0200203static cycle_t xilinx_cc_read(const struct cyclecounter *cc)
Michal Simek519e9f42009-11-06 12:31:00 +0100204{
Michal Simek5955563a2013-08-27 12:04:39 +0200205 return xilinx_read(NULL);
Michal Simek519e9f42009-11-06 12:31:00 +0100206}
207
Michal Simek5955563a2013-08-27 12:04:39 +0200208static struct cyclecounter xilinx_cc = {
209 .read = xilinx_cc_read,
Michal Simek519e9f42009-11-06 12:31:00 +0100210 .mask = CLOCKSOURCE_MASK(32),
Michal Simekc8f77432010-06-10 16:04:05 +0200211 .shift = 8,
Michal Simek519e9f42009-11-06 12:31:00 +0100212};
213
Michal Simek5955563a2013-08-27 12:04:39 +0200214static int __init init_xilinx_timecounter(void)
Michal Simek519e9f42009-11-06 12:31:00 +0100215{
Michal Simek5955563a2013-08-27 12:04:39 +0200216 xilinx_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
217 xilinx_cc.shift);
Michal Simek519e9f42009-11-06 12:31:00 +0100218
Michal Simek5955563a2013-08-27 12:04:39 +0200219 timecounter_init(&xilinx_tc, &xilinx_cc, sched_clock());
Michal Simek519e9f42009-11-06 12:31:00 +0100220
221 return 0;
222}
223
Michal Simekeedbdab2009-03-27 14:25:49 +0100224static struct clocksource clocksource_microblaze = {
Michal Simek5955563a2013-08-27 12:04:39 +0200225 .name = "xilinx_clocksource",
Michal Simekeedbdab2009-03-27 14:25:49 +0100226 .rating = 300,
Michal Simek5955563a2013-08-27 12:04:39 +0200227 .read = xilinx_read,
Michal Simekeedbdab2009-03-27 14:25:49 +0100228 .mask = CLOCKSOURCE_MASK(32),
Michal Simekeedbdab2009-03-27 14:25:49 +0100229 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
230};
231
Michal Simek5955563a2013-08-27 12:04:39 +0200232static int __init xilinx_clocksource_init(void)
Michal Simekeedbdab2009-03-27 14:25:49 +0100233{
Daniel Lezcano05864212016-06-07 00:03:34 +0200234 int ret;
235
236 ret = clocksource_register_hz(&clocksource_microblaze,
237 timer_clock_freq);
238 if (ret) {
239 pr_err("failed to register clocksource");
240 return ret;
241 }
Michal Simekeedbdab2009-03-27 14:25:49 +0100242
243 /* stop timer1 */
Michal Simeka1715bb2014-02-24 15:04:03 +0100244 write_fn(read_fn(timer_baseaddr + TCSR1) & ~TCSR_ENT,
245 timer_baseaddr + TCSR1);
Michal Simekeedbdab2009-03-27 14:25:49 +0100246 /* start timer1 - up counting without interrupt */
Michal Simeka1715bb2014-02-24 15:04:03 +0100247 write_fn(TCSR_TINT|TCSR_ENT|TCSR_ARHT, timer_baseaddr + TCSR1);
Michal Simek519e9f42009-11-06 12:31:00 +0100248
249 /* register timecounter - for ftrace support */
Daniel Lezcano05864212016-06-07 00:03:34 +0200250 return init_xilinx_timecounter();
Michal Simekeedbdab2009-03-27 14:25:49 +0100251}
252
Daniel Lezcano05864212016-06-07 00:03:34 +0200253static int __init xilinx_timer_init(struct device_node *timer)
Michal Simekeedbdab2009-03-27 14:25:49 +0100254{
Michal Simekc1120542013-12-18 17:18:48 +0100255 struct clk *clk;
Michal Simek03fe0d32014-01-27 10:41:59 +0100256 static int initialized;
Michal Simek5a26cd62011-12-09 12:26:16 +0100257 u32 irq;
Michal Simekeedbdab2009-03-27 14:25:49 +0100258 u32 timer_num = 1;
Daniel Lezcano05864212016-06-07 00:03:34 +0200259 int ret;
Michal Simek9e77dab2013-08-27 09:57:52 +0200260
Michal Simek03fe0d32014-01-27 10:41:59 +0100261 if (initialized)
262 return;
263
264 initialized = 1;
265
Michal Simekcfd4eae2013-08-27 11:52:32 +0200266 timer_baseaddr = of_iomap(timer, 0);
267 if (!timer_baseaddr) {
268 pr_err("ERROR: invalid timer base address\n");
Daniel Lezcano05864212016-06-07 00:03:34 +0200269 return -ENXIO;
Michal Simekeedbdab2009-03-27 14:25:49 +0100270 }
271
Michal Simeka1715bb2014-02-24 15:04:03 +0100272 write_fn = timer_write32;
273 read_fn = timer_read32;
274
275 write_fn(TCSR_MDT, timer_baseaddr + TCSR0);
276 if (!(read_fn(timer_baseaddr + TCSR0) & TCSR_MDT)) {
277 write_fn = timer_write32_be;
278 read_fn = timer_read32_be;
279 }
280
Michal Simekcfd4eae2013-08-27 11:52:32 +0200281 irq = irq_of_parse_and_map(timer, 0);
Daniel Lezcano05864212016-06-07 00:03:34 +0200282 if (irq <= 0) {
283 pr_err("Failed to parse and map irq");
284 return -EINVAL;
285 }
Michal Simekcfd4eae2013-08-27 11:52:32 +0200286
287 of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
288 if (timer_num) {
Daniel Lezcano05864212016-06-07 00:03:34 +0200289 pr_err("Please enable two timers in HW\n");
290 return -EINVAL;
Michal Simekcfd4eae2013-08-27 11:52:32 +0200291 }
292
293 pr_info("%s: irq=%d\n", timer->full_name, irq);
Michal Simekeedbdab2009-03-27 14:25:49 +0100294
Michal Simekc1120542013-12-18 17:18:48 +0100295 clk = of_clk_get(timer, 0);
296 if (IS_ERR(clk)) {
297 pr_err("ERROR: timer CCF input clock not found\n");
298 /* If there is clock-frequency property than use it */
299 of_property_read_u32(timer, "clock-frequency",
300 &timer_clock_freq);
301 } else {
302 timer_clock_freq = clk_get_rate(clk);
303 }
304
305 if (!timer_clock_freq) {
306 pr_err("ERROR: Using CPU clock frequency\n");
Michal Simekccea0e62010-10-07 17:39:21 +1000307 timer_clock_freq = cpuinfo.cpu_clock_freq;
Michal Simekc1120542013-12-18 17:18:48 +0100308 }
Michal Simekccea0e62010-10-07 17:39:21 +1000309
310 freq_div_hz = timer_clock_freq / HZ;
Michal Simekeedbdab2009-03-27 14:25:49 +0100311
Daniel Lezcano05864212016-06-07 00:03:34 +0200312 ret = setup_irq(irq, &timer_irqaction);
313 if (ret) {
314 pr_err("Failed to setup IRQ");
315 return ret;
316 }
317
Michal Simekeedbdab2009-03-27 14:25:49 +0100318#ifdef CONFIG_HEART_BEAT
Guenter Roeck79c157a2014-02-17 22:46:54 -0800319 microblaze_setup_heartbeat();
Michal Simekeedbdab2009-03-27 14:25:49 +0100320#endif
Daniel Lezcano05864212016-06-07 00:03:34 +0200321
322 ret = xilinx_clocksource_init();
323 if (ret)
324 return ret;
325
326 ret = xilinx_clockevent_init();
327 if (ret)
328 return ret;
Michal Simek6f34b082010-04-16 09:50:13 +0200329
Michal Simek839396a2013-12-20 10:16:40 +0100330 sched_clock_register(xilinx_clock_read, 32, timer_clock_freq);
Daniel Lezcano05864212016-06-07 00:03:34 +0200331
332 return 0;
Michal Simekeedbdab2009-03-27 14:25:49 +0100333}
Michal Simek4bcd9432013-08-27 11:13:29 +0200334
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200335CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",
Michal Simek4bcd9432013-08-27 11:13:29 +0200336 xilinx_timer_init);