]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/microblaze/kernel/timer.c
MIPS: Remove unneeded #ifdef __KERNEL__ from asm/processor.h
[mirror_ubuntu-artful-kernel.git] / arch / microblaze / kernel / timer.c
CommitLineData
eedbdab9 1/*
1e529803
MS
2 * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2012-2013 Xilinx, Inc.
eedbdab9
MS
4 * 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
eedbdab9 12#include <linux/interrupt.h>
eedbdab9
MS
13#include <linux/delay.h>
14#include <linux/sched.h>
839396ab 15#include <linux/sched_clock.h>
eedbdab9 16#include <linux/clk.h>
eedbdab9 17#include <linux/clockchips.h>
cfd4eaef 18#include <linux/of_address.h>
5c9f303e 19#include <linux/of_irq.h>
eedbdab9 20#include <asm/cpuinfo.h>
eedbdab9 21
cfd4eaef 22static void __iomem *timer_baseaddr;
eedbdab9 23
29e3dbb1
MS
24static unsigned int freq_div_hz;
25static unsigned int timer_clock_freq;
ccea0e6e 26
eedbdab9
MS
27#define TCSR0 (0x00)
28#define TLR0 (0x04)
29#define TCR0 (0x08)
30#define TCSR1 (0x10)
31#define TLR1 (0x14)
32#define TCR1 (0x18)
33
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)
45
a1715bb7
MS
46static unsigned int (*read_fn)(void __iomem *);
47static void (*write_fn)(u32, void __iomem *);
48
49static void timer_write32(u32 val, void __iomem *addr)
50{
51 iowrite32(val, addr);
52}
53
54static unsigned int timer_read32(void __iomem *addr)
55{
56 return ioread32(addr);
57}
58
59static void timer_write32_be(u32 val, void __iomem *addr)
60{
61 iowrite32be(val, addr);
62}
63
64static unsigned int timer_read32_be(void __iomem *addr)
65{
66 return ioread32be(addr);
67}
68
5955563a 69static inline void xilinx_timer0_stop(void)
eedbdab9 70{
a1715bb7
MS
71 write_fn(read_fn(timer_baseaddr + TCSR0) & ~TCSR_ENT,
72 timer_baseaddr + TCSR0);
eedbdab9
MS
73}
74
5955563a 75static inline void xilinx_timer0_start_periodic(unsigned long load_val)
eedbdab9
MS
76{
77 if (!load_val)
78 load_val = 1;
9e77dab6 79 /* loading value to timer reg */
a1715bb7 80 write_fn(load_val, timer_baseaddr + TLR0);
eedbdab9
MS
81
82 /* load the initial value */
a1715bb7 83 write_fn(TCSR_LOAD, timer_baseaddr + TCSR0);
eedbdab9
MS
84
85 /* see timer data sheet for detail
86 * !ENALL - don't enable 'em all
87 * !PWMA - disable pwm
88 * TINT - clear interrupt status
89 * ENT- enable timer itself
f7f4786c 90 * ENIT - enable interrupt
eedbdab9
MS
91 * !LOAD - clear the bit to let go
92 * ARHT - auto reload
93 * !CAPT - no external trigger
94 * !GENT - no external signal
95 * UDT - set the timer as down counter
96 * !MDT0 - generate mode
97 */
a1715bb7
MS
98 write_fn(TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT,
99 timer_baseaddr + TCSR0);
eedbdab9
MS
100}
101
5955563a 102static inline void xilinx_timer0_start_oneshot(unsigned long load_val)
eedbdab9
MS
103{
104 if (!load_val)
105 load_val = 1;
9e77dab6 106 /* loading value to timer reg */
a1715bb7 107 write_fn(load_val, timer_baseaddr + TLR0);
eedbdab9
MS
108
109 /* load the initial value */
a1715bb7 110 write_fn(TCSR_LOAD, timer_baseaddr + TCSR0);
eedbdab9 111
a1715bb7
MS
112 write_fn(TCSR_TINT|TCSR_ENIT|TCSR_ENT|TCSR_ARHT|TCSR_UDT,
113 timer_baseaddr + TCSR0);
eedbdab9
MS
114}
115
5955563a 116static int xilinx_timer_set_next_event(unsigned long delta,
eedbdab9
MS
117 struct clock_event_device *dev)
118{
119 pr_debug("%s: next event, delta %x\n", __func__, (u32)delta);
5955563a 120 xilinx_timer0_start_oneshot(delta);
eedbdab9
MS
121 return 0;
122}
123
5955563a 124static void xilinx_timer_set_mode(enum clock_event_mode mode,
eedbdab9
MS
125 struct clock_event_device *evt)
126{
127 switch (mode) {
128 case CLOCK_EVT_MODE_PERIODIC:
aaa5241e 129 pr_info("%s: periodic\n", __func__);
5955563a 130 xilinx_timer0_start_periodic(freq_div_hz);
eedbdab9
MS
131 break;
132 case CLOCK_EVT_MODE_ONESHOT:
aaa5241e 133 pr_info("%s: oneshot\n", __func__);
eedbdab9
MS
134 break;
135 case CLOCK_EVT_MODE_UNUSED:
aaa5241e 136 pr_info("%s: unused\n", __func__);
eedbdab9
MS
137 break;
138 case CLOCK_EVT_MODE_SHUTDOWN:
aaa5241e 139 pr_info("%s: shutdown\n", __func__);
5955563a 140 xilinx_timer0_stop();
eedbdab9
MS
141 break;
142 case CLOCK_EVT_MODE_RESUME:
aaa5241e 143 pr_info("%s: resume\n", __func__);
eedbdab9
MS
144 break;
145 }
146}
147
5955563a
MS
148static struct clock_event_device clockevent_xilinx_timer = {
149 .name = "xilinx_clockevent",
eedbdab9 150 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
c8f77436 151 .shift = 8,
eedbdab9 152 .rating = 300,
5955563a
MS
153 .set_next_event = xilinx_timer_set_next_event,
154 .set_mode = xilinx_timer_set_mode,
eedbdab9
MS
155};
156
157static inline void timer_ack(void)
158{
a1715bb7 159 write_fn(read_fn(timer_baseaddr + TCSR0), timer_baseaddr + TCSR0);
eedbdab9
MS
160}
161
162static irqreturn_t timer_interrupt(int irq, void *dev_id)
163{
5955563a 164 struct clock_event_device *evt = &clockevent_xilinx_timer;
eedbdab9 165#ifdef CONFIG_HEART_BEAT
79c157a3 166 microblaze_heartbeat();
eedbdab9
MS
167#endif
168 timer_ack();
169 evt->event_handler(evt);
170 return IRQ_HANDLED;
171}
172
173static struct irqaction timer_irqaction = {
174 .handler = timer_interrupt,
db2a7df0 175 .flags = IRQF_TIMER,
eedbdab9 176 .name = "timer",
5955563a 177 .dev_id = &clockevent_xilinx_timer,
eedbdab9
MS
178};
179
5955563a 180static __init void xilinx_clockevent_init(void)
eedbdab9 181{
5955563a 182 clockevent_xilinx_timer.mult =
ccea0e6e 183 div_sc(timer_clock_freq, NSEC_PER_SEC,
5955563a
MS
184 clockevent_xilinx_timer.shift);
185 clockevent_xilinx_timer.max_delta_ns =
186 clockevent_delta2ns((u32)~0, &clockevent_xilinx_timer);
187 clockevent_xilinx_timer.min_delta_ns =
188 clockevent_delta2ns(1, &clockevent_xilinx_timer);
189 clockevent_xilinx_timer.cpumask = cpumask_of(0);
190 clockevents_register_device(&clockevent_xilinx_timer);
eedbdab9
MS
191}
192
839396ab
MS
193static u64 xilinx_clock_read(void)
194{
a1715bb7 195 return read_fn(timer_baseaddr + TCR1);
839396ab
MS
196}
197
5955563a 198static cycle_t xilinx_read(struct clocksource *cs)
eedbdab9
MS
199{
200 /* reading actual value of timer 1 */
839396ab 201 return (cycle_t)xilinx_clock_read();
eedbdab9
MS
202}
203
5955563a 204static struct timecounter xilinx_tc = {
519e9f41
MS
205 .cc = NULL,
206};
207
5955563a 208static cycle_t xilinx_cc_read(const struct cyclecounter *cc)
519e9f41 209{
5955563a 210 return xilinx_read(NULL);
519e9f41
MS
211}
212
5955563a
MS
213static struct cyclecounter xilinx_cc = {
214 .read = xilinx_cc_read,
519e9f41 215 .mask = CLOCKSOURCE_MASK(32),
c8f77436 216 .shift = 8,
519e9f41
MS
217};
218
5955563a 219static int __init init_xilinx_timecounter(void)
519e9f41 220{
5955563a
MS
221 xilinx_cc.mult = div_sc(timer_clock_freq, NSEC_PER_SEC,
222 xilinx_cc.shift);
519e9f41 223
5955563a 224 timecounter_init(&xilinx_tc, &xilinx_cc, sched_clock());
519e9f41
MS
225
226 return 0;
227}
228
eedbdab9 229static struct clocksource clocksource_microblaze = {
5955563a 230 .name = "xilinx_clocksource",
eedbdab9 231 .rating = 300,
5955563a 232 .read = xilinx_read,
eedbdab9 233 .mask = CLOCKSOURCE_MASK(32),
eedbdab9
MS
234 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
235};
236
5955563a 237static int __init xilinx_clocksource_init(void)
eedbdab9 238{
b8f39f7d 239 if (clocksource_register_hz(&clocksource_microblaze, timer_clock_freq))
eedbdab9
MS
240 panic("failed to register clocksource");
241
242 /* stop timer1 */
a1715bb7
MS
243 write_fn(read_fn(timer_baseaddr + TCSR1) & ~TCSR_ENT,
244 timer_baseaddr + TCSR1);
eedbdab9 245 /* start timer1 - up counting without interrupt */
a1715bb7 246 write_fn(TCSR_TINT|TCSR_ENT|TCSR_ARHT, timer_baseaddr + TCSR1);
519e9f41
MS
247
248 /* register timecounter - for ftrace support */
5955563a 249 init_xilinx_timecounter();
eedbdab9
MS
250 return 0;
251}
252
4bcd943e 253static void __init xilinx_timer_init(struct device_node *timer)
eedbdab9 254{
c1120542 255 struct clk *clk;
03fe0d3c 256 static int initialized;
5a26cd69 257 u32 irq;
eedbdab9 258 u32 timer_num = 1;
cfd4eaef 259
03fe0d3c
MS
260 if (initialized)
261 return;
262
263 initialized = 1;
264
cfd4eaef
MS
265 timer_baseaddr = of_iomap(timer, 0);
266 if (!timer_baseaddr) {
267 pr_err("ERROR: invalid timer base address\n");
268 BUG();
269 }
9e77dab6 270
a1715bb7
MS
271 write_fn = timer_write32;
272 read_fn = timer_read32;
273
274 write_fn(TCSR_MDT, timer_baseaddr + TCSR0);
275 if (!(read_fn(timer_baseaddr + TCSR0) & TCSR_MDT)) {
276 write_fn = timer_write32_be;
277 read_fn = timer_read32_be;
278 }
279
9d0ced00 280 irq = irq_of_parse_and_map(timer, 0);
cfd4eaef
MS
281
282 of_property_read_u32(timer, "xlnx,one-timer-only", &timer_num);
eedbdab9 283 if (timer_num) {
cfd4eaef 284 pr_emerg("Please enable two timers in HW\n");
eedbdab9
MS
285 BUG();
286 }
287
cfd4eaef 288 pr_info("%s: irq=%d\n", timer->full_name, irq);
eedbdab9 289
c1120542
MS
290 clk = of_clk_get(timer, 0);
291 if (IS_ERR(clk)) {
292 pr_err("ERROR: timer CCF input clock not found\n");
293 /* If there is clock-frequency property than use it */
294 of_property_read_u32(timer, "clock-frequency",
295 &timer_clock_freq);
296 } else {
297 timer_clock_freq = clk_get_rate(clk);
298 }
299
300 if (!timer_clock_freq) {
301 pr_err("ERROR: Using CPU clock frequency\n");
ccea0e6e 302 timer_clock_freq = cpuinfo.cpu_clock_freq;
c1120542 303 }
ccea0e6e
MS
304
305 freq_div_hz = timer_clock_freq / HZ;
eedbdab9
MS
306
307 setup_irq(irq, &timer_irqaction);
308#ifdef CONFIG_HEART_BEAT
79c157a3 309 microblaze_setup_heartbeat();
eedbdab9 310#endif
5955563a
MS
311 xilinx_clocksource_init();
312 xilinx_clockevent_init();
9c6f6f54 313
839396ab 314 sched_clock_register(xilinx_clock_read, 32, timer_clock_freq);
eedbdab9 315}
4bcd943e
MS
316
317CLOCKSOURCE_OF_DECLARE(xilinx_timer, "xlnx,xps-timer-1.00.a",
318 xilinx_timer_init);