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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * License.  See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2001 Keith M Wesolowski
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2001 Paul Mundt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/rtc/ds1685.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/addrspace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/wbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/ip32/mace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <asm/ip32/crime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <asm/ip32/ip32_ints.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define POWERDOWN_TIMEOUT	120
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * Blink frequency during reboot grace period and when panicked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define POWERDOWN_FREQ		(HZ / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define PANIC_FREQ		(HZ / 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) extern struct platform_device ip32_rtc_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static struct timer_list power_timer, blink_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static unsigned long blink_timer_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int has_panicked, shutting_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static __noreturn void ip32_poweroff(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	void (*poweroff_func)(struct platform_device *) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		symbol_get(ds1685_rtc_poweroff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #ifdef CONFIG_MODULES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/* If the first __symbol_get failed, our module wasn't loaded. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (!poweroff_func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		request_module("rtc-ds1685");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		poweroff_func = symbol_get(ds1685_rtc_poweroff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!poweroff_func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		pr_emerg("RTC not available for power-off.  Spinning forever ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		(*poweroff_func)((struct platform_device *)data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		symbol_put(ds1685_rtc_poweroff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unreachable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static void ip32_machine_restart(char *cmd) __noreturn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void ip32_machine_restart(char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	crime->control = CRIME_CONTROL_HARD_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	unreachable();
^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) static void blink_timeout(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	mace->perif.ctrl.misc = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	mod_timer(&blink_timer, jiffies + blink_timer_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static void ip32_machine_halt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	ip32_poweroff(&ip32_rtc_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static void power_timeout(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ip32_poweroff(&ip32_rtc_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) void ip32_prepare_poweroff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (has_panicked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (shutting_down || kill_cad_pid(SIGINT, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		/* No init process or button pressed twice.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		ip32_poweroff(&ip32_rtc_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	shutting_down = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	blink_timer_timeout = POWERDOWN_FREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	blink_timeout(&blink_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	timer_setup(&power_timer, power_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	add_timer(&power_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int panic_event(struct notifier_block *this, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		       void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned long led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (has_panicked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	has_panicked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	/* turn off the green LED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	mace->perif.ctrl.misc = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	blink_timer_timeout = PANIC_FREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	blink_timeout(&blink_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct notifier_block panic_block = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.notifier_call = panic_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static __init int ip32_reboot_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	/* turn on the green led only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	unsigned long led = mace->perif.ctrl.misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	led |= MACEISA_LED_RED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	led &= ~MACEISA_LED_GREEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	mace->perif.ctrl.misc = led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	_machine_restart = ip32_machine_restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	_machine_halt = ip32_machine_halt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	pm_power_off = ip32_machine_halt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	timer_setup(&blink_timer, blink_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) subsys_initcall(ip32_reboot_setup);