2 * $Id: time.c,v 1.39 1998/12/28 10:28:51 paulus Exp $
3 * Common time routines among all ppc machines.
5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6 * Paul Mackerras' version and mine for PReP and Pmac.
7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
9 * Since the MPC8xx has a programmable interrupt timer, I decided to
10 * use that rather than the decrementer. Two reasons: 1.) the clock
11 * frequency is low, causing 2.) a long wait in the timer interrupt
12 * while ((d = get_dec()) == dval)
13 * loop. The MPC8xx can be driven from a variety of input clocks,
14 * so a number of assumptions have been made here because the kernel
15 * parameter HZ is a constant. We assume (correctly, today :-) that
16 * the MPC8xx on the MBX board is driven from a 32.768 kHz crystal.
17 * This is then divided by 4, providing a 8192 Hz clock into the PIT.
18 * Since it is not possible to get a nice 100 Hz clock out of this, without
19 * creating a software PLL, I have set HZ to 128. -- Dan
21 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
22 * "A Kernel Model for Precision Timekeeping" by Dave Mills
25 #include <linux/config.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/kernel.h>
29 #include <linux/param.h>
30 #include <linux/string.h>
32 #include <linux/interrupt.h>
33 #include <linux/timex.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/mc146818rtc.h>
36 #include <linux/time.h>
37 #include <linux/init.h>
39 #include <asm/segment.h>
41 #include <asm/processor.h>
42 #include <asm/nvram.h>
43 #include <asm/cache.h>
48 #include <asm/8xx_immap.h>
53 /* this is set to the appropriate pmac/prep/chrp func in init_IRQ() */
54 int (*set_rtc_time
)(unsigned long);
56 void smp_local_timer_interrupt(struct pt_regs
*);
58 /* keep track of when we need to update the rtc */
59 unsigned long last_rtc_update
= 0;
61 /* The decrementer counts down by 128 every 128ns on a 601. */
62 #define DECREMENTER_COUNT_601 (1000000000 / HZ)
63 #define COUNT_PERIOD_NUM_601 1
64 #define COUNT_PERIOD_DEN_601 1000
66 unsigned decrementer_count
; /* count value for 1e6/HZ microseconds */
67 unsigned count_period_num
; /* 1 decrementer count equals */
68 unsigned count_period_den
; /* count_period_num / count_period_den us */
71 * timer_interrupt - gets called when the decrementer overflows,
72 * with interrupts disabled.
73 * We set it up to overflow again in 1/HZ seconds.
75 void timer_interrupt(struct pt_regs
* regs
)
78 unsigned long cpu
= smp_processor_id();
79 /* save the HID0 in case dcache was off - see idle.c
80 * this hack should leave for a better solution -- Cort */
81 unsigned dcache_locked
= unlock_dcache();
86 unsigned int loops
= 100000000;
87 while (test_bit(0, &global_irq_lock
)) {
88 if (smp_processor_id() == global_irq_holder
) {
89 printk("uh oh, interrupt while we hold global irq lock!\n");
96 printk("do_IRQ waiting for irq lock (holder=%d)\n", global_irq_holder
);
105 while ((dval
= get_dec()) < 0) {
107 * Wait for the decrementer to change, then jump
108 * in and add decrementer_count to its value
109 * (quickly, before it changes again!)
111 while ((d
= get_dec()) == dval
)
113 set_dec(d
+ decrementer_count
);
114 if ( !smp_processor_id() )
118 * update the rtc when needed
120 if ( xtime
.tv_sec
> last_rtc_update
+ 660 )
122 if (set_rtc_time(xtime
.tv_sec
) == 0)
123 last_rtc_update
= xtime
.tv_sec
;
125 last_rtc_update
= xtime
.tv_sec
- 600; /* do it again in 60 s */
130 smp_local_timer_interrupt(regs
);
134 extern void apus_heartbeat (void);
139 /* restore the HID0 in case dcache was off - see idle.c
140 * this hack should leave for a better solution -- Cort */
141 lock_dcache(dcache_locked
);
145 /* A place holder for time base interrupts, if they are ever enabled.
147 void timebase_interrupt(int irq
, void * dev
, struct pt_regs
* regs
)
149 printk("timebase_interrupt()\n");
152 /* The RTC on the MPC8xx is an internal register.
153 * We want to protect this during power down, so we need to unlock,
154 * modify, and re-lock.
157 mbx_set_rtc_time(unsigned long time
)
159 ((immap_t
*)IMAP_ADDR
)->im_sitk
.sitk_rtck
= KAPWR_KEY
;
160 ((immap_t
*)IMAP_ADDR
)->im_sit
.sit_rtc
= time
;
161 ((immap_t
*)IMAP_ADDR
)->im_sitk
.sitk_rtck
= ~KAPWR_KEY
;
164 #endif /* CONFIG_MBX */
167 * This version of gettimeofday has microsecond resolution.
169 void do_gettimeofday(struct timeval
*tv
)
176 /* XXX we don't seem to have the decrementers synced properly yet */
178 tv
->tv_usec
+= (decrementer_count
- get_dec())
179 * count_period_num
/ count_period_den
;
180 if (tv
->tv_usec
>= 1000000) {
181 tv
->tv_usec
-= 1000000;
185 restore_flags(flags
);
188 void do_settimeofday(struct timeval
*tv
)
193 last_rtc_update
= 0; /* so the rtc gets updated soon */
195 frac_tick
= tv
->tv_usec
% (1000000 / HZ
);
198 xtime
.tv_sec
= tv
->tv_sec
;
199 xtime
.tv_usec
= tv
->tv_usec
- frac_tick
;
200 set_dec(frac_tick
* count_period_den
/ count_period_num
);
201 time_adjust
= 0; /* stop active adjtime() */
202 time_status
|= STA_UNSYNC
;
203 time_state
= TIME_ERROR
; /* p. 24, (a) */
204 time_maxerror
= NTP_PHASE_LIMIT
;
205 time_esterror
= NTP_PHASE_LIMIT
;
206 restore_flags(flags
);
210 __initfunc(void time_init(void))
213 if ((_get_PVR() >> 16) == 1) {
214 /* 601 processor: dec counts down by 128 every 128ns */
215 decrementer_count
= DECREMENTER_COUNT_601
;
216 count_period_num
= COUNT_PERIOD_NUM_601
;
217 count_period_den
= COUNT_PERIOD_DEN_601
;
222 xtime
.tv_sec
= pmac_get_rtc_time();
223 if ( (_get_PVR() >> 16) != 1 && (!smp_processor_id()) )
224 pmac_calibrate_decr();
225 if ( !smp_processor_id() )
226 set_rtc_time
= pmac_set_rtc_time
;
230 xtime
.tv_sec
= chrp_get_rtc_time();
231 if ((_get_PVR() >> 16) != 1)
232 chrp_calibrate_decr();
233 set_rtc_time
= chrp_set_rtc_time
;
236 xtime
.tv_sec
= prep_get_rtc_time();
237 prep_calibrate_decr();
238 set_rtc_time
= prep_set_rtc_time
;
243 xtime
.tv_sec
= apus_get_rtc_time();
244 apus_calibrate_decr();
245 set_rtc_time
= apus_set_rtc_time
;
251 #else /* CONFIG_MBX */
252 mbx_calibrate_decr();
253 set_rtc_time
= mbx_set_rtc_time
;
255 /* First, unlock all of the registers we are going to modify.
256 * To protect them from corruption during power down, registers
257 * that are maintained by keep alive power are "locked". To
258 * modify these registers we have to write the key value to
259 * the key location associated with the register.
261 ((immap_t
*)IMAP_ADDR
)->im_sitk
.sitk_tbscrk
= KAPWR_KEY
;
262 ((immap_t
*)IMAP_ADDR
)->im_sitk
.sitk_rtcsck
= KAPWR_KEY
;
265 /* Disable the RTC one second and alarm interrupts.
267 ((immap_t
*)IMAP_ADDR
)->im_sit
.sit_rtcsc
&=
268 ~(RTCSC_SIE
| RTCSC_ALE
);
270 /* Enabling the decrementer also enables the timebase interrupts
271 * (or from the other point of view, to get decrementer interrupts
272 * we have to enable the timebase). The decrementer interrupt
273 * is wired into the vector table, nothing to do here for that.
275 ((immap_t
*)IMAP_ADDR
)->im_sit
.sit_tbscr
=
276 ((mk_int_int_mask(DEC_INTERRUPT
) << 8) |
277 (TBSCR_TBF
| TBSCR_TBE
));
278 if (request_irq(DEC_INTERRUPT
, timebase_interrupt
, 0, "tbint", NULL
) != 0)
279 panic("Could not allocate timer IRQ!");
281 /* Get time from the RTC.
283 xtime
.tv_sec
= ((immap_t
*)IMAP_ADDR
)->im_sit
.sit_rtc
;
286 #endif /* CONFIG_MBX */
287 set_dec(decrementer_count
);
288 /* mark the rtc/on-chip timer as in sync
289 * so we don't update right away
291 last_rtc_update
= xtime
.tv_sec
;
296 * Uses the on-board timer to calibrate the on-chip decrementer register
297 * for prep systems. On the pmac the OF tells us what the frequency is
298 * but on prep we have to figure it out.
301 int calibrate_done
= 0;
302 volatile int *done_ptr
= &calibrate_done
;
303 __initfunc(void prep_calibrate_decr(void))
307 /* the Powerstack II's have trouble with the timer so
308 * we use a default value -- Cort
310 if ( (_prep_type
== _PREP_Motorola
) &&
311 ((inb(0x800) & 0xF0) & 0x40) )
313 unsigned long freq
, divisor
;
314 static unsigned long t2
= 0;
317 freq
= t2
* 60; /* try to make freq/1e6 an integer */
319 printk("time_init: decrementer frequency = %lu/%lu (%luMHz)\n",
320 freq
, divisor
,t2
>>20);
321 decrementer_count
= freq
/ HZ
/ divisor
;
322 count_period_num
= divisor
;
323 count_period_den
= freq
/ 1000000;
330 #define TIMER0_COUNT 0x40
331 #define TIMER_CONTROL 0x43
332 /* set timer to periodic mode */
333 outb_p(0x34,TIMER_CONTROL
);/* binary, mode 2, LSB/MSB, ch 0 */
334 /* set the clock to ~100 Hz */
335 outb_p(LATCH
& 0xff , TIMER0_COUNT
); /* LSB */
336 outb(LATCH
>> 8 , TIMER0_COUNT
); /* MSB */
338 if (request_irq(0, prep_calibrate_decr_handler
, 0, "timer", NULL
) != 0)
339 panic("Could not allocate timer IRQ!");
341 while ( ! *done_ptr
) /* nothing */; /* wait for calibrate */
342 restore_flags(flags
);
346 __initfunc(void prep_calibrate_decr_handler(int irq
, void *dev
, struct pt_regs
* regs
))
348 unsigned long freq
, divisor
;
349 static unsigned long t1
= 0, t2
= 0;
356 t2
= t1
-t2
; /* decr's in 1/HZ */
357 t2
= t2
*HZ
; /* # decrs in 1s - thus in Hz */
358 freq
= t2
* 60; /* try to make freq/1e6 an integer */
360 printk("time_init: decrementer frequency = %lu/%lu (%luMHz)\n",
361 freq
, divisor
,t2
>>20);
362 decrementer_count
= freq
/ HZ
/ divisor
;
363 count_period_num
= divisor
;
364 count_period_den
= freq
/ 1000000;
369 #else /* CONFIG_MBX */
371 /* The decrementer counts at the system (internal) clock frequency divided by
372 * sixteen, or external oscillator divided by four. Currently, we only
373 * support the MBX, which is system clock divided by sixteen.
375 __initfunc(void mbx_calibrate_decr(void))
377 bd_t
*binfo
= (bd_t
*)res
;
378 int freq
, fp
, divisor
;
380 if ((((immap_t
*)IMAP_ADDR
)->im_clkrst
.car_sccr
& 0x02000000) == 0)
381 printk("WARNING: Wrong decrementer source clock.\n");
383 /* The manual says the frequency is in Hz, but it is really
384 * as MHz. The value 'fp' is the number of decrementer ticks
387 fp
= (binfo
->bi_intfreq
* 1000000) / 16;
388 freq
= fp
*60; /* try to make freq/1e6 an integer */
390 printk("time_init: decrementer frequency = %d/%d\n", freq
, divisor
);
391 decrementer_count
= freq
/ HZ
/ divisor
;
392 count_period_num
= divisor
;
393 count_period_den
= freq
/ 1000000;
395 #endif /* CONFIG_MBX */
397 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
398 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
399 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
401 * [For the Julian calendar (which was used in Russia before 1917,
402 * Britain & colonies before 1752, anywhere else before 1582,
403 * and is still in use by some communities) leave out the
404 * -year/100+year/400 terms, and add 10.]
406 * This algorithm was first published by Gauss (I think).
408 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
409 * machines were long is 32-bit! (However, as time_t is signed, we
410 * will already get problems at other places on 2038-01-19 03:14:08)
412 unsigned long mktime(unsigned int year
, unsigned int mon
,
413 unsigned int day
, unsigned int hour
,
414 unsigned int min
, unsigned int sec
)
417 if (0 >= (int) (mon
-= 2)) { /* 1..12 -> 11,12,1..10 */
418 mon
+= 12; /* Puts Feb last since it has leap day */
422 (unsigned long)(year
/4 - year
/100 + year
/400 + 367*mon
/12 + day
) +
424 )*24 + hour
/* now have hours */
425 )*60 + min
/* now have minutes */
426 )*60 + sec
; /* finally seconds */
429 #define TICK_SIZE tick
431 #define STARTOFTIME 1970
432 #define SECDAY 86400L
433 #define SECYR (SECDAY * 365)
434 #define leapyear(year) ((year) % 4 == 0)
435 #define days_in_year(a) (leapyear(a) ? 366 : 365)
436 #define days_in_month(a) (month_days[(a) - 1])
438 static int month_days
[12] = {
439 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
442 void to_tm(int tim
, struct rtc_time
* tm
)
445 register long hms
, day
;
450 /* Hours, minutes, seconds are easy */
451 tm
->tm_hour
= hms
/ 3600;
452 tm
->tm_min
= (hms
% 3600) / 60;
453 tm
->tm_sec
= (hms
% 3600) % 60;
455 /* Number of years in days */
456 for (i
= STARTOFTIME
; day
>= days_in_year(i
); i
++)
457 day
-= days_in_year(i
);
460 /* Number of months in days left */
461 if (leapyear(tm
->tm_year
))
462 days_in_month(FEBRUARY
) = 29;
463 for (i
= 1; day
>= days_in_month(i
); i
++)
464 day
-= days_in_month(i
);
465 days_in_month(FEBRUARY
) = 28;
468 /* Days are what is left over (+1) from all that. */
469 tm
->tm_mday
= day
+ 1;