1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/cris/arch-v32/kernel/time.c
5 * Copyright (C) 2003-2010 Axis Communications AB
9 #include <linux/timex.h>
10 #include <linux/time.h>
11 #include <linux/clocksource.h>
12 #include <linux/clockchips.h>
13 #include <linux/interrupt.h>
14 #include <linux/swap.h>
15 #include <linux/sched.h>
16 #include <linux/init.h>
17 #include <linux/threads.h>
18 #include <linux/cpufreq.h>
19 #include <linux/sched_clock.h>
21 #include <asm/types.h>
22 #include <asm/signal.h>
24 #include <asm/delay.h>
26 #include <asm/irq_regs.h>
28 #include <hwregs/reg_map.h>
29 #include <hwregs/reg_rdwr.h>
30 #include <hwregs/timer_defs.h>
31 #include <hwregs/intr_vect_defs.h>
32 #ifdef CONFIG_CRIS_MACH_ARTPEC3
33 #include <hwregs/clkgen_defs.h>
36 /* Watchdog defines */
37 #define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */
38 #define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */
39 /* Number of 763 counts before watchdog bites */
40 #define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1)
42 #define CRISV32_TIMER_FREQ (100000000lu)
44 unsigned long timer_regs
[NR_CPUS
] =
49 extern int set_rtc_mmss(unsigned long nowtime
);
51 #ifdef CONFIG_CPU_FREQ
52 static int cris_time_freq_notifier(struct notifier_block
*nb
,
53 unsigned long val
, void *data
);
55 static struct notifier_block cris_time_freq_notifier_block
= {
56 .notifier_call
= cris_time_freq_notifier
,
60 unsigned long get_ns_in_jiffie(void)
62 reg_timer_r_tmr0_data data
;
65 data
= REG_RD(timer
, regi_timer0
, r_tmr0_data
);
66 ns
= (TIMER0_DIV
- data
) * 10;
70 /* From timer MDS describing the hardware watchdog:
71 * 4.3.1 Watchdog Operation
72 * The watchdog timer is an 8-bit timer with a configurable start value.
73 * Once started the watchdog counts downwards with a frequency of 763 Hz
74 * (100/131072 MHz). When the watchdog counts down to 1, it generates an
75 * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
78 /* This gives us 1.3 ms to do something useful when the NMI comes */
80 /* Right now, starting the watchdog is the same as resetting it */
81 #define start_watchdog reset_watchdog
83 #if defined(CONFIG_ETRAX_WATCHDOG)
84 static short int watchdog_key
= 42; /* arbitrary 7 bit number */
87 /* Number of pages to consider "out of memory". It is normal that the memory
88 * is used though, so set this really low. */
89 #define WATCHDOG_MIN_FREE_PAGES 8
91 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
92 /* for reliable NICE_DOGGY behaviour */
93 static int bite_in_progress
;
96 void reset_watchdog(void)
98 #if defined(CONFIG_ETRAX_WATCHDOG)
99 reg_timer_rw_wd_ctrl wd_ctrl
= { 0 };
101 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
102 if (unlikely(bite_in_progress
))
105 /* Only keep watchdog happy as long as we have memory left! */
106 if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES
) {
107 /* Reset the watchdog with the inverse of the old key */
108 /* Invert key, which is 7 bits */
109 watchdog_key
^= ETRAX_WD_KEY_MASK
;
110 wd_ctrl
.cnt
= ETRAX_WD_CNT
;
111 wd_ctrl
.cmd
= regk_timer_start
;
112 wd_ctrl
.key
= watchdog_key
;
113 REG_WR(timer
, regi_timer0
, rw_wd_ctrl
, wd_ctrl
);
118 /* stop the watchdog - we still need the correct key */
120 void stop_watchdog(void)
122 #if defined(CONFIG_ETRAX_WATCHDOG)
123 reg_timer_rw_wd_ctrl wd_ctrl
= { 0 };
124 watchdog_key
^= ETRAX_WD_KEY_MASK
; /* invert key, which is 7 bits */
125 wd_ctrl
.cnt
= ETRAX_WD_CNT
;
126 wd_ctrl
.cmd
= regk_timer_stop
;
127 wd_ctrl
.key
= watchdog_key
;
128 REG_WR(timer
, regi_timer0
, rw_wd_ctrl
, wd_ctrl
);
132 extern void show_registers(struct pt_regs
*regs
);
134 void handle_watchdog_bite(struct pt_regs
*regs
)
136 #if defined(CONFIG_ETRAX_WATCHDOG)
137 extern int cause_of_death
;
140 oops_in_progress
= 1;
141 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
142 bite_in_progress
= 1;
144 printk(KERN_WARNING
"Watchdog bite\n");
146 /* Check if forced restart or unexpected watchdog */
147 if (cause_of_death
== 0xbedead) {
148 #ifdef CONFIG_CRIS_MACH_ARTPEC3
149 /* There is a bug in Artpec-3 (voodoo TR 78) that requires
150 * us to go to lower frequency for the reset to be reliable
152 reg_clkgen_rw_clk_ctrl ctrl
=
153 REG_RD(clkgen
, regi_clkgen
, rw_clk_ctrl
);
155 REG_WR(clkgen
, regi_clkgen
, rw_clk_ctrl
, ctrl
);
160 /* Unexpected watchdog, stop the watchdog and dump registers. */
162 printk(KERN_WARNING
"Oops: bitten by watchdog\n");
163 show_registers(regs
);
164 oops_in_progress
= 0;
165 printk("\n"); /* Flush mtdoops. */
166 #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
169 while(1) /* nothing */;
173 extern void cris_profile_sample(struct pt_regs
*regs
);
174 static void __iomem
*timer_base
;
176 static int crisv32_clkevt_switch_state(struct clock_event_device
*dev
)
178 reg_timer_rw_tmr0_ctrl ctrl
= {
179 .op
= regk_timer_hold
,
180 .freq
= regk_timer_f100
,
183 REG_WR(timer
, timer_base
, rw_tmr0_ctrl
, ctrl
);
187 static int crisv32_clkevt_next_event(unsigned long evt
,
188 struct clock_event_device
*dev
)
190 reg_timer_rw_tmr0_ctrl ctrl
= {
192 .freq
= regk_timer_f100
,
195 REG_WR(timer
, timer_base
, rw_tmr0_div
, evt
);
196 REG_WR(timer
, timer_base
, rw_tmr0_ctrl
, ctrl
);
198 ctrl
.op
= regk_timer_run
;
199 REG_WR(timer
, timer_base
, rw_tmr0_ctrl
, ctrl
);
204 static irqreturn_t
crisv32_timer_interrupt(int irq
, void *dev_id
)
206 struct clock_event_device
*evt
= dev_id
;
207 reg_timer_rw_tmr0_ctrl ctrl
= {
208 .op
= regk_timer_hold
,
209 .freq
= regk_timer_f100
,
211 reg_timer_rw_ack_intr ack
= { .tmr0
= 1 };
212 reg_timer_r_masked_intr intr
;
214 intr
= REG_RD(timer
, timer_base
, r_masked_intr
);
218 REG_WR(timer
, timer_base
, rw_tmr0_ctrl
, ctrl
);
219 REG_WR(timer
, timer_base
, rw_ack_intr
, ack
);
222 #ifdef CONFIG_SYSTEM_PROFILER
223 cris_profile_sample(get_irq_regs());
226 evt
->event_handler(evt
);
231 static struct clock_event_device crisv32_clockevent
= {
232 .name
= "crisv32-timer",
234 .features
= CLOCK_EVT_FEAT_ONESHOT
,
235 .set_state_oneshot
= crisv32_clkevt_switch_state
,
236 .set_state_shutdown
= crisv32_clkevt_switch_state
,
237 .tick_resume
= crisv32_clkevt_switch_state
,
238 .set_next_event
= crisv32_clkevt_next_event
,
241 /* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. */
242 static struct irqaction irq_timer
= {
243 .handler
= crisv32_timer_interrupt
,
244 .flags
= IRQF_TIMER
| IRQF_SHARED
,
245 .name
= "crisv32-timer",
246 .dev_id
= &crisv32_clockevent
,
249 static u64 notrace
crisv32_timer_sched_clock(void)
251 return REG_RD(timer
, timer_base
, r_time
);
254 static void __init
crisv32_timer_init(void)
256 reg_timer_rw_intr_mask timer_intr_mask
;
257 reg_timer_rw_tmr0_ctrl ctrl
= {
258 .op
= regk_timer_hold
,
259 .freq
= regk_timer_f100
,
262 REG_WR(timer
, timer_base
, rw_tmr0_ctrl
, ctrl
);
264 timer_intr_mask
= REG_RD(timer
, timer_base
, rw_intr_mask
);
265 timer_intr_mask
.tmr0
= 1;
266 REG_WR(timer
, timer_base
, rw_intr_mask
, timer_intr_mask
);
269 void __init
time_init(void)
274 /* Probe for the RTC and read it if it exists.
275 * Before the RTC can be probed the loops_per_usec variable needs
276 * to be initialized to make usleep work. A better value for
277 * loops_per_usec is calculated by the kernel later once the
282 irq
= TIMER0_INTR_VECT
;
283 timer_base
= (void __iomem
*) regi_timer0
;
285 crisv32_timer_init();
287 sched_clock_register(crisv32_timer_sched_clock
, 32,
290 clocksource_mmio_init(timer_base
+ REG_RD_ADDR_timer_r_time
,
291 "crisv32-timer", CRISV32_TIMER_FREQ
,
292 300, 32, clocksource_mmio_readl_up
);
294 crisv32_clockevent
.cpumask
= cpu_possible_mask
;
295 crisv32_clockevent
.irq
= irq
;
297 ret
= setup_irq(irq
, &irq_timer
);
299 pr_warn("failed to setup irq %d\n", irq
);
301 clockevents_config_and_register(&crisv32_clockevent
,
305 /* Enable watchdog if we should use one. */
307 #if defined(CONFIG_ETRAX_WATCHDOG)
308 printk(KERN_INFO
"Enabling watchdog...\n");
311 /* If we use the hardware watchdog, we want to trap it as an NMI
312 * and dump registers before it resets us. For this to happen, we
313 * must set the "m" NMI enable flag (which once set, is unset only
314 * when an NMI is taken). */
317 local_save_flags(flags
);
318 flags
|= (1<<30); /* NMI M flag is at bit 30 */
319 local_irq_restore(flags
);
323 #ifdef CONFIG_CPU_FREQ
324 cpufreq_register_notifier(&cris_time_freq_notifier_block
,
325 CPUFREQ_TRANSITION_NOTIFIER
);
329 #ifdef CONFIG_CPU_FREQ
330 static int cris_time_freq_notifier(struct notifier_block
*nb
,
331 unsigned long val
, void *data
)
333 struct cpufreq_freqs
*freqs
= data
;
334 if (val
== CPUFREQ_POSTCHANGE
) {
335 reg_timer_r_tmr0_data data
;
336 reg_timer_rw_tmr0_div div
= (freqs
->new * 500) / HZ
;
338 data
= REG_RD(timer
, timer_regs
[freqs
->cpu
],
341 REG_WR(timer
, timer_regs
[freqs
->cpu
], rw_tmr0_div
, div
);