2 * linux/arch/i386/kernel/time.c
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 * Adapted for PowerPC (PreP) by Gary Thomas
7 * Modified by Cort Dougan (cort@cs.nmt.edu)
8 * copied and modified from intel version
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/param.h>
15 #include <linux/string.h>
17 #include <linux/interrupt.h>
18 #include <linux/timex.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/init.h>
23 #include <asm/segment.h>
25 #include <asm/processor.h>
26 #include <asm/nvram.h>
30 static int nvram_as1
= NVRAM_AS1
;
31 static int nvram_as0
= NVRAM_AS0
;
32 static int nvram_data
= NVRAM_DATA
;
34 void __init
chrp_time_init(void)
36 struct device_node
*rtcs
;
39 rtcs
= find_compatible_devices("rtc", "pnpPNP,b00");
40 if (rtcs
== NULL
|| rtcs
->addrs
== NULL
)
42 base
= rtcs
->addrs
[0].address
;
45 nvram_data
= base
+ 1;
48 int chrp_cmos_clock_read(int addr
)
51 outb(addr
>>8, nvram_as1
);
52 outb(addr
, nvram_as0
);
53 return (inb(nvram_data
));
56 void chrp_cmos_clock_write(unsigned long val
, int addr
)
59 outb(addr
>>8, nvram_as1
);
60 outb(addr
, nvram_as0
);
61 outb(val
, nvram_data
);
66 * Set the hardware clock. -- Cort
68 int chrp_set_rtc_time(unsigned long nowtime
)
70 unsigned char save_control
, save_freq_select
;
75 save_control
= chrp_cmos_clock_read(RTC_CONTROL
); /* tell the clock it's being set */
77 chrp_cmos_clock_write((save_control
|RTC_SET
), RTC_CONTROL
);
79 save_freq_select
= chrp_cmos_clock_read(RTC_FREQ_SELECT
); /* stop and reset prescaler */
81 chrp_cmos_clock_write((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
84 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
85 BIN_TO_BCD(tm
.tm_sec
);
86 BIN_TO_BCD(tm
.tm_min
);
87 BIN_TO_BCD(tm
.tm_hour
);
88 BIN_TO_BCD(tm
.tm_mon
);
89 BIN_TO_BCD(tm
.tm_mday
);
90 BIN_TO_BCD(tm
.tm_year
);
92 chrp_cmos_clock_write(tm
.tm_sec
,RTC_SECONDS
);
93 chrp_cmos_clock_write(tm
.tm_min
,RTC_MINUTES
);
94 chrp_cmos_clock_write(tm
.tm_hour
,RTC_HOURS
);
95 chrp_cmos_clock_write(tm
.tm_mon
,RTC_MONTH
);
96 chrp_cmos_clock_write(tm
.tm_mday
,RTC_DAY_OF_MONTH
);
97 chrp_cmos_clock_write(tm
.tm_year
,RTC_YEAR
);
99 /* The following flags have to be released exactly in this order,
100 * otherwise the DS12887 (popular MC146818A clone with integrated
101 * battery and quartz) will not reset the oscillator and will not
102 * update precisely 500 ms later. You won't find this mentioned in
103 * the Dallas Semiconductor data sheets, but who believes data
104 * sheets anyway ... -- Markus Kuhn
106 chrp_cmos_clock_write(save_control
, RTC_CONTROL
);
107 chrp_cmos_clock_write(save_freq_select
, RTC_FREQ_SELECT
);
109 if ( (time_state
== TIME_ERROR
) || (time_state
== TIME_BAD
) )
110 time_state
= TIME_OK
;
114 unsigned long chrp_get_rtc_time(void)
116 unsigned int year
, mon
, day
, hour
, min
, sec
;
119 /* The Linux interpretation of the CMOS clock register contents:
120 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
121 * RTC registers show the second which has precisely just started.
122 * Let's hope other operating systems interpret the RTC the same way.
124 /* read RTC exactly on falling edge of update flag */
125 for (i
= 0 ; i
< 1000000 ; i
++) /* may take up to 1 second... */
126 if (chrp_cmos_clock_read(RTC_FREQ_SELECT
) & RTC_UIP
)
128 for (i
= 0 ; i
< 1000000 ; i
++) /* must try at least 2.228 ms */
129 if (!(chrp_cmos_clock_read(RTC_FREQ_SELECT
) & RTC_UIP
))
131 do { /* Isn't this overkill ? UIP above should guarantee consistency */
132 sec
= chrp_cmos_clock_read(RTC_SECONDS
);
133 min
= chrp_cmos_clock_read(RTC_MINUTES
);
134 hour
= chrp_cmos_clock_read(RTC_HOURS
);
135 day
= chrp_cmos_clock_read(RTC_DAY_OF_MONTH
);
136 mon
= chrp_cmos_clock_read(RTC_MONTH
);
137 year
= chrp_cmos_clock_read(RTC_YEAR
);
138 } while (sec
!= chrp_cmos_clock_read(RTC_SECONDS
));
139 if (!(chrp_cmos_clock_read(RTC_CONTROL
) & RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
148 if ((year
+= 1900) < 1970)
150 return mktime(year
, mon
, day
, hour
, min
, sec
);
154 void __init
chrp_calibrate_decr(void)
156 struct device_node
*cpu
;
160 if (via_calibrate_decr())
164 * The cpu node should have a timebase-frequency property
165 * to tell us the rate at which the decrementer counts.
167 freq
= 16666000; /* hardcoded default */
168 cpu
= find_type_devices("cpu");
170 fp
= (int *) get_property(cpu
, "timebase-frequency", NULL
);
174 freq
*= 60; /* try to make freq/1e6 an integer */
176 printk("time_init: decrementer frequency = %lu/%d\n", freq
, divisor
);
177 decrementer_count
= freq
/ HZ
/ divisor
;
178 count_period_num
= divisor
;
179 count_period_den
= freq
/ 1000000;