Import changeset
[linux-flexiantxendom0-3.2.10.git] / arch / ppc / kernel / chrp_time.c
1 /*
2  *  linux/arch/i386/kernel/time.c
3  *
4  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
5  *
6  * Adapted for PowerPC (PreP) by Gary Thomas
7  * Modified by Cort Dougan (cort@cs.nmt.edu)
8  *  copied and modified from intel version
9  *
10  */
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>
16 #include <linux/mm.h>
17 #include <linux/interrupt.h>
18 #include <linux/time.h>
19 #include <linux/timex.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/mc146818rtc.h>
22 #include <linux/init.h>
23
24 #include <asm/segment.h>
25 #include <asm/io.h>
26 #include <asm/processor.h>
27 #include <asm/nvram.h>
28 #include <asm/prom.h>
29 #include <asm/init.h>
30 #include <asm/time.h>
31
32 static int nvram_as1 = NVRAM_AS1;
33 static int nvram_as0 = NVRAM_AS0;
34 static int nvram_data = NVRAM_DATA;
35
36 long __init chrp_time_init(void)
37 {
38         struct device_node *rtcs;
39         int base;
40
41         rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
42         if (rtcs == NULL || rtcs->addrs == NULL)
43                 return 0;
44         base = rtcs->addrs[0].address;
45         nvram_as1 = 0;
46         nvram_as0 = base;
47         nvram_data = base + 1;
48         
49         return 0;
50 }
51
52 int __chrp chrp_cmos_clock_read(int addr)
53 {
54         if (nvram_as1 != 0)
55                 outb(addr>>8, nvram_as1);
56         outb(addr, nvram_as0);
57         return (inb(nvram_data));
58 }
59
60 void __chrp chrp_cmos_clock_write(unsigned long val, int addr)
61 {
62         if (nvram_as1 != 0)
63                 outb(addr>>8, nvram_as1);
64         outb(addr, nvram_as0);
65         outb(val, nvram_data);
66         return;
67 }
68
69 /*
70  * Set the hardware clock. -- Cort
71  */
72 int __chrp chrp_set_rtc_time(unsigned long nowtime)
73 {
74         unsigned char save_control, save_freq_select;
75         struct rtc_time tm;
76
77         to_tm(nowtime, &tm);
78
79         save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
80
81         chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
82
83         save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
84         
85         chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
86
87         tm.tm_year -= 1900;
88         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
89                 BIN_TO_BCD(tm.tm_sec);
90                 BIN_TO_BCD(tm.tm_min);
91                 BIN_TO_BCD(tm.tm_hour);
92                 BIN_TO_BCD(tm.tm_mon);
93                 BIN_TO_BCD(tm.tm_mday);
94                 BIN_TO_BCD(tm.tm_year);
95         }
96         chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
97         chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
98         chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
99         chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
100         chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
101         chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
102         
103         /* The following flags have to be released exactly in this order,
104          * otherwise the DS12887 (popular MC146818A clone with integrated
105          * battery and quartz) will not reset the oscillator and will not
106          * update precisely 500 ms later. You won't find this mentioned in
107          * the Dallas Semiconductor data sheets, but who believes data
108          * sheets anyway ...                           -- Markus Kuhn
109          */
110         chrp_cmos_clock_write(save_control, RTC_CONTROL);
111         chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
112
113         if ( (time_state == TIME_ERROR) || (time_state == TIME_BAD) )
114                 time_state = TIME_OK;
115         return 0;
116 }
117
118 unsigned long __chrp chrp_get_rtc_time(void)
119 {
120         unsigned int year, mon, day, hour, min, sec;
121         int uip, i;
122
123         /* The Linux interpretation of the CMOS clock register contents:
124          * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
125          * RTC registers show the second which has precisely just started.
126          * Let's hope other operating systems interpret the RTC the same way.
127          */
128
129         /* Since the UIP flag is set for about 2.2 ms and the clock
130          * is typically written with a precision of 1 jiffy, trying
131          * to obtain a precision better than a few milliseconds is 
132          * an illusion. Only consistency is interesting, this also
133          * allows to use the routine for /dev/rtc without a potential
134          * 1 second kernel busy loop triggered by any reader of /dev/rtc. 
135          */
136
137         for ( i = 0; i<1000000; i++) {
138                 uip = chrp_cmos_clock_read(RTC_FREQ_SELECT);
139                 sec = chrp_cmos_clock_read(RTC_SECONDS);
140                 min = chrp_cmos_clock_read(RTC_MINUTES);
141                 hour = chrp_cmos_clock_read(RTC_HOURS);
142                 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
143                 mon = chrp_cmos_clock_read(RTC_MONTH);
144                 year = chrp_cmos_clock_read(RTC_YEAR);
145                 uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT);
146                 if ((uip & RTC_UIP)==0) break;
147         }
148
149         if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
150           {
151             BCD_TO_BIN(sec);
152             BCD_TO_BIN(min);
153             BCD_TO_BIN(hour);
154             BCD_TO_BIN(day);
155             BCD_TO_BIN(mon);
156             BCD_TO_BIN(year);
157           }
158         if ((year += 1900) < 1970)
159                 year += 100;
160         return mktime(year, mon, day, hour, min, sec);
161 }
162
163
164 void __init chrp_calibrate_decr(void)
165 {
166         struct device_node *cpu;
167         unsigned int freq, *fp;
168
169         if (via_calibrate_decr())
170                 return;
171
172         /*
173          * The cpu node should have a timebase-frequency property
174          * to tell us the rate at which the decrementer counts.
175          */
176         freq = 16666000;                /* hardcoded default */
177         cpu = find_type_devices("cpu");
178         if (cpu != 0) {
179                 fp = (unsigned int *)
180                         get_property(cpu, "timebase-frequency", NULL);
181                 if (fp != 0)
182                         freq = *fp;
183         }
184         printk("time_init: decrementer frequency = %u.%.6u MHz\n",
185                freq/1000000, freq%1000000);
186         tb_ticks_per_jiffy = freq / HZ;
187         tb_to_us = mulhwu_scale_factor(freq, 1000000);
188 }