time.c 11.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Andrew Morton's avatar
Andrew Morton committed
2
 *
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5 6 7 8
 * Copyright (C) 2001 MontaVista Software, ppopov@mvista.com
 * Copied and modified Carsten Langgaard's time.c
 *
 * Carsten Langgaard, carstenl@mips.com
 * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
 *
Andrew Morton's avatar
Andrew Morton committed
9 10
 * ########################################################################
 *
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16 17 18 19 20 21 22 23
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 *
Andrew Morton's avatar
Andrew Morton committed
24 25
 * ########################################################################
 *
Linus Torvalds's avatar
Linus Torvalds committed
26
 * Setting up the clock on the MIPS boards.
Andrew Morton's avatar
Andrew Morton committed
27 28 29 30 31 32
 *
 * Update.  Always configure the kernel with CONFIG_NEW_TIME_C.  This
 * will use the user interface gettimeofday() functions from the
 * arch/mips/kernel/time.c, and we provide the clock interrupt processing
 * and the timer offset compute functions.  If CONFIG_PM is selected,
 * we also ensure the 32KHz timer is available.   -- Dan
Linus Torvalds's avatar
Linus Torvalds committed
33
 */
Andrew Morton's avatar
Andrew Morton committed
34

Ralf Bächle's avatar
Ralf Bächle committed
35
#include <linux/types.h>
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40
#include <linux/config.h>
#include <linux/init.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
41
#include <linux/hardirq.h>
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44

#include <asm/mipsregs.h>
#include <asm/ptrace.h>
Ralf Bächle's avatar
Ralf Bächle committed
45
#include <asm/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
46
#include <asm/div64.h>
Andrew Morton's avatar
Andrew Morton committed
47
#include <asm/mach-au1x00/au1000.h>
Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51

#include <linux/mc146818rtc.h>
#include <linux/timex.h>

Ralf Bächle's avatar
Ralf Bächle committed
52
extern void startup_match20_interrupt(void);
Andrew Morton's avatar
Andrew Morton committed
53
extern void do_softirq(void);
Linus Torvalds's avatar
Linus Torvalds committed
54 55 56 57 58
extern volatile unsigned long wall_jiffies;
unsigned long missed_heart_beats = 0;

static unsigned long r4k_offset; /* Amount to increment compare reg each time */
static unsigned long r4k_cur;    /* What counter should be at next timer irq */
Andrew Morton's avatar
Andrew Morton committed
59 60
int	no_au1xxx_32khz;
void	(*au1k_wait_ptr)(void);
Ralf Bächle's avatar
Ralf Bächle committed
61 62 63

/* Cycle counter value at the previous timer interrupt.. */
static unsigned int timerhi = 0, timerlo = 0;
Linus Torvalds's avatar
Linus Torvalds committed
64

Ralf Bächle's avatar
Ralf Bächle committed
65 66 67 68 69 70 71
#ifdef CONFIG_PM
#define MATCH20_INC 328
extern void startup_match20_interrupt(void);
static unsigned long last_pc0, last_match20;
#endif

static spinlock_t time_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74

static inline void ack_r4ktimer(unsigned long newval)
{
Ralf Bächle's avatar
Ralf Bächle committed
75
	write_c0_compare(newval);
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78 79 80 81 82 83 84 85
}

/*
 * There are a lot of conceptually broken versions of the MIPS timer interrupt
 * handler floating around.  This one is rather different, but the algorithm
 * is provably more robust.
 */
unsigned long wtimer;
void mips_timer_interrupt(struct pt_regs *regs)
{
Ralf Bächle's avatar
Ralf Bächle committed
86 87 88 89
	int irq = 63;
	unsigned long count;

	irq_enter();
Ralf Bächle's avatar
Ralf Bächle committed
90
	kstat_this_cpu.irqs[irq]++;
Ralf Bächle's avatar
Ralf Bächle committed
91

Linus Torvalds's avatar
Linus Torvalds committed
92 93 94 95
	if (r4k_offset == 0)
		goto null;

	do {
Ralf Bächle's avatar
Ralf Bächle committed
96 97 98 99
		count = read_c0_count();
		timerhi += (count < timerlo);   /* Wrap around */
		timerlo = count;

Ralf Bächle's avatar
Ralf Bächle committed
100
		kstat_this_cpu.irqs[irq]++;
Linus Torvalds's avatar
Linus Torvalds committed
101
		do_timer(regs);
102 103 104
#ifndef CONFIG_SMP
		update_process_times(user_mode(regs));
#endif
Linus Torvalds's avatar
Linus Torvalds committed
105 106 107
		r4k_cur += r4k_offset;
		ack_r4ktimer(r4k_cur);

Ralf Bächle's avatar
Ralf Bächle committed
108
	} while (((unsigned long)read_c0_count()
Linus Torvalds's avatar
Linus Torvalds committed
109 110
	         - r4k_cur) < 0x7fffffff);

Ralf Bächle's avatar
Ralf Bächle committed
111
	irq_exit();
Linus Torvalds's avatar
Linus Torvalds committed
112 113 114 115 116 117
	return;

null:
	ack_r4ktimer(0);
}

