2 * linux/arch/cris/arch-v32/kernel/time.c
4 * Copyright (C) 2003-2010 Axis Communications AB
8 #include <linux/timex.h>
9 #include <linux/time.h>
10 #include <linux/clocksource.h>
11 #include <linux/clockchips.h>
12 #include <linux/interrupt.h>
13 #include <linux/swap.h>
14 #include <linux/sched.h>
15 #include <linux/init.h>
16 #include <linux/threads.h>
17 #include <linux/cpufreq.h>
19 #include <asm/types.h>
20 #include <asm/signal.h>
22 #include <asm/delay.h>
24 #include <asm/irq_regs.h>
26 #include <hwregs/reg_map.h>
27 #include <hwregs/reg_rdwr.h>
28 #include <hwregs/timer_defs.h>
29 #include <hwregs/intr_vect_defs.h>
30 #ifdef CONFIG_CRIS_MACH_ARTPEC3
31 #include <hwregs/clkgen_defs.h>
34 /* Watchdog defines */
35 #define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */
36 #define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */
37 /* Number of 763 counts before watchdog bites */
38 #define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1)
40 #define CRISV32_TIMER_FREQ (100000000lu)
42 unsigned long timer_regs[NR_CPUS] =
47 extern int set_rtc_mmss(unsigned long nowtime);
49 #ifdef CONFIG_CPU_FREQ
50 static int cris_time_freq_notifier(struct notifier_block *nb,
51 unsigned long val, void *data);
53 static struct notifier_block cris_time_freq_notifier_block = {
54 .notifier_call = cris_time_freq_notifier,
58 unsigned long get_ns_in_jiffie(void)
60 reg_timer_r_tmr0_data data;
63 data = REG_RD(timer, regi_timer0, r_tmr0_data);
64 ns = (TIMER0_DIV - data) * 10;
68 /* From timer MDS describing the hardware watchdog:
69 * 4.3.1 Watchdog Operation
70 * The watchdog timer is an 8-bit timer with a configurable start value.
71 * Once started the watchdog counts downwards with a frequency of 763 Hz
72 * (100/131072 MHz). When the watchdog counts down to 1, it generates an
73 * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
76 /* This gives us 1.3 ms to do something useful when the NMI comes */
78 /* Right now, starting the watchdog is the same as resetting it */
79 #define start_watchdog reset_watchdog
81 #if defined(CONFIG_ETRAX_WATCHDOG)
82 static short int watchdog_key = 42; /* arbitrary 7 bit number */
85 /* Number of pages to consider "out of memory". It is normal that the memory
86 * is used though, so set this really low. */
87 #define WATCHDOG_MIN_FREE_PAGES 8
89 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
90 /* for reliable NICE_DOGGY behaviour */
91 static int bite_in_progress;
94 void reset_watchdog(void)
96 #if defined(CONFIG_ETRAX_WATCHDOG)
97 reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
99 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
100 if (unlikely(bite_in_progress))
103 /* Only keep watchdog happy as long as we have memory left! */
104 if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
105 /* Reset the watchdog with the inverse of the old key */
106 /* Invert key, which is 7 bits */
107 watchdog_key ^= ETRAX_WD_KEY_MASK;
108 wd_ctrl.cnt = ETRAX_WD_CNT;
109 wd_ctrl.cmd = regk_timer_start;
110 wd_ctrl.key = watchdog_key;
111 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
116 /* stop the watchdog - we still need the correct key */
118 void stop_watchdog(void)
120 #if defined(CONFIG_ETRAX_WATCHDOG)
121 reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
122 watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
123 wd_ctrl.cnt = ETRAX_WD_CNT;
124 wd_ctrl.cmd = regk_timer_stop;
125 wd_ctrl.key = watchdog_key;
126 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
130 extern void show_registers(struct pt_regs *regs);
132 void handle_watchdog_bite(struct pt_regs *regs)
134 #if defined(CONFIG_ETRAX_WATCHDOG)
135 extern int cause_of_death;
138 oops_in_progress = 1;
139 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
140 bite_in_progress = 1;
142 printk(KERN_WARNING "Watchdog bite\n");
144 /* Check if forced restart or unexpected watchdog */
145 if (cause_of_death == 0xbedead) {
146 #ifdef CONFIG_CRIS_MACH_ARTPEC3
147 /* There is a bug in Artpec-3 (voodoo TR 78) that requires
148 * us to go to lower frequency for the reset to be reliable
150 reg_clkgen_rw_clk_ctrl ctrl =
151 REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
153 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
158 /* Unexpected watchdog, stop the watchdog and dump registers. */
160 printk(KERN_WARNING "Oops: bitten by watchdog\n");
161 show_registers(regs);
162 oops_in_progress = 0;
163 printk("\n"); /* Flush mtdoops. */
164 #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
167 while(1) /* nothing */;
171 extern void cris_profile_sample(struct pt_regs *regs);
172 static void __iomem *timer_base;
174 static void crisv32_clkevt_mode(enum clock_event_mode mode,
175 struct clock_event_device *dev)
177 reg_timer_rw_tmr0_ctrl ctrl = {
178 .op = regk_timer_hold,
179 .freq = regk_timer_f100,
182 REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
185 static int crisv32_clkevt_next_event(unsigned long evt,
186 struct clock_event_device *dev)
188 reg_timer_rw_tmr0_ctrl ctrl = {
190 .freq = regk_timer_f100,
193 REG_WR(timer, timer_base, rw_tmr0_div, evt);
194 REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
196 ctrl.op = regk_timer_run;
197 REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
202 static irqreturn_t crisv32_timer_interrupt(int irq, void *dev_id)
204 struct clock_event_device *evt = dev_id;
205 reg_timer_rw_tmr0_ctrl ctrl = {
206 .op = regk_timer_hold,
207 .freq = regk_timer_f100,
209 reg_timer_rw_ack_intr ack = { .tmr0 = 1 };
210 reg_timer_r_masked_intr intr;
212 intr = REG_RD(timer, timer_base, r_masked_intr);
216 REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
217 REG_WR(timer, timer_base, rw_ack_intr, ack);
220 #ifdef CONFIG_SYSTEM_PROFILER
221 cris_profile_sample(get_irq_regs());
224 evt->event_handler(evt);
229 static struct clock_event_device crisv32_clockevent = {
230 .name = "crisv32-timer",
232 .features = CLOCK_EVT_FEAT_ONESHOT,
233 .set_mode = crisv32_clkevt_mode,
234 .set_next_event = crisv32_clkevt_next_event,
237 /* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. */
238 static struct irqaction irq_timer = {
239 .handler = crisv32_timer_interrupt,
240 .flags = IRQF_TIMER | IRQF_SHARED,
241 .name = "crisv32-timer",
242 .dev_id = &crisv32_clockevent,
245 static void __init crisv32_timer_init(void)
247 reg_timer_rw_intr_mask timer_intr_mask;
248 reg_timer_rw_tmr0_ctrl ctrl = {
249 .op = regk_timer_hold,
250 .freq = regk_timer_f100,
253 REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
255 timer_intr_mask = REG_RD(timer, timer_base, rw_intr_mask);
256 timer_intr_mask.tmr0 = 1;
257 REG_WR(timer, timer_base, rw_intr_mask, timer_intr_mask);
260 void __init time_init(void)
265 /* Probe for the RTC and read it if it exists.
266 * Before the RTC can be probed the loops_per_usec variable needs
267 * to be initialized to make usleep work. A better value for
268 * loops_per_usec is calculated by the kernel later once the
273 irq = TIMER0_INTR_VECT;
274 timer_base = (void __iomem *) regi_timer0;
276 crisv32_timer_init();
278 clocksource_mmio_init(timer_base + REG_RD_ADDR_timer_r_time,
279 "crisv32-timer", CRISV32_TIMER_FREQ,
280 300, 32, clocksource_mmio_readl_up);
282 crisv32_clockevent.cpumask = cpu_possible_mask;
283 crisv32_clockevent.irq = irq;
285 ret = setup_irq(irq, &irq_timer);
287 pr_warn("failed to setup irq %d\n", irq);
289 clockevents_config_and_register(&crisv32_clockevent,
293 /* Enable watchdog if we should use one. */
295 #if defined(CONFIG_ETRAX_WATCHDOG)
296 printk(KERN_INFO "Enabling watchdog...\n");
299 /* If we use the hardware watchdog, we want to trap it as an NMI
300 * and dump registers before it resets us. For this to happen, we
301 * must set the "m" NMI enable flag (which once set, is unset only
302 * when an NMI is taken). */
305 local_save_flags(flags);
306 flags |= (1<<30); /* NMI M flag is at bit 30 */
307 local_irq_restore(flags);
311 #ifdef CONFIG_CPU_FREQ
312 cpufreq_register_notifier(&cris_time_freq_notifier_block,
313 CPUFREQ_TRANSITION_NOTIFIER);
317 #ifdef CONFIG_CPU_FREQ
318 static int cris_time_freq_notifier(struct notifier_block *nb,
319 unsigned long val, void *data)
321 struct cpufreq_freqs *freqs = data;
322 if (val == CPUFREQ_POSTCHANGE) {
323 reg_timer_r_tmr0_data data;
324 reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
326 data = REG_RD(timer, timer_regs[freqs->cpu],
329 REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);