1 // SPDX-License-Identifier: GPL-2.0
3 * Support for periodic interrupts (100 per second) and for getting
4 * the current time from the RTC on Power Macintoshes.
6 * We use the decrementer register for our periodic interrupts.
8 * Paul Mackerras August 1996.
9 * Copyright (C) 1996 Paul Mackerras.
10 * Copyright (C) 2003-2005 Benjamin Herrenschmidt.
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/cuda.h>
23 #include <linux/pmu.h>
24 #include <linux/interrupt.h>
25 #include <linux/hardirq.h>
26 #include <linux/rtc.h>
28 #include <asm/sections.h>
31 #include <asm/machdep.h>
33 #include <asm/nvram.h>
41 #define DBG(x...) printk(x)
47 * Calibrate the decrementer frequency with the VIA timer 1.
49 #define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */
52 #define RS 0x200 /* skip between registers */
53 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
54 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
55 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
56 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
57 #define ACR (11*RS) /* Auxiliary control register */
58 #define IFR (13*RS) /* Interrupt flag register */
61 #define T1MODE 0xc0 /* Timer 1 mode */
62 #define T1MODE_CONT 0x40 /* continuous interrupts */
64 /* Bits in IFR and IER */
65 #define T1_INT 0x40 /* Timer 1 interrupt */
67 long __init
pmac_time_init(void)
70 #if defined(CONFIG_NVRAM) && defined(CONFIG_PPC32)
73 delta
= ((s32
)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC
+ 0x9)) << 16;
74 delta
|= ((s32
)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC
+ 0xa)) << 8;
75 delta
|= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC
+ 0xb);
76 if (delta
& 0x00800000UL
)
77 delta
|= 0xFF000000UL
;
78 dst
= ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC
+ 0x8) & 0x80) != 0);
79 printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta
/60,
85 #ifdef CONFIG_PMAC_SMU
86 static time64_t
smu_get_time(void)
90 if (smu_get_rtc_time(&tm
, 1))
92 return rtc_tm_to_time64(&tm
);
96 /* Can't be __init, it's called when suspending and resuming */
97 time64_t
pmac_get_boot_time(void)
99 /* Get the time from the RTC, used only at boot time */
100 switch (sys_ctrler
) {
101 #ifdef CONFIG_ADB_CUDA
102 case SYS_CTRLER_CUDA
:
103 return cuda_get_time();
105 #ifdef CONFIG_ADB_PMU
107 return pmu_get_time();
109 #ifdef CONFIG_PMAC_SMU
111 return smu_get_time();
118 void pmac_get_rtc_time(struct rtc_time
*tm
)
120 /* Get the time from the RTC, used only at boot time */
121 switch (sys_ctrler
) {
122 #ifdef CONFIG_ADB_CUDA
123 case SYS_CTRLER_CUDA
:
124 rtc_time64_to_tm(cuda_get_time(), tm
);
127 #ifdef CONFIG_ADB_PMU
129 rtc_time64_to_tm(pmu_get_time(), tm
);
132 #ifdef CONFIG_PMAC_SMU
134 smu_get_rtc_time(tm
, 1);
142 int pmac_set_rtc_time(struct rtc_time
*tm
)
144 switch (sys_ctrler
) {
145 #ifdef CONFIG_ADB_CUDA
146 case SYS_CTRLER_CUDA
:
147 return cuda_set_rtc_time(tm
);
149 #ifdef CONFIG_ADB_PMU
151 return pmu_set_rtc_time(tm
);
153 #ifdef CONFIG_PMAC_SMU
155 return smu_set_rtc_time(tm
, 1);
164 * Calibrate the decrementer register using VIA timer 1.
165 * This is used both on powermacs and CHRP machines.
167 static int __init
via_calibrate_decr(void)
169 struct device_node
*vias
;
170 volatile unsigned char __iomem
*via
;
171 int count
= VIA_TIMER_FREQ_6
/ 100;
172 unsigned int dstart
, dend
;
173 struct resource rsrc
;
175 vias
= of_find_node_by_name(NULL
, "via-cuda");
177 vias
= of_find_node_by_name(NULL
, "via-pmu");
179 vias
= of_find_node_by_name(NULL
, "via");
180 if (vias
== NULL
|| of_address_to_resource(vias
, 0, &rsrc
)) {
185 via
= ioremap(rsrc
.start
, resource_size(&rsrc
));
187 printk(KERN_ERR
"Failed to map VIA for timer calibration !\n");
191 /* set timer 1 for continuous interrupts */
192 out_8(&via
[ACR
], (via
[ACR
] & ~T1MODE
) | T1MODE_CONT
);
193 /* set the counter to a small value */
194 out_8(&via
[T1CH
], 2);
195 /* set the latch to `count' */
196 out_8(&via
[T1LL
], count
);
197 out_8(&via
[T1LH
], count
>> 8);
198 /* wait until it hits 0 */
199 while ((in_8(&via
[IFR
]) & T1_INT
) == 0)
202 /* clear the interrupt & wait until it hits 0 again */
204 while ((in_8(&via
[IFR
]) & T1_INT
) == 0)
208 ppc_tb_freq
= (dstart
- dend
) * 100 / 6;
217 * Query the OF and get the decr frequency.
219 void __init
pmac_calibrate_decr(void)
221 generic_calibrate_decr();
224 /* We assume MacRISC2 machines have correct device-tree
225 * calibration. That's better since the VIA itself seems
226 * to be slightly off. --BenH
228 if (!of_machine_is_compatible("MacRISC2") &&
229 !of_machine_is_compatible("MacRISC3") &&
230 !of_machine_is_compatible("MacRISC4"))
231 if (via_calibrate_decr())
234 /* Special case: QuickSilver G4s seem to have a badly calibrated
235 * timebase-frequency in OF, VIA is much better on these. We should
236 * probably implement calibration based on the KL timer on these
237 * machines anyway... -BenH
239 if (of_machine_is_compatible("PowerMac3,5"))
240 if (via_calibrate_decr())