[PATCH] ppc64 boot: remove global initializers
[linux-2.6/kvm.git] / arch / ppc / platforms / pmac_time.c
blobedb9fcc64790c64526290fc9f50ad6e314cff489
1 /*
2 * Support for periodic interrupts (100 per second) and for getting
3 * the current time from the RTC on Power Macintoshes.
5 * We use the decrementer register for our periodic interrupts.
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>
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>
33 /* Apparently the RTC stores seconds since 1 Jan 1904 */
34 #define RTC_OFFSET 2082844800
37 * Calibrate the decrementer frequency with the VIA timer 1.
39 #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
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 */
50 /* Bits in ACR */
51 #define T1MODE 0xc0 /* Timer 1 mode */
52 #define T1MODE_CONT 0x40 /* continuous interrupts */
54 /* Bits in IFR and IER */
55 #define T1_INT 0x40 /* Timer 1 interrupt */
57 extern struct timezone sys_tz;
59 long __init
60 pmac_time_init(void)
62 #ifdef CONFIG_NVRAM
63 s32 delta = 0;
64 int dst;
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
80 unsigned long
81 pmac_get_rtc_time(void)
83 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
84 struct adb_request req;
85 unsigned long now;
86 #endif
88 /* Get the time from the RTC */
89 switch (sys_ctrler) {
90 #ifdef CONFIG_ADB_CUDA
91 case SYS_CTRLER_CUDA:
92 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
93 return 0;
94 while (!req.complete)
95 cuda_poll();
96 if (req.reply_len != 7)
97 printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
98 req.reply_len);
99 now = (req.reply[3] << 24) + (req.reply[4] << 16)
100 + (req.reply[5] << 8) + req.reply[6];
101 return now - RTC_OFFSET;
102 #endif /* CONFIG_ADB_CUDA */
103 #ifdef CONFIG_ADB_PMU
104 case SYS_CTRLER_PMU:
105 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
106 return 0;
107 while (!req.complete)
108 pmu_poll();
109 if (req.reply_len != 4)
110 printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
111 req.reply_len);
112 now = (req.reply[0] << 24) + (req.reply[1] << 16)
113 + (req.reply[2] << 8) + req.reply[3];
114 return now - RTC_OFFSET;
115 #endif /* CONFIG_ADB_PMU */
116 default: ;
118 return 0;
122 pmac_set_rtc_time(unsigned long nowtime)
124 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
125 struct adb_request req;
126 #endif
128 nowtime += RTC_OFFSET;
130 switch (sys_ctrler) {
131 #ifdef CONFIG_ADB_CUDA
132 case SYS_CTRLER_CUDA:
133 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
134 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
135 return 0;
136 while (!req.complete)
137 cuda_poll();
138 if ((req.reply_len != 3) && (req.reply_len != 7))
139 printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
140 req.reply_len);
141 return 1;
142 #endif /* CONFIG_ADB_CUDA */
143 #ifdef CONFIG_ADB_PMU
144 case SYS_CTRLER_PMU:
145 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
146 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
147 return 0;
148 while (!req.complete)
149 pmu_poll();
150 if (req.reply_len != 0)
151 printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
152 req.reply_len);
153 return 1;
154 #endif /* CONFIG_ADB_PMU */
155 default:
156 return 0;
161 * Calibrate the decrementer register using VIA timer 1.
162 * This is used both on powermacs and CHRP machines.
164 int __init
165 via_calibrate_decr(void)
167 struct device_node *vias;
168 volatile unsigned char __iomem *via;
169 int count = VIA_TIMER_FREQ_6 / 100;
170 unsigned int dstart, dend;
172 vias = find_devices("via-cuda");
173 if (vias == 0)
174 vias = find_devices("via-pmu");
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);
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)
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)
196 dend = get_dec();
198 tb_ticks_per_jiffy = (dstart - dend) / ((6 * HZ)/100);
199 tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
201 printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
202 tb_ticks_per_jiffy, dstart - dend);
204 iounmap(via);
206 return 1;
209 #ifdef CONFIG_PM
211 * Reset the time after a sleep.
213 static int
214 time_sleep_notify(struct pmu_sleep_notifier *self, int when)
216 static unsigned long time_diff;
217 unsigned long flags;
218 unsigned long seq;
220 switch (when) {
221 case PBOOK_SLEEP_NOW:
222 do {
223 seq = read_seqbegin_irqsave(&xtime_lock, flags);
224 time_diff = xtime.tv_sec - pmac_get_rtc_time();
225 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
226 break;
227 case PBOOK_WAKE:
228 write_seqlock_irqsave(&xtime_lock, flags);
229 xtime.tv_sec = pmac_get_rtc_time() + time_diff;
230 xtime.tv_nsec = 0;
231 last_rtc_update = xtime.tv_sec;
232 write_sequnlock_irqrestore(&xtime_lock, flags);
233 break;
235 return PBOOK_SLEEP_OK;
238 static struct pmu_sleep_notifier time_sleep_notifier = {
239 time_sleep_notify, SLEEP_LEVEL_MISC,
241 #endif /* CONFIG_PM */
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.
248 void __init
249 pmac_calibrate_decr(void)
251 struct device_node *cpu;
252 unsigned int freq, *fp;
254 #ifdef CONFIG_PM
255 pmu_register_sleep_notifier(&time_sleep_notifier);
256 #endif /* CONFIG_PM */
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
262 if (!machine_is_compatible("MacRISC2") &&
263 !machine_is_compatible("MacRISC3") &&
264 !machine_is_compatible("MacRISC4"))
265 if (via_calibrate_decr())
266 return;
268 /* Special case: QuickSilver G4s seem to have a badly calibrated
269 * timebase-frequency in OF, VIA is much better on these. We should
270 * probably implement calibration based on the KL timer on these
271 * machines anyway... -BenH
273 if (machine_is_compatible("PowerMac3,5"))
274 if (via_calibrate_decr())
275 return;
277 * The cpu node should have a timebase-frequency property
278 * to tell us the rate at which the decrementer counts.
280 cpu = find_type_devices("cpu");
281 if (cpu == 0)
282 panic("can't find cpu node in time_init");
283 fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
284 if (fp == 0)
285 panic("can't get cpu timebase frequency");
286 freq = *fp;
287 printk("time_init: decrementer frequency = %u.%.6u MHz\n",
288 freq/1000000, freq%1000000);
289 tb_ticks_per_jiffy = freq / HZ;
290 tb_to_us = mulhwu_scale_factor(freq, 1000000);