]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/cris/arch-v32/kernel/time.c
7c802121c0c36ab82e2b02206e0b51ee895a526a
[karo-tx-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/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>
18 #include <linux/mm.h>
19 #include <asm/types.h>
20 #include <asm/signal.h>
21 #include <asm/io.h>
22 #include <asm/delay.h>
23 #include <asm/irq.h>
24 #include <asm/irq_regs.h>
25
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>
32 #endif
33
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)
39
40 #define CRISV32_TIMER_FREQ      (100000000lu)
41
42 unsigned long timer_regs[NR_CPUS] =
43 {
44         regi_timer0,
45 };
46
47 extern int set_rtc_mmss(unsigned long nowtime);
48
49 #ifdef CONFIG_CPU_FREQ
50 static int cris_time_freq_notifier(struct notifier_block *nb,
51                                    unsigned long val, void *data);
52
53 static struct notifier_block cris_time_freq_notifier_block = {
54         .notifier_call = cris_time_freq_notifier,
55 };
56 #endif
57
58 unsigned long get_ns_in_jiffie(void)
59 {
60         reg_timer_r_tmr0_data data;
61         unsigned long ns;
62
63         data = REG_RD(timer, regi_timer0, r_tmr0_data);
64         ns = (TIMER0_DIV - data) * 10;
65         return ns;
66 }
67
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
74  * chip.
75  */
76 /* This gives us 1.3 ms to do something useful when the NMI comes */
77
78 /* Right now, starting the watchdog is the same as resetting it */
79 #define start_watchdog reset_watchdog
80
81 #if defined(CONFIG_ETRAX_WATCHDOG)
82 static short int watchdog_key = 42;  /* arbitrary 7 bit number */
83 #endif
84
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
88
89 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
90 /* for reliable NICE_DOGGY behaviour */
91 static int bite_in_progress;
92 #endif
93
94 void reset_watchdog(void)
95 {
96 #if defined(CONFIG_ETRAX_WATCHDOG)
97         reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
98
99 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
100         if (unlikely(bite_in_progress))
101                 return;
102 #endif
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);
112         }
113 #endif
114 }
115
116 /* stop the watchdog - we still need the correct key */
117
118 void stop_watchdog(void)
119 {
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);
127 #endif
128 }
129
130 extern void show_registers(struct pt_regs *regs);
131
132 void handle_watchdog_bite(struct pt_regs *regs)
133 {
134 #if defined(CONFIG_ETRAX_WATCHDOG)
135         extern int cause_of_death;
136
137         nmi_enter();
138         oops_in_progress = 1;
139 #if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
140         bite_in_progress = 1;
141 #endif
142         printk(KERN_WARNING "Watchdog bite\n");
143
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
149                  */
150                 reg_clkgen_rw_clk_ctrl ctrl =
151                         REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
152                 ctrl.pll = 0;
153                 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
154 #endif
155                 while(1);
156         }
157
158         /* Unexpected watchdog, stop the watchdog and dump registers. */
159         stop_watchdog();
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
165         reset_watchdog();
166 #endif
167         while(1) /* nothing */;
168 #endif
169 }
170
171 extern void cris_profile_sample(struct pt_regs *regs);
172 static void __iomem *timer_base;
173
174 static void crisv32_clkevt_mode(enum clock_event_mode mode,
175                                 struct clock_event_device *dev)
176 {
177         reg_timer_rw_tmr0_ctrl ctrl = {
178                 .op = regk_timer_hold,
179                 .freq = regk_timer_f100,
180         };
181
182         REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
183 }
184
185 static int crisv32_clkevt_next_event(unsigned long evt,
186                                      struct clock_event_device *dev)
187 {
188         reg_timer_rw_tmr0_ctrl ctrl = {
189                 .op = regk_timer_ld,
190                 .freq = regk_timer_f100,
191         };
192
193         REG_WR(timer, timer_base, rw_tmr0_div, evt);
194         REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
195
196         ctrl.op = regk_timer_run;
197         REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
198
199         return 0;
200 }
201
202 static irqreturn_t crisv32_timer_interrupt(int irq, void *dev_id)
203 {
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,
208         };
209         reg_timer_rw_ack_intr ack = { .tmr0 = 1 };
210         reg_timer_r_masked_intr intr;
211
212         intr = REG_RD(timer, timer_base, r_masked_intr);
213         if (!intr.tmr0)
214                 return IRQ_NONE;
215
216         REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
217         REG_WR(timer, timer_base, rw_ack_intr, ack);
218
219         reset_watchdog();
220 #ifdef CONFIG_SYSTEM_PROFILER
221         cris_profile_sample(get_irq_regs());
222 #endif
223
224         evt->event_handler(evt);
225
226         return IRQ_HANDLED;
227 }
228
229 static struct clock_event_device crisv32_clockevent = {
230         .name = "crisv32-timer",
231         .rating = 300,
232         .features = CLOCK_EVT_FEAT_ONESHOT,
233         .set_mode = crisv32_clkevt_mode,
234         .set_next_event = crisv32_clkevt_next_event,
235 };
236
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,
243 };
244
245 static void __init crisv32_timer_init(void)
246 {
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,
251         };
252
253         REG_WR(timer, timer_base, rw_tmr0_ctrl, ctrl);
254
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);
258 }
259
260 void __init time_init(void)
261 {
262         int irq;
263         int ret;
264
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
269          * clock has started.
270          */
271         loops_per_usec = 50;
272
273         irq = TIMER0_INTR_VECT;
274         timer_base = (void __iomem *) regi_timer0;
275
276         crisv32_timer_init();
277
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);
281
282         crisv32_clockevent.cpumask = cpu_possible_mask;
283         crisv32_clockevent.irq = irq;
284
285         ret = setup_irq(irq, &irq_timer);
286         if (ret)
287                 pr_warn("failed to setup irq %d\n", irq);
288
289         clockevents_config_and_register(&crisv32_clockevent,
290                                         CRISV32_TIMER_FREQ,
291                                         2, 0xffffffff);
292
293         /* Enable watchdog if we should use one. */
294
295 #if defined(CONFIG_ETRAX_WATCHDOG)
296         printk(KERN_INFO "Enabling watchdog...\n");
297         start_watchdog();
298
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). */
303         {
304                 unsigned long flags;
305                 local_save_flags(flags);
306                 flags |= (1<<30); /* NMI M flag is at bit 30 */
307                 local_irq_restore(flags);
308         }
309 #endif
310
311 #ifdef CONFIG_CPU_FREQ
312         cpufreq_register_notifier(&cris_time_freq_notifier_block,
313                                   CPUFREQ_TRANSITION_NOTIFIER);
314 #endif
315 }
316
317 #ifdef CONFIG_CPU_FREQ
318 static int cris_time_freq_notifier(struct notifier_block *nb,
319                                    unsigned long val, void *data)
320 {
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;
325                 do {
326                         data = REG_RD(timer, timer_regs[freqs->cpu],
327                                 r_tmr0_data);
328                 } while (data > 20);
329                 REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
330         }
331         return 0;
332 }
333 #endif