xref: /openbmc/linux/drivers/parisc/power.c (revision 3f07c014)
11da177e4SLinus Torvalds /*
2f30c2269SUwe Zeisberger  * linux/drivers/parisc/power.c
31da177e4SLinus Torvalds  * HP PARISC soft power switch support driver
41da177e4SLinus Torvalds  *
56e16d940SHelge Deller  * Copyright (c) 2001-2007 Helge Deller <deller@gmx.de>
61da177e4SLinus Torvalds  * All rights reserved.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Redistribution and use in source and binary forms, with or without
101da177e4SLinus Torvalds  * modification, are permitted provided that the following conditions
111da177e4SLinus Torvalds  * are met:
121da177e4SLinus Torvalds  * 1. Redistributions of source code must retain the above copyright
131da177e4SLinus Torvalds  *    notice, this list of conditions, and the following disclaimer,
141da177e4SLinus Torvalds  *    without modification.
151da177e4SLinus Torvalds  * 2. The name of the author may not be used to endorse or promote products
161da177e4SLinus Torvalds  *    derived from this software without specific prior written permission.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Alternatively, this software may be distributed under the terms of the
191da177e4SLinus Torvalds  * GNU General Public License ("GPL").
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
221da177e4SLinus Torvalds  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231da177e4SLinus Torvalds  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241da177e4SLinus Torvalds  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
251da177e4SLinus Torvalds  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261da177e4SLinus Torvalds  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271da177e4SLinus Torvalds  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281da177e4SLinus Torvalds  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291da177e4SLinus Torvalds  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  *  HINT:
331da177e4SLinus Torvalds  *  Support of the soft power switch button may be enabled or disabled at
341da177e4SLinus Torvalds  *  runtime through the "/proc/sys/kernel/power" procfs entry.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/init.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/notifier.h>
411da177e4SLinus Torvalds #include <linux/reboot.h>
423f07c014SIngo Molnar #include <linux/sched/signal.h>
436e16d940SHelge Deller #include <linux/kthread.h>
445c04ec74SKyle McMartin #include <linux/pm.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <asm/pdc.h>
471da177e4SLinus Torvalds #include <asm/io.h>
481da177e4SLinus Torvalds #include <asm/led.h>
491da177e4SLinus Torvalds 
506e16d940SHelge Deller #define DRIVER_NAME  "powersw"
516e16d940SHelge Deller #define KTHREAD_NAME "kpowerswd"
521da177e4SLinus Torvalds 
536e16d940SHelge Deller /* how often should the power button be polled ? */
546e16d940SHelge Deller #define POWERSWITCH_POLL_PER_SEC 2
551da177e4SLinus Torvalds 
566e16d940SHelge Deller /* how long does the power button needs to be down until we react ? */
576e16d940SHelge Deller #define POWERSWITCH_DOWN_SEC 2
581da177e4SLinus Torvalds 
596e16d940SHelge Deller /* assembly code to access special registers */
606e16d940SHelge Deller /* taken from PCXL ERS page 82 */
611da177e4SLinus Torvalds #define DIAG_CODE(code)		(0x14000000 + ((code)<<5))
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define MFCPU_X(rDiagReg, t_ch, t_th, code) \
641da177e4SLinus Torvalds 	(DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) )
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define MTCPU(dr, gr)		MFCPU_X(dr, gr,  0, 0x12)       /* move value of gr to dr[dr] */
671da177e4SLinus Torvalds #define MFCPU_C(dr, gr)		MFCPU_X(dr, gr,  0, 0x30)	/* for dr0 and dr8 only ! */
681da177e4SLinus Torvalds #define MFCPU_T(dr, gr)		MFCPU_X(dr,  0, gr, 0xa0)	/* all dr except dr0 and dr8 */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define __getDIAG(dr) ( { 			\
711da177e4SLinus Torvalds         register unsigned long __res asm("r28");\
721da177e4SLinus Torvalds 	 __asm__ __volatile__ (			\
736e16d940SHelge Deller 		".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \
741da177e4SLinus Torvalds 	);					\
751da177e4SLinus Torvalds 	__res;					\
761da177e4SLinus Torvalds } )
771da177e4SLinus Torvalds 
786e16d940SHelge Deller /* local shutdown counter */
798039de10SHelge Deller static int shutdown_timer __read_mostly;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /* check, give feedback and start shutdown after one second */
821da177e4SLinus Torvalds static void process_shutdown(void)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	if (shutdown_timer == 0)
856e16d940SHelge Deller 		printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n");
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	shutdown_timer++;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/* wait until the button was pressed for 1 second */
906e16d940SHelge Deller 	if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) {
916e16d940SHelge Deller 		static const char msg[] = "Shutting down...";
926e16d940SHelge Deller 		printk(KERN_INFO KTHREAD_NAME ": %s\n", msg);
931da177e4SLinus Torvalds 		lcd_print(msg);
946e16d940SHelge Deller 
956e16d940SHelge Deller 		/* send kill signal */
966e16d940SHelge Deller 		if (kill_cad_pid(SIGINT, 1)) {
976e16d940SHelge Deller 			/* just in case killing init process failed */
986e16d940SHelge Deller 			if (pm_power_off)
996e16d940SHelge Deller 				pm_power_off();
1006e16d940SHelge Deller 		}
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 
1056e16d940SHelge Deller /* main power switch task struct */
1066e16d940SHelge Deller static struct task_struct *power_task;
1076e16d940SHelge Deller 
1086e16d940SHelge Deller /* filename in /proc which can be used to enable/disable the power switch */
1096e16d940SHelge Deller #define SYSCTL_FILENAME	"sys/kernel/power"
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* soft power switch enabled/disabled */
1128039de10SHelge Deller int pwrsw_enabled __read_mostly = 1;
1131da177e4SLinus Torvalds 
1146e16d940SHelge Deller /* main kernel thread worker. It polls the button state */
1156e16d940SHelge Deller static int kpowerswd(void *param)
1166e16d940SHelge Deller {
1176e16d940SHelge Deller 	__set_current_state(TASK_RUNNING);
1186e16d940SHelge Deller 
1196e16d940SHelge Deller 	do {
1206e16d940SHelge Deller 		int button_not_pressed;
1216e16d940SHelge Deller 		unsigned long soft_power_reg = (unsigned long) param;
1226e16d940SHelge Deller 
1236e16d940SHelge Deller 		schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
1246e16d940SHelge Deller 
1256e16d940SHelge Deller 		if (unlikely(!pwrsw_enabled))
1266e16d940SHelge Deller 			continue;
1276e16d940SHelge Deller 
1286e16d940SHelge Deller 		if (soft_power_reg) {
1296e16d940SHelge Deller 			/*
1306e16d940SHelge Deller 			 * Non-Gecko-style machines:
1316e16d940SHelge Deller 			 * Check the power switch status which is read from the
1326e16d940SHelge Deller 			 * real I/O location at soft_power_reg.
1336e16d940SHelge Deller 			 * Bit 31 ("the lowest bit) is the status of the power switch.
1346e16d940SHelge Deller 			 * This bit is "1" if the button is NOT pressed.
1356e16d940SHelge Deller 			 */
1366e16d940SHelge Deller 			button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
1376e16d940SHelge Deller 		} else {
1381da177e4SLinus Torvalds 			/*
1391da177e4SLinus Torvalds 			 * On gecko style machines (e.g. 712/xx and 715/xx)
1401da177e4SLinus Torvalds 			 * the power switch status is stored in Bit 0 ("the highest bit")
1411da177e4SLinus Torvalds 			 * of CPU diagnose register 25.
1426e16d940SHelge Deller 			 * Warning: Some machines never reset the DIAG flag, even if
1436e16d940SHelge Deller 			 * the button has been released again.
1441da177e4SLinus Torvalds 			 */
1456e16d940SHelge Deller 			button_not_pressed = (__getDIAG(25) & 0x80000000);
1466e16d940SHelge Deller 		}
1471da177e4SLinus Torvalds 
1486e16d940SHelge Deller 		if (likely(button_not_pressed)) {
1496e16d940SHelge Deller 			if (unlikely(shutdown_timer && /* avoid writing if not necessary */
1506e16d940SHelge Deller 				shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
1511da177e4SLinus Torvalds 				shutdown_timer = 0;
1526e16d940SHelge Deller 				printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
1536e16d940SHelge Deller 			}
1546e16d940SHelge Deller 		} else
1551da177e4SLinus Torvalds 			process_shutdown();
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 
1586e16d940SHelge Deller 	} while (!kthread_should_stop());
1591da177e4SLinus Torvalds 
1606e16d940SHelge Deller 	return 0;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds #if 0
1687d12e780SDavid Howells static void powerfail_interrupt(int code, void *x)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
1711da177e4SLinus Torvalds 	poweroff();
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds #endif
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* parisc_panic_event() is called by the panic handler.
1791da177e4SLinus Torvalds  * As soon as a panic occurs, our tasklets above will not be
1801da177e4SLinus Torvalds  * executed any longer. This function then re-enables the
1811da177e4SLinus Torvalds  * soft-power switch and allows the user to switch off the system
1821da177e4SLinus Torvalds  */
1831da177e4SLinus Torvalds static int parisc_panic_event(struct notifier_block *this,
1841da177e4SLinus Torvalds 		unsigned long event, void *ptr)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	/* re-enable the soft-power switch */
1871da177e4SLinus Torvalds 	pdc_soft_power_button(0);
1881da177e4SLinus Torvalds 	return NOTIFY_DONE;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds static struct notifier_block parisc_panic_block = {
1921da177e4SLinus Torvalds 	.notifier_call	= parisc_panic_event,
1931da177e4SLinus Torvalds 	.priority	= INT_MAX,
1941da177e4SLinus Torvalds };
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static int __init power_init(void)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	unsigned long ret;
2006e16d940SHelge Deller 	unsigned long soft_power_reg;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds #if 0
2031da177e4SLinus Torvalds 	request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
2041da177e4SLinus Torvalds 		0, "powerfail", NULL);
2051da177e4SLinus Torvalds #endif
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* enable the soft power switch if possible */
2081da177e4SLinus Torvalds 	ret = pdc_soft_power_info(&soft_power_reg);
2091da177e4SLinus Torvalds 	if (ret == PDC_OK)
2101da177e4SLinus Torvalds 		ret = pdc_soft_power_button(1);
2111da177e4SLinus Torvalds 	if (ret != PDC_OK)
2121da177e4SLinus Torvalds 		soft_power_reg = -1UL;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	switch (soft_power_reg) {
2156e16d940SHelge Deller 	case 0:		printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
2161da177e4SLinus Torvalds 			break;
2171da177e4SLinus Torvalds 
2186e16d940SHelge Deller 	case -1UL:	printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
2191da177e4SLinus Torvalds 			return -ENODEV;
2201da177e4SLinus Torvalds 
2216e16d940SHelge Deller 	default:	printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
2221da177e4SLinus Torvalds 				soft_power_reg);
2236e16d940SHelge Deller 	}
2246e16d940SHelge Deller 
2256e16d940SHelge Deller 	power_task = kthread_run(kpowerswd, (void*)soft_power_reg, KTHREAD_NAME);
2266e16d940SHelge Deller 	if (IS_ERR(power_task)) {
2276e16d940SHelge Deller 		printk(KERN_ERR DRIVER_NAME ": thread creation failed.  Driver not loaded.\n");
2286e16d940SHelge Deller 		pdc_soft_power_button(0);
2296e16d940SHelge Deller 		return -EIO;
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/* Register a call for panic conditions. */
233e041c683SAlan Stern 	atomic_notifier_chain_register(&panic_notifier_list,
234e041c683SAlan Stern 			&parisc_panic_block);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	return 0;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static void __exit power_exit(void)
2401da177e4SLinus Torvalds {
2416e16d940SHelge Deller 	kthread_stop(power_task);
2421da177e4SLinus Torvalds 
243e041c683SAlan Stern 	atomic_notifier_chain_unregister(&panic_notifier_list,
244e041c683SAlan Stern 			&parisc_panic_block);
2456e16d940SHelge Deller 
2461da177e4SLinus Torvalds 	pdc_soft_power_button(0);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2496e16d940SHelge Deller arch_initcall(power_init);
2501da177e4SLinus Torvalds module_exit(power_exit);
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 
2536e16d940SHelge Deller MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
2541da177e4SLinus Torvalds MODULE_DESCRIPTION("Soft power switch driver");
2551da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
256