time.c 8.6 KB
Newer Older
1
/*
2 3
 *  linux/arch/cris/arch-v32/kernel/time.c
 *
J
Jesper Nilsson 已提交
4
 *  Copyright (C) 2003-2010 Axis Communications AB
5 6 7 8 9
 *
 */

#include <linux/timex.h>
#include <linux/time.h>
J
Jesper Nilsson 已提交
10
#include <linux/clocksource.h>
11 12 13 14 15
#include <linux/interrupt.h>
#include <linux/swap.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/threads.h>
16
#include <linux/cpufreq.h>
17
#include <linux/mm.h>
18 19 20 21 22
#include <asm/types.h>
#include <asm/signal.h>
#include <asm/io.h>
#include <asm/delay.h>
#include <asm/irq.h>
23 24 25 26 27 28 29 30 31
#include <asm/irq_regs.h>

#include <hwregs/reg_map.h>
#include <hwregs/reg_rdwr.h>
#include <hwregs/timer_defs.h>
#include <hwregs/intr_vect_defs.h>
#ifdef CONFIG_CRIS_MACH_ARTPEC3
#include <hwregs/clkgen_defs.h>
#endif
32 33

/* Watchdog defines */
34 35 36 37
#define ETRAX_WD_KEY_MASK	0x7F /* key is 7 bit */
#define ETRAX_WD_HZ		763 /* watchdog counts at 763 Hz */
/* Number of 763 counts before watchdog bites */
#define ETRAX_WD_CNT		((2*ETRAX_WD_HZ)/HZ + 1)
38

J
Jesper Nilsson 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/* Register the continuos readonly timer available in FS and ARTPEC-3.  */
static cycle_t read_cont_rotime(struct clocksource *cs)
{
	return (u32)REG_RD(timer, regi_timer0, r_time);
}

static struct clocksource cont_rotime = {
	.name   = "crisv32_rotime",
	.rating = 300,
	.read   = read_cont_rotime,
	.mask   = CLOCKSOURCE_MASK(32),
	.flags  = CLOCK_SOURCE_IS_CONTINUOUS,
};

static int __init etrax_init_cont_rotime(void)
{
55
	clocksource_register_khz(&cont_rotime, 100000);
J
Jesper Nilsson 已提交
56 57 58 59
	return 0;
}
arch_initcall(etrax_init_cont_rotime);

60 61
unsigned long timer_regs[NR_CPUS] =
{
62
	regi_timer0,
63
#ifdef CONFIG_SMP
64
	regi_timer2
65 66 67 68 69
#endif
};

extern int set_rtc_mmss(unsigned long nowtime);

70
#ifdef CONFIG_CPU_FREQ
71 72
static int cris_time_freq_notifier(struct notifier_block *nb,
				   unsigned long val, void *data);
73 74 75 76 77 78

static struct notifier_block cris_time_freq_notifier_block = {
	.notifier_call = cris_time_freq_notifier,
};
#endif

79 80 81 82 83
unsigned long get_ns_in_jiffie(void)
{
	reg_timer_r_tmr0_data data;
	unsigned long ns;

84
	data = REG_RD(timer, regi_timer0, r_tmr0_data);
85 86 87 88 89 90 91
	ns = (TIMER0_DIV - data) * 10;
	return ns;
}

/* From timer MDS describing the hardware watchdog:
 * 4.3.1 Watchdog Operation
 * The watchdog timer is an 8-bit timer with a configurable start value.
S
Simon Arlott 已提交
92
 * Once started the watchdog counts downwards with a frequency of 763 Hz
93 94 95 96 97 98
 * (100/131072 MHz). When the watchdog counts down to 1, it generates an
 * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
 * chip.
 */
/* This gives us 1.3 ms to do something useful when the NMI comes */

99
/* Right now, starting the watchdog is the same as resetting it */
100 101 102 103 104 105
#define start_watchdog reset_watchdog

#if defined(CONFIG_ETRAX_WATCHDOG)
static short int watchdog_key = 42;  /* arbitrary 7 bit number */
#endif

106 107
/* Number of pages to consider "out of memory". It is normal that the memory
 * is used though, so set this really low. */
108 109
#define WATCHDOG_MIN_FREE_PAGES 8

