Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/drivers/parisc/power.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * HP PARISC soft power switch support driver |
| 4 | * |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 5 | * Copyright (c) 2001-2005 Helge Deller <deller@gmx.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions, and the following disclaimer, |
| 14 | * without modification. |
| 15 | * 2. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * Alternatively, this software may be distributed under the terms of the |
| 19 | * GNU General Public License ("GPL"). |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR |
| 25 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * |
| 31 | * |
| 32 | * |
| 33 | * HINT: |
| 34 | * Support of the soft power switch button may be enabled or disabled at |
| 35 | * runtime through the "/proc/sys/kernel/power" procfs entry. |
| 36 | */ |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/module.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/notifier.h> |
| 43 | #include <linux/reboot.h> |
| 44 | #include <linux/sched.h> |
| 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/workqueue.h> |
| 47 | |
| 48 | #include <asm/pdc.h> |
| 49 | #include <asm/io.h> |
| 50 | #include <asm/led.h> |
| 51 | #include <asm/uaccess.h> |
| 52 | |
| 53 | |
| 54 | #ifdef DEBUG |
| 55 | # define DPRINTK(x...) printk(x) |
| 56 | #else |
| 57 | # define DPRINTK(x...) |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | /* filename in /proc which can be used to enable/disable the power switch */ |
| 62 | #define SYSCTL_FILENAME "sys/kernel/power" |
| 63 | |
| 64 | |
| 65 | #define DIAG_CODE(code) (0x14000000 + ((code)<<5)) |
| 66 | |
| 67 | /* this will go to processor.h or any other place... */ |
| 68 | /* taken from PCXL ERS page 82 */ |
| 69 | #define MFCPU_X(rDiagReg, t_ch, t_th, code) \ |
| 70 | (DIAG_CODE(code) + ((rDiagReg)<<21) + ((t_ch)<<16) + ((t_th)<<0) ) |
| 71 | |
| 72 | #define MTCPU(dr, gr) MFCPU_X(dr, gr, 0, 0x12) /* move value of gr to dr[dr] */ |
| 73 | #define MFCPU_C(dr, gr) MFCPU_X(dr, gr, 0, 0x30) /* for dr0 and dr8 only ! */ |
| 74 | #define MFCPU_T(dr, gr) MFCPU_X(dr, 0, gr, 0xa0) /* all dr except dr0 and dr8 */ |
| 75 | |
| 76 | #define __getDIAG(dr) ( { \ |
| 77 | register unsigned long __res asm("r28");\ |
| 78 | __asm__ __volatile__ ( \ |
| 79 | ".word %1\n nop\n" : "=&r" (__res) : "i" (MFCPU_T(dr,28)) \ |
| 80 | ); \ |
| 81 | __res; \ |
| 82 | } ) |
| 83 | |
| 84 | |
| 85 | static void deferred_poweroff(void *dummy) |
| 86 | { |
Cedric Le Goater | 9ec5209 | 2006-10-02 02:19:00 -0700 | [diff] [blame] | 87 | if (kill_cad_pid(SIGINT, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* just in case killing init process failed */ |
| 89 | machine_power_off(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * This function gets called from interrupt context. |
| 95 | * As it's called within an interrupt, it wouldn't sync if we don't |
| 96 | * use schedule_work(). |
| 97 | */ |
| 98 | |
| 99 | static DECLARE_WORK(poweroff_work, deferred_poweroff, NULL); |
| 100 | |
| 101 | static void poweroff(void) |
| 102 | { |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 103 | static int powering_off __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | if (powering_off) |
| 106 | return; |
| 107 | |
| 108 | powering_off++; |
| 109 | schedule_work(&poweroff_work); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /* local time-counter for shutdown */ |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 114 | static int shutdown_timer __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* check, give feedback and start shutdown after one second */ |
| 117 | static void process_shutdown(void) |
| 118 | { |
| 119 | if (shutdown_timer == 0) |
| 120 | DPRINTK(KERN_INFO "Shutdown requested...\n"); |
| 121 | |
| 122 | shutdown_timer++; |
| 123 | |
| 124 | /* wait until the button was pressed for 1 second */ |
| 125 | if (shutdown_timer == HZ) { |
| 126 | #if defined (DEBUG) || defined(CONFIG_CHASSIS_LCD_LED) |
| 127 | static char msg[] = "Shutting down..."; |
| 128 | #endif |
| 129 | DPRINTK(KERN_INFO "%s\n", msg); |
| 130 | lcd_print(msg); |
| 131 | poweroff(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /* main power switch tasklet struct (scheduled from time.c) */ |
| 137 | DECLARE_TASKLET_DISABLED(power_tasklet, NULL, 0); |
| 138 | |
| 139 | /* soft power switch enabled/disabled */ |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 140 | int pwrsw_enabled __read_mostly = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * On gecko style machines (e.g. 712/xx and 715/xx) |
| 144 | * the power switch status is stored in Bit 0 ("the highest bit") |
| 145 | * of CPU diagnose register 25. |
| 146 | * |
| 147 | */ |
| 148 | static void gecko_tasklet_func(unsigned long unused) |
| 149 | { |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 150 | if (unlikely(!pwrsw_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return; |
| 152 | |
| 153 | if (__getDIAG(25) & 0x80000000) { |
| 154 | /* power switch button not pressed or released again */ |
| 155 | /* Warning: Some machines do never reset this DIAG flag! */ |
| 156 | shutdown_timer = 0; |
| 157 | } else { |
| 158 | process_shutdown(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | /* |
| 165 | * Check the power switch status which is read from the |
| 166 | * real I/O location at soft_power_reg. |
| 167 | * Bit 31 ("the lowest bit) is the status of the power switch. |
| 168 | */ |
| 169 | |
| 170 | static void polling_tasklet_func(unsigned long soft_power_reg) |
| 171 | { |
| 172 | unsigned long current_status; |
| 173 | |
Helge Deller | 8039de1 | 2006-01-10 20:35:03 -0500 | [diff] [blame] | 174 | if (unlikely(!pwrsw_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return; |
| 176 | |
| 177 | current_status = gsc_readl(soft_power_reg); |
| 178 | if (current_status & 0x1) { |
| 179 | /* power switch button not pressed */ |
| 180 | shutdown_timer = 0; |
| 181 | } else { |
| 182 | process_shutdown(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /* |
| 188 | * powerfail interruption handler (irq IRQ_FROM_REGION(CPU_IRQ_REGION)+2) |
| 189 | */ |
| 190 | #if 0 |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 191 | static void powerfail_interrupt(int code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | printk(KERN_CRIT "POWERFAIL INTERRUPTION !\n"); |
| 194 | poweroff(); |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | /* parisc_panic_event() is called by the panic handler. |
| 202 | * As soon as a panic occurs, our tasklets above will not be |
| 203 | * executed any longer. This function then re-enables the |
| 204 | * soft-power switch and allows the user to switch off the system |
| 205 | */ |
| 206 | static int parisc_panic_event(struct notifier_block *this, |
| 207 | unsigned long event, void *ptr) |
| 208 | { |
| 209 | /* re-enable the soft-power switch */ |
| 210 | pdc_soft_power_button(0); |
| 211 | return NOTIFY_DONE; |
| 212 | } |
| 213 | |
| 214 | static struct notifier_block parisc_panic_block = { |
| 215 | .notifier_call = parisc_panic_event, |
| 216 | .priority = INT_MAX, |
| 217 | }; |
| 218 | |
| 219 | |
| 220 | static int __init power_init(void) |
| 221 | { |
| 222 | unsigned long ret; |
| 223 | unsigned long soft_power_reg = 0; |
| 224 | |
| 225 | #if 0 |
| 226 | request_irq( IRQ_FROM_REGION(CPU_IRQ_REGION)+2, &powerfail_interrupt, |
| 227 | 0, "powerfail", NULL); |
| 228 | #endif |
| 229 | |
| 230 | /* enable the soft power switch if possible */ |
| 231 | ret = pdc_soft_power_info(&soft_power_reg); |
| 232 | if (ret == PDC_OK) |
| 233 | ret = pdc_soft_power_button(1); |
| 234 | if (ret != PDC_OK) |
| 235 | soft_power_reg = -1UL; |
| 236 | |
| 237 | switch (soft_power_reg) { |
| 238 | case 0: printk(KERN_INFO "Gecko-style soft power switch enabled.\n"); |
| 239 | power_tasklet.func = gecko_tasklet_func; |
| 240 | break; |
| 241 | |
| 242 | case -1UL: printk(KERN_INFO "Soft power switch support not available.\n"); |
| 243 | return -ENODEV; |
| 244 | |
| 245 | default: printk(KERN_INFO "Soft power switch enabled, polling @ 0x%08lx.\n", |
| 246 | soft_power_reg); |
| 247 | power_tasklet.data = soft_power_reg; |
| 248 | power_tasklet.func = polling_tasklet_func; |
| 249 | } |
| 250 | |
| 251 | /* Register a call for panic conditions. */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 252 | atomic_notifier_chain_register(&panic_notifier_list, |
| 253 | &parisc_panic_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | tasklet_enable(&power_tasklet); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void __exit power_exit(void) |
| 261 | { |
| 262 | if (!power_tasklet.func) |
| 263 | return; |
| 264 | |
| 265 | tasklet_disable(&power_tasklet); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 266 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 267 | &parisc_panic_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | power_tasklet.func = NULL; |
| 269 | pdc_soft_power_button(0); |
| 270 | } |
| 271 | |
| 272 | module_init(power_init); |
| 273 | module_exit(power_exit); |
| 274 | |
| 275 | |
| 276 | MODULE_AUTHOR("Helge Deller"); |
| 277 | MODULE_DESCRIPTION("Soft power switch driver"); |
| 278 | MODULE_LICENSE("Dual BSD/GPL"); |