1/*
2 * Copyright 2001 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4 *
5 * Common time service routines for MIPS machines. See
6 * Documents/MIPS/README.txt.
7 *
8 * This program is free software; you can redistribute  it and/or modify it
9 * under  the terms of  the GNU General  Public License as published by the
10 * Free Software Foundation;  either version 2 of the  License, or (at your
11 * option) any later version.
12 */
13#include <linux/config.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/sched.h>
18#include <linux/param.h>
19#include <linux/time.h>
20#include <linux/smp.h>
21#include <linux/kernel_stat.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/module.h>
25#ifdef CONFIG_KERNPROF
26#include <linux/kernprof.h>
27#endif
28
29#include <asm/bootinfo.h>
30#include <asm/cpu.h>
31#include <asm/time.h>
32#include <asm/hardirq.h>
33#include <asm/div64.h>
34
35/* This is for machines which generate the exact clock. */
36#define USECS_PER_JIFFY (1000000/HZ)
37#define USECS_PER_JIFFY_FRAC ((u32)((1000000ULL << 32) / HZ))
38
39/*
40 * forward reference
41 */
42extern rwlock_t xtime_lock;
43extern volatile unsigned long wall_jiffies;
44
45spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
46
47/*
48 * whether we emulate local_timer_interrupts for SMP machines.
49 */
50int emulate_local_timer_interrupt;
51
52/*
53 * By default we provide the null RTC ops
54 */
55static unsigned long null_rtc_get_time(void)
56{
57	return mktime(2000, 1, 1, 0, 0, 0);
58}
59
60static int null_rtc_set_time(unsigned long sec)
61{
62	return 0;
63}
64
65unsigned long (*rtc_get_time)(void) = null_rtc_get_time;
66int (*rtc_set_time)(unsigned long) = null_rtc_set_time;
67
68
69/*
70 * timeofday services, for syscalls.
71 */
72void do_gettimeofday(struct timeval *tv)
73{
74	unsigned long flags;
75
76	read_lock_irqsave (&xtime_lock, flags);
77	*tv = xtime;
78	tv->tv_usec += do_gettimeoffset();
79
80	/*
81	 * xtime is atomically updated in timer_bh. jiffies - wall_jiffies
82	 * is nonzero if the timer bottom half hasnt executed yet.
83	 */
84	if (jiffies - wall_jiffies)
85		tv->tv_usec += USECS_PER_JIFFY;
86
87	read_unlock_irqrestore (&xtime_lock, flags);
88
89	if (tv->tv_usec >= 1000000) {
90		tv->tv_usec -= 1000000;
91		tv->tv_sec++;
92	}
93}
94
95void do_settimeofday(struct timeval *tv)
96{
97	write_lock_irq (&xtime_lock);
98
99	/* This is revolting. We need to set the xtime.tv_usec
100	 * correctly. However, the value in this location is
101	 * is value at the last tick.
102	 * Discover what correction gettimeofday
103	 * would have done, and then undo it!
104	 */
105	tv->tv_usec -= do_gettimeoffset();
106
107	if (tv->tv_usec < 0) {
108		tv->tv_usec += 1000000;
109		tv->tv_sec--;
110	}
111	xtime = *tv;
112	time_adjust = 0;			/* stop active adjtime() */
113	time_status |= STA_UNSYNC;
114	time_maxerror = NTP_PHASE_LIMIT;
115	time_esterror = NTP_PHASE_LIMIT;
116
117	write_unlock_irq (&xtime_lock);
118}
119
120
121/*
122 * Gettimeoffset routines.  These routines returns the time duration
123 * since last timer interrupt in usecs.
124 *
125 * If the exact CPU counter frequency is known, use fixed_rate_gettimeoffset.
126 * Otherwise use calibrate_gettimeoffset()
127 *
128 * If the CPU does not have counter register all, you can either supply
129 * your own gettimeoffset() routine, or use null_gettimeoffset() routines,
130 * which gives the same resolution as HZ.
131 */
132
133
134/* This is for machines which generate the exact clock. */
135#define USECS_PER_JIFFY (1000000/HZ)
136
137/* usecs per counter cycle, shifted to left by 32 bits */
138static unsigned int sll32_usecs_per_cycle=0;
139
140/* how many counter cycles in a jiffy */
141static unsigned long cycles_per_jiffy=0;
142
143/* Cycle counter value at the previous timer interrupt.. */
144static unsigned int timerhi, timerlo;
145
146/* last time when xtime and rtc are sync'ed up */
147static long last_rtc_update = 0;
148
149/* the function pointer to one of the gettimeoffset funcs*/
150unsigned long (*do_gettimeoffset)(void) = null_gettimeoffset;
151
152#if defined(CONFIG_KERNPROF)
153int prof_freq[NR_CPUS] = { [0 ... NR_CPUS - 1] = HZ };
154int prof_counter[NR_CPUS] = { [0 ... NR_CPUS - 1] = 1 };
155
156/*
157 * Change the frequency of the profiling timer.  The multiplier is specified
158 * by an appropriate ioctl() on /dev/kernprof.
159 */
160int setup_profiling_timer(unsigned int freq)
161{
162	int i;
163
164	/*
165	 * Sanity check.
166	 */
167	if (!freq)
168		return -EINVAL;
169
170	/*
171	 * Set the new multiplier for each CPU. CPUs don't start using the
172	 * new values until the next timer interrupt in which they do process
173	 * accounting. At that time they also adjust their APIC timers
174	 * accordingly.
175	 */
176	for (i = 0; i < NR_CPUS; ++i)
177		prof_freq[i] = freq;
178
179	return 0;
180}
181#endif
182
183unsigned long null_gettimeoffset(void)
184{
185	return 0;
186}
187
188unsigned long fixed_rate_gettimeoffset(void)
189{
190	u32 count;
191	unsigned long res;
192
193	/* Get last timer tick in absolute kernel time */
194	count = read_c0_count();
195
196	/* .. relative to previous jiffy (32 bits is enough) */
197	count -= timerlo;
198
199	__asm__("multu\t%1,%2\n\t"
200	        "mfhi\t%0"
201	        :"=r" (res)
202	        :"r" (count),
203	         "r" (sll32_usecs_per_cycle));
204
205	/*
206	 * Due to possible jiffies inconsistencies, we need to check
207	 * the result so that we'll get a timer that is monotonic.
208	 */
209	if (res >= USECS_PER_JIFFY)
210		res = USECS_PER_JIFFY-1;
211
212	return res;
213}
214
215/*
216 * Cached "1/(clocks per usec)*2^32" value.
217 * It has to be recalculated once each jiffy.
218 */
219static unsigned long cached_quotient;
220
221/* Last jiffy when calibrate_divXX_gettimeoffset() was called. */
222static unsigned long last_jiffies = 0;
223
224
225/*
226 * This is copied from dec/time.c:do_ioasic_gettimeoffset() by Mercij.
227 */
228unsigned long calibrate_div32_gettimeoffset(void)
229{
230	u32 count;
231	unsigned long res, tmp;
232	unsigned long quotient;
233
234	tmp = jiffies;
235
236	quotient = cached_quotient;
237
238	if (last_jiffies != tmp) {
239		last_jiffies = tmp;
240		if (last_jiffies != 0) {
241			unsigned long r0;
242			do_div64_32(r0, timerhi, timerlo, tmp);
243			do_div64_32(quotient, USECS_PER_JIFFY,
244			            USECS_PER_JIFFY_FRAC, r0);
245			cached_quotient = quotient;
246		}
247	}
248
249	/* Get last timer tick in absolute kernel time */
250	count = read_c0_count();
251
252	/* .. relative to previous jiffy (32 bits is enough) */
253	count -= timerlo;
254
255	__asm__("multu  %2,%3"
256	        : "=l" (tmp), "=h" (res)
257	        : "r" (count), "r" (quotient));
258
259	/*
260	 * Due to possible jiffies inconsistencies, we need to check
261	 * the result so that we'll get a timer that is monotonic.
262	 */
263	if (res >= USECS_PER_JIFFY)
264		res = USECS_PER_JIFFY - 1;
265
266	return res;
267}
268
269unsigned long calibrate_div64_gettimeoffset(void)
270{
271	u32 count;
272	unsigned long res, tmp;
273	unsigned long quotient;
274
275	tmp = jiffies;
276
277	quotient = cached_quotient;
278
279	if (tmp && last_jiffies != tmp) {
280		last_jiffies = tmp;
281		__asm__(".set\tnoreorder\n\t"
282	        ".set\tnoat\n\t"
283	        ".set\tmips3\n\t"
284	        "lwu\t%0,%2\n\t"
285	        "dsll32\t$1,%1,0\n\t"
286	        "or\t$1,$1,%0\n\t"
287	        "ddivu\t$0,$1,%3\n\t"
288	        "mflo\t$1\n\t"
289	        "dsll32\t%0,%4,0\n\t"
290	        "nop\n\t"
291	        "ddivu\t$0,%0,$1\n\t"
292	        "mflo\t%0\n\t"
293	        ".set\tmips0\n\t"
294	        ".set\tat\n\t"
295	        ".set\treorder"
296	        :"=&r" (quotient)
297	        :"r" (timerhi),
298	         "m" (timerlo),
299	         "r" (tmp),
300	         "r" (USECS_PER_JIFFY));
301	        cached_quotient = quotient;
302	}
303
304	/* Get last timer tick in absolute kernel time */
305	count = read_c0_count();
306
307	/* .. relative to previous jiffy (32 bits is enough) */
308	count -= timerlo;
309
310	__asm__("multu\t%1,%2\n\t"
311	        "mfhi\t%0"
312	        :"=r" (res)
313	        :"r" (count),
314	         "r" (quotient));
315
316	/*
317	 * Due to possible jiffies inconsistencies, we need to check
318	 * the result so that we'll get a timer that is monotonic.
319	 */
320	if (res >= USECS_PER_JIFFY)
321		res = USECS_PER_JIFFY-1;
322
323	return res;
324}
325
326
327/*
328 * local_timer_interrupt() does profiling and process accounting
329 * on a per-CPU basis.
330 *
331 * In UP mode, it is invoked from the (global) timer_interrupt.
332 *
333 * In SMP mode, it might invoked by per-CPU timer interrupt, or
334 * a broadcasted inter-processor interrupt which itself is triggered
335 * by the global timer interrupt.
336 */
337void local_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
338{
339#ifdef CONFIG_SMP
340	/* in UP mode, update_process_times() is invoked by do_timer() */
341	update_process_times(user_mode(regs));
342#endif
343}
344
345/*
346 * high-level timer interrupt service routines.  This function
347 * is set as irqaction->handler and is invoked through do_IRQ.
348 */
349void timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
350{
351	unsigned int cpu = smp_processor_id();
352
353	if (mips_cpu.options & MIPS_CPU_COUNTER) {
354		unsigned int count;
355
356		/*
357		 * The cycle counter is only 32 bit which is good for about
358		 * a minute at current count rates of upto 150MHz or so.
359		 */
360		count = read_c0_count();
361		timerhi += (count < timerlo);   /* Wrap around */
362		timerlo = count;
363
364		/*
365		 * set up for next timer interrupt - no harm if the machine
366		 * is using another timer interrupt source.
367		 * Note that writing to COMPARE register clears the interrupt
368		 */
369		write_c0_compare(
370					  count + cycles_per_jiffy);
371
372	}
373
374#if defined(CONFIG_KERNPROF)
375	if (prof_freq[cpu] <= HZ) {
376		if (--prof_counter[cpu] == 0) {
377			if (prof_timer_hook)
378				prof_timer_hook(regs);
379			prof_counter[cpu] = HZ / prof_freq[cpu];
380		}
381	}
382#endif
383
384	/*
385	 * call the generic timer interrupt handling
386	 */
387	do_timer(regs);
388
389	/*
390	 * If we have an externally synchronized Linux clock, then update
391	 * CMOS clock accordingly every ~11 minutes. rtc_set_time() has to be
392	 * called as close as possible to 500 ms before the new second starts.
393	 */
394	read_lock (&xtime_lock);
395	if ((time_status & STA_UNSYNC) == 0 &&
396	    xtime.tv_sec > last_rtc_update + 660 &&
397	    xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
398	    xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
399		if (rtc_set_time(xtime.tv_sec) == 0) {
400			last_rtc_update = xtime.tv_sec;
401		} else {
402			last_rtc_update = xtime.tv_sec - 600;
403			/* do it again in 60 s */
404		}
405	}
406	read_unlock (&xtime_lock);
407
408	/*
409	 * If jiffies has overflowed in this timer_interrupt we must
410	 * update the timer[hi]/[lo] to make fast gettimeoffset funcs
411	 * quotient calc still valid. -arca
412	 */
413	if (!jiffies) {
414		timerhi = timerlo = 0;
415	}
416
417#if !defined(CONFIG_SMP)
418	/*
419	 * In UP mode, we call local_timer_interrupt() to do profiling
420	 * and process accouting.
421	 *
422	 * In SMP mode, local_timer_interrupt() is invoked by appropriate
423	 * low-level local timer interrupt handler.
424	 */
425	local_timer_interrupt(0, NULL, regs);
426
427#else	/* CONFIG_SMP */
428
429	if (emulate_local_timer_interrupt) {
430		/*
431		 * this is the place where we send out inter-process
432		 * interrupts and let each CPU do its own profiling
433		 * and process accouting.
434		 *
435		 * Obviously we need to call local_timer_interrupt() for
436		 * the current CPU too.
437		 */
438		panic("Not implemented yet!!!");
439	}
440#endif	/* CONFIG_SMP */
441}
442
443asmlinkage void ll_timer_interrupt(int irq, struct pt_regs *regs)
444{
445	int cpu = smp_processor_id();
446
447	irq_enter(cpu, irq);
448	kstat.irqs[cpu][irq]++;
449
450	/* we keep interrupt disabled all the time */
451	timer_interrupt(irq, NULL, regs);
452
453	irq_exit(cpu, irq);
454
455	if (softirq_pending(cpu))
456		do_softirq();
457}
458
459asmlinkage void ll_local_timer_interrupt(int irq, struct pt_regs *regs)
460{
461	int cpu = smp_processor_id();
462
463	irq_enter(cpu, irq);
464	kstat.irqs[cpu][irq]++;
465
466	/* we keep interrupt disabled all the time */
467	local_timer_interrupt(irq, NULL, regs);
468
469	irq_exit(cpu, irq);
470
471	if (softirq_pending(cpu))
472		do_softirq();
473}
474
475/*
476 * time_init() - it does the following things.
477 *
478 * 1) board_time_init() -
479 * 	a) (optional) set up RTC routines,
480 *      b) (optional) calibrate and set the mips_counter_frequency
481 *	    (only needed if you intended to use fixed_rate_gettimeoffset
482 *	     or use cpu counter as timer interrupt source)
483 * 2) setup xtime based on rtc_get_time().
484 * 3) choose a appropriate gettimeoffset routine.
485 * 4) calculate a couple of cached variables for later usage
486 * 5) board_timer_setup() -
487 *	a) (optional) over-write any choices made above by time_init().
488 *	b) machine specific code should setup the timer irqaction.
489 *	c) enable the timer interrupt
490 */
491
492void (*board_time_init)(void) = NULL;
493void (*board_timer_setup)(struct irqaction *irq) = NULL;
494
495unsigned int mips_counter_frequency = 0;
496
497static struct irqaction timer_irqaction = {
498	timer_interrupt,
499	SA_INTERRUPT,
500	0,
501	"timer",
502	NULL,
503	NULL
504};
505
506void __init time_init(void)
507{
508	if (board_time_init)
509		board_time_init();
510
511	xtime.tv_sec = rtc_get_time();
512	xtime.tv_usec = 0;
513
514	/* choose appropriate gettimeoffset routine */
515	if (!(mips_cpu.options & MIPS_CPU_COUNTER)) {
516		/* no cpu counter - sorry */
517		do_gettimeoffset = null_gettimeoffset;
518	} else if (mips_counter_frequency != 0) {
519		/* we have cpu counter and know counter frequency! */
520		do_gettimeoffset = fixed_rate_gettimeoffset;
521	} else if ((mips_cpu.isa_level == MIPS_CPU_ISA_M32) ||
522		   (mips_cpu.isa_level == MIPS_CPU_ISA_I) ||
523		   (mips_cpu.isa_level == MIPS_CPU_ISA_II) ) {
524		/* we need to calibrate the counter but we don't have
525		 * 64-bit division. */
526		do_gettimeoffset = calibrate_div32_gettimeoffset;
527	} else {
528		/* we need to calibrate the counter but we *do* have
529		 * 64-bit division. */
530		do_gettimeoffset = calibrate_div64_gettimeoffset;
531	}
532
533	/* caclulate cache parameters */
534	if (mips_counter_frequency) {
535		u32 count;
536
537		cycles_per_jiffy = mips_counter_frequency / HZ;
538
539		/* sll32_usecs_per_cycle = 10^6 * 2^32 / mips_counter_freq */
540		/* any better way to do this? */
541		sll32_usecs_per_cycle = mips_counter_frequency / 100000;
542		sll32_usecs_per_cycle = 0xffffffff / sll32_usecs_per_cycle;
543		sll32_usecs_per_cycle *= 10;
544
545		/*
546		 * For those using cpu counter as timer,  this sets up the
547		 * first interrupt
548		 */
549		count = read_c0_count();
550		write_c0_compare(
551					  count + cycles_per_jiffy);
552	}
553
554	/*
555	 * Call board specific timer interrupt setup.
556	 *
557	 * this pointer must be setup in machine setup routine.
558	 *
559	 * Even if the machine choose to use low-level timer interrupt,
560	 * it still needs to setup the timer_irqaction.
561	 * In that case, it might be better to set timer_irqaction.handler
562	 * to be NULL function so that we are sure the high-level code
563	 * is not invoked accidentally.
564	 */
565	board_timer_setup(&timer_irqaction);
566}
567
568#define FEBRUARY		2
569#define STARTOFTIME		1970
570#define SECDAY			86400L
571#define SECYR			(SECDAY * 365)
572#define leapyear(year)		((year) % 4 == 0)
573#define days_in_year(a)		(leapyear(a) ? 366 : 365)
574#define days_in_month(a)	(month_days[(a) - 1])
575
576static int month_days[12] = {
577	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
578};
579
580void to_tm(unsigned long tim, struct rtc_time * tm)
581{
582	long hms, day, gday;
583	int i;
584
585	gday = day = tim / SECDAY;
586	hms = tim % SECDAY;
587
588	/* Hours, minutes, seconds are easy */
589	tm->tm_hour = hms / 3600;
590	tm->tm_min = (hms % 3600) / 60;
591	tm->tm_sec = (hms % 3600) % 60;
592
593	/* Number of years in days */
594	for (i = STARTOFTIME; day >= days_in_year(i); i++)
595	day -= days_in_year(i);
596	tm->tm_year = i;
597
598	/* Number of months in days left */
599	if (leapyear(tm->tm_year))
600	days_in_month(FEBRUARY) = 29;
601	for (i = 1; day >= days_in_month(i); i++)
602	day -= days_in_month(i);
603	days_in_month(FEBRUARY) = 28;
604	tm->tm_mon = i-1;	/* tm_mon starts from 0 to 11 */
605
606	/* Days are what is left over (+1) from all that. */
607	tm->tm_mday = day + 1;
608
609	/*
610	 * Determine the day of week
611	 */
612	tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
613}
614
615EXPORT_SYMBOL(rtc_lock);
616