2 * BK Id: SCCS/s.pmac_time.c 1.26 12/18/02 00:17:27 benh
5 * Support for periodic interrupts (100 per second) and for getting
6 * the current time from the RTC on Power Macintoshes.
8 * We use the decrementer register for our periodic interrupts.
10 * Paul Mackerras August 1996.
11 * Copyright (C) 1996 Paul Mackerras.
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/param.h>
18 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/adb.h>
22 #include <linux/cuda.h>
23 #include <linux/pmu.h>
25 #include <asm/sections.h>
27 #include <asm/system.h>
29 #include <asm/pgtable.h>
30 #include <asm/machdep.h>
31 #include <asm/hardirq.h>
33 #include <asm/nvram.h>
35 extern rwlock_t xtime_lock;
37 /* Apparently the RTC stores seconds since 1 Jan 1904 */
38 #define RTC_OFFSET 2082844800
41 * Calibrate the decrementer frequency with the VIA timer 1.
43 #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
46 #define RS 0x200 /* skip between registers */
47 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
48 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
49 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
50 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
51 #define ACR (11*RS) /* Auxiliary control register */
52 #define IFR (13*RS) /* Interrupt flag register */
55 #define T1MODE 0xc0 /* Timer 1 mode */
56 #define T1MODE_CONT 0x40 /* continuous interrupts */
58 /* Bits in IFR and IER */
59 #define T1_INT 0x40 /* Timer 1 interrupt */
61 extern struct timezone sys_tz;
70 delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
71 delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
72 delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
73 if (delta & 0x00800000UL)
74 delta |= 0xFF000000UL;
75 dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
76 printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
85 pmac_get_rtc_time(void)
87 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
88 struct adb_request req;
92 /* Get the time from the RTC */
94 #ifdef CONFIG_ADB_CUDA
96 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
100 if (req.reply_len != 7)
101 printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
103 now = (req.reply[3] << 24) + (req.reply[4] << 16)
104 + (req.reply[5] << 8) + req.reply[6];
105 return now - RTC_OFFSET;
106 #endif /* CONFIG_ADB_CUDA */
107 #ifdef CONFIG_ADB_PMU
109 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
111 while (!req.complete)
113 if (req.reply_len != 4)
114 printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
116 now = (req.reply[0] << 24) + (req.reply[1] << 16)
117 + (req.reply[2] << 8) + req.reply[3];
118 return now - RTC_OFFSET;
119 #endif /* CONFIG_ADB_PMU */
126 pmac_set_rtc_time(unsigned long nowtime)
128 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
129 struct adb_request req;
132 nowtime += RTC_OFFSET;
134 switch (sys_ctrler) {
135 #ifdef CONFIG_ADB_CUDA
136 case SYS_CTRLER_CUDA:
137 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
138 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
140 while (!req.complete)
142 if ((req.reply_len != 3) && (req.reply_len != 7))
143 printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
146 #endif /* CONFIG_ADB_CUDA */
147 #ifdef CONFIG_ADB_PMU
149 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
150 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
152 while (!req.complete)
154 if (req.reply_len != 0)
155 printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
158 #endif /* CONFIG_ADB_PMU */
165 * Calibrate the decrementer register using VIA timer 1.
166 * This is used both on powermacs and CHRP machines.
169 via_calibrate_decr(void)
171 struct device_node *vias;
172 volatile unsigned char *via;
173 int count = VIA_TIMER_FREQ_6 / HZ;
174 unsigned int dstart, dend;
176 vias = find_devices("via-cuda");
178 vias = find_devices("via-pmu");
180 vias = find_devices("via");
181 if (vias == 0 || vias->n_addrs == 0)
183 via = (volatile unsigned char *)
184 ioremap(vias->addrs[0].address, vias->addrs[0].size);
186 /* set timer 1 for continuous interrupts */
187 out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
188 /* set the counter to a small value */
189 out_8(&via[T1CH], 2);
190 /* set the latch to `count' */
191 out_8(&via[T1LL], count);
192 out_8(&via[T1LH], count >> 8);
193 /* wait until it hits 0 */
194 while ((in_8(&via[IFR]) & T1_INT) == 0)
197 /* clear the interrupt & wait until it hits 0 again */
199 while ((in_8(&via[IFR]) & T1_INT) == 0)
203 tb_ticks_per_jiffy = (dstart - dend) / 6;
204 tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
206 printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
207 tb_ticks_per_jiffy, dstart - dend);
209 iounmap((void *)via);
214 #ifdef CONFIG_PMAC_PBOOK
216 * Reset the time after a sleep.
219 time_sleep_notify(struct pmu_sleep_notifier *self, int when)
221 static unsigned long time_diff;
225 case PBOOK_SLEEP_NOW:
226 read_lock_irqsave(&xtime_lock, flags);
227 time_diff = xtime.tv_sec - pmac_get_rtc_time();
228 read_unlock_irqrestore(&xtime_lock, flags);
231 write_lock_irqsave(&xtime_lock, flags);
232 xtime.tv_sec = pmac_get_rtc_time() + time_diff;
234 last_rtc_update = xtime.tv_sec;
235 write_unlock_irqrestore(&xtime_lock, flags);
238 return PBOOK_SLEEP_OK;
241 static struct pmu_sleep_notifier time_sleep_notifier __pmacdata = {
242 time_sleep_notify, SLEEP_LEVEL_MISC,
244 #endif /* CONFIG_PMAC_PBOOK */
247 * Query the OF and get the decr frequency.
248 * This was taken from the pmac time_init() when merging the prep/pmac
252 pmac_calibrate_decr(void)
254 struct device_node *cpu;
255 unsigned int freq, *fp;
257 #ifdef CONFIG_PMAC_PBOOK
258 pmu_register_sleep_notifier(&time_sleep_notifier);
259 #endif /* CONFIG_PMAC_PBOOK */
261 /* We assume MacRISC2 machines have correct device-tree
262 * calibration. That's better since the VIA itself seems
263 * to be slightly off. --BenH
265 if (!machine_is_compatible("MacRISC2"))
266 if (via_calibrate_decr())
270 * The cpu node should have a timebase-frequency property
271 * to tell us the rate at which the decrementer counts.
273 cpu = find_type_devices("cpu");
275 panic("can't find cpu node in time_init");
276 fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
278 panic("can't get cpu timebase frequency");
280 printk("time_init: decrementer frequency = %u.%.6u MHz\n",
281 freq/1000000, freq%1000000);
282 tb_ticks_per_jiffy = freq / HZ;
283 tb_to_us = mulhwu_scale_factor(freq, 1000000);