blob: c21c91c2bc5681b0c505385e642f366e4a3a6d88 [file] [log] [blame]
Thomas Gleixnera636cd62019-05-19 15:51:34 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Barry Song4898de32012-12-20 19:37:32 +08002/*
3 * System timer for CSR SiRFprimaII
4 *
5 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
Barry Song4898de32012-12-20 19:37:32 +08006 */
7
8#include <linux/kernel.h>
9#include <linux/interrupt.h>
10#include <linux/clockchips.h>
11#include <linux/clocksource.h>
Stephen Boyd05a65482013-02-15 17:02:16 -080012#include <linux/cpu.h>
Barry Song4898de32012-12-20 19:37:32 +080013#include <linux/bitops.h>
14#include <linux/irq.h>
15#include <linux/clk.h>
16#include <linux/slab.h>
17#include <linux/of.h>
18#include <linux/of_irq.h>
19#include <linux/of_address.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070020#include <linux/sched_clock.h>
Uwe Kleine-König980c51a2013-11-11 21:06:11 +010021
Barry Song4898de32012-12-20 19:37:32 +080022#define SIRFSOC_TIMER_32COUNTER_0_CTRL 0x0000
23#define SIRFSOC_TIMER_32COUNTER_1_CTRL 0x0004
24#define SIRFSOC_TIMER_MATCH_0 0x0018
25#define SIRFSOC_TIMER_MATCH_1 0x001c
26#define SIRFSOC_TIMER_COUNTER_0 0x0048
27#define SIRFSOC_TIMER_COUNTER_1 0x004c
28#define SIRFSOC_TIMER_INTR_STATUS 0x0060
29#define SIRFSOC_TIMER_WATCHDOG_EN 0x0064
30#define SIRFSOC_TIMER_64COUNTER_CTRL 0x0068
31#define SIRFSOC_TIMER_64COUNTER_LO 0x006c
32#define SIRFSOC_TIMER_64COUNTER_HI 0x0070
33#define SIRFSOC_TIMER_64COUNTER_LOAD_LO 0x0074
34#define SIRFSOC_TIMER_64COUNTER_LOAD_HI 0x0078
35#define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO 0x007c
36#define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI 0x0080
37
38#define SIRFSOC_TIMER_REG_CNT 6
39
Barry Song5833ac92015-01-12 00:04:43 +080040static unsigned long atlas7_timer_rate;
Yanchang Lief89af12014-11-11 20:42:52 +080041
Barry Song4898de32012-12-20 19:37:32 +080042static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = {
43 SIRFSOC_TIMER_WATCHDOG_EN,
44 SIRFSOC_TIMER_32COUNTER_0_CTRL,
45 SIRFSOC_TIMER_32COUNTER_1_CTRL,
46 SIRFSOC_TIMER_64COUNTER_CTRL,
47 SIRFSOC_TIMER_64COUNTER_RLATCHED_LO,
48 SIRFSOC_TIMER_64COUNTER_RLATCHED_HI,
49};
50
51static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT];
52
53static void __iomem *sirfsoc_timer_base;
Barry Song4898de32012-12-20 19:37:32 +080054
55/* disable count and interrupt */
56static inline void sirfsoc_timer_count_disable(int idx)
57{
58 writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) & ~0x7,
59 sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
60}
61
62/* enable count and interrupt */
63static inline void sirfsoc_timer_count_enable(int idx)
64{
Hao Liu28cf3562014-09-29 01:50:06 +020065 writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) | 0x3,
Barry Song4898de32012-12-20 19:37:32 +080066 sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx);
67}
68
69/* timer interrupt handler */
70static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id)
71{
72 struct clock_event_device *ce = dev_id;
73 int cpu = smp_processor_id();
74
75 /* clear timer interrupt */
76 writel_relaxed(BIT(cpu), sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
77
Viresh Kumar1e729d32015-06-18 16:24:43 +053078 if (clockevent_state_oneshot(ce))
Barry Song4898de32012-12-20 19:37:32 +080079 sirfsoc_timer_count_disable(cpu);
80
81 ce->event_handler(ce);
82
83 return IRQ_HANDLED;
84}
85
86/* read 64-bit timer counter */
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010087static u64 sirfsoc_timer_read(struct clocksource *cs)
Barry Song4898de32012-12-20 19:37:32 +080088{
89 u64 cycles;
90
91 writel_relaxed((readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
92 BIT(0)) & ~BIT(1), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
93
94 cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_HI);
95 cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_LO);
96
97 return cycles;
98}
99
100static int sirfsoc_timer_set_next_event(unsigned long delta,
101 struct clock_event_device *ce)
102{
103 int cpu = smp_processor_id();
104
Hao Liu28cf3562014-09-29 01:50:06 +0200105 /* disable timer first, then modify the related registers */
106 sirfsoc_timer_count_disable(cpu);
107
Barry Song4898de32012-12-20 19:37:32 +0800108 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 +
109 4 * cpu);
110 writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 +
111 4 * cpu);
112
113 /* enable the tick */
114 sirfsoc_timer_count_enable(cpu);
115
116 return 0;
117}
118
Viresh Kumar1e729d32015-06-18 16:24:43 +0530119/* Oneshot is enabled in set_next_event */
120static int sirfsoc_timer_shutdown(struct clock_event_device *evt)
Barry Song4898de32012-12-20 19:37:32 +0800121{
Barry Song4898de32012-12-20 19:37:32 +0800122 sirfsoc_timer_count_disable(smp_processor_id());
Viresh Kumar1e729d32015-06-18 16:24:43 +0530123 return 0;
Barry Song4898de32012-12-20 19:37:32 +0800124}
125
126static void sirfsoc_clocksource_suspend(struct clocksource *cs)
127{
128 int i;
129
130 for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++)
131 sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
132}
133
134static void sirfsoc_clocksource_resume(struct clocksource *cs)
135{
136 int i;
137
138 for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++)
139 writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]);
140
141 writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2],
142 sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
143 writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1],
144 sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
145
146 writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
147 BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
148}
149
Stephen Boyd05a65482013-02-15 17:02:16 -0800150static struct clock_event_device __percpu *sirfsoc_clockevent;
Barry Song4898de32012-12-20 19:37:32 +0800151
152static struct clocksource sirfsoc_clocksource = {
153 .name = "sirfsoc_clocksource",
154 .rating = 200,
155 .mask = CLOCKSOURCE_MASK(64),
156 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
157 .read = sirfsoc_timer_read,
158 .suspend = sirfsoc_clocksource_suspend,
159 .resume = sirfsoc_clocksource_resume,
160};
161
afzal mohammedcc2550b2020-02-27 16:29:02 +0530162static unsigned int sirfsoc_timer_irq, sirfsoc_timer1_irq;
Barry Song4898de32012-12-20 19:37:32 +0800163
Richard Cochraneb0a9d82016-07-13 17:17:07 +0000164static int sirfsoc_local_timer_starting_cpu(unsigned int cpu)
Barry Song4898de32012-12-20 19:37:32 +0800165{
Richard Cochraneb0a9d82016-07-13 17:17:07 +0000166 struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
afzal mohammedcc2550b2020-02-27 16:29:02 +0530167 unsigned int irq;
168 const char *name;
Barry Song4898de32012-12-20 19:37:32 +0800169
afzal mohammedcc2550b2020-02-27 16:29:02 +0530170 if (cpu == 0) {
171 irq = sirfsoc_timer_irq;
172 name = "sirfsoc_timer0";
173 } else {
174 irq = sirfsoc_timer1_irq;
175 name = "sirfsoc_timer1";
176 }
Stephen Boyd05a65482013-02-15 17:02:16 -0800177
afzal mohammedcc2550b2020-02-27 16:29:02 +0530178 ce->irq = irq;
Barry Song4898de32012-12-20 19:37:32 +0800179 ce->name = "local_timer";
Stephen Boyd05a65482013-02-15 17:02:16 -0800180 ce->features = CLOCK_EVT_FEAT_ONESHOT;
181 ce->rating = 200;
Viresh Kumar1e729d32015-06-18 16:24:43 +0530182 ce->set_state_shutdown = sirfsoc_timer_shutdown;
183 ce->set_state_oneshot = sirfsoc_timer_shutdown;
184 ce->tick_resume = sirfsoc_timer_shutdown;
Barry Song4898de32012-12-20 19:37:32 +0800185 ce->set_next_event = sirfsoc_timer_set_next_event;
Barry Song5833ac92015-01-12 00:04:43 +0800186 clockevents_calc_mult_shift(ce, atlas7_timer_rate, 60);
Stephen Boyd05a65482013-02-15 17:02:16 -0800187 ce->max_delta_ns = clockevent_delta2ns(-2, ce);
Nicolai Stange547733c2017-03-30 22:10:21 +0200188 ce->max_delta_ticks = (unsigned long)-2;
Stephen Boyd05a65482013-02-15 17:02:16 -0800189 ce->min_delta_ns = clockevent_delta2ns(2, ce);
Nicolai Stange547733c2017-03-30 22:10:21 +0200190 ce->min_delta_ticks = 2;
Stephen Boyd05a65482013-02-15 17:02:16 -0800191 ce->cpumask = cpumask_of(cpu);
Barry Song4898de32012-12-20 19:37:32 +0800192
afzal mohammedcc2550b2020-02-27 16:29:02 +0530193 BUG_ON(request_irq(ce->irq, sirfsoc_timer_interrupt,
194 IRQF_TIMER | IRQF_NOBALANCING, name, ce));
195 irq_force_affinity(ce->irq, cpumask_of(cpu));
Barry Song4898de32012-12-20 19:37:32 +0800196
197 clockevents_register_device(ce);
198 return 0;
199}
200
Richard Cochraneb0a9d82016-07-13 17:17:07 +0000201static int sirfsoc_local_timer_dying_cpu(unsigned int cpu)
Barry Song4898de32012-12-20 19:37:32 +0800202{
afzal mohammedcc2550b2020-02-27 16:29:02 +0530203 struct clock_event_device *ce = per_cpu_ptr(sirfsoc_clockevent, cpu);
204
Barry Song4898de32012-12-20 19:37:32 +0800205 sirfsoc_timer_count_disable(1);
206
Stephen Boyd05a65482013-02-15 17:02:16 -0800207 if (cpu == 0)
afzal mohammedcc2550b2020-02-27 16:29:02 +0530208 free_irq(sirfsoc_timer_irq, ce);
Stephen Boyd05a65482013-02-15 17:02:16 -0800209 else
afzal mohammedcc2550b2020-02-27 16:29:02 +0530210 free_irq(sirfsoc_timer1_irq, ce);
Richard Cochraneb0a9d82016-07-13 17:17:07 +0000211 return 0;
Barry Song4898de32012-12-20 19:37:32 +0800212}
213
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200214static int __init sirfsoc_clockevent_init(void)
Barry Song4898de32012-12-20 19:37:32 +0800215{
Stephen Boyd05a65482013-02-15 17:02:16 -0800216 sirfsoc_clockevent = alloc_percpu(struct clock_event_device);
217 BUG_ON(!sirfsoc_clockevent);
Barry Song4898de32012-12-20 19:37:32 +0800218
Richard Cochraneb0a9d82016-07-13 17:17:07 +0000219 /* Install and invoke hotplug callbacks */
220 return cpuhp_setup_state(CPUHP_AP_MARCO_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100221 "clockevents/marco:starting",
Richard Cochraneb0a9d82016-07-13 17:17:07 +0000222 sirfsoc_local_timer_starting_cpu,
223 sirfsoc_local_timer_dying_cpu);
Barry Song4898de32012-12-20 19:37:32 +0800224}
225
226/* initialize the kernel jiffy timer source */
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200227static int __init sirfsoc_atlas7_timer_init(struct device_node *np)
Barry Song4898de32012-12-20 19:37:32 +0800228{
Barry Song4898de32012-12-20 19:37:32 +0800229 struct clk *clk;
230
Zhiwu Songc7cff542014-05-05 19:30:04 +0800231 clk = of_clk_get(np, 0);
Barry Song4898de32012-12-20 19:37:32 +0800232 BUG_ON(IS_ERR(clk));
Zhiwu Song38941522014-07-03 20:52:51 +0800233
234 BUG_ON(clk_prepare_enable(clk));
235
Barry Song5833ac92015-01-12 00:04:43 +0800236 atlas7_timer_rate = clk_get_rate(clk);
Barry Song4898de32012-12-20 19:37:32 +0800237
Yanchang Lief89af12014-11-11 20:42:52 +0800238 /* timer dividers: 0, not divided */
239 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
240 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL);
241 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL);
Barry Song4898de32012-12-20 19:37:32 +0800242
243 /* Initialize timer counters to 0 */
244 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO);
245 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI);
246 writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) |
247 BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL);
248 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0);
249 writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1);
250
251 /* Clear all interrupts */
252 writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS);
253
Barry Song5833ac92015-01-12 00:04:43 +0800254 BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate));
Barry Song4898de32012-12-20 19:37:32 +0800255
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200256 return sirfsoc_clockevent_init();
Barry Song4898de32012-12-20 19:37:32 +0800257}
258
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200259static int __init sirfsoc_of_timer_init(struct device_node *np)
Barry Song4898de32012-12-20 19:37:32 +0800260{
Barry Song4898de32012-12-20 19:37:32 +0800261 sirfsoc_timer_base = of_iomap(np, 0);
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200262 if (!sirfsoc_timer_base) {
263 pr_err("unable to map timer cpu registers\n");
264 return -ENXIO;
265 }
Barry Song4898de32012-12-20 19:37:32 +0800266
afzal mohammedcc2550b2020-02-27 16:29:02 +0530267 sirfsoc_timer_irq = irq_of_parse_and_map(np, 0);
268 if (!sirfsoc_timer_irq) {
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200269 pr_err("No irq passed for timer0 via DT\n");
270 return -EINVAL;
271 }
Barry Song4898de32012-12-20 19:37:32 +0800272
afzal mohammedcc2550b2020-02-27 16:29:02 +0530273 sirfsoc_timer1_irq = irq_of_parse_and_map(np, 1);
274 if (!sirfsoc_timer1_irq) {
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200275 pr_err("No irq passed for timer1 via DT\n");
276 return -EINVAL;
277 }
Barry Song4898de32012-12-20 19:37:32 +0800278
Daniel Lezcanoc41c96d2016-06-06 18:01:09 +0200279 return sirfsoc_atlas7_timer_init(np);
Barry Song4898de32012-12-20 19:37:32 +0800280}
Daniel Lezcano17273392017-05-26 16:56:11 +0200281TIMER_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init);