162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * linux/drivers/parisc/power.c 362306a36Sopenharmony_ci * HP PARISC soft power switch support driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2001-2007 Helge Deller <deller@gmx.de> 662306a36Sopenharmony_ci * All rights reserved. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 1062306a36Sopenharmony_ci * modification, are permitted provided that the following conditions 1162306a36Sopenharmony_ci * are met: 1262306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 1362306a36Sopenharmony_ci * notice, this list of conditions, and the following disclaimer, 1462306a36Sopenharmony_ci * without modification. 1562306a36Sopenharmony_ci * 2. The name of the author may not be used to endorse or promote products 1662306a36Sopenharmony_ci * derived from this software without specific prior written permission. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the 1962306a36Sopenharmony_ci * GNU General Public License ("GPL"). 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2262306a36Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2362306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2462306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2562306a36Sopenharmony_ci * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2662306a36Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2762306a36Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2862306a36Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2962306a36Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3062306a36Sopenharmony_ci * 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * HINT: 3362306a36Sopenharmony_ci * Support of the soft power switch button may be enabled or disabled at 3462306a36Sopenharmony_ci * runtime through the "/proc/sys/kernel/power" procfs entry. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#include <linux/module.h> 3862306a36Sopenharmony_ci#include <linux/init.h> 3962306a36Sopenharmony_ci#include <linux/kernel.h> 4062306a36Sopenharmony_ci#include <linux/panic_notifier.h> 4162306a36Sopenharmony_ci#include <linux/reboot.h> 4262306a36Sopenharmony_ci#include <linux/sched/signal.h> 4362306a36Sopenharmony_ci#include <linux/kthread.h> 4462306a36Sopenharmony_ci#include <linux/pm.h> 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci#include <asm/pdc.h> 4762306a36Sopenharmony_ci#include <asm/io.h> 4862306a36Sopenharmony_ci#include <asm/led.h> 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci#define DRIVER_NAME "powersw" 5162306a36Sopenharmony_ci#define KTHREAD_NAME "kpowerswd" 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci/* how often should the power button be polled ? */ 5462306a36Sopenharmony_ci#define POWERSWITCH_POLL_PER_SEC 2 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci/* how long does the power button needs to be down until we react ? */ 5762306a36Sopenharmony_ci#define POWERSWITCH_DOWN_SEC 2 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci/* assembly code to access special registers */ 6062306a36Sopenharmony_ci/* taken from PCXL ERS page 82 */ 6162306a36Sopenharmony_ci#define DIAG_CODE(code) (0x14000000 + ((code)<<5)) 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci#define MFCPU_X(rDiagReg, t_ch, t_th, code) \ 6462306a36Sopenharmony_ci (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) ) 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci#define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */ 6762306a36Sopenharmony_ci#define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */ 6862306a36Sopenharmony_ci#define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */ 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci#define __getDIAG(dr) ( { \ 7162306a36Sopenharmony_ci register unsigned long __res asm("r28");\ 7262306a36Sopenharmony_ci __asm__ __volatile__ ( \ 7362306a36Sopenharmony_ci ".word %1" : "=&r" (__res) : "i" (MFCPU_T(dr,28) ) \ 7462306a36Sopenharmony_ci ); \ 7562306a36Sopenharmony_ci __res; \ 7662306a36Sopenharmony_ci} ) 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci/* local shutdown counter */ 7962306a36Sopenharmony_cistatic int shutdown_timer __read_mostly; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci/* check, give feedback and start shutdown after one second */ 8262306a36Sopenharmony_cistatic void process_shutdown(void) 8362306a36Sopenharmony_ci{ 8462306a36Sopenharmony_ci if (shutdown_timer == 0) 8562306a36Sopenharmony_ci printk(KERN_ALERT KTHREAD_NAME ": Shutdown requested...\n"); 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci shutdown_timer++; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci /* wait until the button was pressed for 1 second */ 9062306a36Sopenharmony_ci if (shutdown_timer == (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC)) { 9162306a36Sopenharmony_ci static const char msg[] = "Shutting down..."; 9262306a36Sopenharmony_ci printk(KERN_INFO KTHREAD_NAME ": %s\n", msg); 9362306a36Sopenharmony_ci lcd_print(msg); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci /* send kill signal */ 9662306a36Sopenharmony_ci if (kill_cad_pid(SIGINT, 1)) { 9762306a36Sopenharmony_ci /* just in case killing init process failed */ 9862306a36Sopenharmony_ci machine_power_off(); 9962306a36Sopenharmony_ci } 10062306a36Sopenharmony_ci } 10162306a36Sopenharmony_ci} 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci/* main power switch task struct */ 10562306a36Sopenharmony_cistatic struct task_struct *power_task; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/* filename in /proc which can be used to enable/disable the power switch */ 10862306a36Sopenharmony_ci#define SYSCTL_FILENAME "sys/kernel/power" 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci/* soft power switch enabled/disabled */ 11162306a36Sopenharmony_ciint pwrsw_enabled __read_mostly = 1; 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci/* main kernel thread worker. It polls the button state */ 11462306a36Sopenharmony_cistatic int kpowerswd(void *param) 11562306a36Sopenharmony_ci{ 11662306a36Sopenharmony_ci __set_current_state(TASK_RUNNING); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci do { 11962306a36Sopenharmony_ci int button_not_pressed; 12062306a36Sopenharmony_ci unsigned long soft_power_reg = (unsigned long) param; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci schedule_timeout_interruptible(pwrsw_enabled ? HZ : HZ/POWERSWITCH_POLL_PER_SEC); 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci if (unlikely(!pwrsw_enabled)) 12562306a36Sopenharmony_ci continue; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci if (soft_power_reg) { 12862306a36Sopenharmony_ci /* 12962306a36Sopenharmony_ci * Non-Gecko-style machines: 13062306a36Sopenharmony_ci * Check the power switch status which is read from the 13162306a36Sopenharmony_ci * real I/O location at soft_power_reg. 13262306a36Sopenharmony_ci * Bit 31 ("the lowest bit) is the status of the power switch. 13362306a36Sopenharmony_ci * This bit is "1" if the button is NOT pressed. 13462306a36Sopenharmony_ci */ 13562306a36Sopenharmony_ci button_not_pressed = (gsc_readl(soft_power_reg) & 0x1); 13662306a36Sopenharmony_ci } else { 13762306a36Sopenharmony_ci /* 13862306a36Sopenharmony_ci * On gecko style machines (e.g. 712/xx and 715/xx) 13962306a36Sopenharmony_ci * the power switch status is stored in Bit 0 ("the highest bit") 14062306a36Sopenharmony_ci * of CPU diagnose register 25. 14162306a36Sopenharmony_ci * Warning: Some machines never reset the DIAG flag, even if 14262306a36Sopenharmony_ci * the button has been released again. 14362306a36Sopenharmony_ci */ 14462306a36Sopenharmony_ci button_not_pressed = (__getDIAG(25) & 0x80000000); 14562306a36Sopenharmony_ci } 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci if (likely(button_not_pressed)) { 14862306a36Sopenharmony_ci if (unlikely(shutdown_timer && /* avoid writing if not necessary */ 14962306a36Sopenharmony_ci shutdown_timer < (POWERSWITCH_DOWN_SEC*POWERSWITCH_POLL_PER_SEC))) { 15062306a36Sopenharmony_ci shutdown_timer = 0; 15162306a36Sopenharmony_ci printk(KERN_INFO KTHREAD_NAME ": Shutdown request aborted.\n"); 15262306a36Sopenharmony_ci } 15362306a36Sopenharmony_ci } else 15462306a36Sopenharmony_ci process_shutdown(); 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci } while (!kthread_should_stop()); 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci return 0; 16062306a36Sopenharmony_ci} 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci/* 16462306a36Sopenharmony_ci * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2) 16562306a36Sopenharmony_ci */ 16662306a36Sopenharmony_ci#if 0 16762306a36Sopenharmony_cistatic void powerfail_interrupt(int code, void *x) 16862306a36Sopenharmony_ci{ 16962306a36Sopenharmony_ci printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n"); 17062306a36Sopenharmony_ci poweroff(); 17162306a36Sopenharmony_ci} 17262306a36Sopenharmony_ci#endif 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci/* 17862306a36Sopenharmony_ci * parisc_panic_event() is called by the panic handler. 17962306a36Sopenharmony_ci * 18062306a36Sopenharmony_ci * As soon as a panic occurs, our tasklets above will not 18162306a36Sopenharmony_ci * be executed any longer. This function then re-enables 18262306a36Sopenharmony_ci * the soft-power switch and allows the user to switch off 18362306a36Sopenharmony_ci * the system. We rely in pdc_soft_power_button_panic() 18462306a36Sopenharmony_ci * since this version spin_trylocks (instead of regular 18562306a36Sopenharmony_ci * spinlock), preventing deadlocks on panic path. 18662306a36Sopenharmony_ci */ 18762306a36Sopenharmony_cistatic int parisc_panic_event(struct notifier_block *this, 18862306a36Sopenharmony_ci unsigned long event, void *ptr) 18962306a36Sopenharmony_ci{ 19062306a36Sopenharmony_ci /* re-enable the soft-power switch */ 19162306a36Sopenharmony_ci pdc_soft_power_button_panic(0); 19262306a36Sopenharmony_ci return NOTIFY_DONE; 19362306a36Sopenharmony_ci} 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_cistatic struct notifier_block parisc_panic_block = { 19662306a36Sopenharmony_ci .notifier_call = parisc_panic_event, 19762306a36Sopenharmony_ci .priority = INT_MAX, 19862306a36Sopenharmony_ci}; 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci/* qemu soft power-off function */ 20162306a36Sopenharmony_cistatic int qemu_power_off(struct sys_off_data *data) 20262306a36Sopenharmony_ci{ 20362306a36Sopenharmony_ci /* this turns the system off via SeaBIOS */ 20462306a36Sopenharmony_ci gsc_writel(0, (unsigned long) data->cb_data); 20562306a36Sopenharmony_ci pdc_soft_power_button(1); 20662306a36Sopenharmony_ci return NOTIFY_DONE; 20762306a36Sopenharmony_ci} 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_cistatic int __init power_init(void) 21062306a36Sopenharmony_ci{ 21162306a36Sopenharmony_ci unsigned long ret; 21262306a36Sopenharmony_ci unsigned long soft_power_reg; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci#if 0 21562306a36Sopenharmony_ci request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt, 21662306a36Sopenharmony_ci 0, "powerfail", NULL); 21762306a36Sopenharmony_ci#endif 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci /* enable the soft power switch if possible */ 22062306a36Sopenharmony_ci ret = pdc_soft_power_info(&soft_power_reg); 22162306a36Sopenharmony_ci if (ret == PDC_OK) 22262306a36Sopenharmony_ci ret = pdc_soft_power_button(1); 22362306a36Sopenharmony_ci if (ret != PDC_OK) 22462306a36Sopenharmony_ci soft_power_reg = -1UL; 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci switch (soft_power_reg) { 22762306a36Sopenharmony_ci case 0: printk(KERN_INFO DRIVER_NAME ": Gecko-style soft power switch enabled.\n"); 22862306a36Sopenharmony_ci break; 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci case -1UL: printk(KERN_INFO DRIVER_NAME ": Soft power switch support not available.\n"); 23162306a36Sopenharmony_ci return -ENODEV; 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci default: printk(KERN_INFO DRIVER_NAME ": Soft power switch at 0x%08lx enabled.\n", 23462306a36Sopenharmony_ci soft_power_reg); 23562306a36Sopenharmony_ci } 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci power_task = NULL; 23862306a36Sopenharmony_ci if (running_on_qemu && soft_power_reg) 23962306a36Sopenharmony_ci register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_DEFAULT, 24062306a36Sopenharmony_ci qemu_power_off, (void *)soft_power_reg); 24162306a36Sopenharmony_ci if (!running_on_qemu || soft_power_reg) 24262306a36Sopenharmony_ci power_task = kthread_run(kpowerswd, (void*)soft_power_reg, 24362306a36Sopenharmony_ci KTHREAD_NAME); 24462306a36Sopenharmony_ci if (IS_ERR(power_task)) { 24562306a36Sopenharmony_ci printk(KERN_ERR DRIVER_NAME ": thread creation failed. Driver not loaded.\n"); 24662306a36Sopenharmony_ci pdc_soft_power_button(0); 24762306a36Sopenharmony_ci return -EIO; 24862306a36Sopenharmony_ci } 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci /* Register a call for panic conditions. */ 25162306a36Sopenharmony_ci atomic_notifier_chain_register(&panic_notifier_list, 25262306a36Sopenharmony_ci &parisc_panic_block); 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci return 0; 25562306a36Sopenharmony_ci} 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_cistatic void __exit power_exit(void) 25862306a36Sopenharmony_ci{ 25962306a36Sopenharmony_ci kthread_stop(power_task); 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci atomic_notifier_chain_unregister(&panic_notifier_list, 26262306a36Sopenharmony_ci &parisc_panic_block); 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci pdc_soft_power_button(0); 26562306a36Sopenharmony_ci} 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ciarch_initcall(power_init); 26862306a36Sopenharmony_cimodule_exit(power_exit); 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ciMODULE_AUTHOR("Helge Deller <deller@gmx.de>"); 27262306a36Sopenharmony_ciMODULE_DESCRIPTION("Soft power switch driver"); 27362306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL"); 274