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