Ralf Bächle's avatar
Ralf Bächle committed
118 119 120 121 122 123 124
#ifdef CONFIG_PM
void counter0_irq(int irq, void *dev_id, struct pt_regs *regs)
{
	unsigned long pc0;
	int time_elapsed;
	static int jiffie_drift = 0;

Andrew Morton's avatar
Andrew Morton committed
125
	kstat.irqs[0][irq]++;
Ralf Bächle's avatar
Ralf Bächle committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
		/* should never happen! */
		printk(KERN_WARNING "counter 0 w status eror\n");
		return;
	}

	pc0 = au_readl(SYS_TOYREAD);
	if (pc0 < last_match20) {
		/* counter overflowed */
		time_elapsed = (0xffffffff - last_match20) + pc0;
	}
	else {
		time_elapsed = pc0 - last_match20;
	}

	while (time_elapsed > 0) {
		do_timer(regs);
143 144 145
#ifndef CONFIG_SMP
		update_process_times(user_mode(regs));
#endif
Ralf Bächle's avatar
Ralf Bächle committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		time_elapsed -= MATCH20_INC;
		last_match20 += MATCH20_INC;
		jiffie_drift++;
	}

	last_pc0 = pc0;
	au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
	au_sync();

	/* our counter ticks at 10.009765625 ms/tick, we we're running
	 * almost 10uS too slow per tick.
	 */

	if (jiffie_drift >= 999) {
		jiffie_drift -= 999;
		do_timer(regs); /* increment jiffies by one */
162 163 164
#ifndef CONFIG_SMP
		update_process_times(user_mode(regs));
#endif
Ralf Bächle's avatar
Ralf Bächle committed
165 166
	}
}
Andrew Morton's avatar
Andrew Morton committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

/* When we wakeup from sleep, we have to "catch up" on all of the
 * timer ticks we have missed.
 */
void
wakeup_counter0_adjust(void)
{
	unsigned long pc0;
	int time_elapsed;

	pc0 = au_readl(SYS_TOYREAD);
	if (pc0 < last_match20) {
		/* counter overflowed */
		time_elapsed = (0xffffffff - last_match20) + pc0;
	}
	else {
		time_elapsed = pc0 - last_match20;
	}

	while (time_elapsed > 0) {
		time_elapsed -= MATCH20_INC;
		last_match20 += MATCH20_INC;
	}

	last_pc0 = pc0;
	au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
	au_sync();

}

/* This is just for debugging to set the timer for a sleep delay.
*/
void
wakeup_counter0_set(int ticks)
{
	unsigned long pc0;

	pc0 = au_readl(SYS_TOYREAD);
	last_pc0 = pc0;
	au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
	au_sync();
}
#endif

/* I haven't found anyone that doesn't use a 12 MHz source clock,
 * but just in case.....
 */
#ifdef CONFIG_AU1000_SRC_CLK
#define AU1000_SRC_CLK	CONFIG_AU1000_SRC_CLK
#else
#define AU1000_SRC_CLK	12000000
Ralf Bächle's avatar
Ralf Bächle committed
218 219 220
#endif

/*
Andrew Morton's avatar
Andrew Morton committed
221 222 223 224 225 226 227 228 229 230
 * We read the real processor speed from the PLL.  This is important
 * because it is more accurate than computing it from the 32KHz
 * counter, if it exists.  If we don't have an accurate processor
 * speed, all of the peripherals that derive their clocks based on
 * this advertised speed will introduce error and sometimes not work
 * properly.  This function is futher convoluted to still allow configurations
 * to do that in case they have really, really old silicon with a
 * write-only PLL register, that we need the 32KHz when power management
 * "wait" is enabled, and we need to detect if the 32KHz isn't present
 * but requested......got it? :-)		-- Dan
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235
 */
