1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fa7f9e7fSGreg Ungerer /*
3fa7f9e7fSGreg Ungerer * linux/arch/m68k/kernel/time.c
4fa7f9e7fSGreg Ungerer *
5fa7f9e7fSGreg Ungerer * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6fa7f9e7fSGreg Ungerer *
7fa7f9e7fSGreg Ungerer * This file contains the m68k-specific time handling details.
8fa7f9e7fSGreg Ungerer * Most of the stuff is located in the machine specific files.
9fa7f9e7fSGreg Ungerer *
10fa7f9e7fSGreg Ungerer * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11fa7f9e7fSGreg Ungerer * "A Kernel Model for Precision Timekeeping" by Dave Mills
12fa7f9e7fSGreg Ungerer */
13fa7f9e7fSGreg Ungerer
14fa7f9e7fSGreg Ungerer #include <linux/errno.h>
155bc8c7cdSGeert Uytterhoeven #include <linux/export.h>
16fa7f9e7fSGreg Ungerer #include <linux/module.h>
17fa7f9e7fSGreg Ungerer #include <linux/sched.h>
184f17722cSIngo Molnar #include <linux/sched/loadavg.h>
19fa7f9e7fSGreg Ungerer #include <linux/kernel.h>
20fa7f9e7fSGreg Ungerer #include <linux/param.h>
21fa7f9e7fSGreg Ungerer #include <linux/string.h>
22fa7f9e7fSGreg Ungerer #include <linux/mm.h>
23fa7f9e7fSGreg Ungerer #include <linux/rtc.h>
24fa7f9e7fSGreg Ungerer #include <linux/platform_device.h>
25fa7f9e7fSGreg Ungerer
26fa7f9e7fSGreg Ungerer #include <asm/machdep.h>
27fa7f9e7fSGreg Ungerer #include <asm/io.h>
28fa7f9e7fSGreg Ungerer #include <asm/irq_regs.h>
29fa7f9e7fSGreg Ungerer
30fa7f9e7fSGreg Ungerer #include <linux/time.h>
31fa7f9e7fSGreg Ungerer #include <linux/timex.h>
32fa7f9e7fSGreg Ungerer #include <linux/profile.h>
33fa7f9e7fSGreg Ungerer
34017ceceeSGeert Uytterhoeven
35017ceceeSGeert Uytterhoeven unsigned long (*mach_random_get_entropy)(void);
365bc8c7cdSGeert Uytterhoeven EXPORT_SYMBOL_GPL(mach_random_get_entropy);
37017ceceeSGeert Uytterhoeven
38fa7f9e7fSGreg Ungerer #ifdef CONFIG_HEARTBEAT
timer_heartbeat(void)39d6444094SArnd Bergmann void timer_heartbeat(void)
40d6444094SArnd Bergmann {
41fa7f9e7fSGreg Ungerer /* use power LED as a heartbeat instead -- much more useful
42fa7f9e7fSGreg Ungerer for debugging -- based on the version for PReP by Cort */
43fa7f9e7fSGreg Ungerer /* acts like an actual heart beat -- ie thump-thump-pause... */
44fa7f9e7fSGreg Ungerer if (mach_heartbeat) {
45fa7f9e7fSGreg Ungerer static unsigned cnt = 0, period = 0, dist = 0;
46fa7f9e7fSGreg Ungerer
47fa7f9e7fSGreg Ungerer if (cnt == 0 || cnt == dist)
48fa7f9e7fSGreg Ungerer mach_heartbeat( 1 );
49fa7f9e7fSGreg Ungerer else if (cnt == 7 || cnt == dist+7)
50fa7f9e7fSGreg Ungerer mach_heartbeat( 0 );
51fa7f9e7fSGreg Ungerer
52fa7f9e7fSGreg Ungerer if (++cnt > period) {
53fa7f9e7fSGreg Ungerer cnt = 0;
54fa7f9e7fSGreg Ungerer /* The hyperbolic function below modifies the heartbeat period
55fa7f9e7fSGreg Ungerer * length in dependency of the current (5min) load. It goes
56fa7f9e7fSGreg Ungerer * through the points f(0)=126, f(1)=86, f(5)=51,
57fa7f9e7fSGreg Ungerer * f(inf)->30. */
58fa7f9e7fSGreg Ungerer period = ((672<<FSHIFT)/(5*avenrun[0]+(7<<FSHIFT))) + 30;
59fa7f9e7fSGreg Ungerer dist = period / 4;
60fa7f9e7fSGreg Ungerer }
61fa7f9e7fSGreg Ungerer }
62fa7f9e7fSGreg Ungerer }
63d6444094SArnd Bergmann #endif /* CONFIG_HEARTBEAT */
64fa7f9e7fSGreg Ungerer
65065f109fSArnd Bergmann #ifdef CONFIG_M68KCLASSIC
661300eec9SGreg Ungerer /* machine dependent timer functions */
671300eec9SGreg Ungerer int (*mach_hwclk) (int, struct rtc_time*);
681300eec9SGreg Ungerer EXPORT_SYMBOL(mach_hwclk);
691300eec9SGreg Ungerer
701300eec9SGreg Ungerer int (*mach_get_rtc_pll)(struct rtc_pll_info *);
711300eec9SGreg Ungerer int (*mach_set_rtc_pll)(struct rtc_pll_info *);
721300eec9SGreg Ungerer EXPORT_SYMBOL(mach_get_rtc_pll);
731300eec9SGreg Ungerer EXPORT_SYMBOL(mach_set_rtc_pll);
741300eec9SGreg Ungerer
75065f109fSArnd Bergmann #if !IS_BUILTIN(CONFIG_RTC_DRV_GENERIC)
read_persistent_clock64(struct timespec64 * ts)76065f109fSArnd Bergmann void read_persistent_clock64(struct timespec64 *ts)
77fa7f9e7fSGreg Ungerer {
78fa7f9e7fSGreg Ungerer struct rtc_time time;
79b65769fcSFinn Thain
80fa7f9e7fSGreg Ungerer ts->tv_sec = 0;
81fa7f9e7fSGreg Ungerer ts->tv_nsec = 0;
82fa7f9e7fSGreg Ungerer
83b65769fcSFinn Thain if (!mach_hwclk)
84b65769fcSFinn Thain return;
85b65769fcSFinn Thain
86fa7f9e7fSGreg Ungerer mach_hwclk(0, &time);
87fa7f9e7fSGreg Ungerer
88065f109fSArnd Bergmann ts->tv_sec = mktime64(time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
89fa7f9e7fSGreg Ungerer time.tm_hour, time.tm_min, time.tm_sec);
90fa7f9e7fSGreg Ungerer }
91065f109fSArnd Bergmann #endif
92fa7f9e7fSGreg Ungerer
93065f109fSArnd Bergmann #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
rtc_generic_get_time(struct device * dev,struct rtc_time * tm)94923c904cSArnd Bergmann static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
95923c904cSArnd Bergmann {
96923c904cSArnd Bergmann mach_hwclk(0, tm);
976efe2649SAlexandre Belloni return 0;
98923c904cSArnd Bergmann }
99923c904cSArnd Bergmann
rtc_generic_set_time(struct device * dev,struct rtc_time * tm)100923c904cSArnd Bergmann static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
101923c904cSArnd Bergmann {
102923c904cSArnd Bergmann if (mach_hwclk(1, tm) < 0)
103923c904cSArnd Bergmann return -EOPNOTSUPP;
104923c904cSArnd Bergmann return 0;
105923c904cSArnd Bergmann }
106923c904cSArnd Bergmann
rtc_ioctl(struct device * dev,unsigned int cmd,unsigned long arg)107598e8a1fSArnd Bergmann static int rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
108598e8a1fSArnd Bergmann {
109598e8a1fSArnd Bergmann struct rtc_pll_info pll;
110598e8a1fSArnd Bergmann struct rtc_pll_info __user *argp = (void __user *)arg;
111598e8a1fSArnd Bergmann
112598e8a1fSArnd Bergmann switch (cmd) {
113598e8a1fSArnd Bergmann case RTC_PLL_GET:
114598e8a1fSArnd Bergmann if (!mach_get_rtc_pll || mach_get_rtc_pll(&pll))
115598e8a1fSArnd Bergmann return -EINVAL;
116598e8a1fSArnd Bergmann return copy_to_user(argp, &pll, sizeof pll) ? -EFAULT : 0;
117598e8a1fSArnd Bergmann
118598e8a1fSArnd Bergmann case RTC_PLL_SET:
119598e8a1fSArnd Bergmann if (!mach_set_rtc_pll)
120598e8a1fSArnd Bergmann return -EINVAL;
121598e8a1fSArnd Bergmann if (!capable(CAP_SYS_TIME))
122598e8a1fSArnd Bergmann return -EACCES;
123598e8a1fSArnd Bergmann if (copy_from_user(&pll, argp, sizeof(pll)))
124598e8a1fSArnd Bergmann return -EFAULT;
125598e8a1fSArnd Bergmann return mach_set_rtc_pll(&pll);
126598e8a1fSArnd Bergmann }
127598e8a1fSArnd Bergmann
128598e8a1fSArnd Bergmann return -ENOIOCTLCMD;
129598e8a1fSArnd Bergmann }
130598e8a1fSArnd Bergmann
131923c904cSArnd Bergmann static const struct rtc_class_ops generic_rtc_ops = {
132598e8a1fSArnd Bergmann .ioctl = rtc_ioctl,
133923c904cSArnd Bergmann .read_time = rtc_generic_get_time,
134923c904cSArnd Bergmann .set_time = rtc_generic_set_time,
135923c904cSArnd Bergmann };
136fa7f9e7fSGreg Ungerer
rtc_init(void)137fa7f9e7fSGreg Ungerer static int __init rtc_init(void)
138fa7f9e7fSGreg Ungerer {
139fa7f9e7fSGreg Ungerer struct platform_device *pdev;
140fa7f9e7fSGreg Ungerer
141fa7f9e7fSGreg Ungerer if (!mach_hwclk)
142fa7f9e7fSGreg Ungerer return -ENODEV;
143fa7f9e7fSGreg Ungerer
144923c904cSArnd Bergmann pdev = platform_device_register_data(NULL, "rtc-generic", -1,
145923c904cSArnd Bergmann &generic_rtc_ops,
146923c904cSArnd Bergmann sizeof(generic_rtc_ops));
1478c6ffba0SRusty Russell return PTR_ERR_OR_ZERO(pdev);
148fa7f9e7fSGreg Ungerer }
149fa7f9e7fSGreg Ungerer
150fa7f9e7fSGreg Ungerer module_init(rtc_init);
151065f109fSArnd Bergmann #endif /* CONFIG_RTC_DRV_GENERIC */
152065f109fSArnd Bergmann #endif /* CONFIG M68KCLASSIC */
1537b1f6207SStephen Warren
time_init(void)1547b1f6207SStephen Warren void __init time_init(void)
1557b1f6207SStephen Warren {
156f9a01539SArnd Bergmann mach_sched_init();
1577b1f6207SStephen Warren }
158