110 111 112
/* for reliable NICE_DOGGY behaviour */
static int bite_in_progress;

J
Jesper Nilsson 已提交
113
void reset_watchdog(void)
114 115 116 117
{
#if defined(CONFIG_ETRAX_WATCHDOG)
	reg_timer_rw_wd_ctrl wd_ctrl = { 0 };

118 119 120 121
#if defined(CONFIG_ETRAX_WATCHDOG_NICE_DOGGY)
	if (unlikely(bite_in_progress))
		return;
#endif
122
	/* Only keep watchdog happy as long as we have memory left! */
123
	if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
124 125 126
		/* Reset the watchdog with the inverse of the old key */
		/* Invert key, which is 7 bits */
		watchdog_key ^= ETRAX_WD_KEY_MASK;
127 128 129
		wd_ctrl.cnt = ETRAX_WD_CNT;
		wd_ctrl.cmd = regk_timer_start;
		wd_ctrl.key = watchdog_key;
130
		REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
131 132 133 134 135 136
	}
#endif
}

/* stop the watchdog - we still need the correct key */

J
Jesper Nilsson 已提交
137
void stop_watchdog(void)
138 139 140 141 142 143 144
{
#if defined(CONFIG_ETRAX_WATCHDOG)
	reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
	watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
	wd_ctrl.cnt = ETRAX_WD_CNT;
	wd_ctrl.cmd = regk_timer_stop;
	wd_ctrl.key = watchdog_key;
145
	REG_WR(timer, regi_timer0, rw_wd_ctrl, wd_ctrl);
146 147 148 149 150
#endif
}

extern void show_registers(struct pt_regs *regs);

J
Jesper Nilsson 已提交
151
void handle_watchdog_bite(struct pt_regs *regs)
152 153 154 155
{
#if defined(CONFIG_ETRAX_WATCHDOG)
	extern int cause_of_death;

156
	nmi_enter();
157
	oops_in_progress = 1;
158
	bite_in_progress = 1;
159
	printk(KERN_WARNING "Watchdog bite\n");
160 161 162

	/* Check if forced restart or unexpected watchdog */
	if (cause_of_death == 0xbedead) {
163 164 165 166 167 168 169 170 171
#ifdef CONFIG_CRIS_MACH_ARTPEC3
		/* There is a bug in Artpec-3 (voodoo TR 78) that requires
		 * us to go to lower frequency for the reset to be reliable
		 */
		reg_clkgen_rw_clk_ctrl ctrl =
			REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
		ctrl.pll = 0;
		REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, ctrl);
#endif
172 173 174
		while(1);
	}

175
	/* Unexpected watchdog, stop the watchdog and dump registers. */
176
	stop_watchdog();
177 178 179
	printk(KERN_WARNING "Oops: bitten by watchdog\n");
	show_registers(regs);
	oops_in_progress = 0;
180
	printk("\n"); /* Flush mtdoops.  */
181 182 183 184 185 186 187 188 189
#ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
	reset_watchdog();
#endif
	while(1) /* nothing */;
#endif
}

/*
 * timer_interrupt() needs to keep up the real-time clock,
190
 * as well as call the "xtime_update()" routine every clocktick.
191 192 193
 */
extern void cris_do_profile(struct pt_regs *regs);

J
Jesper Nilsson 已提交
194
static inline irqreturn_t timer_interrupt(int irq, void *dev_id)
195
{
196
	struct pt_regs *regs = get_irq_regs();
197 198 199 200 201 202 203 204 205
	int cpu = smp_processor_id();
	reg_timer_r_masked_intr masked_intr;
	reg_timer_rw_ack_intr ack_intr = { 0 };

	/* Check if the timer interrupt is for us (a tmr0 int) */
	masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr);
	if (!masked_intr.tmr0)
		return IRQ_NONE;

206
	/* Acknowledge the timer irq. */
207 208 209
	ack_intr.tmr0 = 1;
	REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr);

210
	/* Reset watchdog otherwise it resets us! */
211 212
	reset_watchdog();

213
	/* Update statistics. */
