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.
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>
17 #include <linux/init.h>
18 #include <linux/adb.h>
19 #include <linux/cuda.h>
20 #include <linux/pmu.h>
22 #include <asm/sections.h>
24 #include <asm/system.h>
26 #include <asm/pgtable.h>
27 #include <asm/machdep.h>
28 #include <asm/hardirq.h>
30 #include <asm/nvram.h>
32 extern rwlock_t xtime_lock;
34 /* Apparently the RTC stores seconds since 1 Jan 1904 */
35 #define RTC_OFFSET 2082844800
38 * Calibrate the decrementer frequency with the VIA timer 1.
40 #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
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 */
52 #define T1MODE 0xc0 /* Timer 1 mode */
53 #define T1MODE_CONT 0x40 /* continuous interrupts */
55 /* Bits in IFR and IER */
56 #define T1_INT 0x40 /* Timer 1 interrupt */
58 extern struct timezone sys_tz;
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,
82 pmac_get_rtc_time(void)
84 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
85 struct adb_request req;
89 /* Get the time from the RTC */
91 #ifdef CONFIG_ADB_CUDA
93 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
97 if (req.reply_len != 7)
98 printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
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
106 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
108 while (!req.complete)
110 if (req.reply_len != 4)
111 printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
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 */
123 pmac_set_rtc_time(unsigned long nowtime)
125 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
126 struct adb_request req;
129 nowtime += RTC_OFFSET;
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)
137 while (!req.complete)
139 if ((req.reply_len != 3) && (req.reply_len != 7))
140 printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
143 #endif /* CONFIG_ADB_CUDA */
144 #ifdef CONFIG_ADB_PMU
146 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
147 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
149 while (!req.complete)
151 if (req.reply_len != 0)
152 printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
155 #endif /* CONFIG_ADB_PMU */
162 * Calibrate the decrementer register using VIA timer 1.
163 * This is used both on powermacs and CHRP machines.
166 via_calibrate_decr(void)
168 struct device_node *vias;
169 volatile unsigned char *via;
170 int count = VIA_TIMER_FREQ_6 / HZ;
171 unsigned int dstart, dend;
173 vias = find_devices("via-cuda");
175 vias = find_devices("via-pmu");
177 vias = find_devices("via");
178 if (vias == 0 || vias->n_addrs == 0)
180 via = (volatile unsigned char *)
181 ioremap(vias->addrs[0].address, vias->addrs[0].size);
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)
194 /* clear the interrupt & wait until it hits 0 again */
196 while ((in_8(&via[IFR]) & T1_INT) == 0)
200 tb_ticks_per_jiffy = (dstart - dend) / 6;
201 tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
203 printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
204 tb_ticks_per_jiffy, dstart - dend);
206 iounmap((void *)via);
211 #ifdef CONFIG_PMAC_PBOOK
213 * Reset the time after a sleep.
216 time_sleep_notify(struct pmu_sleep_notifier *self, int when)
218 static unsigned long time_diff;
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);
228 write_lock_irqsave(&xtime_lock, flags);
229 xtime.tv_sec = pmac_get_rtc_time() + time_diff;
231 last_rtc_update = xtime.tv_sec;
232 write_unlock_irqrestore(&xtime_lock, flags);
235 return PBOOK_SLEEP_OK;
238 static struct pmu_sleep_notifier time_sleep_notifier __pmacdata = {
239 time_sleep_notify, SLEEP_LEVEL_MISC,
241 #endif /* CONFIG_PMAC_PBOOK */
244 * Query the OF and get the decr frequency.
245 * This was taken from the pmac time_init() when merging the prep/pmac
249 pmac_calibrate_decr(void)
251 struct device_node *cpu;
252 unsigned int freq, *fp;
254 #ifdef CONFIG_PMAC_PBOOK
255 pmu_register_sleep_notifier(&time_sleep_notifier);
256 #endif /* CONFIG_PMAC_PBOOK */
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 if (via_calibrate_decr())
267 * The cpu node should have a timebase-frequency property
268 * to tell us the rate at which the decrementer counts.
270 cpu = find_type_devices("cpu");
272 panic("can't find cpu node in time_init");
273 fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
275 panic("can't get cpu timebase frequency");
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);