]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ppc/platforms/chrp_time.c
[PATCH] RTC: Remove RTC UIP synchronization on PPC CHRP (arch/ppc)
[linux-2.6-omap-h63xx.git] / arch / ppc / platforms / chrp_time.c
1 /*
2  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
3  *
4  * Adapted for PowerPC (PReP) by Gary Thomas
5  * Modified by Cort Dougan (cort@cs.nmt.edu).
6  * Copied and modified from arch/i386/kernel/time.c
7  *
8  */
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/param.h>
13 #include <linux/string.h>
14 #include <linux/mm.h>
15 #include <linux/interrupt.h>
16 #include <linux/time.h>
17 #include <linux/timex.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/mc146818rtc.h>
20 #include <linux/init.h>
21 #include <linux/bcd.h>
22
23 #include <asm/io.h>
24 #include <asm/nvram.h>
25 #include <asm/prom.h>
26 #include <asm/sections.h>
27 #include <asm/time.h>
28
29 extern spinlock_t rtc_lock;
30
31 static int nvram_as1 = NVRAM_AS1;
32 static int nvram_as0 = NVRAM_AS0;
33 static int nvram_data = NVRAM_DATA;
34
35 long __init chrp_time_init(void)
36 {
37         struct device_node *rtcs;
38         int base;
39
40         rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
41         if (rtcs == NULL)
42                 rtcs = find_compatible_devices("rtc", "ds1385-rtc");
43         if (rtcs == NULL || rtcs->addrs == NULL)
44                 return 0;
45         base = rtcs->addrs[0].address;
46         nvram_as1 = 0;
47         nvram_as0 = base;
48         nvram_data = base + 1;
49
50         return 0;
51 }
52
53 int chrp_cmos_clock_read(int addr)
54 {
55         if (nvram_as1 != 0)
56                 outb(addr>>8, nvram_as1);
57         outb(addr, nvram_as0);
58         return (inb(nvram_data));
59 }
60
61 void chrp_cmos_clock_write(unsigned long val, int addr)
62 {
63         if (nvram_as1 != 0)
64                 outb(addr>>8, nvram_as1);
65         outb(addr, nvram_as0);
66         outb(val, nvram_data);
67         return;
68 }
69
70 /*
71  * Set the hardware clock. -- Cort
72  */
73 int chrp_set_rtc_time(unsigned long nowtime)
74 {
75         unsigned char save_control, save_freq_select;
76         struct rtc_time tm;
77
78         spin_lock(&rtc_lock);
79         to_tm(nowtime, &tm);
80
81         save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
82
83         chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
84
85         save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
86
87         chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
88
89         tm.tm_year -= 1900;
90         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
91                 BIN_TO_BCD(tm.tm_sec);
92                 BIN_TO_BCD(tm.tm_min);
93                 BIN_TO_BCD(tm.tm_hour);
94                 BIN_TO_BCD(tm.tm_mon);
95                 BIN_TO_BCD(tm.tm_mday);
96                 BIN_TO_BCD(tm.tm_year);
97         }
98         chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
99         chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
100         chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
101         chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
102         chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
103         chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
104
105         /* The following flags have to be released exactly in this order,
106          * otherwise the DS12887 (popular MC146818A clone with integrated
107          * battery and quartz) will not reset the oscillator and will not
108          * update precisely 500 ms later. You won't find this mentioned in
109          * the Dallas Semiconductor data sheets, but who believes data
110          * sheets anyway ...                           -- Markus Kuhn
111          */
112         chrp_cmos_clock_write(save_control, RTC_CONTROL);
113         chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
114
115         spin_unlock(&rtc_lock);
116         return 0;
117 }
118
119 unsigned long chrp_get_rtc_time(void)
120 {
121         unsigned int year, mon, day, hour, min, sec;
122
123         do {
124                 sec = chrp_cmos_clock_read(RTC_SECONDS);
125                 min = chrp_cmos_clock_read(RTC_MINUTES);
126                 hour = chrp_cmos_clock_read(RTC_HOURS);
127                 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
128                 mon = chrp_cmos_clock_read(RTC_MONTH);
129                 year = chrp_cmos_clock_read(RTC_YEAR);
130         } while (sec != chrp_cmos_clock_read(RTC_SECONDS));
131
132         if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
133           {
134             BCD_TO_BIN(sec);
135             BCD_TO_BIN(min);
136             BCD_TO_BIN(hour);
137             BCD_TO_BIN(day);
138             BCD_TO_BIN(mon);
139             BCD_TO_BIN(year);
140           }
141         if ((year += 1900) < 1970)
142                 year += 100;
143         return mktime(year, mon, day, hour, min, sec);
144 }
145
146 /*
147  * Calibrate the decrementer frequency with the VIA timer 1.
148  */
149 #define VIA_TIMER_FREQ_6        4700000 /* time 1 frequency * 6 */
150
151 /* VIA registers */
152 #define RS              0x200           /* skip between registers */
153 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
154 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
155 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
156 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
157 #define ACR             (11*RS)         /* Auxiliary control register */
158 #define IFR             (13*RS)         /* Interrupt flag register */
159
160 /* Bits in ACR */
161 #define T1MODE          0xc0            /* Timer 1 mode */
162 #define T1MODE_CONT     0x40            /*  continuous interrupts */
163
164 /* Bits in IFR and IER */
165 #define T1_INT          0x40            /* Timer 1 interrupt */
166
167 static int __init chrp_via_calibrate_decr(void)
168 {
169         struct device_node *vias;
170         volatile unsigned char __iomem *via;
171         int count = VIA_TIMER_FREQ_6 / 100;
172         unsigned int dstart, dend;
173
174         vias = find_devices("via-cuda");
175         if (vias == 0)
176                 vias = find_devices("via");
177         if (vias == 0 || vias->n_addrs == 0)
178                 return 0;
179         via = ioremap(vias->addrs[0].address, vias->addrs[0].size);
180
181         /* set timer 1 for continuous interrupts */
182         out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
183         /* set the counter to a small value */
184         out_8(&via[T1CH], 2);
185         /* set the latch to `count' */
186         out_8(&via[T1LL], count);
187         out_8(&via[T1LH], count >> 8);
188         /* wait until it hits 0 */
189         while ((in_8(&via[IFR]) & T1_INT) == 0)
190                 ;
191         dstart = get_dec();
192         /* clear the interrupt & wait until it hits 0 again */
193         in_8(&via[T1CL]);
194         while ((in_8(&via[IFR]) & T1_INT) == 0)
195                 ;
196         dend = get_dec();
197
198         tb_ticks_per_jiffy = (dstart - dend) / ((6 * HZ)/100);
199         tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
200
201         printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
202                tb_ticks_per_jiffy, dstart - dend);
203
204         iounmap(via);
205         
206         return 1;
207 }
208
209 void __init chrp_calibrate_decr(void)
210 {
211         struct device_node *cpu;
212         unsigned int freq, *fp;
213
214         if (chrp_via_calibrate_decr())
215                 return;
216
217         /*
218          * The cpu node should have a timebase-frequency property
219          * to tell us the rate at which the decrementer counts.
220          */
221         freq = 16666000;                /* hardcoded default */
222         cpu = find_type_devices("cpu");
223         if (cpu != 0) {
224                 fp = (unsigned int *)
225                         get_property(cpu, "timebase-frequency", NULL);
226                 if (fp != 0)
227                         freq = *fp;
228         }
229         printk("time_init: decrementer frequency = %u.%.6u MHz\n",
230                freq/1000000, freq%1000000);
231         tb_ticks_per_jiffy = freq / HZ;
232         tb_to_us = mulhwu_scale_factor(freq, 1000000);
233 }