blob: 1b803960717d1398d7b84b52b0cacecab7965a00 [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 Simekeedbdab2009-03-27 14:25:49 +010015#include <linux/clk.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010016#include <linux/clockchips.h>
Michal Simekcfd4eae2013-08-27 11:52:32 +020017#include <linux/of_address.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010018#include <asm/cpuinfo.h>
Michal Simekc8f77432010-06-10 16:04:05 +020019#include <linux/cnt32_to_63.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010020
Michal Simekcfd4eae2013-08-27 11:52:32 +020021static void __iomem *timer_baseaddr;
Michal Simekeedbdab2009-03-27 14:25:49 +010022
Michal Simek29e3dbb2011-02-07 11:33:47 +010023static unsigned int freq_div_hz;
24static unsigned int timer_clock_freq;
Michal Simekccea0e62010-10-07 17:39:21 +100025
Michal Simekeedbdab2009-03-27 14:25:49 +010026#define TCSR0 (0x00)
27#define TLR0 (0x04)
28#define TCR0 (0x08)
29#define TCSR1 (0x10)
30#define TLR1 (0x14)
31#define TCR1 (0x18)
32
33#define TCSR_MDT (1<<0)
34#define TCSR_UDT (1<<1)
35#define TCSR_GENT (1<<2)
36#define TCSR_CAPT (1<<3)
37#define TCSR_ARHT (1<<4)
38#define TCSR_LOAD (1<<5)
39#define TCSR_ENIT (1<<6)
40#define TCSR_ENT (1<<7)
41#define TCSR_TINT (1<<8)
42#define TCSR_PWMA (1<<9)
43#define TCSR_ENALL (1<<10)
44
45static inline void microblaze_timer0_stop(void)
46{
Michal Simek9e77dab2013-08-27 09:57:52 +020047 out_be32(timer_baseaddr + TCSR0,
48 in_be32(timer_baseaddr + TCSR0) & ~TCSR_ENT);
Michal Simekeedbdab2009-03-27 14:25:49 +010049}
50
51static inline void microblaze_timer0_start_periodic(unsigned long load_val)
52{
53 if (!load_val)
54 load_val = 1;
Michal Simek9e77dab2013-08-27 09:57:52 +020055 /* loading value to timer reg */
56 out_be32(timer_baseaddr + TLR0, load_val);
Michal Simekeedbdab2009-03-27 14:25:49 +010057
58 /* load the initial value */
Michal Simek9e77dab2013-08-27 09:57:52 +020059 out_be32(timer_baseaddr + TCSR0, TCSR_LOAD);
Michal Simekeedbdab2009-03-27 14:25:49 +010060
61 /* see timer data sheet for detail
62 * !ENALL - don't enable 'em all
63 * !PWMA - disable pwm
64 * TINT - clear interrupt status
65 * ENT- enable timer itself
Michal Simekf7f47862011-04-05 15:49:22 +020066 * ENIT - enable interrupt
Michal Simekeedbdab2009-03-27 14:25:49 +010067 * !LOAD - clear the bit to let go
68 * ARHT - auto reload
69 * !CAPT - no external trigger
70 * !GENT - no external signal
71 * UDT - set the timer as down counter
72 * !MDT0 - generate mode
73 */
Michal Simek9e77dab2013-08-27 09:57:52 +020074 out_be32(timer_baseaddr + TCSR0,
Michal Simekeedbdab2009-03-27 14:25:49 +010075 TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
76}
77
78static inline void microblaze_timer0_start_oneshot(unsigned long load_val)
79{
80 if (!load_val)
81 load_val = 1;
Michal Simek9e77dab2013-08-27 09:57:52 +020082 /* loading value to timer reg */
83 out_be32(timer_baseaddr + TLR0, load_val);
Michal Simekeedbdab2009-03-27 14:25:49 +010084
85 /* load the initial value */
Michal Simek9e77dab2013-08-27 09:57:52 +020086 out_be32(timer_baseaddr + TCSR0, TCSR_LOAD);
Michal Simekeedbdab2009-03-27 14:25:49 +010087
Michal Simek9e77dab2013-08-27 09:57:52 +020088 out_be32(timer_baseaddr + TCSR0,
Michal Simekeedbdab2009-03-27 14:25:49 +010089 TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT);
90}
91
92static int microblaze_timer_set_next_event(unsigned long delta,
93 struct clock_event_device *dev)
94{
95 pr_debug("%s: next event, delta %x\n", __func__, (u32)delta);
96 microblaze_timer0_start_oneshot(delta);
97 return 0;
98}
99
100static void microblaze_timer_set_mode(enum clock_event_mode mode,
101 struct clock_event_device *evt)
102{
103 switch (mode) {
104 case CLOCK_EVT_MODE_PERIODIC:
Michal Simekaaa52412012-10-04 14:24:58 +0200105 pr_info("%s: periodic\n", __func__);
Michal Simekccea0e62010-10-07 17:39:21 +1000106 microblaze_timer0_start_periodic(freq_div_hz);
Michal Simekeedbdab2009-03-27 14:25:49 +0100107 break;
108 case CLOCK_EVT_MODE_ONESHOT:
Michal Simekaaa52412012-10-04 14:24:58 +0200109 pr_info("%s: oneshot\n", __func__);
Michal Simekeedbdab2009-03-27 14:25:49 +0100110 break;
111 case CLOCK_EVT_MODE_UNUSED:
Michal Simekaaa52412012-10-04 14:24:58 +0200112 pr_info("%s: unused\n", __func__);
Michal Simekeedbdab2009-03-27 14:25:49 +0100113 break;
114 case CLOCK_EVT_MODE_SHUTDOWN:
Michal Simekaaa52412012-10-04 14:24:58 +0200115 pr_info("%s: shutdown\n", __func__);
Michal Simekeedbdab2009-03-27 14:25:49 +0100116 microblaze_timer0_stop();
117 break;
118 case CLOCK_EVT_MODE_RESUME:
Michal Simekaaa52412012-10-04 14:24:58 +0200119 pr_info("%s: resume\n", __func__);
Michal Simekeedbdab2009-03-27 14:25:49 +0100120 break;
121 }
122}
123
124static struct clock_event_device clockevent_microblaze_timer = {
125 .name = "microblaze_clockevent",
126 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
Michal Simekc8f77432010-06-10 16:04:05 +0200127 .shift = 8,
Michal Simekeedbdab2009-03-27 14:25:49 +0100128 .rating = 300,
129 .set_next_event = microblaze_timer_set_next_event,
130 .set_mode = microblaze_timer_set_mode,
131};
132
133static inline void timer_ack(void)
134{
Michal Simek9e77dab2013-08-27 09:57:52 +0200135 out_be32(timer_baseaddr + TCSR0, in_be32(timer_baseaddr + TCSR0));
Michal Simekeedbdab2009-03-27 14:25:49 +0100136}
137
138static irqreturn_t timer_interrupt(int irq, void *dev_id)
139{
140 struct clock_event_device *evt = &clockevent_microblaze_timer;
141#ifdef CONFIG_HEART_BEAT
142 heartbeat();
143#endif
144 timer_ack();
145 evt->event_handler(evt);
146 return IRQ_HANDLED;
147}
148
149static struct irqaction timer_irqaction = {
150 .handler = timer_interrupt,
151 .flags = IRQF_DISABLED | IRQF_TIMER,
152 .name = "timer",
153 .dev_id = &clockevent_microblaze_timer,
154};
155
156static __init void microblaze_clockevent_init(void)
157{
158 clockevent_microblaze_timer.mult =
Michal Simekccea0e62010-10-07 17:39:21 +1000159 div_sc(timer_clock_freq, NSEC_PER_SEC,
Michal Simekeedbdab2009-03-27 14:25:49 +0100160 clockevent_microblaze_timer.shift);
161 clockevent_microblaze_timer.max_delta_ns =
162 clockevent_delta2ns((u32)~0, &clockevent_microblaze_timer);
163 clockevent_microblaze_timer.min_delta_ns =
164 clockevent_delta2ns(1, &clockevent_microblaze_timer);
165 clockevent_microblaze_timer.cpumask = cpumask_of(0);
166 clockevents_register_device(&clockevent_microblaze_timer);
167}
168
Coly Lif57f2fe2009-04-23 03:05:31 +0800169static cycle_t microblaze_read(struct clocksource *cs)
Michal Simekeedbdab2009-03-27 14:25:49 +0100170{
171 /* reading actual value of timer 1 */
Michal Simek9e77dab2013-08-27 09:57:52 +0200172 return (cycle_t) (in_be32(timer_baseaddr + TCR1));
Michal Simekeedbdab2009-03-27 14:25:49 +0100173}
174
Michal Simek519e9f42009-11-06 12:31:00 +0100175static struct timecounter microblaze_tc = {
176 .cc = NULL,
177};
178
179static cycle_t microblaze_cc_read(const struct cyclecounter *cc)
180{
181 return microblaze_read(NULL);
182}
183
184static struct cyclecounter microblaze_cc = {
185 .read = microblaze_cc_read,
186 .mask = CLOCKSOURCE_MASK(32),
Michal Simekc8f77432010-06-10 16:04:05 +0200187 .shift = 8,
Michal Simek519e9f42009-11-06 12:31:00 +0100188};
189
Michal Simek29e3dbb2011-02-07 11:33:47 +0100190static int __init init_microblaze_timecounter(void)
Michal Simek519e9f42009-11-06 12:31:00 +0100191{
Michal Simekccea0e62010-10-07 17:39:21 +1000192 microblaze_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
Michal Simek519e9f42009-11-06 12:31:00 +0100193 microblaze_cc.shift);
194
195 timecounter_init(&microblaze_tc, &microblaze_cc, sched_clock());
196
197 return 0;
198}
199
Michal Simekeedbdab2009-03-27 14:25:49 +0100200static struct clocksource clocksource_microblaze = {
201 .name = "microblaze_clocksource",
202 .rating = 300,
203 .read = microblaze_read,
204 .mask = CLOCKSOURCE_MASK(32),
Michal Simekeedbdab2009-03-27 14:25:49 +0100205 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
206};
207
208static int __init microblaze_clocksource_init(void)
209{
John Stultzb8f39f72010-04-26 20:22:23 -0700210 if (clocksource_register_hz(&clocksource_microblaze, timer_clock_freq))
Michal Simekeedbdab2009-03-27 14:25:49 +0100211 panic("failed to register clocksource");
212
213 /* stop timer1 */
Michal Simek9e77dab2013-08-27 09:57:52 +0200214 out_be32(timer_baseaddr + TCSR1,
215 in_be32(timer_baseaddr + TCSR1) & ~TCSR_ENT);
Michal Simekeedbdab2009-03-27 14:25:49 +0100216 /* start timer1 - up counting without interrupt */
Michal Simek9e77dab2013-08-27 09:57:52 +0200217 out_be32(timer_baseaddr + TCSR1, TCSR_TINT|TCSR_ENT|TCSR_ARHT);
Michal Simek519e9f42009-11-06 12:31:00 +0100218
219 /* register timecounter - for ftrace support */
220 init_microblaze_timecounter();
Michal Simekeedbdab2009-03-27 14:25:49 +0100221 return 0;
222}
223
Michal Simek6f34b082010-04-16 09:50:13 +0200224/*
225 * We have to protect accesses before timer initialization
226 * and return 0 for sched_clock function below.
227 */
228static int timer_initialized;
229
Michal Simek4bcd9432013-08-27 11:13:29 +0200230static void __init xilinx_timer_init(struct device_node *timer)
Michal Simekeedbdab2009-03-27 14:25:49 +0100231{
Michal Simek5a26cd62011-12-09 12:26:16 +0100232 u32 irq;
Michal Simekeedbdab2009-03-27 14:25:49 +0100233 u32 timer_num = 1;
Michal Simekcfd4eae2013-08-27 11:52:32 +0200234 int ret;
Michal Simek9e77dab2013-08-27 09:57:52 +0200235
Michal Simekcfd4eae2013-08-27 11:52:32 +0200236 timer_baseaddr = of_iomap(timer, 0);
237 if (!timer_baseaddr) {
238 pr_err("ERROR: invalid timer base address\n");
Michal Simekeedbdab2009-03-27 14:25:49 +0100239 BUG();
240 }
241
Michal Simekcfd4eae2013-08-27 11:52:32 +0200242 irq = irq_of_parse_and_map(timer, 0);
243
244 of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
245 if (timer_num) {
246 pr_emerg("Please enable two timers in HW\n");
247 BUG();
248 }
249
250 pr_info("%s: irq=%d\n", timer->full_name, irq);
Michal Simekeedbdab2009-03-27 14:25:49 +0100251
Michal Simekccea0e62010-10-07 17:39:21 +1000252 /* If there is clock-frequency property than use it */
Michal Simekcfd4eae2013-08-27 11:52:32 +0200253 ret = of_property_read_u32(timer, "clock-frequency", &timer_clock_freq);
254 if (ret < 0)
Michal Simekccea0e62010-10-07 17:39:21 +1000255 timer_clock_freq = cpuinfo.cpu_clock_freq;
256
257 freq_div_hz = timer_clock_freq / HZ;
Michal Simekeedbdab2009-03-27 14:25:49 +0100258
259 setup_irq(irq, &timer_irqaction);
260#ifdef CONFIG_HEART_BEAT
261 setup_heartbeat();
262#endif
263 microblaze_clocksource_init();
264 microblaze_clockevent_init();
Michal Simek6f34b082010-04-16 09:50:13 +0200265 timer_initialized = 1;
266}
267
268unsigned long long notrace sched_clock(void)
269{
270 if (timer_initialized) {
271 struct clocksource *cs = &clocksource_microblaze;
Michal Simek9c6f6f52011-09-23 09:52:24 +0200272
273 cycle_t cyc = cnt32_to_63(cs->read(NULL)) & LLONG_MAX;
Michal Simek6f34b082010-04-16 09:50:13 +0200274 return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
275 }
276 return 0;
Michal Simekeedbdab2009-03-27 14:25:49 +0100277}
Michal Simek4bcd9432013-08-27 11:13:29 +0200278
279CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",
280 xilinx_timer_init);