Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5 */
6
7/* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1, Each can be
8 * programmed to go from @count to @limit and optionally interrupt.
9 * We've designated TIMER0 for clockevents and TIMER1 for clocksource
10 *
11 * ARCv2 based HS38 cores have RTC (in-core) and GFRC (inside ARConnect/MCIP)
12 * which are suitable for UP and SMP based clocksources respectively
13 */
14
15#include <linux/interrupt.h>
16#include <linux/bits.h>
17#include <linux/clk.h>
18#include <linux/clk-provider.h>
19#include <linux/clocksource.h>
20#include <linux/clockchips.h>
21#include <linux/cpu.h>
22#include <linux/of.h>
23#include <linux/of_irq.h>
24#include <linux/sched_clock.h>
25
26#include <soc/arc/timers.h>
27#include <soc/arc/mcip.h>
28
29
30static unsigned long arc_timer_freq;
31
32static int noinline arc_get_timer_clk(struct device_node *node)
33{
34 struct clk *clk;
35 int ret;
36
37 clk = of_clk_get(node, 0);
38 if (IS_ERR(clk)) {
39 pr_err("timer missing clk\n");
40 return PTR_ERR(clk);
41 }
42
43 ret = clk_prepare_enable(clk);
44 if (ret) {
45 pr_err("Couldn't enable parent clk\n");
46 return ret;
47 }
48
49 arc_timer_freq = clk_get_rate(clk);
50
51 return 0;
52}
53
54/********** Clock Source Device *********/
55
56#ifdef CONFIG_ARC_TIMERS_64BIT
57
58static u64 arc_read_gfrc(struct clocksource *cs)
59{
60 unsigned long flags;
61 u32 l, h;
62
63 /*
64 * From a programming model pov, there seems to be just one instance of
65 * MCIP_CMD/MCIP_READBACK however micro-architecturally there's
66 * an instance PER ARC CORE (not per cluster), and there are dedicated
67 * hardware decode logic (per core) inside ARConnect to handle
68 * simultaneous read/write accesses from cores via those two registers.
69 * So several concurrent commands to ARConnect are OK if they are
70 * trying to access two different sub-components (like GFRC,
71 * inter-core interrupt, etc...). HW also supports simultaneously
72 * accessing GFRC by multiple cores.
73 * That's why it is safe to disable hard interrupts on the local CPU
74 * before access to GFRC instead of taking global MCIP spinlock
75 * defined in arch/arc/kernel/mcip.c
76 */
77 local_irq_save(flags);
78
79 __mcip_cmd(CMD_GFRC_READ_LO, 0);
80 l = read_aux_reg(ARC_REG_MCIP_READBACK);
81
82 __mcip_cmd(CMD_GFRC_READ_HI, 0);
83 h = read_aux_reg(ARC_REG_MCIP_READBACK);
84
85 local_irq_restore(flags);
86
87 return (((u64)h) << 32) | l;
88}
89
90static notrace u64 arc_gfrc_clock_read(void)
91{
92 return arc_read_gfrc(NULL);
93}
94
95static struct clocksource arc_counter_gfrc = {
96 .name = "ARConnect GFRC",
97 .rating = 400,
98 .read = arc_read_gfrc,
99 .mask = CLOCKSOURCE_MASK(64),
100 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
101};
102
103static int __init arc_cs_setup_gfrc(struct device_node *node)
104{
105 struct mcip_bcr mp;
106 int ret;
107
108 READ_BCR(ARC_REG_MCIP_BCR, mp);
109 if (!mp.gfrc) {
110 pr_warn("Global-64-bit-Ctr clocksource not detected\n");
111 return -ENXIO;
112 }
113
114 ret = arc_get_timer_clk(node);
115 if (ret)
116 return ret;
117
118 sched_clock_register(arc_gfrc_clock_read, 64, arc_timer_freq);
119
120 return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
121}
122TIMER_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
123
124#define AUX_RTC_CTRL 0x103
125#define AUX_RTC_LOW 0x104
126#define AUX_RTC_HIGH 0x105
127
128static u64 arc_read_rtc(struct clocksource *cs)
129{
130 unsigned long status;
131 u32 l, h;
132
133 /*
134 * hardware has an internal state machine which tracks readout of
135 * low/high and updates the CTRL.status if
136 * - interrupt/exception taken between the two reads
137 * - high increments after low has been read
138 */
139 do {
140 l = read_aux_reg(AUX_RTC_LOW);
141 h = read_aux_reg(AUX_RTC_HIGH);
142 status = read_aux_reg(AUX_RTC_CTRL);
143 } while (!(status & BIT(31)));
144
145 return (((u64)h) << 32) | l;
146}
147
148static notrace u64 arc_rtc_clock_read(void)
149{
150 return arc_read_rtc(NULL);
151}
152
153static struct clocksource arc_counter_rtc = {
154 .name = "ARCv2 RTC",
155 .rating = 350,
156 .read = arc_read_rtc,
157 .mask = CLOCKSOURCE_MASK(64),
158 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
159};
160
161static int __init arc_cs_setup_rtc(struct device_node *node)
162{
163 struct bcr_timer timer;
164 int ret;
165
166 READ_BCR(ARC_REG_TIMERS_BCR, timer);
167 if (!timer.rtc) {
168 pr_warn("Local-64-bit-Ctr clocksource not detected\n");
169 return -ENXIO;
170 }
171
172 /* Local to CPU hence not usable in SMP */
173 if (IS_ENABLED(CONFIG_SMP)) {
174 pr_warn("Local-64-bit-Ctr not usable in SMP\n");
175 return -EINVAL;
176 }
177
178 ret = arc_get_timer_clk(node);
179 if (ret)
180 return ret;
181
182 write_aux_reg(AUX_RTC_CTRL, 1);
183
184 sched_clock_register(arc_rtc_clock_read, 64, arc_timer_freq);
185
186 return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
187}
188TIMER_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
189
190#endif
191
192/*
193 * 32bit TIMER1 to keep counting monotonically and wraparound
194 */
195
196static u64 arc_read_timer1(struct clocksource *cs)
197{
198 return (u64) read_aux_reg(ARC_REG_TIMER1_CNT);
199}
200
201static notrace u64 arc_timer1_clock_read(void)
202{
203 return arc_read_timer1(NULL);
204}
205
206static struct clocksource arc_counter_timer1 = {
207 .name = "ARC Timer1",
208 .rating = 300,
209 .read = arc_read_timer1,
210 .mask = CLOCKSOURCE_MASK(32),
211 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
212};
213
214static int __init arc_cs_setup_timer1(struct device_node *node)
215{
216 int ret;
217
218 /* Local to CPU hence not usable in SMP */
219 if (IS_ENABLED(CONFIG_SMP))
220 return -EINVAL;
221
222 ret = arc_get_timer_clk(node);
223 if (ret)
224 return ret;
225
226 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
227 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
228 write_aux_reg(ARC_REG_TIMER1_CTRL, ARC_TIMER_CTRL_NH);
229
230 sched_clock_register(arc_timer1_clock_read, 32, arc_timer_freq);
231
232 return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
233}
234
235/********** Clock Event Device *********/
236
237static int arc_timer_irq;
238
239/*
240 * Arm the timer to interrupt after @cycles
241 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
242 */
243static void arc_timer_event_setup(unsigned int cycles)
244{
245 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
246 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
247
248 write_aux_reg(ARC_REG_TIMER0_CTRL, ARC_TIMER_CTRL_IE | ARC_TIMER_CTRL_NH);
249}
250
251
252static int arc_clkevent_set_next_event(unsigned long delta,
253 struct clock_event_device *dev)
254{
255 arc_timer_event_setup(delta);
256 return 0;
257}
258
259static int arc_clkevent_set_periodic(struct clock_event_device *dev)
260{
261 /*
262 * At X Hz, 1 sec = 1000ms -> X cycles;
263 * 10ms -> X / 100 cycles
264 */
265 arc_timer_event_setup(arc_timer_freq / HZ);
266 return 0;
267}
268
269static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
270 .name = "ARC Timer0",
271 .features = CLOCK_EVT_FEAT_ONESHOT |
272 CLOCK_EVT_FEAT_PERIODIC,
273 .rating = 300,
274 .set_next_event = arc_clkevent_set_next_event,
275 .set_state_periodic = arc_clkevent_set_periodic,
276};
277
278static irqreturn_t timer_irq_handler(int irq, void *dev_id)
279{
280 /*
281 * Note that generic IRQ core could have passed @evt for @dev_id if
282 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
283 */
284 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
285 int irq_reenable = clockevent_state_periodic(evt);
286
287 /*
288 * 1. ACK the interrupt
289 * - For ARC700, any write to CTRL reg ACKs it, so just rewrite
290 * Count when [N]ot [H]alted bit.
291 * - For HS3x, it is a bit subtle. On taken count-down interrupt,
292 * IP bit [3] is set, which needs to be cleared for ACK'ing.
293 * The write below can only update the other two bits, hence
294 * explicitly clears IP bit
295 * 2. Re-arm interrupt if periodic by writing to IE bit [0]
296 */
297 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | ARC_TIMER_CTRL_NH);
298
299 evt->event_handler(evt);
300
301 return IRQ_HANDLED;
302}
303
304
305static int arc_timer_starting_cpu(unsigned int cpu)
306{
307 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
308
309 evt->cpumask = cpumask_of(smp_processor_id());
310
311 clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
312 enable_percpu_irq(arc_timer_irq, 0);
313 return 0;
314}
315
316static int arc_timer_dying_cpu(unsigned int cpu)
317{
318 disable_percpu_irq(arc_timer_irq);
319 return 0;
320}
321
322/*
323 * clockevent setup for boot CPU
324 */
325static int __init arc_clockevent_setup(struct device_node *node)
326{
327 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
328 int ret;
329
330 arc_timer_irq = irq_of_parse_and_map(node, 0);
331 if (arc_timer_irq <= 0) {
332 pr_err("clockevent: missing irq\n");
333 return -EINVAL;
334 }
335
336 ret = arc_get_timer_clk(node);
337 if (ret)
338 return ret;
339
340 /* Needs apriori irq_set_percpu_devid() done in intc map function */
341 ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
342 "Timer0 (per-cpu-tick)", evt);
343 if (ret) {
344 pr_err("clockevent: unable to request irq\n");
345 return ret;
346 }
347
348 ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
349 "clockevents/arc/timer:starting",
350 arc_timer_starting_cpu,
351 arc_timer_dying_cpu);
352 if (ret) {
353 pr_err("Failed to setup hotplug state\n");
354 return ret;
355 }
356 return 0;
357}
358
359static int __init arc_of_timer_init(struct device_node *np)
360{
361 static int init_count = 0;
362 int ret;
363
364 if (!init_count) {
365 init_count = 1;
366 ret = arc_clockevent_setup(np);
367 } else {
368 ret = arc_cs_setup_timer1(np);
369 }
370
371 return ret;
372}
373TIMER_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);
1/*
2 * Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10/* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1, Each can be
11 * programmed to go from @count to @limit and optionally interrupt.
12 * We've designated TIMER0 for clockevents and TIMER1 for clocksource
13 *
14 * ARCv2 based HS38 cores have RTC (in-core) and GFRC (inside ARConnect/MCIP)
15 * which are suitable for UP and SMP based clocksources respectively
16 */
17
18#include <linux/interrupt.h>
19#include <linux/clk.h>
20#include <linux/clk-provider.h>
21#include <linux/clocksource.h>
22#include <linux/clockchips.h>
23#include <linux/cpu.h>
24#include <linux/of.h>
25#include <linux/of_irq.h>
26
27#include <soc/arc/timers.h>
28#include <soc/arc/mcip.h>
29
30
31static unsigned long arc_timer_freq;
32
33static int noinline arc_get_timer_clk(struct device_node *node)
34{
35 struct clk *clk;
36 int ret;
37
38 clk = of_clk_get(node, 0);
39 if (IS_ERR(clk)) {
40 pr_err("timer missing clk\n");
41 return PTR_ERR(clk);
42 }
43
44 ret = clk_prepare_enable(clk);
45 if (ret) {
46 pr_err("Couldn't enable parent clk\n");
47 return ret;
48 }
49
50 arc_timer_freq = clk_get_rate(clk);
51
52 return 0;
53}
54
55/********** Clock Source Device *********/
56
57#ifdef CONFIG_ARC_TIMERS_64BIT
58
59static u64 arc_read_gfrc(struct clocksource *cs)
60{
61 unsigned long flags;
62 u32 l, h;
63
64 local_irq_save(flags);
65
66 __mcip_cmd(CMD_GFRC_READ_LO, 0);
67 l = read_aux_reg(ARC_REG_MCIP_READBACK);
68
69 __mcip_cmd(CMD_GFRC_READ_HI, 0);
70 h = read_aux_reg(ARC_REG_MCIP_READBACK);
71
72 local_irq_restore(flags);
73
74 return (((u64)h) << 32) | l;
75}
76
77static struct clocksource arc_counter_gfrc = {
78 .name = "ARConnect GFRC",
79 .rating = 400,
80 .read = arc_read_gfrc,
81 .mask = CLOCKSOURCE_MASK(64),
82 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
83};
84
85static int __init arc_cs_setup_gfrc(struct device_node *node)
86{
87 struct mcip_bcr mp;
88 int ret;
89
90 READ_BCR(ARC_REG_MCIP_BCR, mp);
91 if (!mp.gfrc) {
92 pr_warn("Global-64-bit-Ctr clocksource not detected\n");
93 return -ENXIO;
94 }
95
96 ret = arc_get_timer_clk(node);
97 if (ret)
98 return ret;
99
100 return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
101}
102TIMER_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
103
104#define AUX_RTC_CTRL 0x103
105#define AUX_RTC_LOW 0x104
106#define AUX_RTC_HIGH 0x105
107
108static u64 arc_read_rtc(struct clocksource *cs)
109{
110 unsigned long status;
111 u32 l, h;
112
113 /*
114 * hardware has an internal state machine which tracks readout of
115 * low/high and updates the CTRL.status if
116 * - interrupt/exception taken between the two reads
117 * - high increments after low has been read
118 */
119 do {
120 l = read_aux_reg(AUX_RTC_LOW);
121 h = read_aux_reg(AUX_RTC_HIGH);
122 status = read_aux_reg(AUX_RTC_CTRL);
123 } while (!(status & _BITUL(31)));
124
125 return (((u64)h) << 32) | l;
126}
127
128static struct clocksource arc_counter_rtc = {
129 .name = "ARCv2 RTC",
130 .rating = 350,
131 .read = arc_read_rtc,
132 .mask = CLOCKSOURCE_MASK(64),
133 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
134};
135
136static int __init arc_cs_setup_rtc(struct device_node *node)
137{
138 struct bcr_timer timer;
139 int ret;
140
141 READ_BCR(ARC_REG_TIMERS_BCR, timer);
142 if (!timer.rtc) {
143 pr_warn("Local-64-bit-Ctr clocksource not detected\n");
144 return -ENXIO;
145 }
146
147 /* Local to CPU hence not usable in SMP */
148 if (IS_ENABLED(CONFIG_SMP)) {
149 pr_warn("Local-64-bit-Ctr not usable in SMP\n");
150 return -EINVAL;
151 }
152
153 ret = arc_get_timer_clk(node);
154 if (ret)
155 return ret;
156
157 write_aux_reg(AUX_RTC_CTRL, 1);
158
159 return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
160}
161TIMER_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
162
163#endif
164
165/*
166 * 32bit TIMER1 to keep counting monotonically and wraparound
167 */
168
169static u64 arc_read_timer1(struct clocksource *cs)
170{
171 return (u64) read_aux_reg(ARC_REG_TIMER1_CNT);
172}
173
174static struct clocksource arc_counter_timer1 = {
175 .name = "ARC Timer1",
176 .rating = 300,
177 .read = arc_read_timer1,
178 .mask = CLOCKSOURCE_MASK(32),
179 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
180};
181
182static int __init arc_cs_setup_timer1(struct device_node *node)
183{
184 int ret;
185
186 /* Local to CPU hence not usable in SMP */
187 if (IS_ENABLED(CONFIG_SMP))
188 return -EINVAL;
189
190 ret = arc_get_timer_clk(node);
191 if (ret)
192 return ret;
193
194 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
195 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
196 write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
197
198 return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
199}
200
201/********** Clock Event Device *********/
202
203static int arc_timer_irq;
204
205/*
206 * Arm the timer to interrupt after @cycles
207 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
208 */
209static void arc_timer_event_setup(unsigned int cycles)
210{
211 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
212 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
213
214 write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
215}
216
217
218static int arc_clkevent_set_next_event(unsigned long delta,
219 struct clock_event_device *dev)
220{
221 arc_timer_event_setup(delta);
222 return 0;
223}
224
225static int arc_clkevent_set_periodic(struct clock_event_device *dev)
226{
227 /*
228 * At X Hz, 1 sec = 1000ms -> X cycles;
229 * 10ms -> X / 100 cycles
230 */
231 arc_timer_event_setup(arc_timer_freq / HZ);
232 return 0;
233}
234
235static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
236 .name = "ARC Timer0",
237 .features = CLOCK_EVT_FEAT_ONESHOT |
238 CLOCK_EVT_FEAT_PERIODIC,
239 .rating = 300,
240 .set_next_event = arc_clkevent_set_next_event,
241 .set_state_periodic = arc_clkevent_set_periodic,
242};
243
244static irqreturn_t timer_irq_handler(int irq, void *dev_id)
245{
246 /*
247 * Note that generic IRQ core could have passed @evt for @dev_id if
248 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
249 */
250 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
251 int irq_reenable = clockevent_state_periodic(evt);
252
253 /*
254 * 1. ACK the interrupt
255 * - For ARC700, any write to CTRL reg ACKs it, so just rewrite
256 * Count when [N]ot [H]alted bit.
257 * - For HS3x, it is a bit subtle. On taken count-down interrupt,
258 * IP bit [3] is set, which needs to be cleared for ACK'ing.
259 * The write below can only update the other two bits, hence
260 * explicitly clears IP bit
261 * 2. Re-arm interrupt if periodic by writing to IE bit [0]
262 */
263 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
264
265 evt->event_handler(evt);
266
267 return IRQ_HANDLED;
268}
269
270
271static int arc_timer_starting_cpu(unsigned int cpu)
272{
273 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
274
275 evt->cpumask = cpumask_of(smp_processor_id());
276
277 clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
278 enable_percpu_irq(arc_timer_irq, 0);
279 return 0;
280}
281
282static int arc_timer_dying_cpu(unsigned int cpu)
283{
284 disable_percpu_irq(arc_timer_irq);
285 return 0;
286}
287
288/*
289 * clockevent setup for boot CPU
290 */
291static int __init arc_clockevent_setup(struct device_node *node)
292{
293 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
294 int ret;
295
296 arc_timer_irq = irq_of_parse_and_map(node, 0);
297 if (arc_timer_irq <= 0) {
298 pr_err("clockevent: missing irq\n");
299 return -EINVAL;
300 }
301
302 ret = arc_get_timer_clk(node);
303 if (ret) {
304 pr_err("clockevent: missing clk\n");
305 return ret;
306 }
307
308 /* Needs apriori irq_set_percpu_devid() done in intc map function */
309 ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
310 "Timer0 (per-cpu-tick)", evt);
311 if (ret) {
312 pr_err("clockevent: unable to request irq\n");
313 return ret;
314 }
315
316 ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
317 "clockevents/arc/timer:starting",
318 arc_timer_starting_cpu,
319 arc_timer_dying_cpu);
320 if (ret) {
321 pr_err("Failed to setup hotplug state\n");
322 return ret;
323 }
324 return 0;
325}
326
327static int __init arc_of_timer_init(struct device_node *np)
328{
329 static int init_count = 0;
330 int ret;
331
332 if (!init_count) {
333 init_count = 1;
334 ret = arc_clockevent_setup(np);
335 } else {
336 ret = arc_cs_setup_timer1(np);
337 }
338
339 return ret;
340}
341TIMER_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);