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>
40f39650deSAndy Shevchenko #include <linux/panic_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 */
process_shutdown(void)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 */
98c14b302cSHelge Deller machine_power_off();
996e16d940SHelge Deller }
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds
1046e16d940SHelge Deller /* main power switch task struct */
1056e16d940SHelge Deller static struct task_struct *power_task;
1066e16d940SHelge Deller
1076e16d940SHelge Deller /* filename in /proc which can be used to enable/disable the power switch */
1086e16d940SHelge Deller #define SYSCTL_FILENAME "sys/kernel/power"
1091da177e4SLinus Torvalds
1101da177e4SLinus Torvalds /* soft power switch enabled/disabled */
1118039de10SHelge Deller int pwrsw_enabled __read_mostly = 1;
1121da177e4SLinus Torvalds
1136e16d940SHelge Deller /* main kernel thread worker. It polls the button state */
kpowerswd(void * param)1146e16d940SHelge Deller static int kpowerswd(void *param)
1156e16d940SHelge Deller {
1166e16d940SHelge Deller __set_current_state(TASK_RUNNING);
1176e16d940SHelge Deller
1186e16d940SHelge Deller do {
1196e16d940SHelge Deller int button_not_pressed;
1206e16d940SHelge Deller unsigned long soft_power_reg = (unsigned long) param;
1216e16d940SHelge Deller
1226e16d940SHelge Deller schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC);
1236e16d940SHelge Deller
1246e16d940SHelge Deller if (unlikely(!pwrsw_enabled))
1256e16d940SHelge Deller continue;
1266e16d940SHelge Deller
1276e16d940SHelge Deller if (soft_power_reg) {
1286e16d940SHelge Deller /*
1296e16d940SHelge Deller * Non-Gecko-style machines:
1306e16d940SHelge Deller * Check the power switch status which is read from the
1316e16d940SHelge Deller * real I/O location at soft_power_reg.
1326e16d940SHelge Deller * Bit 31 ("the lowest bit) is the status of the power switch.
1336e16d940SHelge Deller * This bit is "1" if the button is NOT pressed.
1346e16d940SHelge Deller */
1356e16d940SHelge Deller button_not_pressed = (gsc_readl(soft_power_reg) & 0x1);
1366e16d940SHelge Deller } else {
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds * On gecko style machines (e.g. 712/xx and 715/xx)
1391da177e4SLinus Torvalds * the power switch status is stored in Bit 0 ("the highest bit")
1401da177e4SLinus Torvalds * of CPU diagnose register 25.
1416e16d940SHelge Deller * Warning: Some machines never reset the DIAG flag, even if
1426e16d940SHelge Deller * the button has been released again.
1431da177e4SLinus Torvalds */
1446e16d940SHelge Deller button_not_pressed = (__getDIAG(25) & 0x80000000);
1456e16d940SHelge Deller }
1461da177e4SLinus Torvalds
1476e16d940SHelge Deller if (likely(button_not_pressed)) {
1486e16d940SHelge Deller if (unlikely(shutdown_timer && /* avoid writing if not necessary */
1496e16d940SHelge Deller shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) {
1501da177e4SLinus Torvalds shutdown_timer = 0;
1516e16d940SHelge Deller printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n");
1526e16d940SHelge Deller }
1536e16d940SHelge Deller } else
1541da177e4SLinus Torvalds process_shutdown();
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds
1576e16d940SHelge Deller } while (!kthread_should_stop());
1581da177e4SLinus Torvalds
1596e16d940SHelge Deller return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvalds
1631da177e4SLinus Torvalds /*
1641da177e4SLinus Torvalds * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2)
1651da177e4SLinus Torvalds */
1661da177e4SLinus Torvalds #if 0
1677d12e780SDavid Howells static void powerfail_interrupt(int code, void *x)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n");
1701da177e4SLinus Torvalds poweroff();
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds #endif
1731da177e4SLinus Torvalds
1741da177e4SLinus Torvalds
1751da177e4SLinus Torvalds
1761da177e4SLinus Torvalds
177829632daSGuilherme G. Piccoli /*
178829632daSGuilherme G. Piccoli * parisc_panic_event() is called by the panic handler.
179829632daSGuilherme G. Piccoli *
180829632daSGuilherme G. Piccoli * As soon as a panic occurs, our tasklets above will not
181829632daSGuilherme G. Piccoli * be executed any longer. This function then re-enables
182829632daSGuilherme G. Piccoli * the soft-power switch and allows the user to switch off
183829632daSGuilherme G. Piccoli * the system. We rely in pdc_soft_power_button_panic()
184829632daSGuilherme G. Piccoli * since this version spin_trylocks (instead of regular
185829632daSGuilherme G. Piccoli * spinlock), preventing deadlocks on panic path.
1861da177e4SLinus Torvalds */
parisc_panic_event(struct notifier_block * this,unsigned long event,void * ptr)1871da177e4SLinus Torvalds static int parisc_panic_event(struct notifier_block *this,
1881da177e4SLinus Torvalds unsigned long event, void *ptr)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds /* re-enable the soft-power switch */
191829632daSGuilherme G. Piccoli pdc_soft_power_button_panic(0);
1921da177e4SLinus Torvalds return NOTIFY_DONE;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds
1951da177e4SLinus Torvalds static struct notifier_block parisc_panic_block = {
1961da177e4SLinus Torvalds .notifier_call = parisc_panic_event,
1971da177e4SLinus Torvalds .priority = INT_MAX,
1981da177e4SLinus Torvalds };
1991da177e4SLinus Torvalds
20047b81664SHelge Deller /* qemu soft power-off function */
qemu_power_off(struct sys_off_data * data)20147b81664SHelge Deller static int qemu_power_off(struct sys_off_data *data)
20247b81664SHelge Deller {
20347b81664SHelge Deller /* this turns the system off via SeaBIOS */
204c0940ebeSHelge Deller gsc_writel(0, (unsigned long) data->cb_data);
20547b81664SHelge Deller pdc_soft_power_button(1);
20647b81664SHelge Deller return NOTIFY_DONE;
20747b81664SHelge Deller }
2081da177e4SLinus Torvalds
power_init(void)2091da177e4SLinus Torvalds static int __init power_init(void)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds unsigned long ret;
2126e16d940SHelge Deller unsigned long soft_power_reg;
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds #if 0
2151da177e4SLinus Torvalds request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt,
2161da177e4SLinus Torvalds 0, "powerfail", NULL);
2171da177e4SLinus Torvalds #endif
2181da177e4SLinus Torvalds
2191da177e4SLinus Torvalds /* enable the soft power switch if possible */
2201da177e4SLinus Torvalds ret = pdc_soft_power_info(&soft_power_reg);
2211da177e4SLinus Torvalds if (ret == PDC_OK)
2221da177e4SLinus Torvalds ret = pdc_soft_power_button(1);
2231da177e4SLinus Torvalds if (ret != PDC_OK)
2241da177e4SLinus Torvalds soft_power_reg = -1UL;
2251da177e4SLinus Torvalds
2261da177e4SLinus Torvalds switch (soft_power_reg) {
2276e16d940SHelge Deller case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n");
2281da177e4SLinus Torvalds break;
2291da177e4SLinus Torvalds
2306e16d940SHelge Deller case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n");
2311da177e4SLinus Torvalds return -ENODEV;
2321da177e4SLinus Torvalds
2336e16d940SHelge Deller default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n",
2341da177e4SLinus Torvalds soft_power_reg);
2356e16d940SHelge Deller }
2366e16d940SHelge Deller
23747b81664SHelge Deller power_task = NULL;
23847b81664SHelge Deller if (running_on_qemu && soft_power_reg)
23947b81664SHelge Deller register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT,
24047b81664SHelge Deller qemu_power_off, (void *)soft_power_reg);
241*46942efbSHelge Deller if (!running_on_qemu || soft_power_reg)
24247b81664SHelge Deller power_task = kthread_run(kpowerswd, (void*)soft_power_reg,
24347b81664SHelge Deller KTHREAD_NAME);
2446e16d940SHelge Deller if (IS_ERR(power_task)) {
2456e16d940SHelge Deller printk(KERN_ERR DRIVER_NAME ": thread creation failed. Driver not loaded.\n");
2466e16d940SHelge Deller pdc_soft_power_button(0);
2476e16d940SHelge Deller return -EIO;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds
2501da177e4SLinus Torvalds /* Register a call for panic conditions. */
251e041c683SAlan Stern atomic_notifier_chain_register(&panic_notifier_list,
252e041c683SAlan Stern &parisc_panic_block);
2531da177e4SLinus Torvalds
2541da177e4SLinus Torvalds return 0;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds
power_exit(void)2571da177e4SLinus Torvalds static void __exit power_exit(void)
2581da177e4SLinus Torvalds {
2596e16d940SHelge Deller kthread_stop(power_task);
2601da177e4SLinus Torvalds
261e041c683SAlan Stern atomic_notifier_chain_unregister(&panic_notifier_list,
262e041c683SAlan Stern &parisc_panic_block);
2636e16d940SHelge Deller
2641da177e4SLinus Torvalds pdc_soft_power_button(0);
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds
2676e16d940SHelge Deller arch_initcall(power_init);
2681da177e4SLinus Torvalds module_exit(power_exit);
2691da177e4SLinus Torvalds
2701da177e4SLinus Torvalds
2716e16d940SHelge Deller MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
2721da177e4SLinus Torvalds MODULE_DESCRIPTION("Soft power switch driver");
2731da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
274