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) 1997, 1998, 2001, 03, 05, 06 by Ralf Baechle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/linkage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/rtc/ds1286.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/sgialib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/sgi/ioc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/sgi/hpc3.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/sgi/mc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/sgi/ip22.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Just powerdown if init hasn't done after POWERDOWN_TIMEOUT seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * I'm not sure if this feature is a good idea, for now it's here just to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * make the power button make behave just like under IRIX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define POWERDOWN_TIMEOUT	120
^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)  * Blink frequency during reboot grace period and when panicked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define POWERDOWN_FREQ		(HZ / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define PANIC_FREQ		(HZ / 8)
^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, debounce_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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define MACHINE_PANICED		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define MACHINE_SHUTTING_DOWN	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int machine_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static void __noreturn sgi_machine_power_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	/* Disable watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	tmp = hpc3c0->rtcregs[RTC_CMD] & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	hpc3c0->rtcregs[RTC_CMD] = tmp | RTC_WAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	hpc3c0->rtcregs[RTC_WSEC] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	hpc3c0->rtcregs[RTC_WHSEC] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		sgioc->panel = ~SGIOC_PANEL_POWERON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		/* Good bye cruel world ...  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		/* If we're still running, we probably got sent an alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		   interrupt.  Read the flag to clear it.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		tmp = hpc3c0->rtcregs[RTC_HOURS_ALARM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void __noreturn sgi_machine_restart(char *command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (machine_state & MACHINE_SHUTTING_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		sgi_machine_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void __noreturn sgi_machine_halt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (machine_state & MACHINE_SHUTTING_DOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		sgi_machine_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ArcEnterInteractiveMode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static void power_timeout(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	sgi_machine_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static void blink_timeout(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/* XXX fix this for fullhouse  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	sgi_ioc_reset ^= (SGIOC_RESET_LC0OFF|SGIOC_RESET_LC1OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	sgioc->reset = sgi_ioc_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	mod_timer(&blink_timer, jiffies + blink_timer_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static void debounce(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	del_timer(&debounce_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (sgint->istat1 & SGINT_ISTAT1_PWR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		/* Interrupt still being sent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		debounce_timer.expires = jiffies + (HZ / 20); /* 0.05s	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		add_timer(&debounce_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			       SGIOC_PANEL_VOLDNINTR | SGIOC_PANEL_VOLDNHOLD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			       SGIOC_PANEL_VOLUPINTR | SGIOC_PANEL_VOLUPHOLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (machine_state & MACHINE_PANICED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		sgimc->cpuctrl0 |= SGIMC_CCTRL0_SYSINIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	enable_irq(SGI_PANEL_IRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline void power_button(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (machine_state & MACHINE_PANICED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if ((machine_state & MACHINE_SHUTTING_DOWN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			kill_cad_pid(SIGINT, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		/* No init process or button pressed twice.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		sgi_machine_power_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	machine_state |= MACHINE_SHUTTING_DOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	blink_timer_timeout = POWERDOWN_FREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	blink_timeout(&blink_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	timer_setup(&power_timer, power_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	add_timer(&power_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static irqreturn_t panel_int(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	unsigned int buttons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	buttons = sgioc->panel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	sgioc->panel = SGIOC_PANEL_POWERON | SGIOC_PANEL_POWERINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (sgint->istat1 & SGINT_ISTAT1_PWR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		/* Wait until interrupt goes away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		disable_irq_nosync(SGI_PANEL_IRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		timer_setup(&debounce_timer, debounce, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		debounce_timer.expires = jiffies + 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		add_timer(&debounce_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* Power button was pressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 * ioc.ps page 22: "The Panel Register is called Power Control by Full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * House. Only lowest 2 bits are used. Guiness uses upper four bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * for volume control". This is not true, all bits are pulled high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 * on fullhouse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (!(buttons & SGIOC_PANEL_POWERINTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		power_button();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int panic_event(struct notifier_block *this, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		      void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (machine_state & MACHINE_PANICED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	machine_state |= MACHINE_PANICED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	blink_timer_timeout = PANIC_FREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	blink_timeout(&blink_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static struct notifier_block panic_block = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	.notifier_call	= panic_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int __init reboot_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	_machine_restart = sgi_machine_restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	_machine_halt = sgi_machine_halt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	pm_power_off = sgi_machine_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	res = request_irq(SGI_PANEL_IRQ, panel_int, 0, "Front Panel", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		printk(KERN_ERR "Allocation of front panel IRQ failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	timer_setup(&blink_timer, blink_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) subsys_initcall(reboot_setup);