Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[sfrench/cifs-2.6.git] / arch / ppc / platforms / chrp_time.c
1 /*
2  *  arch/ppc/platforms/chrp_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 arch/i386/kernel/time.c
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 #include <linux/bcd.h>
24
25 #include <asm/io.h>
26 #include <asm/nvram.h>
27 #include <asm/prom.h>
28 #include <asm/sections.h>
29 #include <asm/time.h>
30
31 extern spinlock_t rtc_lock;
32
33 static int nvram_as1 = NVRAM_AS1;
34 static int nvram_as0 = NVRAM_AS0;
35 static int nvram_data = NVRAM_DATA;
36
37 long __init chrp_time_init(void)
38 {
39         struct device_node *rtcs;
40         int base;
41
42         rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
43         if (rtcs == NULL)
44                 rtcs = find_compatible_devices("rtc", "ds1385-rtc");
45         if (rtcs == NULL || rtcs->addrs == NULL)
46                 return 0;
47         base = rtcs->addrs[0].address;
48         nvram_as1 = 0;
49         nvram_as0 = base;
50         nvram_data = base + 1;
51
52         return 0;
53 }
54
55 int chrp_cmos_clock_read(int addr)
56 {
57         if (nvram_as1 != 0)
58                 outb(addr>>8, nvram_as1);
59         outb(addr, nvram_as0);
60         return (inb(nvram_data));
61 }
62
63 void chrp_cmos_clock_write(unsigned long val, int addr)
64 {
65         if (nvram_as1 != 0)
66                 outb(addr>>8, nvram_as1);
67         outb(addr, nvram_as0);
68         outb(val, nvram_data);
69         return;
70 }
71
72 /*
73  * Set the hardware clock. -- Cort
74  */
75 int chrp_set_rtc_time(unsigned long nowtime)
76 {
77         unsigned char save_control, save_freq_select;
78         struct rtc_time tm;
79
80         spin_lock(&rtc_lock);
81         to_tm(nowtime, &tm);
82
83         save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
84
85         chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
86
87         save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
88
89         chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
90
91         tm.tm_year -= 1900;
92         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
93                 BIN_TO_BCD(tm.tm_sec);
94                 BIN_TO_BCD(tm.tm_min);
95                 BIN_TO_BCD(tm.tm_hour);
96                 BIN_TO_BCD(tm.tm_mon);
97                 BIN_TO_BCD(tm.tm_mday);
98                 BIN_TO_BCD(tm.tm_year);
99         }
100         chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
101         chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
102         chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
103         chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
104         chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
105         chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
106
107         /* The following flags have to be released exactly in this order,
108          * otherwise the DS12887 (popular MC146818A clone with integrated
109          * battery and quartz) will not reset the oscillator and will not
110          * update precisely 500 ms later. You won't find this mentioned in
111          * the Dallas Semiconductor data sheets, but who believes data
112          * sheets anyway ...                           -- Markus Kuhn
113          */
114         chrp_cmos_clock_write(save_control, RTC_CONTROL);
115         chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
116
117         spin_unlock(&rtc_lock);
118         return 0;
119 }
120
121 unsigned long chrp_get_rtc_time(void)
122 {
123         unsigned int year, mon, day, hour, min, sec;
124         int uip, i;
125
126         /* The Linux interpretation of the CMOS clock register contents:
127          * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
128          * RTC registers show the second which has precisely just started.
129          * Let's hope other operating systems interpret the RTC the same way.
130          */
131
132         /* Since the UIP flag is set for about 2.2 ms and the clock
133          * is typically written with a precision of 1 jiffy, trying
134          * to obtain a precision better than a few milliseconds is
135          * an illusion. Only consistency is interesting, this also
136          * allows to use the routine for /dev/rtc without a potential
137          * 1 second kernel busy loop triggered by any reader of /dev/rtc.
138          */
139
140         for ( i = 0; i<1000000; i++) {
141                 uip = chrp_cmos_clock_read(RTC_FREQ_SELECT);
142                 sec = chrp_cmos_clock_read(RTC_SECONDS);
143                 min = chrp_cmos_clock_read(RTC_MINUTES);
144                 hour = chrp_cmos_clock_read(RTC_HOURS);
145                 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
146                 mon = chrp_cmos_clock_read(RTC_MONTH);
147                 year = chrp_cmos_clock_read(RTC_YEAR);
148                 uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT);
149                 if ((uip & RTC_UIP)==0) break;
150         }
151
152         if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
153           {
154             BCD_TO_BIN(sec);
155             BCD_TO_BIN(min);
156             BCD_TO_BIN(hour);
157             BCD_TO_BIN(day);
158             BCD_TO_BIN(mon);
159             BCD_TO_BIN(year);
160           }
161         if ((year += 1900) < 1970)
162                 year += 100;
163         return mktime(year, mon, day, hour, min, sec);
164 }
165
166 /*
167  * Calibrate the decrementer frequency with the VIA timer 1.
168  */
169 #define VIA_TIMER_FREQ_6        4700000 /* time 1 frequency * 6 */
170
171 /* VIA registers */
172 #define RS              0x200           /* skip between registers */
173 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
174 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
175 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
176 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
177 #define ACR             (11*RS)         /* Auxiliary control register */
178 #define IFR             (13*RS)         /* Interrupt flag register */
179
180 /* Bits in ACR */
181 #define T1MODE          0xc0            /* Timer 1 mode */
182 #define T1MODE_CONT     0x40            /*  continuous interrupts */
183
184 /* Bits in IFR and IER */
185 #define T1_INT          0x40            /* Timer 1 interrupt */
186
187 static int __init chrp_via_calibrate_decr(void)
188 {
189         struct device_node *vias;
190         volatile unsigned char __iomem *via;
191         int count = VIA_TIMER_FREQ_6 / 100;
192         unsigned int dstart, dend;
193
194         vias = find_devices("via-cuda");
195         if (vias == 0)
196                 vias = find_devices("via");
197         if (vias == 0 || vias->n_addrs == 0)
198                 return 0;
199         via = ioremap(vias->addrs[0].address, vias->addrs[0].size);
200
201         /* set timer 1 for continuous interrupts */
202         out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
203         /* set the counter to a small value */
204         out_8(&via[T1CH], 2);
205         /* set the latch to `count' */
206         out_8(&via[T1LL], count);
207         out_8(&via[T1LH], count >> 8);
208         /* wait until it hits 0 */
209         while ((in_8(&via[IFR]) & T1_INT) == 0)
210                 ;
211         dstart = get_dec();
212         /* clear the interrupt & wait until it hits 0 again */
213         in_8(&via[T1CL]);
214         while ((in_8(&via[IFR]) & T1_INT) == 0)
215                 ;
216         dend = get_dec();
217
218         tb_ticks_per_jiffy = (dstart - dend) / ((6 * HZ)/100);
219         tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
220
221         printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
222                tb_ticks_per_jiffy, dstart - dend);
223
224         iounmap(via);
225         
226         return 1;
227 }
228
229 void __init chrp_calibrate_decr(void)
230 {
231         struct device_node *cpu;
232         unsigned int freq, *fp;
233
234         if (chrp_via_calibrate_decr())
235                 return;
236
237         /*
238          * The cpu node should have a timebase-frequency property
239          * to tell us the rate at which the decrementer counts.
240          */
241         freq = 16666000;                /* hardcoded default */
242         cpu = find_type_devices("cpu");
243         if (cpu != 0) {
244                 fp = (unsigned int *)
245                         get_property(cpu, "timebase-frequency", NULL);
246                 if (fp != 0)
247                         freq = *fp;
248         }
249         printk("time_init: decrementer frequency = %u.%.6u MHz\n",
250                freq/1000000, freq%1000000);
251         tb_ticks_per_jiffy = freq / HZ;
252         tb_to_us = mulhwu_scale_factor(freq, 1000000);
253 }