Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/arch/m68k/kernel/time.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 1991, 1992, 1995  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This file contains the m68k-specific time handling details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Most of the stuff is located in the machine specific files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * 1997-09-10	Updated NTP code according to technical memorandum Jan '96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *		"A Kernel Model for Precision Timekeeping" by Dave Mills
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sched/loadavg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/machdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <asm/irq_regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/profile.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) unsigned long (*mach_random_get_entropy)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) EXPORT_SYMBOL_GPL(mach_random_get_entropy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * timer_interrupt() needs to keep up the real-time clock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * as well as call the "xtime_update()" routine every clocktick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static irqreturn_t timer_interrupt(int irq, void *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	xtime_update(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	update_process_times(user_mode(get_irq_regs()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	profile_tick(CPU_PROFILING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #ifdef CONFIG_HEARTBEAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/* use power LED as a heartbeat instead -- much more useful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	   for debugging -- based on the version for PReP by Cort */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	/* acts like an actual heart beat -- ie thump-thump-pause... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (mach_heartbeat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	    static unsigned cnt = 0, period = 0, dist = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	    if (cnt == 0 || cnt == dist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		mach_heartbeat( 1 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	    else if (cnt == 7 || cnt == dist+7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		mach_heartbeat( 0 );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	    if (++cnt > period) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		/* The hyperbolic function below modifies the heartbeat period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		 * length in dependency of the current (5min) load. It goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		 * through the points f(0)=126, f(1)=86, f(5)=51,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		 * f(inf)->30. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		period = ((672<<FSHIFT)/(5*avenrun[0]+(7<<FSHIFT))) + 30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		dist = period / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	    }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #endif /* CONFIG_HEARTBEAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #ifdef CONFIG_M68KCLASSIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #if !IS_BUILTIN(CONFIG_RTC_DRV_GENERIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) void read_persistent_clock64(struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct rtc_time time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	ts->tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ts->tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (!mach_hwclk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	mach_hwclk(0, &time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ts->tv_sec = mktime64(time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			      time.tm_hour, time.tm_min, time.tm_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	mach_hwclk(0, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (mach_hwclk(1, tm) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct rtc_pll_info pll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct rtc_pll_info __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	case RTC_PLL_GET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (!mach_get_rtc_pll || mach_get_rtc_pll(&pll))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return copy_to_user(argp, &pll, sizeof pll) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	case RTC_PLL_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		if (!mach_set_rtc_pll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (!capable(CAP_SYS_TIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (copy_from_user(&pll, argp, sizeof(pll)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return mach_set_rtc_pll(&pll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static const struct rtc_class_ops generic_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	.ioctl = rtc_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.read_time = rtc_generic_get_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.set_time = rtc_generic_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int __init rtc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!mach_hwclk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	pdev = platform_device_register_data(NULL, "rtc-generic", -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 					     &generic_rtc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 					     sizeof(generic_rtc_ops));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return PTR_ERR_OR_ZERO(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) module_init(rtc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #endif /* CONFIG_RTC_DRV_GENERIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #endif /* CONFIG M68KCLASSIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) void __init time_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	mach_sched_init(timer_interrupt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }