blob: 8c4b45efd7b6712f8dc13da94c55bc8ee51ca369 [file] [log] [blame]
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +01001/*
Mikael Starvik51533b62005-07-27 11:44:44 -07002 * linux/arch/cris/arch-v32/kernel/time.c
3 *
Jesper Nilsson60dbd662010-07-30 17:33:07 +02004 * Copyright (C) 2003-2010 Axis Communications AB
Mikael Starvik51533b62005-07-27 11:44:44 -07005 *
6 */
7
Mikael Starvik51533b62005-07-27 11:44:44 -07008#include <linux/timex.h>
9#include <linux/time.h>
Jesper Nilsson60dbd662010-07-30 17:33:07 +020010#include <linux/clocksource.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070011#include <linux/interrupt.h>
12#include <linux/swap.h>
13#include <linux/sched.h>
14#include <linux/init.h>
15#include <linux/threads.h>
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010016#include <linux/cpufreq.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070017#include <asm/types.h>
18#include <asm/signal.h>
19#include <asm/io.h>
20#include <asm/delay.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070021#include <asm/irq.h>
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010022#include <asm/irq_regs.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070023
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010024#include <hwregs/reg_map.h>
25#include <hwregs/reg_rdwr.h>
26#include <hwregs/timer_defs.h>
27#include <hwregs/intr_vect_defs.h>
28#ifdef CONFIG_CRIS_MACH_ARTPEC3
29#include <hwregs/clkgen_defs.h>
30#endif
Mikael Starvik51533b62005-07-27 11:44:44 -070031
32/* Watchdog defines */
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010033#define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */
34#define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */
35/* Number of 763 counts before watchdog bites */
36#define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1)
Mikael Starvik51533b62005-07-27 11:44:44 -070037
Jesper Nilsson60dbd662010-07-30 17:33:07 +020038/* Register the continuos readonly timer available in FS and ARTPEC-3. */
39static cycle_t read_cont_rotime(struct clocksource *cs)
40{
41 return (u32)REG_RD(timer, regi_timer0, r_time);
42}
43
44static struct clocksource cont_rotime = {
45 .name = "crisv32_rotime",
46 .rating = 300,
47 .read = read_cont_rotime,
48 .mask = CLOCKSOURCE_MASK(32),
Jesper Nilsson60dbd662010-07-30 17:33:07 +020049 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
50};
51
52static int __init etrax_init_cont_rotime(void)
53{
John Stultz027f6ad2010-10-19 17:58:48 -070054 clocksource_register_khz(&cont_rotime, 100000);
Jesper Nilsson60dbd662010-07-30 17:33:07 +020055 return 0;
56}
57arch_initcall(etrax_init_cont_rotime);
58
59
Mikael Starvik51533b62005-07-27 11:44:44 -070060unsigned long timer_regs[NR_CPUS] =
61{
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010062 regi_timer0,
Mikael Starvik51533b62005-07-27 11:44:44 -070063#ifdef CONFIG_SMP
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010064 regi_timer2
Mikael Starvik51533b62005-07-27 11:44:44 -070065#endif
66};
67
Mikael Starvik51533b62005-07-27 11:44:44 -070068extern int set_rtc_mmss(unsigned long nowtime);
Mikael Starvik51533b62005-07-27 11:44:44 -070069
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010070#ifdef CONFIG_CPU_FREQ
71static int
72cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
73 void *data);
74
75static struct notifier_block cris_time_freq_notifier_block = {
76 .notifier_call = cris_time_freq_notifier,
77};
78#endif
79
Mikael Starvik51533b62005-07-27 11:44:44 -070080unsigned long get_ns_in_jiffie(void)
81{
82 reg_timer_r_tmr0_data data;
83 unsigned long ns;
84
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010085 data = REG_RD(timer, regi_timer0, r_tmr0_data);
Mikael Starvik51533b62005-07-27 11:44:44 -070086 ns = (TIMER0_DIV - data) * 10;
87 return ns;
88}
89
Mikael Starvik51533b62005-07-27 11:44:44 -070090
91/* From timer MDS describing the hardware watchdog:
92 * 4.3.1 Watchdog Operation
93 * The watchdog timer is an 8-bit timer with a configurable start value.
Simon Arlott49b4ff32007-10-20 01:08:50 +020094 * Once started the watchdog counts downwards with a frequency of 763 Hz
Mikael Starvik51533b62005-07-27 11:44:44 -070095 * (100/131072 MHz). When the watchdog counts down to 1, it generates an
96 * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
97 * chip.
98 */
99/* This gives us 1.3 ms to do something useful when the NMI comes */
100
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100101/* Right now, starting the watchdog is the same as resetting it */
Mikael Starvik51533b62005-07-27 11:44:44 -0700102#define start_watchdog reset_watchdog
103
104#if defined(CONFIG_ETRAX_WATCHDOG)
105static short int watchdog_key = 42; /* arbitrary 7 bit number */
106#endif
107
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100108/* Number of pages to consider "out of memory". It is normal that the memory
109 * is used though, so set this really low. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700110#define WATCHDOG_MIN_FREE_PAGES 8
111
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200112void reset_watchdog(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700113{
114#if defined(CONFIG_ETRAX_WATCHDOG)
115 reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
116
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100117 /* Only keep watchdog happy as long as we have memory left! */
Mikael Starvik51533b62005-07-27 11:44:44 -0700118 if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100119 /* Reset the watchdog with the inverse of the old key */
120 /* Invert key, which is 7 bits */
121 watchdog_key ^= ETRAX_WD_KEY_MASK;
Mikael Starvik51533b62005-07-27 11:44:44 -0700122 wd_ctrl.cnt = ETRAX_WD_CNT;
123 wd_ctrl.cmd = regk_timer_start;
124 wd_ctrl.key = watchdog_key;
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100125 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
Mikael Starvik51533b62005-07-27 11:44:44 -0700126 }
127#endif
128}
129
130/* stop the watchdog - we still need the correct key */
131
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200132void stop_watchdog(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700133{
134#if defined(CONFIG_ETRAX_WATCHDOG)
135 reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
136 watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
137 wd_ctrl.cnt = ETRAX_WD_CNT;
138 wd_ctrl.cmd = regk_timer_stop;
139 wd_ctrl.key = watchdog_key;
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100140 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
Mikael Starvik51533b62005-07-27 11:44:44 -0700141#endif
142}
143
144extern void show_registers(struct pt_regs *regs);
145
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200146void handle_watchdog_bite(struct pt_regs *regs)
Mikael Starvik51533b62005-07-27 11:44:44 -0700147{
148#if defined(CONFIG_ETRAX_WATCHDOG)
149 extern int cause_of_death;
150
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100151 oops_in_progress = 1;
152 printk(KERN_WARNING "Watchdog bite\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700153
154 /* Check if forced restart or unexpected watchdog */
155 if (cause_of_death == 0xbedead) {
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100156#ifdef CONFIG_CRIS_MACH_ARTPEC3
157 /* There is a bug in Artpec-3 (voodoo TR 78) that requires
158 * us to go to lower frequency for the reset to be reliable
159 */
160 reg_clkgen_rw_clk_ctrl ctrl =
161 REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
162 ctrl.pll = 0;
163 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
164#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700165 while(1);
166 }
167
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100168 /* Unexpected watchdog, stop the watchdog and dump registers. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700169 stop_watchdog();
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100170 printk(KERN_WARNING "Oops: bitten by watchdog\n");
171 show_registers(regs);
172 oops_in_progress = 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700173#ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
174 reset_watchdog();
175#endif
176 while(1) /* nothing */;
177#endif
178}
179
Mikael Starvik51533b62005-07-27 11:44:44 -0700180/*
181 * timer_interrupt() needs to keep up the real-time clock,
Torben Hohn36cb07b2011-01-27 15:59:41 +0100182 * as well as call the "xtime_update()" routine every clocktick.
Mikael Starvik51533b62005-07-27 11:44:44 -0700183 */
Mikael Starvik51533b62005-07-27 11:44:44 -0700184extern void cris_do_profile(struct pt_regs *regs);
185
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200186static inline irqreturn_t timer_interrupt(int irq, void *dev_id)
Mikael Starvik51533b62005-07-27 11:44:44 -0700187{
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100188 struct pt_regs *regs = get_irq_regs();
Mikael Starvik51533b62005-07-27 11:44:44 -0700189 int cpu = smp_processor_id();
190 reg_timer_r_masked_intr masked_intr;
191 reg_timer_rw_ack_intr ack_intr = { 0 };
192
193 /* Check if the timer interrupt is for us (a tmr0 int) */
194 masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr);
195 if (!masked_intr.tmr0)
196 return IRQ_NONE;
197
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100198 /* Acknowledge the timer irq. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700199 ack_intr.tmr0 = 1;
200 REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr);
201
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100202 /* Reset watchdog otherwise it resets us! */
Mikael Starvik51533b62005-07-27 11:44:44 -0700203 reset_watchdog();
204
205 /* Update statistics. */
206 update_process_times(user_mode(regs));
207
208 cris_do_profile(regs); /* Save profiling information */
209
210 /* The master CPU is responsible for the time keeping. */
211 if (cpu != 0)
212 return IRQ_HANDLED;
213
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100214 /* Call the real timer interrupt handler */
Torben Hohn36cb07b2011-01-27 15:59:41 +0100215 xtime_update(1);
Mikael Starvik51533b62005-07-27 11:44:44 -0700216 return IRQ_HANDLED;
217}
218
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100219/* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain.
220 * It needs to be IRQF_DISABLED to make the jiffies update work properly.
Mikael Starvik51533b62005-07-27 11:44:44 -0700221 */
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100222static struct irqaction irq_timer = {
223 .handler = timer_interrupt,
Thomas Gleixneraa7135f2006-07-01 19:29:14 -0700224 .flags = IRQF_SHARED | IRQF_DISABLED,
Thomas Gleixneraa7135f2006-07-01 19:29:14 -0700225 .name = "timer"
226};
Mikael Starvik51533b62005-07-27 11:44:44 -0700227
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200228void __init cris_timer_init(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700229{
230 int cpu = smp_processor_id();
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100231 reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 };
232 reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV;
Mikael Starvik51533b62005-07-27 11:44:44 -0700233 reg_timer_rw_intr_mask timer_intr_mask;
234
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100235 /* Setup the etrax timers.
Mikael Starvik51533b62005-07-27 11:44:44 -0700236 * Base frequency is 100MHz, divider 1000000 -> 100 HZ
237 * We use timer0, so timer1 is free.
238 * The trig timer is used by the fasttimer API if enabled.
239 */
240
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100241 tmr0_ctrl.op = regk_timer_ld;
Mikael Starvik51533b62005-07-27 11:44:44 -0700242 tmr0_ctrl.freq = regk_timer_f100;
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100243 REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div);
244 REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */
245 tmr0_ctrl.op = regk_timer_run;
246 REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */
Mikael Starvik51533b62005-07-27 11:44:44 -0700247
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100248 /* Enable the timer irq. */
249 timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask);
250 timer_intr_mask.tmr0 = 1;
251 REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700252}
253
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200254void __init time_init(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700255{
256 reg_intr_vect_rw_mask intr_mask;
257
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100258 /* Probe for the RTC and read it if it exists.
Mikael Starvik51533b62005-07-27 11:44:44 -0700259 * Before the RTC can be probed the loops_per_usec variable needs
260 * to be initialized to make usleep work. A better value for
261 * loops_per_usec is calculated by the kernel later once the
262 * clock has started.
263 */
264 loops_per_usec = 50;
265
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100266 /* Start CPU local timer. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700267 cris_timer_init();
268
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100269 /* Enable the timer irq in global config. */
270 intr_mask = REG_RD_VECT(intr_vect, regi_irq, rw_mask, 1);
271 intr_mask.timer0 = 1;
272 REG_WR_VECT(intr_vect, regi_irq, rw_mask, 1, intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700273
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100274 /* Now actually register the timer irq handler that calls
275 * timer_interrupt(). */
276 setup_irq(TIMER0_INTR_VECT, &irq_timer);
Mikael Starvik51533b62005-07-27 11:44:44 -0700277
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100278 /* Enable watchdog if we should use one. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700279
280#if defined(CONFIG_ETRAX_WATCHDOG)
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100281 printk(KERN_INFO "Enabling watchdog...\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700282 start_watchdog();
283
284 /* If we use the hardware watchdog, we want to trap it as an NMI
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100285 * and dump registers before it resets us. For this to happen, we
286 * must set the "m" NMI enable flag (which once set, is unset only
287 * when an NMI is taken). */
288 {
289 unsigned long flags;
290 local_save_flags(flags);
291 flags |= (1<<30); /* NMI M flag is at bit 30 */
292 local_irq_restore(flags);
293 }
294#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700295
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100296#ifdef CONFIG_CPU_FREQ
297 cpufreq_register_notifier(&cris_time_freq_notifier_block,
298 CPUFREQ_TRANSITION_NOTIFIER);
Mikael Starvik51533b62005-07-27 11:44:44 -0700299#endif
300}
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100301
302#ifdef CONFIG_CPU_FREQ
303static int
304cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
305 void *data)
306{
307 struct cpufreq_freqs *freqs = data;
308 if (val == CPUFREQ_POSTCHANGE) {
309 reg_timer_r_tmr0_data data;
310 reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
311 do {
312 data = REG_RD(timer, timer_regs[freqs->cpu],
313 r_tmr0_data);
314 } while (data > 20);
315 REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
316 }
317 return 0;
318}
319#endif