unsigned long cal_r4koff(void)
{
	unsigned long count;
	unsigned long cpu_speed;
Ralf Bächle's avatar
Ralf Bächle committed
236
	unsigned long flags;
Andrew Morton's avatar
Andrew Morton committed
237
	unsigned long counter;
Linus Torvalds's avatar
Linus Torvalds committed
238

Ralf Bächle's avatar
Ralf Bächle committed
239
	spin_lock_irqsave(&time_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
240

Andrew Morton's avatar
Andrew Morton committed
241 242 243
	/* Power management cares if we don't have a 32KHz counter.
	*/
	no_au1xxx_32khz = 0;
Ralf Bächle's avatar
Ralf Bächle committed
244
	counter = au_readl(SYS_COUNTER_CNTRL);
Andrew Morton's avatar
Andrew Morton committed
245 246
	if (counter & SYS_CNTRL_E0) {
		int trim_divide = 16;
Linus Torvalds's avatar
Linus Torvalds committed
247

Andrew Morton's avatar
Andrew Morton committed
248
		au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
Linus Torvalds's avatar
Linus Torvalds committed
249

Andrew Morton's avatar
Andrew Morton committed
250 251 252 253
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
		/* RTC now ticks at 32.768/16 kHz */
		au_writel(trim_divide-1, SYS_RTCTRIM);
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
Ralf Bächle's avatar
Ralf Bächle committed
254

Andrew Morton's avatar
Andrew Morton committed
255 256 257
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
		au_writel (0, SYS_TOYWRITE);
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
Linus Torvalds's avatar
Linus Torvalds committed
258

Andrew Morton's avatar
Andrew Morton committed
259 260 261
#if defined(CONFIG_AU1000_USE32K)
		{
			unsigned long start, end;
Linus Torvalds's avatar
Linus Torvalds committed
262

Andrew Morton's avatar
Andrew Morton committed
263 264 265 266 267
			start = au_readl(SYS_RTCREAD);
			start += 2;
			/* wait for the beginning of a new tick
			*/
			while (au_readl(SYS_RTCREAD) < start);
Linus Torvalds's avatar
Linus Torvalds committed
268

Andrew Morton's avatar
Andrew Morton committed
269 270 271
			/* Start r4k counter.
			*/
			write_c0_count(0);
Linus Torvalds's avatar
Linus Torvalds committed
272

Andrew Morton's avatar
Andrew Morton committed
273 274 275
			/* Wait 0.5 seconds.
			*/
			end = start + (32768 / trim_divide)/2;
Linus Torvalds's avatar
Linus Torvalds committed
276

Andrew Morton's avatar
Andrew Morton committed
277
			while (end > au_readl(SYS_RTCREAD));
Linus Torvalds's avatar
Linus Torvalds committed
278

Andrew Morton's avatar
Andrew Morton committed
279 280 281 282 283 284 285
			count = read_c0_count();
			cpu_speed = count * 2;
		}
#else
		cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * 
			AU1000_SRC_CLK;
		count = cpu_speed / 2;
Ralf Bächle's avatar
Ralf Bächle committed
286
#endif
Andrew Morton's avatar
Andrew Morton committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	}
	else {
		/* The 32KHz oscillator isn't running, so assume there
		 * isn't one and grab the processor speed from the PLL.
		 * NOTE: some old silicon doesn't allow reading the PLL.
		 */
		cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
		count = cpu_speed / 2;
		no_au1xxx_32khz = 1;
	}
	mips_hpt_frequency = count;
	// Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
	set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
	spin_unlock_irqrestore(&time_lock, flags);
	return (cpu_speed / HZ);
Ralf Bächle's avatar
Ralf Bächle committed
302
}
Linus Torvalds's avatar
Linus Torvalds committed
303

Ralf Bächle's avatar
Ralf Bächle committed
304 305 306
/* This is for machines which generate the exact clock. */
#define USECS_PER_JIFFY (1000000/HZ)
#define USECS_PER_JIFFY_FRAC (0x100000000*1000000/HZ&0xffffffff)
Linus Torvalds's avatar
Linus Torvalds committed
307

Andrew Morton's avatar
Andrew Morton committed
308

Linus Torvalds's avatar
Linus Torvalds committed
309 310 311 312 313 314 315 316
static unsigned long
div64_32(unsigned long v1, unsigned long v2, unsigned long v3)
{
	unsigned long r0;
	do_div64_32(r0, v1, v2, v3);
	return r0;
}

Andrew Morton's avatar
Andrew Morton committed
317
static unsigned long do_fast_cp0_gettimeoffset(void)
Linus Torvalds's avatar
Linus Torvalds committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
	u32 count;
	unsigned long res, tmp;
	unsigned long r0;

	/* Last jiffy when do_fast_gettimeoffset() was called. */
	static unsigned long last_jiffies=0;
	unsigned long quotient;

	/*
	 * Cached "1/(clocks per usec)*2^32" value.
	 * It has to be recalculated once each jiffy.
	 */
	static unsigned long cached_quotient=0;

	tmp = jiffies;

	quotient = cached_quotient;

	if (tmp && last_jiffies != tmp) {
		last_jiffies = tmp;
		if (last_jiffies != 0) {
			r0 = div64_32(timerhi, timerlo, tmp);
			quotient = div64_32(USECS_PER_JIFFY, USECS_PER_JIFFY_FRAC, r0);
			cached_quotient = quotient;
		}
	}

	/* Get last timer tick in absolute kernel time */
Ralf Bächle's avatar
Ralf Bächle committed
347
	count = read_c0_count();
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354 355 356 357 358

	/* .. relative to previous jiffy (32 bits is enough) */
	count -= timerlo;

	__asm__("multu\t%1,%2\n\t"
		"mfhi\t%0"
		:"=r" (res)
		:"r" (count),
		 "r" (quotient));

	/*
Andrew Morton's avatar
Andrew Morton committed
359
 	 * Due to possible jiffies inconsistencies, we need to check 
Linus Torvalds's avatar
Linus Torvalds committed
360 361 362 363 364 365
	 * the result so that we'll get a timer that is monotonic.
	 */
	if (res >= USECS_PER_JIFFY)
		res = USECS_PER_JIFFY-1;

	return res;
Andrew Morton's avatar
Andrew Morton committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
}

