blob: a4173906e94525a56184ece0d3d1db89e4d66bf3 [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 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100012#include <linux/errno.h>
13#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/param.h>
16#include <linux/string.h>
17#include <linux/mm.h>
18#include <linux/init.h>
19#include <linux/time.h>
20#include <linux/adb.h>
21#include <linux/cuda.h>
22#include <linux/pmu.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100023#include <linux/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100024#include <linux/hardirq.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100025#include <linux/rtc.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026
27#include <asm/sections.h>
28#include <asm/prom.h>
29#include <asm/system.h>
30#include <asm/io.h>
31#include <asm/pgtable.h>
32#include <asm/machdep.h>
33#include <asm/time.h>
34#include <asm/nvram.h>
Paul Mackerras35499c02005-10-22 16:02:39 +100035#include <asm/smu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100036
Paul Mackerrasf2783c12005-10-20 09:23:26 +100037#undef DEBUG
38
39#ifdef DEBUG
40#define DBG(x...) printk(x)
41#else
42#define DBG(x...)
43#endif
44
Paul Mackerras14cf11a2005-09-26 16:04:21 +100045/* Apparently the RTC stores seconds since 1 Jan 1904 */
46#define RTC_OFFSET 2082844800
47
48/*
49 * Calibrate the decrementer frequency with the VIA timer 1.
50 */
51#define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
52
53/* VIA registers */
54#define RS 0x200 /* skip between registers */
55#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
56#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
57#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
58#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
59#define ACR (11*RS) /* Auxiliary control register */
60#define IFR (13*RS) /* Interrupt flag register */
61
62/* Bits in ACR */
63#define T1MODE 0xc0 /* Timer 1 mode */
64#define T1MODE_CONT 0x40 /* continuous interrupts */
65
66/* Bits in IFR and IER */
67#define T1_INT 0x40 /* Timer 1 interrupt */
68
Paul Mackerrasf2783c12005-10-20 09:23:26 +100069long __init pmac_time_init(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070{
Paul Mackerras14cf11a2005-09-26 16:04:21 +100071 s32 delta = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +100072#ifdef CONFIG_NVRAM
Paul Mackerras14cf11a2005-09-26 16:04:21 +100073 int dst;
74
75 delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
76 delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
77 delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
78 if (delta & 0x00800000UL)
79 delta |= 0xFF000000UL;
80 dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
81 printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
82 dst ? "on" : "off");
Paul Mackerras14cf11a2005-09-26 16:04:21 +100083#endif
Paul Mackerras35499c02005-10-22 16:02:39 +100084 return delta;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100085}
86
Paul Mackerras35499c02005-10-22 16:02:39 +100087static void to_rtc_time(unsigned long now, struct rtc_time *tm)
88{
89 to_tm(now, tm);
90 tm->tm_year -= 1900;
91 tm->tm_mon -= 1;
92}
93
94static unsigned long from_rtc_time(struct rtc_time *tm)
95{
96 return mktime(tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
97 tm->tm_hour, tm->tm_min, tm->tm_sec);
98}
99
100#ifdef CONFIG_ADB_CUDA
101static unsigned long cuda_get_time(void)
102{
103 struct adb_request req;
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +1100104 unsigned int now;
Paul Mackerras35499c02005-10-22 16:02:39 +1000105
106 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
107 return 0;
108 while (!req.complete)
109 cuda_poll();
110 if (req.reply_len != 7)
111 printk(KERN_ERR "cuda_get_time: got %d byte reply\n",
112 req.reply_len);
113 now = (req.reply[3] << 24) + (req.reply[4] << 16)
114 + (req.reply[5] << 8) + req.reply[6];
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +1100115 return ((unsigned long)now) - RTC_OFFSET;
Paul Mackerras35499c02005-10-22 16:02:39 +1000116}
117
118#define cuda_get_rtc_time(tm) to_rtc_time(cuda_get_time(), (tm))
119
120static int cuda_set_rtc_time(struct rtc_time *tm)
121{
122 unsigned int nowtime;
123 struct adb_request req;
124
125 nowtime = from_rtc_time(tm) + RTC_OFFSET;
126 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
127 nowtime >> 24, nowtime >> 16, nowtime >> 8,
128 nowtime) < 0)
129 return -ENXIO;
130 while (!req.complete)
131 cuda_poll();
132 if ((req.reply_len != 3) && (req.reply_len != 7))
133 printk(KERN_ERR "cuda_set_rtc_time: got %d byte reply\n",
134 req.reply_len);
135 return 0;
136}
137
138#else
139#define cuda_get_time() 0
140#define cuda_get_rtc_time(tm)
141#define cuda_set_rtc_time(tm) 0
142#endif
143
144#ifdef CONFIG_ADB_PMU
145static unsigned long pmu_get_time(void)
146{
147 struct adb_request req;
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +1100148 unsigned int now;
Paul Mackerras35499c02005-10-22 16:02:39 +1000149
150 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
151 return 0;
152 pmu_wait_complete(&req);
153 if (req.reply_len != 4)
154 printk(KERN_ERR "pmu_get_time: got %d byte reply from PMU\n",
155 req.reply_len);
156 now = (req.reply[0] << 24) + (req.reply[1] << 16)
157 + (req.reply[2] << 8) + req.reply[3];
Benjamin Herrenschmidt0c37ec22005-11-14 14:55:58 +1100158 return ((unsigned long)now) - RTC_OFFSET;
Paul Mackerras35499c02005-10-22 16:02:39 +1000159}
160
161#define pmu_get_rtc_time(tm) to_rtc_time(pmu_get_time(), (tm))
162
163static int pmu_set_rtc_time(struct rtc_time *tm)
164{
165 unsigned int nowtime;
166 struct adb_request req;
167
168 nowtime = from_rtc_time(tm) + RTC_OFFSET;
169 if (pmu_request(&req, NULL, 5, PMU_SET_RTC, nowtime >> 24,
170 nowtime >> 16, nowtime >> 8, nowtime) < 0)
171 return -ENXIO;
172 pmu_wait_complete(&req);
173 if (req.reply_len != 0)
174 printk(KERN_ERR "pmu_set_rtc_time: %d byte reply from PMU\n",
175 req.reply_len);
176 return 0;
177}
178
179#else
180#define pmu_get_time() 0
181#define pmu_get_rtc_time(tm)
182#define pmu_set_rtc_time(tm) 0
183#endif
184
185#ifdef CONFIG_PMAC_SMU
186static unsigned long smu_get_time(void)
187{
188 struct rtc_time tm;
189
190 if (smu_get_rtc_time(&tm, 1))
191 return 0;
192 return from_rtc_time(&tm);
193}
194
195#else
196#define smu_get_time() 0
197#define smu_get_rtc_time(tm, spin)
198#define smu_set_rtc_time(tm, spin) 0
199#endif
200
Paul Mackerrasba76cd52005-11-14 21:56:57 +1100201/* Can't be __init, it's called when suspending and resuming */
202unsigned long pmac_get_boot_time(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000203{
Paul Mackerras35499c02005-10-22 16:02:39 +1000204 /* Get the time from the RTC, used only at boot time */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000205 switch (sys_ctrler) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206 case SYS_CTRLER_CUDA:
Paul Mackerras35499c02005-10-22 16:02:39 +1000207 return cuda_get_time();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000208 case SYS_CTRLER_PMU:
Paul Mackerras35499c02005-10-22 16:02:39 +1000209 return pmu_get_time();
210 case SYS_CTRLER_SMU:
211 return smu_get_time();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212 default:
213 return 0;
214 }
215}
216
Paul Mackerras35499c02005-10-22 16:02:39 +1000217void pmac_get_rtc_time(struct rtc_time *tm)
218{
219 /* Get the time from the RTC, used only at boot time */
220 switch (sys_ctrler) {
221 case SYS_CTRLER_CUDA:
222 cuda_get_rtc_time(tm);
223 break;
224 case SYS_CTRLER_PMU:
225 pmu_get_rtc_time(tm);
226 break;
227 case SYS_CTRLER_SMU:
228 smu_get_rtc_time(tm, 1);
229 break;
230 default:
231 ;
232 }
233}
234
235int pmac_set_rtc_time(struct rtc_time *tm)
236{
237 switch (sys_ctrler) {
238 case SYS_CTRLER_CUDA:
239 return cuda_set_rtc_time(tm);
240 case SYS_CTRLER_PMU:
241 return pmu_set_rtc_time(tm);
242 case SYS_CTRLER_SMU:
243 return smu_set_rtc_time(tm, 1);
244 default:
245 return -ENODEV;
246 }
247}
248
249#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000250/*
251 * Calibrate the decrementer register using VIA timer 1.
252 * This is used both on powermacs and CHRP machines.
253 */
Paul Mackerras35499c02005-10-22 16:02:39 +1000254int __init via_calibrate_decr(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000255{
256 struct device_node *vias;
257 volatile unsigned char __iomem *via;
258 int count = VIA_TIMER_FREQ_6 / 100;
259 unsigned int dstart, dend;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100260 struct resource rsrc;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000261
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100262 vias = of_find_node_by_name(NULL, "via-cuda");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000263 if (vias == 0)
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100264 vias = of_find_node_by_name(NULL, "via-pmu");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000265 if (vias == 0)
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100266 vias = of_find_node_by_name(NULL, "via");
267 if (vias == 0 || of_address_to_resource(vias, 0, &rsrc))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000268 return 0;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100269 via = ioremap(rsrc.start, rsrc.end - rsrc.start + 1);
270 if (via == NULL) {
271 printk(KERN_ERR "Failed to map VIA for timer calibration !\n");
272 return 0;
273 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000274
275 /* set timer 1 for continuous interrupts */
276 out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
277 /* set the counter to a small value */
278 out_8(&via[T1CH], 2);
279 /* set the latch to `count' */
280 out_8(&via[T1LL], count);
281 out_8(&via[T1LH], count >> 8);
282 /* wait until it hits 0 */
283 while ((in_8(&via[IFR]) & T1_INT) == 0)
284 ;
285 dstart = get_dec();
286 /* clear the interrupt & wait until it hits 0 again */
287 in_8(&via[T1CL]);
288 while ((in_8(&via[IFR]) & T1_INT) == 0)
289 ;
290 dend = get_dec();
291
Paul Mackerras5d14a182005-10-20 22:33:06 +1000292 ppc_tb_freq = (dstart - dend) * 100 / 6;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000293
294 iounmap(via);
295
296 return 1;
297}
Paul Mackerras35499c02005-10-22 16:02:39 +1000298#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000299
300#ifdef CONFIG_PM
301/*
302 * Reset the time after a sleep.
303 */
304static int
305time_sleep_notify(struct pmu_sleep_notifier *self, int when)
306{
307 static unsigned long time_diff;
308 unsigned long flags;
309 unsigned long seq;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000310 struct timespec tv;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000311
312 switch (when) {
313 case PBOOK_SLEEP_NOW:
314 do {
315 seq = read_seqbegin_irqsave(&xtime_lock, flags);
Paul Mackerras143a1de2005-10-19 23:11:21 +1000316 time_diff = xtime.tv_sec - pmac_get_boot_time();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000317 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
318 break;
319 case PBOOK_WAKE:
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000320 tv.tv_sec = pmac_get_boot_time() + time_diff;
321 tv.tv_nsec = 0;
322 do_settimeofday(&tv);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000323 break;
324 }
325 return PBOOK_SLEEP_OK;
326}
327
328static struct pmu_sleep_notifier time_sleep_notifier = {
329 time_sleep_notify, SLEEP_LEVEL_MISC,
330};
331#endif /* CONFIG_PM */
332
333/*
334 * Query the OF and get the decr frequency.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000335 */
Paul Mackerras35499c02005-10-22 16:02:39 +1000336void __init pmac_calibrate_decr(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000337{
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100338#if defined(CONFIG_PM) && defined(CONFIG_ADB_PMU)
Paul Mackerras35499c02005-10-22 16:02:39 +1000339 /* XXX why here? */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000340 pmu_register_sleep_notifier(&time_sleep_notifier);
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100341#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000342
Paul Mackerras35499c02005-10-22 16:02:39 +1000343 generic_calibrate_decr();
344
345#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000346 /* We assume MacRISC2 machines have correct device-tree
347 * calibration. That's better since the VIA itself seems
348 * to be slightly off. --BenH
349 */
350 if (!machine_is_compatible("MacRISC2") &&
351 !machine_is_compatible("MacRISC3") &&
352 !machine_is_compatible("MacRISC4"))
353 if (via_calibrate_decr())
354 return;
355
356 /* Special case: QuickSilver G4s seem to have a badly calibrated
357 * timebase-frequency in OF, VIA is much better on these. We should
358 * probably implement calibration based on the KL timer on these
359 * machines anyway... -BenH
360 */
361 if (machine_is_compatible("PowerMac3,5"))
362 if (via_calibrate_decr())
363 return;
Paul Mackerras35499c02005-10-22 16:02:39 +1000364#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000365}