[PARISC] PDC_CHASSIS is implemented on all machines
[pandora-kernel.git] / arch / parisc / kernel / pdc_chassis.c
1 /* 
2  *    interfaces to Chassis Codes via PDC (firmware)
3  *
4  *    Copyright (C) 2002 Laurent Canet <canetl@esiee.fr>
5  *    Copyright (C) 2002-2006 Thibaut VARENE <varenet@parisc-linux.org>
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License, version 2, as
9  *    published by the Free Software Foundation.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with this program; if not, write to the Free Software
18  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *    TODO: poll chassis warns, trigger (configurable) machine shutdown when
21  *              needed.
22  */
23
24 #undef PDC_CHASSIS_DEBUG
25 #ifdef PDC_CHASSIS_DEBUG
26 #define DPRINTK(fmt, args...)   printk(fmt, ## args)
27 #else
28 #define DPRINTK(fmt, args...)
29 #endif
30
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/reboot.h>
34 #include <linux/notifier.h>
35 #include <linux/cache.h>
36 #include <linux/proc_fs.h>
37
38 #include <asm/pdc_chassis.h>
39 #include <asm/processor.h>
40 #include <asm/pdc.h>
41 #include <asm/pdcpat.h>
42
43
44 #ifdef CONFIG_PDC_CHASSIS
45 static unsigned int pdc_chassis_enabled __read_mostly = 1;
46
47
48 /**
49  * pdc_chassis_setup() - Enable/disable pdc_chassis code at boot time.
50  * @str configuration param: 0 to disable chassis log
51  * @return 1
52  */
53  
54 static int __init pdc_chassis_setup(char *str)
55 {
56         /*panic_timeout = simple_strtoul(str, NULL, 0);*/
57         get_option(&str, &pdc_chassis_enabled);
58         return 1;
59 }
60 __setup("pdcchassis=", pdc_chassis_setup);
61
62
63 /** 
64  * pdc_chassis_checkold() - Checks for old PDC_CHASSIS compatibility
65  * @pdc_chassis_old: 1 if old pdc chassis style
66  * 
67  * Currently, only E class and A180 are known to work with this.
68  * Inspired by Christoph Plattner
69  */
70 #if 0
71 static void __init pdc_chassis_checkold(void)
72 {
73         switch(CPU_HVERSION) {
74                 case 0x480:             /* E25 */
75                 case 0x481:             /* E35 */
76                 case 0x482:             /* E45 */
77                 case 0x483:             /* E55 */
78                 case 0x516:             /* A180 */
79                         break;
80
81                 default:
82                         break;
83         }
84         DPRINTK(KERN_DEBUG "%s: pdc_chassis_checkold(); pdc_chassis_old = %d\n", __FILE__, pdc_chassis_old);
85 }
86 #endif
87
88 /**
89  * pdc_chassis_panic_event() - Called by the panic handler.
90  *
91  * As soon as a panic occurs, we should inform the PDC.
92  */
93
94 static int pdc_chassis_panic_event(struct notifier_block *this,
95                         unsigned long event, void *ptr)
96 {
97         pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
98                 return NOTIFY_DONE;
99 }   
100
101
102 static struct notifier_block pdc_chassis_panic_block = {
103         .notifier_call = pdc_chassis_panic_event,
104         .priority = INT_MAX,
105 };
106
107
108 /**
109  * parisc_reboot_event() - Called by the reboot handler.
110  *
111  * As soon as a reboot occurs, we should inform the PDC.
112  */
113
114 static int pdc_chassis_reboot_event(struct notifier_block *this,
115                         unsigned long event, void *ptr)
116 {
117         pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
118                 return NOTIFY_DONE;
119 }   
120
121
122 static struct notifier_block pdc_chassis_reboot_block = {
123         .notifier_call = pdc_chassis_reboot_event,
124         .priority = INT_MAX,
125 };
126 #endif /* CONFIG_PDC_CHASSIS */
127
128
129 /**
130  * parisc_pdc_chassis_init() - Called at boot time.
131  */
132
133 void __init parisc_pdc_chassis_init(void)
134 {
135 #ifdef CONFIG_PDC_CHASSIS
136         int handle = 0;
137         if (likely(pdc_chassis_enabled)) {
138                 DPRINTK(KERN_DEBUG "%s: parisc_pdc_chassis_init()\n", __FILE__);
139
140                 /* Let see if we have something to handle... */
141                 /* Check for PDC_PAT */
142                 if (is_pdc_pat()) {
143                         printk(KERN_INFO "Enabling PDC_PAT chassis codes support.\n");
144                         handle = 1;
145                 }
146                 else {
147                         printk(KERN_INFO "Enabling regular chassis codes support.\n");
148                         handle = 1;
149                 }
150
151                 if (handle) {
152                         /* initialize panic notifier chain */
153                         atomic_notifier_chain_register(&panic_notifier_list,
154                                         &pdc_chassis_panic_block);
155
156                         /* initialize reboot notifier chain */
157                         register_reboot_notifier(&pdc_chassis_reboot_block);
158                 }
159         }
160 #endif /* CONFIG_PDC_CHASSIS */
161 }
162
163
164 /** 
165  * pdc_chassis_send_status() - Sends a predefined message to the chassis,
166  * and changes the front panel LEDs according to the new system state
167  * @retval: PDC call return value.
168  *
169  * Only machines with 64 bits PDC PAT and those reported in
170  * pdc_chassis_checkold() are supported atm.
171  * 
172  * returns 0 if no error, -1 if no supported PDC is present or invalid message,
173  * else returns the appropriate PDC error code.
174  * 
175  * For a list of predefined messages, see asm-parisc/pdc_chassis.h
176  */
177
178 int pdc_chassis_send_status(int message)
179 {
180         /* Maybe we should do that in an other way ? */
181         int retval = 0;
182 #ifdef CONFIG_PDC_CHASSIS
183         if (likely(pdc_chassis_enabled)) {
184
185                 DPRINTK(KERN_DEBUG "%s: pdc_chassis_send_status(%d)\n", __FILE__, message);
186
187 #ifdef CONFIG_64BIT
188                 if (is_pdc_pat()) {
189                         switch(message) {
190                                 case PDC_CHASSIS_DIRECT_BSTART:
191                                         retval = pdc_pat_chassis_send_log(PDC_CHASSIS_PMSG_BSTART, PDC_CHASSIS_LSTATE_RUN_NORMAL);
192                                         break;
193
194                                 case PDC_CHASSIS_DIRECT_BCOMPLETE:
195                                         retval = pdc_pat_chassis_send_log(PDC_CHASSIS_PMSG_BCOMPLETE, PDC_CHASSIS_LSTATE_RUN_NORMAL);
196                                         break;
197
198                                 case PDC_CHASSIS_DIRECT_SHUTDOWN:
199                                         retval = pdc_pat_chassis_send_log(PDC_CHASSIS_PMSG_SHUTDOWN, PDC_CHASSIS_LSTATE_NONOS);
200                                         break;
201
202                                 case PDC_CHASSIS_DIRECT_PANIC:
203                                         retval = pdc_pat_chassis_send_log(PDC_CHASSIS_PMSG_PANIC, PDC_CHASSIS_LSTATE_RUN_CRASHREC);
204                                         break;
205
206                                 case PDC_CHASSIS_DIRECT_LPMC:
207                                         retval = pdc_pat_chassis_send_log(PDC_CHASSIS_PMSG_LPMC, PDC_CHASSIS_LSTATE_RUN_SYSINT);
208                                         break;
209
210                                 case PDC_CHASSIS_DIRECT_HPMC:
211                                         retval = pdc_pat_chassis_send_log(PDC_CHASSIS_PMSG_HPMC, PDC_CHASSIS_LSTATE_RUN_NCRIT);
212                                         break;
213
214                                 default:
215                                         retval = -1;
216                         }
217                 } else retval = -1;
218 #else
219                 if (1) {
220                         switch (message) {
221                                 case PDC_CHASSIS_DIRECT_BSTART:
222                                         retval = pdc_chassis_disp(PDC_CHASSIS_DISP_DATA(OSTAT_INIT));
223                                         break;
224
225                                 case PDC_CHASSIS_DIRECT_BCOMPLETE:
226                                         retval = pdc_chassis_disp(PDC_CHASSIS_DISP_DATA(OSTAT_RUN));
227                                         break;
228
229                                 case PDC_CHASSIS_DIRECT_SHUTDOWN:
230                                         retval = pdc_chassis_disp(PDC_CHASSIS_DISP_DATA(OSTAT_SHUT));
231                                         break;
232
233                                 case PDC_CHASSIS_DIRECT_HPMC:
234                                 case PDC_CHASSIS_DIRECT_PANIC:
235                                         retval = pdc_chassis_disp(PDC_CHASSIS_DISP_DATA(OSTAT_FLT));
236                                         break;
237
238                                 case PDC_CHASSIS_DIRECT_LPMC:
239                                         retval = pdc_chassis_disp(PDC_CHASSIS_DISP_DATA(OSTAT_WARN));
240                                         break;
241
242                                 default:
243                                         retval = -1;
244                         }
245                 } else retval = -1;
246 #endif /* CONFIG_64BIT */
247         }       /* if (pdc_chassis_enabled) */
248 #endif /* CONFIG_PDC_CHASSIS */
249         return retval;
250 }
251
252 #ifdef CONFIG_PDC_CHASSIS_WARN
253 #ifdef CONFIG_PROC_FS
254 static int pdc_chassis_warn_pread(char *page, char **start, off_t off,
255                 int count, int *eof, void *data)
256 {
257         char *out = page;
258         int len, ret;
259         unsigned long warn;
260         u32 warnreg;
261
262         ret = pdc_chassis_warn(&warn);
263         if (ret != PDC_OK)
264                 return -EIO;
265
266         warnreg = (warn & 0xFFFFFFFF);
267
268         if ((warnreg >> 24) & 0xFF)
269                 out += sprintf(out, "Chassis component failure! (eg fan or PSU): 0x%.2x\n", ((warnreg >> 24) & 0xFF));
270
271         out += sprintf(out, "Battery: %s\n", (warnreg & 0x04) ? "Low!" : "OK");
272         out += sprintf(out, "Temp low: %s\n", (warnreg & 0x02) ? "Exceeded!" : "OK");
273         out += sprintf(out, "Temp mid: %s\n", (warnreg & 0x01) ? "Exceeded!" : "OK");
274
275         len = out - page - off;
276         if (len < count) {
277                 *eof = 1;
278                 if (len <= 0) return 0;
279         } else {
280                 len = count;
281         }
282         *start = page + off;
283         return len;
284 }
285
286 static int __init pdc_chassis_create_procfs(void)
287 {
288         printk(KERN_INFO "Enabling PDC chassis warnings support.\n");
289         create_proc_read_entry("chassis", 0400, NULL, pdc_chassis_warn_pread,
290                                 NULL);
291         return 0;
292 }
293
294 __initcall(pdc_chassis_create_procfs);
295
296 #endif /* CONFIG_PROC_FS */
297 #endif /* CONFIG_PDC_CHASSIS_WARN */