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 * Copyright (C) 2003-2005 Benjamin Herrenschmidt.
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/param.h>
17 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/time.h>
21 #include <linux/adb.h>
22 #include <linux/pmu.h>
23 #include <linux/interrupt.h>
24 #include <linux/rtc.h>
26 #include <asm/sections.h>
28 #include <asm/system.h>
30 #include <asm/pgtable.h>
31 #include <asm/machdep.h>
33 #include <asm/nvram.h>
39 #define DBG(x...) printk(x)
44 /* Apparently the RTC stores seconds since 1 Jan 1904 */
45 #define RTC_OFFSET 2082844800
48 * Calibrate the decrementer frequency with the VIA timer 1.
50 #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
52 extern struct timezone sys_tz;
53 extern void to_tm(int tim, struct rtc_time * tm);
55 void pmac_get_rtc_time(struct rtc_time *tm)
59 case SYS_CTRLER_PMU: {
60 /* TODO: Move that to a function in the PMU driver */
61 struct adb_request req;
64 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
66 pmu_wait_complete(&req);
67 if (req.reply_len != 4)
68 printk(KERN_ERR "pmac_get_rtc_time: PMU returned a %d"
69 " bytes reply\n", req.reply_len);
70 now = (req.reply[0] << 24) + (req.reply[1] << 16)
71 + (req.reply[2] << 8) + req.reply[3];
72 DBG("get: %u -> %u\n", (int)now, (int)(now - RTC_OFFSET));
79 DBG("-> tm_mday: %d, tm_mon: %d, tm_year: %d, %d:%02d:%02d\n",
80 tm->tm_mday, tm->tm_mon, tm->tm_year,
81 tm->tm_hour, tm->tm_min, tm->tm_sec);
84 #endif /* CONFIG_ADB_PMU */
86 #ifdef CONFIG_PMAC_SMU
88 smu_get_rtc_time(tm, 1);
90 #endif /* CONFIG_PMAC_SMU */
96 int pmac_set_rtc_time(struct rtc_time *tm)
100 case SYS_CTRLER_PMU: {
101 /* TODO: Move that to a function in the PMU driver */
102 struct adb_request req;
103 unsigned int nowtime;
105 DBG("set: tm_mday: %d, tm_mon: %d, tm_year: %d,"
107 tm->tm_mday, tm->tm_mon, tm->tm_year,
108 tm->tm_hour, tm->tm_min, tm->tm_sec);
110 nowtime = mktime(tm->tm_year + 1900, tm->tm_mon + 1,
111 tm->tm_mday, tm->tm_hour, tm->tm_min,
114 DBG("-> %u -> %u\n", (int)nowtime,
115 (int)(nowtime + RTC_OFFSET));
116 nowtime += RTC_OFFSET;
118 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
119 nowtime >> 24, nowtime >> 16,
120 nowtime >> 8, nowtime) < 0)
122 pmu_wait_complete(&req);
123 if (req.reply_len != 0)
124 printk(KERN_ERR "pmac_set_rtc_time: PMU returned a %d"
125 " bytes reply\n", req.reply_len);
128 #endif /* CONFIG_ADB_PMU */
130 #ifdef CONFIG_PMAC_SMU
132 return smu_set_rtc_time(tm, 1);
133 #endif /* CONFIG_PMAC_SMU */
139 unsigned long __init pmac_get_boot_time(void)
143 pmac_get_rtc_time(&tm);
144 return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
145 tm.tm_hour, tm.tm_min, tm.tm_sec);
149 * Query the OF and get the decr frequency.
150 * FIXME: merge this with generic_calibrate_decr
152 void __init pmac_calibrate_decr(void)
154 struct device_node *cpu;
158 * The cpu node should have a timebase-frequency property
159 * to tell us the rate at which the decrementer counts.
161 cpu = find_type_devices("cpu");
163 panic("can't find cpu node in time_init");
164 fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
166 panic("can't get cpu timebase frequency");
169 fp = (unsigned int *)get_property(cpu, "clock-frequency", NULL);
171 panic("can't get cpu processor frequency");