more changes on original files
[linux-2.4.git] / arch / ppc / platforms / pmac_time.c
1 /*
2  * Support for periodic interrupts (100 per second) and for getting
3  * the current time from the RTC on Power Macintoshes.
4  *
5  * We use the decrementer register for our periodic interrupts.
6  *
7  * Paul Mackerras       August 1996.
8  * Copyright (C) 1996 Paul Mackerras.
9  */
10 #include <linux/config.h>
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/init.h>
18 #include <linux/adb.h>
19 #include <linux/cuda.h>
20 #include <linux/pmu.h>
21
22 #include <asm/sections.h>
23 #include <asm/prom.h>
24 #include <asm/system.h>
25 #include <asm/io.h>
26 #include <asm/pgtable.h>
27 #include <asm/machdep.h>
28 #include <asm/hardirq.h>
29 #include <asm/time.h>
30 #include <asm/nvram.h>
31
32 extern rwlock_t xtime_lock;
33
34 /* Apparently the RTC stores seconds since 1 Jan 1904 */
35 #define RTC_OFFSET      2082844800
36
37 /*
38  * Calibrate the decrementer frequency with the VIA timer 1.
39  */
40 #define VIA_TIMER_FREQ_6        4700000 /* time 1 frequency * 6 */
41
42 /* VIA registers */
43 #define RS              0x200           /* skip between registers */
44 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
45 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
46 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
47 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
48 #define ACR             (11*RS)         /* Auxiliary control register */
49 #define IFR             (13*RS)         /* Interrupt flag register */
50
51 /* Bits in ACR */
52 #define T1MODE          0xc0            /* Timer 1 mode */
53 #define T1MODE_CONT     0x40            /*  continuous interrupts */
54
55 /* Bits in IFR and IER */
56 #define T1_INT          0x40            /* Timer 1 interrupt */
57
58 extern struct timezone sys_tz;
59
60 long __init
61 pmac_time_init(void)
62 {
63 #ifdef CONFIG_NVRAM
64         s32 delta = 0;
65         int dst;
66
67         delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
68         delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
69         delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
70         if (delta & 0x00800000UL)
71                 delta |= 0xFF000000UL;
72         dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
73         printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
74                 dst ? "on" : "off");
75         return delta;
76 #else
77         return 0;
78 #endif
79 }
80
81 unsigned long __pmac
82 pmac_get_rtc_time(void)
83 {
84 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
85         struct adb_request req;
86         unsigned long now;
87 #endif
88
89         /* Get the time from the RTC */
90         switch (sys_ctrler) {
91 #ifdef CONFIG_ADB_CUDA
92         case SYS_CTRLER_CUDA:
93                 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
94                         return 0;
95                 while (!req.complete)
96                         cuda_poll();
97                 if (req.reply_len != 7)
98                         printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
99                                req.reply_len);
100                 now = (req.reply[3] << 24) + (req.reply[4] << 16)
101                         + (req.reply[5] << 8) + req.reply[6];
102                 return now - RTC_OFFSET;
103 #endif /* CONFIG_ADB_CUDA */
104 #ifdef CONFIG_ADB_PMU
105         case SYS_CTRLER_PMU:
106                 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
107                         return 0;
108                 while (!req.complete)
109                         pmu_poll();
110                 if (req.reply_len != 4)
111                         printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
112                                req.reply_len);
113                 now = (req.reply[0] << 24) + (req.reply[1] << 16)
114                         + (req.reply[2] << 8) + req.reply[3];
115                 return now - RTC_OFFSET;
116 #endif /* CONFIG_ADB_PMU */
117         default: ;
118         }
119         return 0;
120 }
121
122 int __pmac
123 pmac_set_rtc_time(unsigned long nowtime)
124 {
125 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
126         struct adb_request req;
127 #endif
128
129         nowtime += RTC_OFFSET;
130
131         switch (sys_ctrler) {
132 #ifdef CONFIG_ADB_CUDA
133         case SYS_CTRLER_CUDA:
134                 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
135                                  nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
136                         return 0;
137                 while (!req.complete)
138                         cuda_poll();
139                 if ((req.reply_len != 3) && (req.reply_len != 7))
140                         printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
141                                req.reply_len);
142                 return 1;
143 #endif /* CONFIG_ADB_CUDA */
144 #ifdef CONFIG_ADB_PMU
145         case SYS_CTRLER_PMU:
146                 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
147                                 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
148                         return 0;
149                 while (!req.complete)
150                         pmu_poll();
151                 if (req.reply_len != 0)
152                         printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
153                                req.reply_len);
154                 return 1;
155 #endif /* CONFIG_ADB_PMU */
156         default:
157                 return 0;
158         }
159 }
160
161 /*
162  * Calibrate the decrementer register using VIA timer 1.
163  * This is used both on powermacs and CHRP machines.
164  */
165 int __init
166 via_calibrate_decr(void)
167 {
168         struct device_node *vias;
169         volatile unsigned char *via;
170         int count = VIA_TIMER_FREQ_6 / HZ;
171         unsigned int dstart, dend;
172
173         vias = find_devices("via-cuda");
174         if (vias == 0)
175                 vias = find_devices("via-pmu");
176         if (vias == 0)
177                 vias = find_devices("via");
178         if (vias == 0 || vias->n_addrs == 0)
179                 return 0;
180         via = (volatile unsigned char *)
181                 ioremap(vias->addrs[0].address, vias->addrs[0].size);
182
183         /* set timer 1 for continuous interrupts */
184         out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
185         /* set the counter to a small value */
186         out_8(&via[T1CH], 2);
187         /* set the latch to `count' */
188         out_8(&via[T1LL], count);
189         out_8(&via[T1LH], count >> 8);
190         /* wait until it hits 0 */
191         while ((in_8(&via[IFR]) & T1_INT) == 0)
192                 ;
193         dstart = get_dec();
194         /* clear the interrupt & wait until it hits 0 again */
195         in_8(&via[T1CL]);
196         while ((in_8(&via[IFR]) & T1_INT) == 0)
197                 ;
198         dend = get_dec();
199
200         tb_ticks_per_jiffy = (dstart - dend) / 6;
201         tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
202
203         printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
204                tb_ticks_per_jiffy, dstart - dend);
205
206         iounmap((void *)via);
207
208         return 1;
209 }
210
211 #ifdef CONFIG_PMAC_PBOOK
212 /*
213  * Reset the time after a sleep.
214  */
215 static int __pmac
216 time_sleep_notify(struct pmu_sleep_notifier *self, int when)
217 {
218         static unsigned long time_diff;
219         unsigned long flags;
220
221         switch (when) {
222         case PBOOK_SLEEP_NOW:
223                 read_lock_irqsave(&xtime_lock, flags);
224                 time_diff = xtime.tv_sec - pmac_get_rtc_time();
225                 read_unlock_irqrestore(&xtime_lock, flags);
226                 break;
227         case PBOOK_WAKE:
228                 write_lock_irqsave(&xtime_lock, flags);
229                 xtime.tv_sec = pmac_get_rtc_time() + time_diff;
230                 xtime.tv_usec = 0;
231                 last_rtc_update = xtime.tv_sec;
232                 write_unlock_irqrestore(&xtime_lock, flags);
233                 break;
234         }
235         return PBOOK_SLEEP_OK;
236 }
237
238 static struct pmu_sleep_notifier time_sleep_notifier __pmacdata = {
239         time_sleep_notify, SLEEP_LEVEL_MISC,
240 };
241 #endif /* CONFIG_PMAC_PBOOK */
242
243 /*
244  * Query the OF and get the decr frequency.
245  * This was taken from the pmac time_init() when merging the prep/pmac
246  * time functions.
247  */
248 void __init
249 pmac_calibrate_decr(void)
250 {
251         struct device_node *cpu;
252         unsigned int freq, *fp;
253
254 #ifdef CONFIG_PMAC_PBOOK
255         pmu_register_sleep_notifier(&time_sleep_notifier);
256 #endif /* CONFIG_PMAC_PBOOK */
257
258         /* We assume MacRISC2 machines have correct device-tree
259          * calibration. That's better since the VIA itself seems
260          * to be slightly off. --BenH
261          */
262         if (!machine_is_compatible("MacRISC2"))
263                 if (via_calibrate_decr())
264                         return;
265
266         /*
267          * The cpu node should have a timebase-frequency property
268          * to tell us the rate at which the decrementer counts.
269          */
270         cpu = find_type_devices("cpu");
271         if (cpu == 0)
272                 panic("can't find cpu node in time_init");
273         fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
274         if (fp == 0)
275                 panic("can't get cpu timebase frequency");
276         freq = *fp;
277         printk("time_init: decrementer frequency = %u.%.6u MHz\n",
278                freq/1000000, freq%1000000);
279         tb_ticks_per_jiffy = freq / HZ;
280         tb_to_us = mulhwu_scale_factor(freq, 1000000);
281 }