2 * Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
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.
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
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
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>
25 #include <linux/of_irq.h>
27 #include <soc/arc/timers.h>
28 #include <soc/arc/mcip.h>
31 static unsigned long arc_timer_freq
;
33 static int noinline
arc_get_timer_clk(struct device_node
*node
)
38 clk
= of_clk_get(node
, 0);
40 pr_err("timer missing clk\n");
44 ret
= clk_prepare_enable(clk
);
46 pr_err("Couldn't enable parent clk\n");
50 arc_timer_freq
= clk_get_rate(clk
);
55 /********** Clock Source Device *********/
57 #ifdef CONFIG_ARC_TIMERS_64BIT
59 static u64
arc_read_gfrc(struct clocksource
*cs
)
65 * From a programming model pov, there seems to be just one instance of
66 * MCIP_CMD/MCIP_READBACK however micro-architecturally there's
67 * an instance PER ARC CORE (not per cluster), and there are dedicated
68 * hardware decode logic (per core) inside ARConnect to handle
69 * simultaneous read/write accesses from cores via those two registers.
70 * So several concurrent commands to ARConnect are OK if they are
71 * trying to access two different sub-components (like GFRC,
72 * inter-core interrupt, etc...). HW also supports simultaneously
73 * accessing GFRC by multiple cores.
74 * That's why it is safe to disable hard interrupts on the local CPU
75 * before access to GFRC instead of taking global MCIP spinlock
76 * defined in arch/arc/kernel/mcip.c
78 local_irq_save(flags
);
80 __mcip_cmd(CMD_GFRC_READ_LO
, 0);
81 l
= read_aux_reg(ARC_REG_MCIP_READBACK
);
83 __mcip_cmd(CMD_GFRC_READ_HI
, 0);
84 h
= read_aux_reg(ARC_REG_MCIP_READBACK
);
86 local_irq_restore(flags
);
88 return (((u64
)h
) << 32) | l
;
91 static struct clocksource arc_counter_gfrc
= {
92 .name
= "ARConnect GFRC",
94 .read
= arc_read_gfrc
,
95 .mask
= CLOCKSOURCE_MASK(64),
96 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
99 static int __init
arc_cs_setup_gfrc(struct device_node
*node
)
104 READ_BCR(ARC_REG_MCIP_BCR
, mp
);
106 pr_warn("Global-64-bit-Ctr clocksource not detected\n");
110 ret
= arc_get_timer_clk(node
);
114 return clocksource_register_hz(&arc_counter_gfrc
, arc_timer_freq
);
116 TIMER_OF_DECLARE(arc_gfrc
, "snps,archs-timer-gfrc", arc_cs_setup_gfrc
);
118 #define AUX_RTC_CTRL 0x103
119 #define AUX_RTC_LOW 0x104
120 #define AUX_RTC_HIGH 0x105
122 static u64
arc_read_rtc(struct clocksource
*cs
)
124 unsigned long status
;
128 * hardware has an internal state machine which tracks readout of
129 * low/high and updates the CTRL.status if
130 * - interrupt/exception taken between the two reads
131 * - high increments after low has been read
134 l
= read_aux_reg(AUX_RTC_LOW
);
135 h
= read_aux_reg(AUX_RTC_HIGH
);
136 status
= read_aux_reg(AUX_RTC_CTRL
);
137 } while (!(status
& _BITUL(31)));
139 return (((u64
)h
) << 32) | l
;
142 static struct clocksource arc_counter_rtc
= {
145 .read
= arc_read_rtc
,
146 .mask
= CLOCKSOURCE_MASK(64),
147 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
150 static int __init
arc_cs_setup_rtc(struct device_node
*node
)
152 struct bcr_timer timer
;
155 READ_BCR(ARC_REG_TIMERS_BCR
, timer
);
157 pr_warn("Local-64-bit-Ctr clocksource not detected\n");
161 /* Local to CPU hence not usable in SMP */
162 if (IS_ENABLED(CONFIG_SMP
)) {
163 pr_warn("Local-64-bit-Ctr not usable in SMP\n");
167 ret
= arc_get_timer_clk(node
);
171 write_aux_reg(AUX_RTC_CTRL
, 1);
173 return clocksource_register_hz(&arc_counter_rtc
, arc_timer_freq
);
175 TIMER_OF_DECLARE(arc_rtc
, "snps,archs-timer-rtc", arc_cs_setup_rtc
);
180 * 32bit TIMER1 to keep counting monotonically and wraparound
183 static u64
arc_read_timer1(struct clocksource
*cs
)
185 return (u64
) read_aux_reg(ARC_REG_TIMER1_CNT
);
188 static struct clocksource arc_counter_timer1
= {
189 .name
= "ARC Timer1",
191 .read
= arc_read_timer1
,
192 .mask
= CLOCKSOURCE_MASK(32),
193 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
196 static int __init
arc_cs_setup_timer1(struct device_node
*node
)
200 /* Local to CPU hence not usable in SMP */
201 if (IS_ENABLED(CONFIG_SMP
))
204 ret
= arc_get_timer_clk(node
);
208 write_aux_reg(ARC_REG_TIMER1_LIMIT
, ARC_TIMERN_MAX
);
209 write_aux_reg(ARC_REG_TIMER1_CNT
, 0);
210 write_aux_reg(ARC_REG_TIMER1_CTRL
, TIMER_CTRL_NH
);
212 return clocksource_register_hz(&arc_counter_timer1
, arc_timer_freq
);
215 /********** Clock Event Device *********/
217 static int arc_timer_irq
;
220 * Arm the timer to interrupt after @cycles
221 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
223 static void arc_timer_event_setup(unsigned int cycles
)
225 write_aux_reg(ARC_REG_TIMER0_LIMIT
, cycles
);
226 write_aux_reg(ARC_REG_TIMER0_CNT
, 0); /* start from 0 */
228 write_aux_reg(ARC_REG_TIMER0_CTRL
, TIMER_CTRL_IE
| TIMER_CTRL_NH
);
232 static int arc_clkevent_set_next_event(unsigned long delta
,
233 struct clock_event_device
*dev
)
235 arc_timer_event_setup(delta
);
239 static int arc_clkevent_set_periodic(struct clock_event_device
*dev
)
242 * At X Hz, 1 sec = 1000ms -> X cycles;
243 * 10ms -> X / 100 cycles
245 arc_timer_event_setup(arc_timer_freq
/ HZ
);
249 static DEFINE_PER_CPU(struct clock_event_device
, arc_clockevent_device
) = {
250 .name
= "ARC Timer0",
251 .features
= CLOCK_EVT_FEAT_ONESHOT
|
252 CLOCK_EVT_FEAT_PERIODIC
,
254 .set_next_event
= arc_clkevent_set_next_event
,
255 .set_state_periodic
= arc_clkevent_set_periodic
,
258 static irqreturn_t
timer_irq_handler(int irq
, void *dev_id
)
261 * Note that generic IRQ core could have passed @evt for @dev_id if
262 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
264 struct clock_event_device
*evt
= this_cpu_ptr(&arc_clockevent_device
);
265 int irq_reenable
= clockevent_state_periodic(evt
);
268 * 1. ACK the interrupt
269 * - For ARC700, any write to CTRL reg ACKs it, so just rewrite
270 * Count when [N]ot [H]alted bit.
271 * - For HS3x, it is a bit subtle. On taken count-down interrupt,
272 * IP bit [3] is set, which needs to be cleared for ACK'ing.
273 * The write below can only update the other two bits, hence
274 * explicitly clears IP bit
275 * 2. Re-arm interrupt if periodic by writing to IE bit [0]
277 write_aux_reg(ARC_REG_TIMER0_CTRL
, irq_reenable
| TIMER_CTRL_NH
);
279 evt
->event_handler(evt
);
285 static int arc_timer_starting_cpu(unsigned int cpu
)
287 struct clock_event_device
*evt
= this_cpu_ptr(&arc_clockevent_device
);
289 evt
->cpumask
= cpumask_of(smp_processor_id());
291 clockevents_config_and_register(evt
, arc_timer_freq
, 0, ARC_TIMERN_MAX
);
292 enable_percpu_irq(arc_timer_irq
, 0);
296 static int arc_timer_dying_cpu(unsigned int cpu
)
298 disable_percpu_irq(arc_timer_irq
);
303 * clockevent setup for boot CPU
305 static int __init
arc_clockevent_setup(struct device_node
*node
)
307 struct clock_event_device
*evt
= this_cpu_ptr(&arc_clockevent_device
);
310 arc_timer_irq
= irq_of_parse_and_map(node
, 0);
311 if (arc_timer_irq
<= 0) {
312 pr_err("clockevent: missing irq\n");
316 ret
= arc_get_timer_clk(node
);
318 pr_err("clockevent: missing clk\n");
322 /* Needs apriori irq_set_percpu_devid() done in intc map function */
323 ret
= request_percpu_irq(arc_timer_irq
, timer_irq_handler
,
324 "Timer0 (per-cpu-tick)", evt
);
326 pr_err("clockevent: unable to request irq\n");
330 ret
= cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING
,
331 "clockevents/arc/timer:starting",
332 arc_timer_starting_cpu
,
333 arc_timer_dying_cpu
);
335 pr_err("Failed to setup hotplug state\n");
341 static int __init
arc_of_timer_init(struct device_node
*np
)
343 static int init_count
= 0;
348 ret
= arc_clockevent_setup(np
);
350 ret
= arc_cs_setup_timer1(np
);
355 TIMER_OF_DECLARE(arc_clkevt
, "snps,arc-timer", arc_of_timer_init
);