blob: 340ffeea0a9dcf323da1c273d5e959f1afc9281f [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Greg Ungererfa7f9e72012-01-23 16:23:33 +10002/*
3 * linux/arch/m68k/kernel/time.c
4 *
5 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 *
7 * This file contains the m68k-specific time handling details.
8 * Most of the stuff is located in the machine specific files.
9 *
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 */
13
14#include <linux/errno.h>
Geert Uytterhoeven5bc8c7c2014-07-10 18:03:17 +020015#include <linux/export.h>
Greg Ungererfa7f9e72012-01-23 16:23:33 +100016#include <linux/module.h>
17#include <linux/sched.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010018#include <linux/sched/loadavg.h>
Greg Ungererfa7f9e72012-01-23 16:23:33 +100019#include <linux/kernel.h>
20#include <linux/param.h>
21#include <linux/string.h>
22#include <linux/mm.h>
23#include <linux/rtc.h>
24#include <linux/platform_device.h>
25
26#include <asm/machdep.h>
27#include <asm/io.h>
28#include <asm/irq_regs.h>
29
30#include <linux/time.h>
31#include <linux/timex.h>
32#include <linux/profile.h>
33
Geert Uytterhoeven017cece2013-10-18 13:10:08 +020034
35unsigned long (*mach_random_get_entropy)(void);
Geert Uytterhoeven5bc8c7c2014-07-10 18:03:17 +020036EXPORT_SYMBOL_GPL(mach_random_get_entropy);
Geert Uytterhoeven017cece2013-10-18 13:10:08 +020037
Greg Ungererfa7f9e72012-01-23 16:23:33 +100038#ifdef CONFIG_HEARTBEAT
Arnd Bergmannd6444092020-09-24 16:37:37 +020039void timer_heartbeat(void)
40{
Greg Ungererfa7f9e72012-01-23 16:23:33 +100041 /* use power LED as a heartbeat instead -- much more useful
42 for debugging -- based on the version for PReP by Cort */
43 /* acts like an actual heart beat -- ie thump-thump-pause... */
44 if (mach_heartbeat) {
45 static unsigned cnt = 0, period = 0, dist = 0;
46
47 if (cnt == 0 || cnt == dist)
48 mach_heartbeat( 1 );
49 else if (cnt == 7 || cnt == dist+7)
50 mach_heartbeat( 0 );
51
52 if (++cnt > period) {
53 cnt = 0;
54 /* The hyperbolic function below modifies the heartbeat period
55 * length in dependency of the current (5min) load. It goes
56 * through the points f(0)=126, f(1)=86, f(5)=51,
57 * f(inf)->30. */
58 period = ((672<<FSHIFT)/(5*avenrun[0]+(7<<FSHIFT))) + 30;
59 dist = period / 4;
60 }
61 }
Greg Ungererfa7f9e72012-01-23 16:23:33 +100062}
Arnd Bergmannd6444092020-09-24 16:37:37 +020063#endif /* CONFIG_HEARTBEAT */
Greg Ungererfa7f9e72012-01-23 16:23:33 +100064
Arnd Bergmann065f1092018-04-23 10:52:28 +020065#ifdef CONFIG_M68KCLASSIC
66#if !IS_BUILTIN(CONFIG_RTC_DRV_GENERIC)
67void read_persistent_clock64(struct timespec64 *ts)
Greg Ungererfa7f9e72012-01-23 16:23:33 +100068{
69 struct rtc_time time;
Finn Thainb65769f2018-04-23 11:02:57 +100070
Greg Ungererfa7f9e72012-01-23 16:23:33 +100071 ts->tv_sec = 0;
72 ts->tv_nsec = 0;
73
Finn Thainb65769f2018-04-23 11:02:57 +100074 if (!mach_hwclk)
75 return;
Greg Ungererfa7f9e72012-01-23 16:23:33 +100076
Finn Thainb65769f2018-04-23 11:02:57 +100077 mach_hwclk(0, &time);
78
Arnd Bergmann065f1092018-04-23 10:52:28 +020079 ts->tv_sec = mktime64(time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
80 time.tm_hour, time.tm_min, time.tm_sec);
Greg Ungererfa7f9e72012-01-23 16:23:33 +100081}
Arnd Bergmann065f1092018-04-23 10:52:28 +020082#endif
Greg Ungererfa7f9e72012-01-23 16:23:33 +100083
Arnd Bergmann065f1092018-04-23 10:52:28 +020084#if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
Arnd Bergmann923c9042016-05-30 20:57:57 +020085static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
86{
87 mach_hwclk(0, tm);
Alexandre Belloni6efe2642018-02-21 22:37:24 +010088 return 0;
Arnd Bergmann923c9042016-05-30 20:57:57 +020089}
90
91static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
92{
93 if (mach_hwclk(1, tm) < 0)
94 return -EOPNOTSUPP;
95 return 0;
96}
97
Arnd Bergmann598e8a12016-05-30 20:57:58 +020098static int rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
99{
100 struct rtc_pll_info pll;
101 struct rtc_pll_info __user *argp = (void __user *)arg;
102
103 switch (cmd) {
104 case RTC_PLL_GET:
105 if (!mach_get_rtc_pll || mach_get_rtc_pll(&pll))
106 return -EINVAL;
107 return copy_to_user(argp, &pll, sizeof pll) ? -EFAULT : 0;
108
109 case RTC_PLL_SET:
110 if (!mach_set_rtc_pll)
111 return -EINVAL;
112 if (!capable(CAP_SYS_TIME))
113 return -EACCES;
114 if (copy_from_user(&pll, argp, sizeof(pll)))
115 return -EFAULT;
116 return mach_set_rtc_pll(&pll);
117 }
118
119 return -ENOIOCTLCMD;
120}
121
Arnd Bergmann923c9042016-05-30 20:57:57 +0200122static const struct rtc_class_ops generic_rtc_ops = {
Arnd Bergmann598e8a12016-05-30 20:57:58 +0200123 .ioctl = rtc_ioctl,
Arnd Bergmann923c9042016-05-30 20:57:57 +0200124 .read_time = rtc_generic_get_time,
125 .set_time = rtc_generic_set_time,
126};
Greg Ungererfa7f9e72012-01-23 16:23:33 +1000127
Greg Ungererfa7f9e72012-01-23 16:23:33 +1000128static int __init rtc_init(void)
129{
130 struct platform_device *pdev;
131
132 if (!mach_hwclk)
133 return -ENODEV;
134
Arnd Bergmann923c9042016-05-30 20:57:57 +0200135 pdev = platform_device_register_data(NULL, "rtc-generic", -1,
136 &generic_rtc_ops,
137 sizeof(generic_rtc_ops));
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930138 return PTR_ERR_OR_ZERO(pdev);
Greg Ungererfa7f9e72012-01-23 16:23:33 +1000139}
140
141module_init(rtc_init);
Arnd Bergmann065f1092018-04-23 10:52:28 +0200142#endif /* CONFIG_RTC_DRV_GENERIC */
143#endif /* CONFIG M68KCLASSIC */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700144
145void __init time_init(void)
146{
Arnd Bergmannf9a01532020-09-24 17:29:17 +0200147 mach_sched_init();
Stephen Warren7b1f6202012-11-07 17:58:54 -0700148}