11da177e4SLinus Torvalds /* 2f30c2269SUwe Zeisberger * linux/drivers/parisc/power.c 31da177e4SLinus Torvalds * HP PARISC soft power switch support driver 41da177e4SLinus Torvalds * 58039de10SHelge Deller * Copyright (c) 2001-2005 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 * 331da177e4SLinus Torvalds * HINT: 341da177e4SLinus Torvalds * Support of the soft power switch button may be enabled or disabled at 351da177e4SLinus Torvalds * runtime through the "/proc/sys/kernel/power" procfs entry. 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <linux/module.h> 391da177e4SLinus Torvalds #include <linux/init.h> 401da177e4SLinus Torvalds #include <linux/kernel.h> 411da177e4SLinus Torvalds #include <linux/string.h> 421da177e4SLinus Torvalds #include <linux/notifier.h> 431da177e4SLinus Torvalds #include <linux/reboot.h> 441da177e4SLinus Torvalds #include <linux/sched.h> 451da177e4SLinus Torvalds #include <linux/interrupt.h> 461da177e4SLinus Torvalds #include <linux/workqueue.h> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <asm/pdc.h> 491da177e4SLinus Torvalds #include <asm/io.h> 501da177e4SLinus Torvalds #include <asm/led.h> 511da177e4SLinus Torvalds #include <asm/uaccess.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #ifdef DEBUG 551da177e4SLinus Torvalds # define DPRINTK(x...) printk(x) 561da177e4SLinus Torvalds #else 571da177e4SLinus Torvalds # define DPRINTK(x...) 581da177e4SLinus Torvalds #endif 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* filename in /proc which can be used to enable/disable the power switch */ 621da177e4SLinus Torvalds #define SYSCTL_FILENAME "sys/kernel/power" 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define DIAG_CODE(code) (0x14000000 + ((code)<<5)) 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* this will go to processor.h or any other place... */ 681da177e4SLinus Torvalds /* taken from PCXL ERS page 82 */ 691da177e4SLinus Torvalds #define MFCPU_X(rDiagReg, t_ch, t_th, code) \ 701da177e4SLinus Torvalds (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) ) 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */ 731da177e4SLinus Torvalds #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */ 741da177e4SLinus Torvalds #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */ 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds #define __getDIAG(dr) ( { \ 771da177e4SLinus Torvalds register unsigned long __res asm("r28");\ 781da177e4SLinus Torvalds __asm__ __volatile__ ( \ 791da177e4SLinus Torvalds ".word %1\n nop\n" : "=&r" (__res) : "i" (MFCPU_T(dr,28)) \ 801da177e4SLinus Torvalds ); \ 811da177e4SLinus Torvalds __res; \ 821da177e4SLinus Torvalds } ) 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static void deferred_poweroff(void *dummy) 861da177e4SLinus Torvalds { 879ec52099SCedric Le Goater if (kill_cad_pid(SIGINT, 1)) { 881da177e4SLinus Torvalds /* just in case killing init process failed */ 891da177e4SLinus Torvalds machine_power_off(); 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* 941da177e4SLinus Torvalds * This function gets called from interrupt context. 951da177e4SLinus Torvalds * As it's called within an interrupt, it wouldn't sync if we don't 961da177e4SLinus Torvalds * use schedule_work(). 971da177e4SLinus Torvalds */ 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds static DECLARE_WORK(poweroff_work, deferred_poweroff, NULL); 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static void poweroff(void) 1021da177e4SLinus Torvalds { 1038039de10SHelge Deller static int powering_off __read_mostly; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds if (powering_off) 1061da177e4SLinus Torvalds return; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds powering_off++; 1091da177e4SLinus Torvalds schedule_work(&poweroff_work); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* local time-counter for shutdown */ 1148039de10SHelge Deller static int shutdown_timer __read_mostly; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* check, give feedback and start shutdown after one second */ 1171da177e4SLinus Torvalds static void process_shutdown(void) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds if (shutdown_timer == 0) 1201da177e4SLinus Torvalds DPRINTK(KERN_INFO "Shutdown requested...\n"); 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds shutdown_timer++; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* wait until the button was pressed for 1 second */ 1251da177e4SLinus Torvalds if (shutdown_timer == HZ) { 1261da177e4SLinus Torvalds #if defined (DEBUG) || defined(CONFIG_CHASSIS_LCD_LED) 1271da177e4SLinus Torvalds static char msg[] = "Shutting down..."; 1281da177e4SLinus Torvalds #endif 1291da177e4SLinus Torvalds DPRINTK(KERN_INFO "%s\n", msg); 1301da177e4SLinus Torvalds lcd_print(msg); 1311da177e4SLinus Torvalds poweroff(); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* main power switch tasklet struct (scheduled from time.c) */ 1371da177e4SLinus Torvalds DECLARE_TASKLET_DISABLED(power_tasklet, NULL, 0); 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* soft power switch enabled/disabled */ 1408039de10SHelge Deller int pwrsw_enabled __read_mostly = 1; 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* 1431da177e4SLinus Torvalds * On gecko style machines (e.g. 712/xx and 715/xx) 1441da177e4SLinus Torvalds * the power switch status is stored in Bit 0 ("the highest bit") 1451da177e4SLinus Torvalds * of CPU diagnose register 25. 1461da177e4SLinus Torvalds * 1471da177e4SLinus Torvalds */ 1481da177e4SLinus Torvalds static void gecko_tasklet_func(unsigned long unused) 1491da177e4SLinus Torvalds { 1508039de10SHelge Deller if (unlikely(!pwrsw_enabled)) 1511da177e4SLinus Torvalds return; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds if (__getDIAG(25) & 0x80000000) { 1541da177e4SLinus Torvalds /* power switch button not pressed or released again */ 1551da177e4SLinus Torvalds /* Warning: Some machines do never reset this DIAG flag! */ 1561da177e4SLinus Torvalds shutdown_timer = 0; 1571da177e4SLinus Torvalds } else { 1581da177e4SLinus Torvalds process_shutdown(); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds /* 1651da177e4SLinus Torvalds * Check the power switch status which is read from the 1661da177e4SLinus Torvalds * real I/O location at soft_power_reg. 1671da177e4SLinus Torvalds * Bit 31 ("the lowest bit) is the status of the power switch. 1681da177e4SLinus Torvalds */ 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static void polling_tasklet_func(unsigned long soft_power_reg) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds unsigned long current_status; 1731da177e4SLinus Torvalds 1748039de10SHelge Deller if (unlikely(!pwrsw_enabled)) 1751da177e4SLinus Torvalds return; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds current_status = gsc_readl(soft_power_reg); 1781da177e4SLinus Torvalds if (current_status & 0x1) { 1791da177e4SLinus Torvalds /* power switch button not pressed */ 1801da177e4SLinus Torvalds shutdown_timer = 0; 1811da177e4SLinus Torvalds } else { 1821da177e4SLinus Torvalds process_shutdown(); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2) 1891da177e4SLinus Torvalds */ 1901da177e4SLinus Torvalds #if 0 1917d12e780SDavid Howells static void powerfail_interrupt(int code, void *x) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n"); 1941da177e4SLinus Torvalds poweroff(); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds #endif 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /* parisc_panic_event() is called by the panic handler. 2021da177e4SLinus Torvalds * As soon as a panic occurs, our tasklets above will not be 2031da177e4SLinus Torvalds * executed any longer. This function then re-enables the 2041da177e4SLinus Torvalds * soft-power switch and allows the user to switch off the system 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds static int parisc_panic_event(struct notifier_block *this, 2071da177e4SLinus Torvalds unsigned long event, void *ptr) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds /* re-enable the soft-power switch */ 2101da177e4SLinus Torvalds pdc_soft_power_button(0); 2111da177e4SLinus Torvalds return NOTIFY_DONE; 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds static struct notifier_block parisc_panic_block = { 2151da177e4SLinus Torvalds .notifier_call = parisc_panic_event, 2161da177e4SLinus Torvalds .priority = INT_MAX, 2171da177e4SLinus Torvalds }; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static int __init power_init(void) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds unsigned long ret; 2231da177e4SLinus Torvalds unsigned long soft_power_reg = 0; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds #if 0 2261da177e4SLinus Torvalds request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt, 2271da177e4SLinus Torvalds 0, "powerfail", NULL); 2281da177e4SLinus Torvalds #endif 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* enable the soft power switch if possible */ 2311da177e4SLinus Torvalds ret = pdc_soft_power_info(&soft_power_reg); 2321da177e4SLinus Torvalds if (ret == PDC_OK) 2331da177e4SLinus Torvalds ret = pdc_soft_power_button(1); 2341da177e4SLinus Torvalds if (ret != PDC_OK) 2351da177e4SLinus Torvalds soft_power_reg = -1UL; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds switch (soft_power_reg) { 2381da177e4SLinus Torvalds case 0: printk(KERN_INFO "Gecko-style soft power switch enabled.\n"); 2391da177e4SLinus Torvalds power_tasklet.func = gecko_tasklet_func; 2401da177e4SLinus Torvalds break; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds case -1UL: printk(KERN_INFO "Soft power switch support not available.\n"); 2431da177e4SLinus Torvalds return -ENODEV; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds default: printk(KERN_INFO "Soft power switch enabled, polling @ 0x%08lx.\n", 2461da177e4SLinus Torvalds soft_power_reg); 2471da177e4SLinus Torvalds power_tasklet.data = soft_power_reg; 2481da177e4SLinus Torvalds power_tasklet.func = polling_tasklet_func; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /* Register a call for panic conditions. */ 252e041c683SAlan Stern atomic_notifier_chain_register(&panic_notifier_list, 253e041c683SAlan Stern &parisc_panic_block); 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds tasklet_enable(&power_tasklet); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds return 0; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static void __exit power_exit(void) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds if (!power_tasklet.func) 2631da177e4SLinus Torvalds return; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds tasklet_disable(&power_tasklet); 266e041c683SAlan Stern atomic_notifier_chain_unregister(&panic_notifier_list, 267e041c683SAlan Stern &parisc_panic_block); 2681da177e4SLinus Torvalds power_tasklet.func = NULL; 2691da177e4SLinus Torvalds pdc_soft_power_button(0); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds module_init(power_init); 2731da177e4SLinus Torvalds module_exit(power_exit); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds MODULE_AUTHOR("Helge Deller"); 2771da177e4SLinus Torvalds MODULE_DESCRIPTION("Soft power switch driver"); 2781da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL"); 279