214 215 216 217 218 219 220 221
	update_process_times(user_mode(regs));

	cris_do_profile(regs); /* Save profiling information */

	/* The master CPU is responsible for the time keeping. */
	if (cpu != 0)
		return IRQ_HANDLED;

222
	/* Call the real timer interrupt handler */
223
	xtime_update(1);
224
	return IRQ_HANDLED;
225 226
}

227
/* Timer is IRQF_SHARED so drivers can add stuff to the timer irq chain. */
228 229
static struct irqaction irq_timer = {
	.handler = timer_interrupt,
230
	.flags = IRQF_SHARED,
231 232
	.name = "timer"
};
233

J
Jesper Nilsson 已提交
234
void __init cris_timer_init(void)
235 236
{
	int cpu = smp_processor_id();
237 238
	reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 };
	reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV;
239 240
	reg_timer_rw_intr_mask timer_intr_mask;

241
	/* Setup the etrax timers.
242 243 244 245 246
	 * Base frequency is 100MHz, divider 1000000 -> 100 HZ
	 * We use timer0, so timer1 is free.
	 * The trig timer is used by the fasttimer API if enabled.
	 */

247
	tmr0_ctrl.op = regk_timer_ld;
248
	tmr0_ctrl.freq = regk_timer_f100;
249 250 251 252 253 254 255 256 257
	REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div);
	REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */
	tmr0_ctrl.op = regk_timer_run;
	REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */

	/* Enable the timer irq. */
	timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask);
	timer_intr_mask.tmr0 = 1;
	REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask);
258 259
}

J
Jesper Nilsson 已提交
260
void __init time_init(void)
261 262 263
{
	reg_intr_vect_rw_mask intr_mask;

264
	/* Probe for the RTC and read it if it exists.
265 266 267 268 269 270 271
	 * Before the RTC can be probed the loops_per_usec variable needs
	 * to be initialized to make usleep work. A better value for
	 * loops_per_usec is calculated by the kernel later once the
	 * clock has started.
	 */
	loops_per_usec = 50;

272
	/* Start CPU local timer. */
273 274
	cris_timer_init();

275 276 277 278
	/* Enable the timer irq in global config. */
	intr_mask = REG_RD_VECT(intr_vect, regi_irq, rw_mask, 1);
	intr_mask.timer0 = 1;
	REG_WR_VECT(intr_vect, regi_irq, rw_mask, 1, intr_mask);
279

280 281 282
	/* Now actually register the timer irq handler that calls
	 * timer_interrupt(). */
	setup_irq(TIMER0_INTR_VECT, &irq_timer);
283

284
	/* Enable watchdog if we should use one. */
285 286

#if defined(CONFIG_ETRAX_WATCHDOG)
287
	printk(KERN_INFO "Enabling watchdog...\n");
288 289 290
	start_watchdog();

	/* If we use the hardware watchdog, we want to trap it as an NMI
291 292 293 294 295 296 297 298 299 300 301 302 303
	 * and dump registers before it resets us.  For this to happen, we
	 * must set the "m" NMI enable flag (which once set, is unset only
	 * when an NMI is taken). */
	{
		unsigned long flags;
		local_save_flags(flags);
		flags |= (1<<30); /* NMI M flag is at bit 30 */
		local_irq_restore(flags);
	}
#endif

#ifdef CONFIG_CPU_FREQ
	cpufreq_register_notifier(&cris_time_freq_notifier_block,
304
				  CPUFREQ_TRANSITION_NOTIFIER);
305 306
#endif
}
307 308

#ifdef CONFIG_CPU_FREQ
309 310
static int cris_time_freq_notifier(struct notifier_block *nb,
				   unsigned long val, void *data)
311 312 313 314 315 316 317 318 319 320 321 322 323 324
{
	struct cpufreq_freqs *freqs = data;
	if (val == CPUFREQ_POSTCHANGE) {
		reg_timer_r_tmr0_data data;
		reg_timer_rw_tmr0_div div = (freqs->new * 500) / HZ;
		do {
			data = REG_RD(timer, timer_regs[freqs->cpu],
				r_tmr0_data);
		} while (data > 20);
		REG_WR(timer, timer_regs[freqs->cpu], rw_tmr0_div, div);
	}
	return 0;
}
#endif