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