#ifdef CONFIG_PM
static unsigned long do_fast_pm_gettimeoffset(void)
{
	unsigned long pc0;
	unsigned long offset;

	pc0 = au_readl(SYS_TOYREAD);
	au_sync();
	offset = pc0 - last_pc0;
	if (offset > 2*MATCH20_INC) {
		printk("huge offset %x, last_pc0 %x last_match20 %x pc0 %x\n", 
				(unsigned)offset, (unsigned)last_pc0, 
				(unsigned)last_match20, (unsigned)pc0);
	}
	offset = (unsigned long)((offset * 305) / 10);
	return offset;
}
#endif

void au1xxx_timer_setup(struct irqaction *irq)
{
        unsigned int est_freq;
	extern unsigned long (*do_gettimeoffset)(void);
	extern void au1k_wait(void);

	printk("calculating r4koff... ");
	r4k_offset = cal_r4koff();
	printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);

	//est_freq = 2*r4k_offset*HZ;	
	est_freq = r4k_offset*HZ;	
	est_freq += 5000;    /* round */
	est_freq -= est_freq%10000;
	printk("CPU frequency %d.%02d MHz\n", est_freq/1000000, 
	       (est_freq%1000000)*100/1000000);
 	set_au1x00_speed(est_freq);
 	set_au1x00_lcd_clock(); // program the LCD clock

	r4k_cur = (read_c0_count() + r4k_offset);
	write_c0_compare(r4k_cur);

	/* no RTC on the pb1000 */
	xtime.tv_sec = 0;
	//xtime.tv_usec = 0;

#ifdef CONFIG_PM
	/*
	 * setup counter 0, since it keeps ticking after a
	 * 'wait' instruction has been executed. The CP0 timer and
	 * counter 1 do NOT continue running after 'wait'
	 *
	 * It's too early to call request_irq() here, so we handle
	 * counter 0 interrupt as a special irq and it doesn't show
	 * up under /proc/interrupts.
	 *
	 * Check to ensure we really have a 32KHz oscillator before
	 * we do this.
	 */
	if (no_au1xxx_32khz) {
		unsigned int c0_status;

		printk("WARNING: no 32KHz clock found.\n");
		do_gettimeoffset = do_fast_cp0_gettimeoffset;

		/* Ensure we get CPO_COUNTER interrupts.
		*/
		c0_status = read_c0_status();
		c0_status |= IE_IRQ5;
		write_c0_status(c0_status);
	}
	else {
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
		au_writel(0, SYS_TOYWRITE);
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);

		au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
		au_writel(~0, SYS_WAKESRC);
		au_sync();
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);

		/* setup match20 to interrupt once every 10ms */
		last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
		au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
		au_sync();
		while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
		startup_match20_interrupt();

		do_gettimeoffset = do_fast_pm_gettimeoffset;

		/* We can use the real 'wait' instruction.
		*/
		au1k_wait_ptr = au1k_wait;
	}

#else
	/* We have to do this here instead of in timer_init because
	 * the generic code in arch/mips/kernel/time.c will write
	 * over our function pointer.
	 */
	do_gettimeoffset = do_fast_cp0_gettimeoffset;
Ralf Bächle's avatar
Ralf Bächle committed
468
#endif
Linus Torvalds's avatar
Linus Torvalds committed
469
}
Andrew Morton's avatar
Andrew Morton committed
470 471 472 473

void __init au1xxx_time_init(void)
{
}