blob: 6773fc83a670f08568a059bfa81c2c2e7a3bfa7f [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>
21#include <asm/rtc.h>
22#include <asm/irq.h>
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010023#include <asm/irq_regs.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070024
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010025#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
Mikael Starvik51533b62005-07-27 11:44:44 -070032
33/* Watchdog defines */
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010034#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)
Mikael Starvik51533b62005-07-27 11:44:44 -070038
Jesper Nilsson60dbd662010-07-30 17:33:07 +020039/* Register the continuos readonly timer available in FS and ARTPEC-3. */
40static cycle_t read_cont_rotime(struct clocksource *cs)
41{
42 return (u32)REG_RD(timer, regi_timer0, r_time);
43}
44
45static struct clocksource cont_rotime = {
46 .name = "crisv32_rotime",
47 .rating = 300,
48 .read = read_cont_rotime,
49 .mask = CLOCKSOURCE_MASK(32),
Jesper Nilsson60dbd662010-07-30 17:33:07 +020050 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
51};
52
53static int __init etrax_init_cont_rotime(void)
54{
John Stultz027f6ad2010-10-19 17:58:48 -070055 clocksource_register_khz(&cont_rotime, 100000);
Jesper Nilsson60dbd662010-07-30 17:33:07 +020056 return 0;
57}
58arch_initcall(etrax_init_cont_rotime);
59
60
Mikael Starvik51533b62005-07-27 11:44:44 -070061unsigned long timer_regs[NR_CPUS] =
62{
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010063 regi_timer0,
Mikael Starvik51533b62005-07-27 11:44:44 -070064#ifdef CONFIG_SMP
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010065 regi_timer2
Mikael Starvik51533b62005-07-27 11:44:44 -070066#endif
67};
68
Mikael Starvik51533b62005-07-27 11:44:44 -070069extern int set_rtc_mmss(unsigned long nowtime);
Mikael Starvik51533b62005-07-27 11:44:44 -070070extern int have_rtc;
71
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010072#ifdef CONFIG_CPU_FREQ
73static int
74cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
75 void *data);
76
77static struct notifier_block cris_time_freq_notifier_block = {
78 .notifier_call = cris_time_freq_notifier,
79};
80#endif
81
Mikael Starvik51533b62005-07-27 11:44:44 -070082unsigned long get_ns_in_jiffie(void)
83{
84 reg_timer_r_tmr0_data data;
85 unsigned long ns;
86
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +010087 data = REG_RD(timer, regi_timer0, r_tmr0_data);
Mikael Starvik51533b62005-07-27 11:44:44 -070088 ns = (TIMER0_DIV - data) * 10;
89 return ns;
90}
91
Mikael Starvik51533b62005-07-27 11:44:44 -070092
93/* From timer MDS describing the hardware watchdog:
94 * 4.3.1 Watchdog Operation
95 * The watchdog timer is an 8-bit timer with a configurable start value.
Simon Arlott49b4ff32007-10-20 01:08:50 +020096 * Once started the watchdog counts downwards with a frequency of 763 Hz
Mikael Starvik51533b62005-07-27 11:44:44 -070097 * (100/131072 MHz). When the watchdog counts down to 1, it generates an
98 * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
99 * chip.
100 */
101/* This gives us 1.3 ms to do something useful when the NMI comes */
102
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100103/* Right now, starting the watchdog is the same as resetting it */
Mikael Starvik51533b62005-07-27 11:44:44 -0700104#define start_watchdog reset_watchdog
105
106#if defined(CONFIG_ETRAX_WATCHDOG)
107static short int watchdog_key = 42; /* arbitrary 7 bit number */
108#endif
109
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100110/* Number of pages to consider "out of memory". It is normal that the memory
111 * is used though, so set this really low. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700112#define WATCHDOG_MIN_FREE_PAGES 8
113
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200114void reset_watchdog(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700115{
116#if defined(CONFIG_ETRAX_WATCHDOG)
117 reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
118
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100119 /* Only keep watchdog happy as long as we have memory left! */
Mikael Starvik51533b62005-07-27 11:44:44 -0700120 if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100121 /* Reset the watchdog with the inverse of the old key */
122 /* Invert key, which is 7 bits */
123 watchdog_key ^= ETRAX_WD_KEY_MASK;
Mikael Starvik51533b62005-07-27 11:44:44 -0700124 wd_ctrl.cnt = ETRAX_WD_CNT;
125 wd_ctrl.cmd = regk_timer_start;
126 wd_ctrl.key = watchdog_key;
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100127 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
Mikael Starvik51533b62005-07-27 11:44:44 -0700128 }
129#endif
130}
131
132/* stop the watchdog - we still need the correct key */
133
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200134void stop_watchdog(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700135{
136#if defined(CONFIG_ETRAX_WATCHDOG)
137 reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
138 watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
139 wd_ctrl.cnt = ETRAX_WD_CNT;
140 wd_ctrl.cmd = regk_timer_stop;
141 wd_ctrl.key = watchdog_key;
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100142 REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
Mikael Starvik51533b62005-07-27 11:44:44 -0700143#endif
144}
145
146extern void show_registers(struct pt_regs *regs);
147
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200148void handle_watchdog_bite(struct pt_regs *regs)
Mikael Starvik51533b62005-07-27 11:44:44 -0700149{
150#if defined(CONFIG_ETRAX_WATCHDOG)
151 extern int cause_of_death;
152
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100153 oops_in_progress = 1;
154 printk(KERN_WARNING "Watchdog bite\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700155
156 /* Check if forced restart or unexpected watchdog */
157 if (cause_of_death == 0xbedead) {
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100158#ifdef CONFIG_CRIS_MACH_ARTPEC3
159 /* There is a bug in Artpec-3 (voodoo TR 78) that requires
160 * us to go to lower frequency for the reset to be reliable
161 */
162 reg_clkgen_rw_clk_ctrl ctrl =
163 REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
164 ctrl.pll = 0;
165 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
166#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700167 while(1);
168 }
169
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100170 /* Unexpected watchdog, stop the watchdog and dump registers. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700171 stop_watchdog();
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100172 printk(KERN_WARNING "Oops: bitten by watchdog\n");
173 show_registers(regs);
174 oops_in_progress = 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700175#ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
176 reset_watchdog();
177#endif
178 while(1) /* nothing */;
179#endif
180}
181
Mikael Starvik51533b62005-07-27 11:44:44 -0700182/*
183 * timer_interrupt() needs to keep up the real-time clock,
Torben Hohn36cb07b2011-01-27 15:59:41 +0100184 * as well as call the "xtime_update()" routine every clocktick.
Mikael Starvik51533b62005-07-27 11:44:44 -0700185 */
Mikael Starvik51533b62005-07-27 11:44:44 -0700186extern void cris_do_profile(struct pt_regs *regs);
187
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200188static inline irqreturn_t timer_interrupt(int irq, void *dev_id)
Mikael Starvik51533b62005-07-27 11:44:44 -0700189{
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100190 struct pt_regs *regs = get_irq_regs();
Mikael Starvik51533b62005-07-27 11:44:44 -0700191 int cpu = smp_processor_id();
192 reg_timer_r_masked_intr masked_intr;
193 reg_timer_rw_ack_intr ack_intr = { 0 };
194
195 /* Check if the timer interrupt is for us (a tmr0 int) */
196 masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr);
197 if (!masked_intr.tmr0)
198 return IRQ_NONE;
199
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100200 /* Acknowledge the timer irq. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700201 ack_intr.tmr0 = 1;
202 REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr);
203
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100204 /* Reset watchdog otherwise it resets us! */
Mikael Starvik51533b62005-07-27 11:44:44 -0700205 reset_watchdog();
206
207 /* Update statistics. */
208 update_process_times(user_mode(regs));
209
210 cris_do_profile(regs); /* Save profiling information */
211
212 /* The master CPU is responsible for the time keeping. */
213 if (cpu != 0)
214 return IRQ_HANDLED;
215
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100216 /* Call the real timer interrupt handler */
Torben Hohn36cb07b2011-01-27 15:59:41 +0100217 xtime_update(1);
Mikael Starvik51533b62005-07-27 11:44:44 -0700218 return IRQ_HANDLED;
219}
220
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100221/* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain.
222 * It needs to be IRQF_DISABLED to make the jiffies update work properly.
Mikael Starvik51533b62005-07-27 11:44:44 -0700223 */
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100224static struct irqaction irq_timer = {
225 .handler = timer_interrupt,
Thomas Gleixneraa7135f2006-07-01 19:29:14 -0700226 .flags = IRQF_SHARED | IRQF_DISABLED,
Thomas Gleixneraa7135f2006-07-01 19:29:14 -0700227 .name = "timer"
228};
Mikael Starvik51533b62005-07-27 11:44:44 -0700229
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200230void __init cris_timer_init(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700231{
232 int cpu = smp_processor_id();
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100233 reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 };
234 reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV;
Mikael Starvik51533b62005-07-27 11:44:44 -0700235 reg_timer_rw_intr_mask timer_intr_mask;
236
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100237 /* Setup the etrax timers.
Mikael Starvik51533b62005-07-27 11:44:44 -0700238 * Base frequency is 100MHz, divider 1000000 -> 100 HZ
239 * We use timer0, so timer1 is free.
240 * The trig timer is used by the fasttimer API if enabled.
241 */
242
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100243 tmr0_ctrl.op = regk_timer_ld;
Mikael Starvik51533b62005-07-27 11:44:44 -0700244 tmr0_ctrl.freq = regk_timer_f100;
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100245 REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div);
246 REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */
247 tmr0_ctrl.op = regk_timer_run;
248 REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */
Mikael Starvik51533b62005-07-27 11:44:44 -0700249
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100250 /* Enable the timer irq. */
251 timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask);
252 timer_intr_mask.tmr0 = 1;
253 REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700254}
255
Jesper Nilsson60dbd662010-07-30 17:33:07 +0200256void __init time_init(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700257{
258 reg_intr_vect_rw_mask intr_mask;
259
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100260 /* Probe for the RTC and read it if it exists.
Mikael Starvik51533b62005-07-27 11:44:44 -0700261 * Before the RTC can be probed the loops_per_usec variable needs
262 * to be initialized to make usleep work. A better value for
263 * loops_per_usec is calculated by the kernel later once the
264 * clock has started.
265 */
266 loops_per_usec = 50;
267
John Stultz8eff8a52010-03-03 19:57:28 -0800268 if(RTC_INIT() < 0)
Mikael Starvik51533b62005-07-27 11:44:44 -0700269 have_rtc = 0;
John Stultz8eff8a52010-03-03 19:57:28 -0800270 else
Mikael Starvik51533b62005-07-27 11:44:44 -0700271 have_rtc = 1;
Mikael Starvik51533b62005-07-27 11:44:44 -0700272
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100273 /* Start CPU local timer. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700274 cris_timer_init();
275
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100276 /* Enable the timer irq in global config. */
277 intr_mask = REG_RD_VECT(intr_vect, regi_irq, rw_mask, 1);
278 intr_mask.timer0 = 1;
279 REG_WR_VECT(intr_vect, regi_irq, rw_mask, 1, intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700280
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100281 /* Now actually register the timer irq handler that calls
282 * timer_interrupt(). */
283 setup_irq(TIMER0_INTR_VECT, &irq_timer);
Mikael Starvik51533b62005-07-27 11:44:44 -0700284
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100285 /* Enable watchdog if we should use one. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700286
287#if defined(CONFIG_ETRAX_WATCHDOG)
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100288 printk(KERN_INFO "Enabling watchdog...\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700289 start_watchdog();
290
291 /* If we use the hardware watchdog, we want to trap it as an NMI
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100292 * and dump registers before it resets us. For this to happen, we
293 * must set the "m" NMI enable flag (which once set, is unset only
294 * when an NMI is taken). */
295 {
296 unsigned long flags;
297 local_save_flags(flags);
298 flags |= (1<<30); /* NMI M flag is at bit 30 */
299 local_irq_restore(flags);
300 }
301#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700302
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100303#ifdef CONFIG_CPU_FREQ
304 cpufreq_register_notifier(&cris_time_freq_notifier_block,
305 CPUFREQ_TRANSITION_NOTIFIER);
Mikael Starvik51533b62005-07-27 11:44:44 -0700306#endif
307}
Jesper Nilssonfbdb5f82007-12-04 17:25:45 +0100308
309#ifdef CONFIG_CPU_FREQ
310static int
311cris_time_freq_notifier(struct notifier_block *nb, unsigned long val,
312 void *data)
313{
314 struct cpufreq_freqs *freqs = data;
315 if (val == CPUFREQ_POSTCHANGE) {
316 reg_timer_r_tmr0_data data;
317 reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
318 do {
319 data = REG_RD(timer, timer_regs[freqs->cpu],
320 r_tmr0_data);
321 } while (data > 20);
322 REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
323 }
324 return 0;
325}
326#endif