CRISv32: Add missing include for mm.h
[cascardo/linux.git] / arch / cris / arch-v32 / kernel / time.c
1 /*
2  *  linux/arch/cris/arch-v32/kernel/time.c
3  *
4  *  Copyright (C) 2003-2010 Axis Communications AB
5  *
6  */
7
8 #include <linux/timex.h>
9 #include <linux/time.h>
10 #include <linux/clocksource.h>
11 #include <linux/interrupt.h>
12 #include <linux/swap.h>
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/threads.h>
16 #include <linux/cpufreq.h>
17 #include <linux/mm.h>
18 #include <asm/types.h>
19 #include <asm/signal.h>
20 #include <asm/io.h>
21 #include <asm/delay.h>
22 #include <asm/irq.h>
23 #include <asm/irq_regs.h>
24
25 #include <hwregs/reg_map.h>
26 #include <hwregs/reg_rdwr.h>
27 #include <hwregs/timer_defs.h>
28 #include <hwregs/intr_vect_defs.h>
29 #ifdef CONFIG_CRIS_MACH_ARTPEC3
30 #include <hwregs/clkgen_defs.h>
31 #endif
32
33 /* Watchdog defines */
34 #define ETRAX_WD_KEY_MASK       0x7F /* key is 7 bit */
35 #define ETRAX_WD_HZ             763 /* watchdog counts at 763 Hz */
36 /* Number of 763 counts before watchdog bites */
37 #define ETRAX_WD_CNT            ((2*ETRAX_WD_HZ)/HZ + 1)
38
39 /* Register the continuos readonly timer available in FS and ARTPEC-3.  */
40 static cycle_t read_cont_rotime(struct clocksource *cs)
41 {
42         return (u32)REG_RD(timer, regi_timer0, r_time);
43 }
44
45 static struct clocksource cont_rotime = {
46         .name   = "crisv32_rotime",
47         .rating = 300,
48         .read   = read_cont_rotime,
49         .mask   = CLOCKSOURCE_MASK(32),
50         .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
51 };
52
53 static int __init etrax_init_cont_rotime(void)
54 {
55         clocksource_register_khz(&cont_rotime, 100000);
56         return 0;
57 }
58 arch_initcall(etrax_init_cont_rotime);
59
60
61 unsigned long timer_regs[NR_CPUS] =
62 {
63         regi_timer0,
64 #ifdef CONFIG_SMP
65         regi_timer2
66 #endif
67 };
68
69 extern int set_rtc_mmss(unsigned long nowtime);
70
71 #ifdef CONFIG_CPU_FREQ
72 static int
73 cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
74                         void *data);
75
76 static struct notifier_block cris_time_freq_notifier_block = {
77         .notifier_call = cris_time_freq_notifier,
78 };
79 #endif
80
81 unsigned long get_ns_in_jiffie(void)
82 {
83         reg_timer_r_tmr0_data data;
84         unsigned long ns;
85
86         data = REG_RD(timer, regi_timer0, r_tmr0_data);
87         ns = (TIMER0_DIV - data) * 10;
88         return ns;
89 }
90
91
92 /* From timer MDS describing the hardware watchdog:
93  * 4.3.1 Watchdog Operation
94  * The watchdog timer is an 8-bit timer with a configurable start value.
95  * Once started the watchdog counts downwards with a frequency of 763 Hz
96  * (100/131072 MHz). When the watchdog counts down to 1, it generates an
97  * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
98  * chip.
99  */
100 /* This gives us 1.3 ms to do something useful when the NMI comes */
101
102 /* Right now, starting the watchdog is the same as resetting it */
103 #define start_watchdog reset_watchdog
104
105 #if defined(CONFIG_ETRAX_WATCHDOG)
106 static short int watchdog_key = 42;  /* arbitrary 7 bit number */
107 #endif
108
109 /* Number of pages to consider "out of memory". It is normal that the memory
110  * is used though, so set this really low. */
111 #define WATCHDOG_MIN_FREE_PAGES 8
112
113 void reset_watchdog(void)
114 {
115 #if defined(CONFIG_ETRAX_WATCHDOG)
116         reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
117
118         /* Only keep watchdog happy as long as we have memory left! */
119         if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
120                 /* Reset the watchdog with the inverse of the old key */
121                 /* Invert key, which is 7 bits */
122                 watchdog_key ^= ETRAX_WD_KEY_MASK;
123                 wd_ctrl.cnt = ETRAX_WD_CNT;
124                 wd_ctrl.cmd = regk_timer_start;
125                 wd_ctrl.key = watchdog_key;
126                 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
127         }
128 #endif
129 }
130
131 /* stop the watchdog - we still need the correct key */
132
133 void stop_watchdog(void)
134 {
135 #if defined(CONFIG_ETRAX_WATCHDOG)
136         reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
137         watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
138         wd_ctrl.cnt = ETRAX_WD_CNT;
139         wd_ctrl.cmd = regk_timer_stop;
140         wd_ctrl.key = watchdog_key;
141         REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
142 #endif
143 }
144
145 extern void show_registers(struct pt_regs *regs);
146
147 void handle_watchdog_bite(struct pt_regs *regs)
148 {
149 #if defined(CONFIG_ETRAX_WATCHDOG)
150         extern int cause_of_death;
151
152         oops_in_progress = 1;
153         printk(KERN_WARNING "Watchdog bite\n");
154
155         /* Check if forced restart or unexpected watchdog */
156         if (cause_of_death == 0xbedead) {
157 #ifdef CONFIG_CRIS_MACH_ARTPEC3
158                 /* There is a bug in Artpec-3 (voodoo TR 78) that requires
159                  * us to go to lower frequency for the reset to be reliable
160                  */
161                 reg_clkgen_rw_clk_ctrl ctrl =
162                         REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
163                 ctrl.pll = 0;
164                 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
165 #endif
166                 while(1);
167         }
168
169         /* Unexpected watchdog, stop the watchdog and dump registers. */
170         stop_watchdog();
171         printk(KERN_WARNING "Oops: bitten by watchdog\n");
172         show_registers(regs);
173         oops_in_progress = 0;
174 #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
175         reset_watchdog();
176 #endif
177         while(1) /* nothing */;
178 #endif
179 }
180
181 /*
182  * timer_interrupt() needs to keep up the real-time clock,
183  * as well as call the "xtime_update()" routine every clocktick.
184  */
185 extern void cris_do_profile(struct pt_regs *regs);
186
187 static inline irqreturn_t timer_interrupt(int irq, void *dev_id)
188 {
189         struct pt_regs *regs = get_irq_regs();
190         int cpu = smp_processor_id();
191         reg_timer_r_masked_intr masked_intr;
192         reg_timer_rw_ack_intr ack_intr = { 0 };
193
194         /* Check if the timer interrupt is for us (a tmr0 int) */
195         masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr);
196         if (!masked_intr.tmr0)
197                 return IRQ_NONE;
198
199         /* Acknowledge the timer irq. */
200         ack_intr.tmr0 = 1;
201         REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr);
202
203         /* Reset watchdog otherwise it resets us! */
204         reset_watchdog();
205
206         /* Update statistics. */
207         update_process_times(user_mode(regs));
208
209         cris_do_profile(regs); /* Save profiling information */
210
211         /* The master CPU is responsible for the time keeping. */
212         if (cpu != 0)
213                 return IRQ_HANDLED;
214
215         /* Call the real timer interrupt handler */
216         xtime_update(1);
217         return IRQ_HANDLED;
218 }
219
220 /* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. */
221 static struct irqaction irq_timer = {
222         .handler = timer_interrupt,
223         .flags = IRQF_SHARED,
224         .name = "timer"
225 };
226
227 void __init cris_timer_init(void)
228 {
229         int cpu = smp_processor_id();
230         reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 };
231         reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV;
232         reg_timer_rw_intr_mask timer_intr_mask;
233
234         /* Setup the etrax timers.
235          * Base frequency is 100MHz, divider 1000000 -> 100 HZ
236          * We use timer0, so timer1 is free.
237          * The trig timer is used by the fasttimer API if enabled.
238          */
239
240         tmr0_ctrl.op = regk_timer_ld;
241         tmr0_ctrl.freq = regk_timer_f100;
242         REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div);
243         REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */
244         tmr0_ctrl.op = regk_timer_run;
245         REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */
246
247         /* Enable the timer irq. */
248         timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask);
249         timer_intr_mask.tmr0 = 1;
250         REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask);
251 }
252
253 void __init time_init(void)
254 {
255         reg_intr_vect_rw_mask intr_mask;
256
257         /* Probe for the RTC and read it if it exists.
258          * Before the RTC can be probed the loops_per_usec variable needs
259          * to be initialized to make usleep work. A better value for
260          * loops_per_usec is calculated by the kernel later once the
261          * clock has started.
262          */
263         loops_per_usec = 50;
264
265         /* Start CPU local timer. */
266         cris_timer_init();
267
268         /* Enable the timer irq in global config. */
269         intr_mask = REG_RD_VECT(intr_vect, regi_irq, rw_mask, 1);
270         intr_mask.timer0 = 1;
271         REG_WR_VECT(intr_vect, regi_irq, rw_mask, 1, intr_mask);
272
273         /* Now actually register the timer irq handler that calls
274          * timer_interrupt(). */
275         setup_irq(TIMER0_INTR_VECT, &irq_timer);
276
277         /* Enable watchdog if we should use one. */
278
279 #if defined(CONFIG_ETRAX_WATCHDOG)
280         printk(KERN_INFO "Enabling watchdog...\n");
281         start_watchdog();
282
283         /* If we use the hardware watchdog, we want to trap it as an NMI
284          * and dump registers before it resets us.  For this to happen, we
285          * must set the "m" NMI enable flag (which once set, is unset only
286          * when an NMI is taken). */
287         {
288                 unsigned long flags;
289                 local_save_flags(flags);
290                 flags |= (1<<30); /* NMI M flag is at bit 30 */
291                 local_irq_restore(flags);
292         }
293 #endif
294
295 #ifdef CONFIG_CPU_FREQ
296         cpufreq_register_notifier(&cris_time_freq_notifier_block,
297                 CPUFREQ_TRANSITION_NOTIFIER);
298 #endif
299 }
300
301 #ifdef CONFIG_CPU_FREQ
302 static int
303 cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
304                         void *data)
305 {
306         struct cpufreq_freqs *freqs = data;
307         if (val == CPUFREQ_POSTCHANGE) {
308                 reg_timer_r_tmr0_data data;
309                 reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
310                 do {
311                         data = REG_RD(timer, timer_regs[freqs->cpu],
312                                 r_tmr0_data);
313                 } while (data > 20);
314                 REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
315         }
316         return 0;
317 }
318 #endif