blob: 82982bf6453c107f5ace3170023f4dc373526e8e [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
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.
Paul Mackerrasf2783c12005-10-20 09:23:26 +10009 * Copyright (C) 2003-2005 Benjamin Herrenschmidt.
10 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +100011 */
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>
18#include <linux/mm.h>
19#include <linux/init.h>
20#include <linux/time.h>
21#include <linux/adb.h>
22#include <linux/cuda.h>
23#include <linux/pmu.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100024#include <linux/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100025#include <linux/hardirq.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100026#include <linux/rtc.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027
28#include <asm/sections.h>
29#include <asm/prom.h>
30#include <asm/system.h>
31#include <asm/io.h>
32#include <asm/pgtable.h>
33#include <asm/machdep.h>
34#include <asm/time.h>
35#include <asm/nvram.h>
Paul Mackerras35499c02005-10-22 16:02:39 +100036#include <asm/smu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037
Paul Mackerrasf2783c12005-10-20 09:23:26 +100038#undef DEBUG
39
40#ifdef DEBUG
41#define DBG(x...) printk(x)
42#else
43#define DBG(x...)
44#endif
45
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046/* Apparently the RTC stores seconds since 1 Jan 1904 */
47#define RTC_OFFSET 2082844800
48
49/*
50 * Calibrate the decrementer frequency with the VIA timer 1.
51 */
52#define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
53
54/* VIA registers */
55#define RS 0x200 /* skip between registers */
56#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
57#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
58#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
59#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
60#define ACR (11*RS) /* Auxiliary control register */
61#define IFR (13*RS) /* Interrupt flag register */
62
63/* Bits in ACR */
64#define T1MODE 0xc0 /* Timer 1 mode */
65#define T1MODE_CONT 0x40 /* continuous interrupts */
66
67/* Bits in IFR and IER */
68#define T1_INT 0x40 /* Timer 1 interrupt */
69
Paul Mackerrasf2783c12005-10-20 09:23:26 +100070long __init pmac_time_init(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100071{
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072 s32 delta = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +100073#ifdef CONFIG_NVRAM
Paul Mackerras14cf11a2005-09-26 16:04:21 +100074 int dst;
75
76 delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
77 delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
78 delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
79 if (delta & 0x00800000UL)
80 delta |= 0xFF000000UL;
81 dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
82 printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
83 dst ? "on" : "off");
Paul Mackerras14cf11a2005-09-26 16:04:21 +100084#endif
Paul Mackerras35499c02005-10-22 16:02:39 +100085 return delta;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100086}
87
Paul Mackerras35499c02005-10-22 16:02:39 +100088static void to_rtc_time(unsigned long now, struct rtc_time *tm)
89{
90 to_tm(now, tm);
91 tm->tm_year -= 1900;
92 tm->tm_mon -= 1;
93}
94
95static unsigned long from_rtc_time(struct rtc_time *tm)
96{
97 return mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
98 tm->tm_hour, tm->tm_min, tm->tm_sec);
99}
100
101#ifdef CONFIG_ADB_CUDA
102static unsigned long cuda_get_time(void)
103{
104 struct adb_request req;
105 unsigned long now;
106
107 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
108 return 0;
109 while (!req.complete)
110 cuda_poll();
111 if (req.reply_len != 7)
112 printk(KERN_ERR "cuda_get_time: got %d byte reply\n",
113 req.reply_len);
114 now = (req.reply[3] << 24) + (req.reply[4] << 16)
115 + (req.reply[5] << 8) + req.reply[6];
116 if (now < RTC_OFFSET)
117 return 0;
118 return now - RTC_OFFSET;
119}
120
121#define cuda_get_rtc_time(tm) to_rtc_time(cuda_get_time(), (tm))
122
123static int cuda_set_rtc_time(struct rtc_time *tm)
124{
125 unsigned int nowtime;
126 struct adb_request req;
127
128 nowtime = from_rtc_time(tm) + RTC_OFFSET;
129 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
130 nowtime >> 24, nowtime >> 16, nowtime >> 8,
131 nowtime) < 0)
132 return -ENXIO;
133 while (!req.complete)
134 cuda_poll();
135 if ((req.reply_len != 3) && (req.reply_len != 7))
136 printk(KERN_ERR "cuda_set_rtc_time: got %d byte reply\n",
137 req.reply_len);
138 return 0;
139}
140
141#else
142#define cuda_get_time() 0
143#define cuda_get_rtc_time(tm)
144#define cuda_set_rtc_time(tm) 0
145#endif
146
147#ifdef CONFIG_ADB_PMU
148static unsigned long pmu_get_time(void)
149{
150 struct adb_request req;
151 unsigned long now;
152
153 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
154 return 0;
155 pmu_wait_complete(&req);
156 if (req.reply_len != 4)
157 printk(KERN_ERR "pmu_get_time: got %d byte reply from PMU\n",
158 req.reply_len);
159 now = (req.reply[0] << 24) + (req.reply[1] << 16)
160 + (req.reply[2] << 8) + req.reply[3];
161 if (now < RTC_OFFSET)
162 return 0;
163 return now - RTC_OFFSET;
164}
165
166#define pmu_get_rtc_time(tm) to_rtc_time(pmu_get_time(), (tm))
167
168static int pmu_set_rtc_time(struct rtc_time *tm)
169{
170 unsigned int nowtime;
171 struct adb_request req;
172
173 nowtime = from_rtc_time(tm) + RTC_OFFSET;
174 if (pmu_request(&req, NULL, 5, PMU_SET_RTC, nowtime >> 24,
175 nowtime >> 16, nowtime >> 8, nowtime) < 0)
176 return -ENXIO;
177 pmu_wait_complete(&req);
178 if (req.reply_len != 0)
179 printk(KERN_ERR "pmu_set_rtc_time: %d byte reply from PMU\n",
180 req.reply_len);
181 return 0;
182}
183
184#else
185#define pmu_get_time() 0
186#define pmu_get_rtc_time(tm)
187#define pmu_set_rtc_time(tm) 0
188#endif
189
190#ifdef CONFIG_PMAC_SMU
191static unsigned long smu_get_time(void)
192{
193 struct rtc_time tm;
194
195 if (smu_get_rtc_time(&tm, 1))
196 return 0;
197 return from_rtc_time(&tm);
198}
199
200#else
201#define smu_get_time() 0
202#define smu_get_rtc_time(tm, spin)
203#define smu_set_rtc_time(tm, spin) 0
204#endif
205
Paul Mackerras143a1de2005-10-19 23:11:21 +1000206unsigned long pmac_get_boot_time(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000207{
Paul Mackerras35499c02005-10-22 16:02:39 +1000208 /* Get the time from the RTC, used only at boot time */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000209 switch (sys_ctrler) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000210 case SYS_CTRLER_CUDA:
Paul Mackerras35499c02005-10-22 16:02:39 +1000211 return cuda_get_time();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212 case SYS_CTRLER_PMU:
Paul Mackerras35499c02005-10-22 16:02:39 +1000213 return pmu_get_time();
214 case SYS_CTRLER_SMU:
215 return smu_get_time();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000216 default:
217 return 0;
218 }
219}
220
Paul Mackerras35499c02005-10-22 16:02:39 +1000221void pmac_get_rtc_time(struct rtc_time *tm)
222{
223 /* Get the time from the RTC, used only at boot time */
224 switch (sys_ctrler) {
225 case SYS_CTRLER_CUDA:
226 cuda_get_rtc_time(tm);
227 break;
228 case SYS_CTRLER_PMU:
229 pmu_get_rtc_time(tm);
230 break;
231 case SYS_CTRLER_SMU:
232 smu_get_rtc_time(tm, 1);
233 break;
234 default:
235 ;
236 }
237}
238
239int pmac_set_rtc_time(struct rtc_time *tm)
240{
241 switch (sys_ctrler) {
242 case SYS_CTRLER_CUDA:
243 return cuda_set_rtc_time(tm);
244 case SYS_CTRLER_PMU:
245 return pmu_set_rtc_time(tm);
246 case SYS_CTRLER_SMU:
247 return smu_set_rtc_time(tm, 1);
248 default:
249 return -ENODEV;
250 }
251}
252
253#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000254/*
255 * Calibrate the decrementer register using VIA timer 1.
256 * This is used both on powermacs and CHRP machines.
257 */
Paul Mackerras35499c02005-10-22 16:02:39 +1000258int __init via_calibrate_decr(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000259{
260 struct device_node *vias;
261 volatile unsigned char __iomem *via;
262 int count = VIA_TIMER_FREQ_6 / 100;
263 unsigned int dstart, dend;
264
265 vias = find_devices("via-cuda");
266 if (vias == 0)
267 vias = find_devices("via-pmu");
268 if (vias == 0)
269 vias = find_devices("via");
270 if (vias == 0 || vias->n_addrs == 0)
271 return 0;
272 via = ioremap(vias->addrs[0].address, vias->addrs[0].size);
273
274 /* set timer 1 for continuous interrupts */
275 out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
276 /* set the counter to a small value */
277 out_8(&via[T1CH], 2);
278 /* set the latch to `count' */
279 out_8(&via[T1LL], count);
280 out_8(&via[T1LH], count >> 8);
281 /* wait until it hits 0 */
282 while ((in_8(&via[IFR]) & T1_INT) == 0)
283 ;
284 dstart = get_dec();
285 /* clear the interrupt & wait until it hits 0 again */
286 in_8(&via[T1CL]);
287 while ((in_8(&via[IFR]) & T1_INT) == 0)
288 ;
289 dend = get_dec();
290
Paul Mackerras5d14a182005-10-20 22:33:06 +1000291 ppc_tb_freq = (dstart - dend) * 100 / 6;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000292
293 iounmap(via);
294
295 return 1;
296}
Paul Mackerras35499c02005-10-22 16:02:39 +1000297#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000298
299#ifdef CONFIG_PM
300/*
301 * Reset the time after a sleep.
302 */
303static int
304time_sleep_notify(struct pmu_sleep_notifier *self, int when)
305{
306 static unsigned long time_diff;
307 unsigned long flags;
308 unsigned long seq;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000309 struct timespec tv;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000310
311 switch (when) {
312 case PBOOK_SLEEP_NOW:
313 do {
314 seq = read_seqbegin_irqsave(&xtime_lock, flags);
Paul Mackerras143a1de2005-10-19 23:11:21 +1000315 time_diff = xtime.tv_sec - pmac_get_boot_time();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000316 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
317 break;
318 case PBOOK_WAKE:
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000319 tv.tv_sec = pmac_get_boot_time() + time_diff;
320 tv.tv_nsec = 0;
321 do_settimeofday(&tv);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322 break;
323 }
324 return PBOOK_SLEEP_OK;
325}
326
327static struct pmu_sleep_notifier time_sleep_notifier = {
328 time_sleep_notify, SLEEP_LEVEL_MISC,
329};
330#endif /* CONFIG_PM */
331
332/*
333 * Query the OF and get the decr frequency.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000334 */
Paul Mackerras35499c02005-10-22 16:02:39 +1000335void __init pmac_calibrate_decr(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000336{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000337#ifdef CONFIG_PM
Paul Mackerras35499c02005-10-22 16:02:39 +1000338 /* XXX why here? */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000339 pmu_register_sleep_notifier(&time_sleep_notifier);
340#endif /* CONFIG_PM */
341
Paul Mackerras35499c02005-10-22 16:02:39 +1000342 generic_calibrate_decr();
343
344#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000345 /* We assume MacRISC2 machines have correct device-tree
346 * calibration. That's better since the VIA itself seems
347 * to be slightly off. --BenH
348 */
349 if (!machine_is_compatible("MacRISC2") &&
350 !machine_is_compatible("MacRISC3") &&
351 !machine_is_compatible("MacRISC4"))
352 if (via_calibrate_decr())
353 return;
354
355 /* Special case: QuickSilver G4s seem to have a badly calibrated
356 * timebase-frequency in OF, VIA is much better on these. We should
357 * probably implement calibration based on the KL timer on these
358 * machines anyway... -BenH
359 */
360 if (machine_is_compatible("PowerMac3,5"))
361 if (via_calibrate_decr())
362 return;
Paul Mackerras35499c02005-10-22 16:02:39 +1000363#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000364}