Path: blob/master/arch/powerpc/platforms/powermac/time.c
26481 views
// SPDX-License-Identifier: GPL-2.01/*2* Support for periodic interrupts (100 per second) and for getting3* 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.9* Copyright (C) 2003-2005 Benjamin Herrenschmidt.10*11*/12#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/string_choices.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>24#include <linux/interrupt.h>25#include <linux/hardirq.h>26#include <linux/rtc.h>27#include <linux/of_address.h>2829#include <asm/early_ioremap.h>30#include <asm/sections.h>31#include <asm/machdep.h>32#include <asm/time.h>33#include <asm/nvram.h>34#include <asm/smu.h>3536#include "pmac.h"3738#undef DEBUG3940#ifdef DEBUG41#define DBG(x...) printk(x)42#else43#define DBG(x...)44#endif4546/*47* Calibrate the decrementer frequency with the VIA timer 1.48*/49#define VIA_TIMER_FREQ_6 4700000 /* time 1 frequency * 6 */5051/* VIA registers */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 */5960/* Bits in ACR */61#define T1MODE 0xc0 /* Timer 1 mode */62#define T1MODE_CONT 0x40 /* continuous interrupts */6364/* Bits in IFR and IER */65#define T1_INT 0x40 /* Timer 1 interrupt */6667long __init pmac_time_init(void)68{69s32 delta = 0;70#if defined(CONFIG_NVRAM) && defined(CONFIG_PPC32)71int dst;7273delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;74delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;75delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);76if (delta & 0x00800000UL)77delta |= 0xFF000000UL;78dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);79printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,80str_on_off(dst));81#endif82return delta;83}8485#ifdef CONFIG_PMAC_SMU86static time64_t smu_get_time(void)87{88struct rtc_time tm;8990if (smu_get_rtc_time(&tm, 1))91return 0;92return rtc_tm_to_time64(&tm);93}94#endif9596/* Can't be __init, it's called when suspending and resuming */97time64_t pmac_get_boot_time(void)98{99/* Get the time from the RTC, used only at boot time */100switch (sys_ctrler) {101#ifdef CONFIG_ADB_CUDA102case SYS_CTRLER_CUDA:103return cuda_get_time();104#endif105#ifdef CONFIG_ADB_PMU106case SYS_CTRLER_PMU:107return pmu_get_time();108#endif109#ifdef CONFIG_PMAC_SMU110case SYS_CTRLER_SMU:111return smu_get_time();112#endif113default:114return 0;115}116}117118void pmac_get_rtc_time(struct rtc_time *tm)119{120/* Get the time from the RTC, used only at boot time */121switch (sys_ctrler) {122#ifdef CONFIG_ADB_CUDA123case SYS_CTRLER_CUDA:124rtc_time64_to_tm(cuda_get_time(), tm);125break;126#endif127#ifdef CONFIG_ADB_PMU128case SYS_CTRLER_PMU:129rtc_time64_to_tm(pmu_get_time(), tm);130break;131#endif132#ifdef CONFIG_PMAC_SMU133case SYS_CTRLER_SMU:134smu_get_rtc_time(tm, 1);135break;136#endif137default:138;139}140}141142int pmac_set_rtc_time(struct rtc_time *tm)143{144switch (sys_ctrler) {145#ifdef CONFIG_ADB_CUDA146case SYS_CTRLER_CUDA:147return cuda_set_rtc_time(tm);148#endif149#ifdef CONFIG_ADB_PMU150case SYS_CTRLER_PMU:151return pmu_set_rtc_time(tm);152#endif153#ifdef CONFIG_PMAC_SMU154case SYS_CTRLER_SMU:155return smu_set_rtc_time(tm, 1);156#endif157default:158return -ENODEV;159}160}161162#ifdef CONFIG_PPC32163/*164* Calibrate the decrementer register using VIA timer 1.165* This is used both on powermacs and CHRP machines.166*/167static int __init via_calibrate_decr(void)168{169struct device_node *vias;170volatile unsigned char __iomem *via;171int count = VIA_TIMER_FREQ_6 / 100;172unsigned int dstart, dend;173struct resource rsrc;174175vias = of_find_node_by_name(NULL, "via-cuda");176if (vias == NULL)177vias = of_find_node_by_name(NULL, "via-pmu");178if (vias == NULL)179vias = of_find_node_by_name(NULL, "via");180if (vias == NULL || of_address_to_resource(vias, 0, &rsrc)) {181of_node_put(vias);182return 0;183}184of_node_put(vias);185via = early_ioremap(rsrc.start, resource_size(&rsrc));186if (via == NULL) {187printk(KERN_ERR "Failed to map VIA for timer calibration !\n");188return 0;189}190191/* set timer 1 for continuous interrupts */192out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);193/* set the counter to a small value */194out_8(&via[T1CH], 2);195/* set the latch to `count' */196out_8(&via[T1LL], count);197out_8(&via[T1LH], count >> 8);198/* wait until it hits 0 */199while ((in_8(&via[IFR]) & T1_INT) == 0)200;201dstart = get_dec();202/* clear the interrupt & wait until it hits 0 again */203in_8(&via[T1CL]);204while ((in_8(&via[IFR]) & T1_INT) == 0)205;206dend = get_dec();207208ppc_tb_freq = (dstart - dend) * 100 / 6;209210early_iounmap((void *)via, resource_size(&rsrc));211212return 1;213}214#endif215216/*217* Query the OF and get the decr frequency.218*/219void __init pmac_calibrate_decr(void)220{221generic_calibrate_decr();222223#ifdef CONFIG_PPC32224/* We assume MacRISC2 machines have correct device-tree225* calibration. That's better since the VIA itself seems226* to be slightly off. --BenH227*/228if (!of_machine_is_compatible("MacRISC2") &&229!of_machine_is_compatible("MacRISC3") &&230!of_machine_is_compatible("MacRISC4"))231if (via_calibrate_decr())232return;233234/* Special case: QuickSilver G4s seem to have a badly calibrated235* timebase-frequency in OF, VIA is much better on these. We should236* probably implement calibration based on the KL timer on these237* machines anyway... -BenH238*/239if (of_machine_is_compatible("PowerMac3,5"))240if (via_calibrate_decr())241return;242#endif243}244245246