blob: d765cc1bf060bc30d17e1e3ba78d01429dedf457 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: envctrl.c,v 1.25 2002/01/15 09:01:26 davem Exp $
2 * envctrl.c: Temperature and Fan monitoring on Machines providing it.
3 *
4 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 2000 Vinh Truong (vinh.truong@eng.sun.com)
6 * VT - The implementation is to support Sun Microelectronics (SME) platform
7 * environment monitoring. SME platforms use pcf8584 as the i2c bus
8 * controller to access pcf8591 (8-bit A/D and D/A converter) and
9 * pcf8571 (256 x 8-bit static low-voltage RAM with I2C-bus interface).
10 * At board level, it follows SME Firmware I2C Specification. Reference:
11 * http://www-eu2.semiconductors.com/pip/PCF8584P
12 * http://www-eu2.semiconductors.com/pip/PCF8574AP
13 * http://www-eu2.semiconductors.com/pip/PCF8591P
14 *
15 * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
16 * Eric Brower <ebrower@usa.net>
17 *
18 * DB - Audit every copy_to_user in envctrl_read.
19 * Daniele Bellucci <bellucda@tiscali.it>
20 */
21
David S. Miller4b502422005-07-24 19:35:08 -070022#define __KERNEL_SYSCALLS__
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/config.h>
25#include <linux/module.h>
26#include <linux/sched.h>
Christoph Hellwig218b29e2005-08-09 12:30:07 -070027#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
29#include <linux/delay.h>
30#include <linux/ioport.h>
31#include <linux/init.h>
32#include <linux/miscdevice.h>
33#include <linux/mm.h>
34#include <linux/slab.h>
35#include <linux/kernel.h>
36
37#include <asm/ebus.h>
38#include <asm/uaccess.h>
39#include <asm/envctrl.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int errno;
42#include <asm/unistd.h>
43
44#define ENVCTRL_MINOR 162
45
46#define PCF8584_ADDRESS 0x55
47
48#define CONTROL_PIN 0x80
49#define CONTROL_ES0 0x40
50#define CONTROL_ES1 0x20
51#define CONTROL_ES2 0x10
52#define CONTROL_ENI 0x08
53#define CONTROL_STA 0x04
54#define CONTROL_STO 0x02
55#define CONTROL_ACK 0x01
56
57#define STATUS_PIN 0x80
58#define STATUS_STS 0x20
59#define STATUS_BER 0x10
60#define STATUS_LRB 0x08
61#define STATUS_AD0 0x08
62#define STATUS_AAB 0x04
63#define STATUS_LAB 0x02
64#define STATUS_BB 0x01
65
66/*
67 * CLK Mode Register.
68 */
69#define BUS_CLK_90 0x00
70#define BUS_CLK_45 0x01
71#define BUS_CLK_11 0x02
72#define BUS_CLK_1_5 0x03
73
74#define CLK_3 0x00
75#define CLK_4_43 0x10
76#define CLK_6 0x14
77#define CLK_8 0x18
78#define CLK_12 0x1c
79
80#define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
81#define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
82
83/* Monitor type of i2c child device.
84 * Firmware definitions.
85 */
86#define PCF8584_MAX_CHANNELS 8
87#define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
88#define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
89#define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
90#define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
91
92/* Monitor type of i2c child device.
93 * Driver definitions.
94 */
95#define ENVCTRL_NOMON 0
96#define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
97#define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
98#define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
99#define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
100 /* monitor */
101#define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
102#define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
103#define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
104#define ENVCTRL_GLOBALADDR_MON 8 /* global address */
105
106/* Child device type.
107 * Driver definitions.
108 */
109#define I2C_ADC 0 /* pcf8591 */
110#define I2C_GPIO 1 /* pcf8571 */
111
112/* Data read from child device may need to decode
113 * through a data table and a scale.
114 * Translation type as defined by firmware.
115 */
116#define ENVCTRL_TRANSLATE_NO 0
117#define ENVCTRL_TRANSLATE_PARTIAL 1
118#define ENVCTRL_TRANSLATE_COMBINED 2
119#define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
120#define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
121
122/* Driver miscellaneous definitions. */
123#define ENVCTRL_MAX_CPU 4
124#define CHANNEL_DESC_SZ 256
125
126/* Mask values for combined GlobalAddress/PowerStatus node */
127#define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
128#define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
129
130/* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
131 * (see envctrl_init_i2c_child)
132 */
133#define ENVCTRL_CPCI_IGNORED_NODE 0x70
134
135#define PCF8584_DATA 0x00
136#define PCF8584_CSR 0x01
137
138/* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
139 * Property of a port or channel as defined by the firmware.
140 */
141struct pcf8584_channel {
142 unsigned char chnl_no;
143 unsigned char io_direction;
144 unsigned char type;
145 unsigned char last;
146};
147
148/* Each child device may have one or more tables of bytes to help decode
149 * data. Table property as defined by the firmware.
150 */
151struct pcf8584_tblprop {
152 unsigned int type;
153 unsigned int scale;
154 unsigned int offset; /* offset from the beginning of the table */
155 unsigned int size;
156};
157
158/* i2c child */
159struct i2c_child_t {
160 /* Either ADC or GPIO. */
161 unsigned char i2ctype;
162 unsigned long addr;
163 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
164
165 /* Channel info. */
166 unsigned int total_chnls; /* Number of monitor channels. */
167 unsigned char fan_mask; /* Byte mask for fan status channels. */
168 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
169 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
170
171 /* Properties of all monitor channels. */
172 unsigned int total_tbls; /* Number of monitor tables. */
173 char *tables; /* Pointer to table(s). */
174 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
175 char mon_type[PCF8584_MAX_CHANNELS];
176};
177
178static void __iomem *i2c;
179static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
180static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
181static unsigned int warning_temperature = 0;
182static unsigned int shutdown_temperature = 0;
183static char read_cpu;
184
185/* Forward declarations. */
186static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
187
188/* Function Description: Test the PIN bit (Pending Interrupt Not)
189 * to test when serial transmission is completed .
190 * Return : None.
191 */
192static void envtrl_i2c_test_pin(void)
193{
194 int limit = 1000000;
195
196 while (--limit > 0) {
197 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
198 break;
199 udelay(1);
200 }
201
202 if (limit <= 0)
203 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
204}
205
206/* Function Description: Test busy bit.
207 * Return : None.
208 */
209static void envctrl_i2c_test_bb(void)
210{
211 int limit = 1000000;
212
213 while (--limit > 0) {
214 /* Busy bit 0 means busy. */
215 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
216 break;
217 udelay(1);
218 }
219
220 if (limit <= 0)
221 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
222}
223
224/* Function Description: Send the address for a read access.
225 * Return : 0 if not acknowledged, otherwise acknowledged.
226 */
227static int envctrl_i2c_read_addr(unsigned char addr)
228{
229 envctrl_i2c_test_bb();
230
231 /* Load address. */
232 writeb(addr + 1, i2c + PCF8584_DATA);
233
234 envctrl_i2c_test_bb();
235
236 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
237
238 /* Wait for PIN. */
239 envtrl_i2c_test_pin();
240
241 /* CSR 0 means acknowledged. */
242 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
243 return readb(i2c + PCF8584_DATA);
244 } else {
245 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
246 return 0;
247 }
248}
249
250/* Function Description: Send the address for write mode.
251 * Return : None.
252 */
253static void envctrl_i2c_write_addr(unsigned char addr)
254{
255 envctrl_i2c_test_bb();
256 writeb(addr, i2c + PCF8584_DATA);
257
258 /* Generate Start condition. */
259 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
260}
261
262/* Function Description: Read 1 byte of data from addr
263 * set by envctrl_i2c_read_addr()
264 * Return : Data from address set by envctrl_i2c_read_addr().
265 */
266static unsigned char envctrl_i2c_read_data(void)
267{
268 envtrl_i2c_test_pin();
269 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
270 return readb(i2c + PCF8584_DATA);
271}
272
273/* Function Description: Instruct the device which port to read data from.
274 * Return : None.
275 */
276static void envctrl_i2c_write_data(unsigned char port)
277{
278 envtrl_i2c_test_pin();
279 writeb(port, i2c + PCF8584_DATA);
280}
281
282/* Function Description: Generate Stop condition after last byte is sent.
283 * Return : None.
284 */
285static void envctrl_i2c_stop(void)
286{
287 envtrl_i2c_test_pin();
288 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
289}
290
291/* Function Description: Read adc device.
292 * Return : Data at address and port.
293 */
294static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
295{
296 /* Send address. */
297 envctrl_i2c_write_addr(addr);
298
299 /* Setup port to read. */
300 envctrl_i2c_write_data(port);
301 envctrl_i2c_stop();
302
303 /* Read port. */
304 envctrl_i2c_read_addr(addr);
305
306 /* Do a single byte read and send stop. */
307 envctrl_i2c_read_data();
308 envctrl_i2c_stop();
309
310 return readb(i2c + PCF8584_DATA);
311}
312
313/* Function Description: Read gpio device.
314 * Return : Data at address.
315 */
316static unsigned char envctrl_i2c_read_8574(unsigned char addr)
317{
318 unsigned char rd;
319
320 envctrl_i2c_read_addr(addr);
321
322 /* Do a single byte read and send stop. */
323 rd = envctrl_i2c_read_data();
324 envctrl_i2c_stop();
325 return rd;
326}
327
328/* Function Description: Decode data read from an adc device using firmware
329 * table.
330 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
331 */
332static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
333 int scale, char *tbl, char *bufdata)
334{
335 int len = 0;
336
337 switch (translate_type) {
338 case ENVCTRL_TRANSLATE_NO:
339 /* No decode necessary. */
340 len = 1;
341 bufdata[0] = data;
342 break;
343
344 case ENVCTRL_TRANSLATE_FULL:
345 /* Decode this way: data = table[data]. */
346 len = 1;
347 bufdata[0] = tbl[data];
348 break;
349
350 case ENVCTRL_TRANSLATE_SCALE:
351 /* Decode this way: data = table[data]/scale */
352 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
353 len = strlen(bufdata);
354 bufdata[len - 1] = bufdata[len - 2];
355 bufdata[len - 2] = '.';
356 break;
357
358 default:
359 break;
360 };
361
362 return len;
363}
364
365/* Function Description: Read cpu-related data such as cpu temperature, voltage.
366 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
367 */
368static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
369 char mon_type, unsigned char *bufdata)
370{
371 unsigned char data;
372 int i;
373 char *tbl, j = -1;
374
375 /* Find the right monitor type and channel. */
376 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
377 if (pchild->mon_type[i] == mon_type) {
378 if (++j == cpu) {
379 break;
380 }
381 }
382 }
383
384 if (j != cpu)
385 return 0;
386
387 /* Read data from address and port. */
388 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
389 (unsigned char)pchild->chnl_array[i].chnl_no);
390
391 /* Find decoding table. */
392 tbl = pchild->tables + pchild->tblprop_array[i].offset;
393
394 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
395 pchild->tblprop_array[i].scale,
396 tbl, bufdata);
397}
398
399/* Function Description: Read noncpu-related data such as motherboard
400 * temperature.
401 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
402 */
403static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
404 char mon_type, unsigned char *bufdata)
405{
406 unsigned char data;
407 int i;
408 char *tbl = NULL;
409
410 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
411 if (pchild->mon_type[i] == mon_type)
412 break;
413 }
414
415 if (i >= PCF8584_MAX_CHANNELS)
416 return 0;
417
418 /* Read data from address and port. */
419 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
420 (unsigned char)pchild->chnl_array[i].chnl_no);
421
422 /* Find decoding table. */
423 tbl = pchild->tables + pchild->tblprop_array[i].offset;
424
425 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
426 pchild->tblprop_array[i].scale,
427 tbl, bufdata);
428}
429
430/* Function Description: Read fan status.
431 * Return : Always 1 byte. Status stored in bufdata.
432 */
433static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
434 unsigned char data,
435 char *bufdata)
436{
437 unsigned char tmp, ret = 0;
438 int i, j = 0;
439
440 tmp = data & pchild->fan_mask;
441
442 if (tmp == pchild->fan_mask) {
443 /* All bits are on. All fans are functioning. */
444 ret = ENVCTRL_ALL_FANS_GOOD;
445 } else if (tmp == 0) {
446 /* No bits are on. No fans are functioning. */
447 ret = ENVCTRL_ALL_FANS_BAD;
448 } else {
449 /* Go through all channels, mark 'on' the matched bits.
450 * Notice that fan_mask may have discontiguous bits but
451 * return mask are always contiguous. For example if we
452 * monitor 4 fans at channels 0,1,2,4, the return mask
453 * should be 00010000 if only fan at channel 4 is working.
454 */
455 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
456 if (pchild->fan_mask & chnls_mask[i]) {
457 if (!(chnls_mask[i] & tmp))
458 ret |= chnls_mask[j];
459
460 j++;
461 }
462 }
463 }
464
465 bufdata[0] = ret;
466 return 1;
467}
468
469/* Function Description: Read global addressing line.
470 * Return : Always 1 byte. Status stored in bufdata.
471 */
472static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
473 unsigned char data,
474 char *bufdata)
475{
476 /* Translatation table is not necessary, as global
477 * addr is the integer value of the GA# bits.
478 *
479 * NOTE: MSB is documented as zero, but I see it as '1' always....
480 *
481 * -----------------------------------------------
482 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
483 * -----------------------------------------------
484 * GA0 - GA4 integer value of Global Address (backplane slot#)
485 * DEG 0 = cPCI Power supply output is starting to degrade
486 * 1 = cPCI Power supply output is OK
487 * FAL 0 = cPCI Power supply has failed
488 * 1 = cPCI Power supply output is OK
489 */
490 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
491 return 1;
492}
493
494/* Function Description: Read standard voltage and power supply status.
495 * Return : Always 1 byte. Status stored in bufdata.
496 */
497static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
498 unsigned char data,
499 char *bufdata)
500{
501 unsigned char tmp, ret = 0;
502 int i, j = 0;
503
504 tmp = data & pchild->voltage_mask;
505
506 /* Two channels are used to monitor voltage and power supply. */
507 if (tmp == pchild->voltage_mask) {
508 /* All bits are on. Voltage and power supply are okay. */
509 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
510 } else if (tmp == 0) {
511 /* All bits are off. Voltage and power supply are bad */
512 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
513 } else {
514 /* Either voltage or power supply has problem. */
515 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
516 if (pchild->voltage_mask & chnls_mask[i]) {
517 j++;
518
519 /* Break out when there is a mismatch. */
520 if (!(chnls_mask[i] & tmp))
521 break;
522 }
523 }
524
525 /* Make a wish that hardware will always use the
526 * first channel for voltage and the second for
527 * power supply.
528 */
529 if (j == 1)
530 ret = ENVCTRL_VOLTAGE_BAD;
531 else
532 ret = ENVCTRL_POWERSUPPLY_BAD;
533 }
534
535 bufdata[0] = ret;
536 return 1;
537}
538
539/* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
540 * Return: Number of read bytes. 0 for error.
541 */
542static ssize_t
543envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
544{
545 struct i2c_child_t *pchild;
546 unsigned char data[10];
547 int ret = 0;
548
549 /* Get the type of read as decided in ioctl() call.
550 * Find the appropriate i2c child.
551 * Get the data and put back to the user buffer.
552 */
553
554 switch ((int)(long)file->private_data) {
555 case ENVCTRL_RD_WARNING_TEMPERATURE:
556 if (warning_temperature == 0)
557 return 0;
558
559 data[0] = (unsigned char)(warning_temperature);
560 ret = 1;
561 if (copy_to_user(buf, data, ret))
562 ret = -EFAULT;
563 break;
564
565 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
566 if (shutdown_temperature == 0)
567 return 0;
568
569 data[0] = (unsigned char)(shutdown_temperature);
570 ret = 1;
571 if (copy_to_user(buf, data, ret))
572 ret = -EFAULT;
573 break;
574
575 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
576 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
577 return 0;
578 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
579 if (copy_to_user(buf, data, ret))
580 ret = -EFAULT;
581 break;
582
583 case ENVCTRL_RD_CPU_TEMPERATURE:
584 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
585 return 0;
586 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
587
588 /* Reset cpu to the default cpu0. */
589 if (copy_to_user(buf, data, ret))
590 ret = -EFAULT;
591 break;
592
593 case ENVCTRL_RD_CPU_VOLTAGE:
594 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
595 return 0;
596 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
597
598 /* Reset cpu to the default cpu0. */
599 if (copy_to_user(buf, data, ret))
600 ret = -EFAULT;
601 break;
602
603 case ENVCTRL_RD_SCSI_TEMPERATURE:
604 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
605 return 0;
606 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
607 if (copy_to_user(buf, data, ret))
608 ret = -EFAULT;
609 break;
610
611 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
612 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
613 return 0;
614 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
615 if (copy_to_user(buf, data, ret))
616 ret = -EFAULT;
617 break;
618
619 case ENVCTRL_RD_FAN_STATUS:
620 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
621 return 0;
622 data[0] = envctrl_i2c_read_8574(pchild->addr);
623 ret = envctrl_i2c_fan_status(pchild,data[0], data);
624 if (copy_to_user(buf, data, ret))
625 ret = -EFAULT;
626 break;
627
628 case ENVCTRL_RD_GLOBALADDRESS:
629 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
630 return 0;
631 data[0] = envctrl_i2c_read_8574(pchild->addr);
632 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
633 if (copy_to_user(buf, data, ret))
634 ret = -EFAULT;
635 break;
636
637 case ENVCTRL_RD_VOLTAGE_STATUS:
638 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
639 /* If voltage monitor not present, check for CPCI equivalent */
640 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
641 return 0;
642 data[0] = envctrl_i2c_read_8574(pchild->addr);
643 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
644 if (copy_to_user(buf, data, ret))
645 ret = -EFAULT;
646 break;
647
648 default:
649 break;
650
651 };
652
653 return ret;
654}
655
656/* Function Description: Command what to read. Mapped to user ioctl().
657 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
658 */
659static int
660envctrl_ioctl(struct inode *inode, struct file *file,
661 unsigned int cmd, unsigned long arg)
662{
663 char __user *infobuf;
664
665 switch (cmd) {
666 case ENVCTRL_RD_WARNING_TEMPERATURE:
667 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
668 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
669 case ENVCTRL_RD_FAN_STATUS:
670 case ENVCTRL_RD_VOLTAGE_STATUS:
671 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
672 case ENVCTRL_RD_SCSI_TEMPERATURE:
673 case ENVCTRL_RD_GLOBALADDRESS:
674 file->private_data = (void *)(long)cmd;
675 break;
676
677 case ENVCTRL_RD_CPU_TEMPERATURE:
678 case ENVCTRL_RD_CPU_VOLTAGE:
679 /* Check to see if application passes in any cpu number,
680 * the default is cpu0.
681 */
682 infobuf = (char __user *) arg;
683 if (infobuf == NULL) {
684 read_cpu = 0;
685 }else {
686 get_user(read_cpu, infobuf);
687 }
688
689 /* Save the command for use when reading. */
690 file->private_data = (void *)(long)cmd;
691 break;
692
693 default:
694 return -EINVAL;
695 };
696
697 return 0;
698}
699
700/* Function Description: open device. Mapped to user open().
701 * Return: Always 0.
702 */
703static int
704envctrl_open(struct inode *inode, struct file *file)
705{
706 file->private_data = NULL;
707 return 0;
708}
709
710/* Function Description: Open device. Mapped to user close().
711 * Return: Always 0.
712 */
713static int
714envctrl_release(struct inode *inode, struct file *file)
715{
716 return 0;
717}
718
719static struct file_operations envctrl_fops = {
720 .owner = THIS_MODULE,
721 .read = envctrl_read,
722 .ioctl = envctrl_ioctl,
723 .open = envctrl_open,
724 .release = envctrl_release,
725};
726
727static struct miscdevice envctrl_dev = {
728 ENVCTRL_MINOR,
729 "envctrl",
730 &envctrl_fops
731};
732
733/* Function Description: Set monitor type based on firmware description.
734 * Return: None.
735 */
736static void envctrl_set_mon(struct i2c_child_t *pchild,
737 char *chnl_desc,
738 int chnl_no)
739{
740 /* Firmware only has temperature type. It does not distinguish
741 * different kinds of temperatures. We use channel description
742 * to disinguish them.
743 */
744 if (!(strcmp(chnl_desc,"temp,cpu")) ||
745 !(strcmp(chnl_desc,"temp,cpu0")) ||
746 !(strcmp(chnl_desc,"temp,cpu1")) ||
747 !(strcmp(chnl_desc,"temp,cpu2")) ||
748 !(strcmp(chnl_desc,"temp,cpu3")))
749 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
750
751 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
752 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
753 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
754 !(strcmp(chnl_desc,"vddcore,cpu3")))
755 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
756
757 if (!(strcmp(chnl_desc,"temp,motherboard")))
758 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
759
760 if (!(strcmp(chnl_desc,"temp,scsi")))
761 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
762
763 if (!(strcmp(chnl_desc,"temp,ethernet")))
764 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
765}
766
767/* Function Description: Initialize monitor channel with channel desc,
768 * decoding tables, monitor type, optional properties.
769 * Return: None.
770 */
771static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
772{
773 char chnls_desc[CHANNEL_DESC_SZ];
774 int i = 0, len;
775 char *pos = chnls_desc;
776
777 /* Firmware describe channels into a stream separated by a '\0'. */
778 len = prom_getproperty(node, "channels-description", chnls_desc,
779 CHANNEL_DESC_SZ);
780 chnls_desc[CHANNEL_DESC_SZ - 1] = '\0';
781
782 while (len > 0) {
783 int l = strlen(pos) + 1;
784 envctrl_set_mon(pchild, pos, i++);
785 len -= l;
786 pos += l;
787 }
788
789 /* Get optional properties. */
790 len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
791 sizeof(warning_temperature));
792 len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
793 sizeof(shutdown_temperature));
794}
795
796/* Function Description: Initialize child device monitoring fan status.
797 * Return: None.
798 */
799static void envctrl_init_fanstat(struct i2c_child_t *pchild)
800{
801 int i;
802
803 /* Go through all channels and set up the mask. */
804 for (i = 0; i < pchild->total_chnls; i++)
805 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
806
807 /* We only need to know if this child has fan status monitored.
808 * We don't care which channels since we have the mask already.
809 */
810 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
811}
812
813/* Function Description: Initialize child device for global addressing line.
814 * Return: None.
815 */
816static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
817{
818 int i;
819
820 /* Voltage/PowerSupply monitoring is piggybacked
821 * with Global Address on CompactPCI. See comments
822 * within envctrl_i2c_globaladdr for bit assignments.
823 *
824 * The mask is created here by assigning mask bits to each
825 * bit position that represents PCF8584_VOLTAGE_TYPE data.
826 * Channel numbers are not consecutive within the globaladdr
827 * node (why?), so we use the actual counter value as chnls_mask
828 * index instead of the chnl_array[x].chnl_no value.
829 *
830 * NOTE: This loop could be replaced with a constant representing
831 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
832 */
833 for (i = 0; i < pchild->total_chnls; i++) {
834 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
835 pchild->voltage_mask |= chnls_mask[i];
836 }
837 }
838
839 /* We only need to know if this child has global addressing
840 * line monitored. We don't care which channels since we know
841 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
842 */
843 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
844}
845
846/* Initialize child device monitoring voltage status. */
847static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
848{
849 int i;
850
851 /* Go through all channels and set up the mask. */
852 for (i = 0; i < pchild->total_chnls; i++)
853 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
854
855 /* We only need to know if this child has voltage status monitored.
856 * We don't care which channels since we have the mask already.
857 */
858 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
859}
860
861/* Function Description: Initialize i2c child device.
862 * Return: None.
863 */
864static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
865 struct i2c_child_t *pchild)
866{
867 int node, len, i, tbls_size = 0;
868
869 node = edev_child->prom_node;
870
871 /* Get device address. */
872 len = prom_getproperty(node, "reg",
873 (char *) &(pchild->addr),
874 sizeof(pchild->addr));
875
876 /* Get tables property. Read firmware temperature tables. */
877 len = prom_getproperty(node, "translation",
878 (char *) pchild->tblprop_array,
879 (PCF8584_MAX_CHANNELS *
880 sizeof(struct pcf8584_tblprop)));
881 if (len > 0) {
882 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
883 for (i = 0; i < pchild->total_tbls; i++) {
884 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
885 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
886 }
887 }
888
889 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
890 if (pchild->tables == NULL){
891 printk("envctrl: Failed to allocate table.\n");
892 return;
893 }
894 len = prom_getproperty(node, "tables",
895 (char *) pchild->tables, tbls_size);
896 if (len <= 0) {
897 printk("envctrl: Failed to get table.\n");
898 return;
899 }
900 }
901
902 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
903 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
904 * "For Factory Use Only."
905 *
906 * We ignore the node on these platforms by assigning the
907 * 'NULL' monitor type.
908 */
909 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
910 int len;
911 char prop[56];
912
913 len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
914 if (0 < len && (0 == strncmp(prop, "SUNW,UltraSPARC-IIi-cEngine", len)))
915 {
916 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
917 pchild->mon_type[len] = ENVCTRL_NOMON;
918 }
919 return;
920 }
921 }
922
923 /* Get the monitor channels. */
924 len = prom_getproperty(node, "channels-in-use",
925 (char *) pchild->chnl_array,
926 (PCF8584_MAX_CHANNELS *
927 sizeof(struct pcf8584_channel)));
928 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
929
930 for (i = 0; i < pchild->total_chnls; i++) {
931 switch (pchild->chnl_array[i].type) {
932 case PCF8584_TEMP_TYPE:
933 envctrl_init_adc(pchild, node);
934 break;
935
936 case PCF8584_GLOBALADDR_TYPE:
937 envctrl_init_globaladdr(pchild);
938 i = pchild->total_chnls;
939 break;
940
941 case PCF8584_FANSTAT_TYPE:
942 envctrl_init_fanstat(pchild);
943 i = pchild->total_chnls;
944 break;
945
946 case PCF8584_VOLTAGE_TYPE:
947 if (pchild->i2ctype == I2C_ADC) {
948 envctrl_init_adc(pchild,node);
949 } else {
950 envctrl_init_voltage_status(pchild);
951 }
952 i = pchild->total_chnls;
953 break;
954
955 default:
956 break;
957 };
958 }
959}
960
961/* Function Description: Search the child device list for a device.
962 * Return : The i2c child if found. NULL otherwise.
963 */
964static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
965{
966 int i, j;
967
968 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
969 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
970 if (i2c_childlist[i].mon_type[j] == mon_type) {
971 return (struct i2c_child_t *)(&(i2c_childlist[i]));
972 }
973 }
974 }
975 return NULL;
976}
977
978static void envctrl_do_shutdown(void)
979{
980 static int inprog = 0;
981 static char *envp[] = {
982 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
983 char *argv[] = {
984 "/sbin/shutdown", "-h", "now", NULL };
985
986 if (inprog != 0)
987 return;
988
989 inprog = 1;
990 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
991 if (0 > execve("/sbin/shutdown", argv, envp)) {
992 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
993 inprog = 0; /* unlikely to succeed, but we could try again */
994 }
995}
996
997static struct task_struct *kenvctrld_task;
998
999static int kenvctrld(void *__unused)
1000{
1001 int poll_interval;
1002 int whichcpu;
1003 char tempbuf[10];
1004 struct i2c_child_t *cputemp;
1005
1006 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
1007 printk(KERN_ERR
1008 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1009 return -ENODEV;
1010 }
1011
Nishanth Aravamudancb39d262005-07-24 19:34:33 -07001012 poll_interval = 5000; /* TODO env_mon_interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1015 for (;;) {
Christoph Hellwig218b29e2005-08-09 12:30:07 -07001016 msleep_interruptible(poll_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Christoph Hellwig218b29e2005-08-09 12:30:07 -07001018 if (kthread_should_stop())
1019 break;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1022 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1023 ENVCTRL_CPUTEMP_MON,
1024 tempbuf)) {
1025 if (tempbuf[0] >= shutdown_temperature) {
1026 printk(KERN_CRIT
1027 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1028 "shutdown threshold %i C\n",
1029 current->comm, whichcpu,
1030 tempbuf[0], shutdown_temperature);
1031 envctrl_do_shutdown();
1032 }
1033 }
1034 }
1035 }
1036 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1037 return 0;
1038}
1039
1040static int __init envctrl_init(void)
1041{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct linux_ebus *ebus = NULL;
1043 struct linux_ebus_device *edev = NULL;
1044 struct linux_ebus_child *edev_child = NULL;
1045 int err, i = 0;
1046
1047 for_each_ebus(ebus) {
1048 for_each_ebusdev(edev, ebus) {
1049 if (!strcmp(edev->prom_name, "bbc")) {
1050 /* If we find a boot-bus controller node,
1051 * then this envctrl driver is not for us.
1052 */
1053 return -ENODEV;
1054 }
1055 }
1056 }
1057
1058 /* Traverse through ebus and ebus device list for i2c device and
1059 * adc and gpio nodes.
1060 */
1061 for_each_ebus(ebus) {
1062 for_each_ebusdev(edev, ebus) {
1063 if (!strcmp(edev->prom_name, "i2c")) {
1064 i2c = ioremap(edev->resource[0].start, 0x2);
1065 for_each_edevchild(edev, edev_child) {
1066 if (!strcmp("gpio", edev_child->prom_name)) {
1067 i2c_childlist[i].i2ctype = I2C_GPIO;
1068 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1069 }
1070 if (!strcmp("adc", edev_child->prom_name)) {
1071 i2c_childlist[i].i2ctype = I2C_ADC;
1072 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1073 }
1074 }
1075 goto done;
1076 }
1077 }
1078 }
1079
1080done:
1081 if (!edev) {
1082 printk("envctrl: I2C device not found.\n");
1083 return -ENODEV;
1084 }
1085
1086 /* Set device address. */
1087 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1088 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1089
1090 /* Set system clock and SCL frequencies. */
1091 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1092 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1093
1094 /* Enable serial interface. */
1095 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1096 udelay(200);
1097
1098 /* Register the device as a minor miscellaneous device. */
1099 err = misc_register(&envctrl_dev);
1100 if (err) {
1101 printk("envctrl: Unable to get misc minor %d\n",
1102 envctrl_dev.minor);
1103 goto out_iounmap;
1104 }
1105
1106 /* Note above traversal routine post-incremented 'i' to accommodate
1107 * a next child device, so we decrement before reverse-traversal of
1108 * child devices.
1109 */
1110 printk("envctrl: initialized ");
1111 for (--i; i >= 0; --i) {
1112 printk("[%s 0x%lx]%s",
1113 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1114 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1115 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1116 }
1117
Christoph Hellwig218b29e2005-08-09 12:30:07 -07001118 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1119 if (IS_ERR(kenvctrld_task)) {
David S. Miller38c18442005-08-09 14:43:14 -07001120 err = PTR_ERR(kenvctrld_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 goto out_deregister;
Christoph Hellwig218b29e2005-08-09 12:30:07 -07001122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return 0;
1125
1126out_deregister:
1127 misc_deregister(&envctrl_dev);
1128out_iounmap:
1129 iounmap(i2c);
1130 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1131 if (i2c_childlist[i].tables)
1132 kfree(i2c_childlist[i].tables);
1133 }
1134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137static void __exit envctrl_cleanup(void)
1138{
1139 int i;
1140
Christoph Hellwig218b29e2005-08-09 12:30:07 -07001141 kthread_stop(kenvctrld_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 iounmap(i2c);
1144 misc_deregister(&envctrl_dev);
1145
1146 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1147 if (i2c_childlist[i].tables)
1148 kfree(i2c_childlist[i].tables);
1149 }
1150}
1151
1152module_init(envctrl_init);
1153module_exit(envctrl_cleanup);
1154MODULE_LICENSE("GPL");