1 /* $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.
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
15 * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
16 * Eric Brower <ebrower@usa.net>
18 * DB - Audit every copy_to_user in envctrl_read.
19 * Daniele Bellucci <bellucda@tiscali.it>
22 #define __KERNEL_SYSCALLS__
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/delay.h>
29 #include <linux/ioport.h>
30 #include <linux/init.h>
31 #include <linux/miscdevice.h>
33 #include <linux/slab.h>
34 #include <linux/kernel.h>
37 #include <asm/uaccess.h>
38 #include <asm/envctrl.h>
41 #include <asm/unistd.h>
43 #define ENVCTRL_MINOR 162
45 #define PCF8584_ADDRESS 0x55
47 #define CONTROL_PIN 0x80
48 #define CONTROL_ES0 0x40
49 #define CONTROL_ES1 0x20
50 #define CONTROL_ES2 0x10
51 #define CONTROL_ENI 0x08
52 #define CONTROL_STA 0x04
53 #define CONTROL_STO 0x02
54 #define CONTROL_ACK 0x01
56 #define STATUS_PIN 0x80
57 #define STATUS_STS 0x20
58 #define STATUS_BER 0x10
59 #define STATUS_LRB 0x08
60 #define STATUS_AD0 0x08
61 #define STATUS_AAB 0x04
62 #define STATUS_LAB 0x02
63 #define STATUS_BB 0x01
68 #define BUS_CLK_90 0x00
69 #define BUS_CLK_45 0x01
70 #define BUS_CLK_11 0x02
71 #define BUS_CLK_1_5 0x03
79 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
80 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
82 /* Monitor type of i2c child device.
83 * Firmware definitions.
85 #define PCF8584_MAX_CHANNELS 8
86 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
87 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
88 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
89 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
91 /* Monitor type of i2c child device.
94 #define ENVCTRL_NOMON 0
95 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
96 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
97 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
98 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
100 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
101 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
102 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
103 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
105 /* Child device type.
106 * Driver definitions.
108 #define I2C_ADC 0 /* pcf8591 */
109 #define I2C_GPIO 1 /* pcf8571 */
111 /* Data read from child device may need to decode
112 * through a data table and a scale.
113 * Translation type as defined by firmware.
115 #define ENVCTRL_TRANSLATE_NO 0
116 #define ENVCTRL_TRANSLATE_PARTIAL 1
117 #define ENVCTRL_TRANSLATE_COMBINED 2
118 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
119 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
121 /* Driver miscellaneous definitions. */
122 #define ENVCTRL_MAX_CPU 4
123 #define CHANNEL_DESC_SZ 256
125 /* Mask values for combined GlobalAddress/PowerStatus node */
126 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
127 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
129 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
130 * (see envctrl_init_i2c_child)
132 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
134 #define PCF8584_DATA 0x00
135 #define PCF8584_CSR 0x01
137 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
138 * Property of a port or channel as defined by the firmware.
140 struct pcf8584_channel {
141 unsigned char chnl_no;
142 unsigned char io_direction;
147 /* Each child device may have one or more tables of bytes to help decode
148 * data. Table property as defined by the firmware.
150 struct pcf8584_tblprop {
153 unsigned int offset; /* offset from the beginning of the table */
159 /* Either ADC or GPIO. */
160 unsigned char i2ctype;
162 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
165 unsigned int total_chnls; /* Number of monitor channels. */
166 unsigned char fan_mask; /* Byte mask for fan status channels. */
167 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
168 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
170 /* Properties of all monitor channels. */
171 unsigned int total_tbls; /* Number of monitor tables. */
172 char *tables; /* Pointer to table(s). */
173 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
174 char mon_type[PCF8584_MAX_CHANNELS];
177 static void __iomem *i2c;
178 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
179 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
180 static unsigned int warning_temperature = 0;
181 static unsigned int shutdown_temperature = 0;
182 static char read_cpu;
184 /* Forward declarations. */
185 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
187 /* Function Description: Test the PIN bit (Pending Interrupt Not)
188 * to test when serial transmission is completed .
191 static void envtrl_i2c_test_pin(void)
195 while (--limit > 0) {
196 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
202 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
205 /* Function Description: Test busy bit.
208 static void envctrl_i2c_test_bb(void)
212 while (--limit > 0) {
213 /* Busy bit 0 means busy. */
214 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
220 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
223 /* Function Description: Send the address for a read access.
224 * Return : 0 if not acknowledged, otherwise acknowledged.
226 static int envctrl_i2c_read_addr(unsigned char addr)
228 envctrl_i2c_test_bb();
231 writeb(addr + 1, i2c + PCF8584_DATA);
233 envctrl_i2c_test_bb();
235 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
238 envtrl_i2c_test_pin();
240 /* CSR 0 means acknowledged. */
241 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
242 return readb(i2c + PCF8584_DATA);
244 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
249 /* Function Description: Send the address for write mode.
252 static void envctrl_i2c_write_addr(unsigned char addr)
254 envctrl_i2c_test_bb();
255 writeb(addr, i2c + PCF8584_DATA);
257 /* Generate Start condition. */
258 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
261 /* Function Description: Read 1 byte of data from addr
262 * set by envctrl_i2c_read_addr()
263 * Return : Data from address set by envctrl_i2c_read_addr().
265 static unsigned char envctrl_i2c_read_data(void)
267 envtrl_i2c_test_pin();
268 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
269 return readb(i2c + PCF8584_DATA);
272 /* Function Description: Instruct the device which port to read data from.
275 static void envctrl_i2c_write_data(unsigned char port)
277 envtrl_i2c_test_pin();
278 writeb(port, i2c + PCF8584_DATA);
281 /* Function Description: Generate Stop condition after last byte is sent.
284 static void envctrl_i2c_stop(void)
286 envtrl_i2c_test_pin();
287 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
290 /* Function Description: Read adc device.
291 * Return : Data at address and port.
293 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
296 envctrl_i2c_write_addr(addr);
298 /* Setup port to read. */
299 envctrl_i2c_write_data(port);
303 envctrl_i2c_read_addr(addr);
305 /* Do a single byte read and send stop. */
306 envctrl_i2c_read_data();
309 return readb(i2c + PCF8584_DATA);
312 /* Function Description: Read gpio device.
313 * Return : Data at address.
315 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
319 envctrl_i2c_read_addr(addr);
321 /* Do a single byte read and send stop. */
322 rd = envctrl_i2c_read_data();
327 /* Function Description: Decode data read from an adc device using firmware
329 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
331 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
332 int scale, char *tbl, char *bufdata)
336 switch (translate_type) {
337 case ENVCTRL_TRANSLATE_NO:
338 /* No decode necessary. */
343 case ENVCTRL_TRANSLATE_FULL:
344 /* Decode this way: data = table[data]. */
346 bufdata[0] = tbl[data];
349 case ENVCTRL_TRANSLATE_SCALE:
350 /* Decode this way: data = table[data]/scale */
351 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
352 len = strlen(bufdata);
353 bufdata[len - 1] = bufdata[len - 2];
354 bufdata[len - 2] = '.';
364 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
365 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
367 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
368 char mon_type, unsigned char *bufdata)
374 /* Find the right monitor type and channel. */
375 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
376 if (pchild->mon_type[i] == mon_type) {
386 /* Read data from address and port. */
387 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
388 (unsigned char)pchild->chnl_array[i].chnl_no);
390 /* Find decoding table. */
391 tbl = pchild->tables + pchild->tblprop_array[i].offset;
393 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
394 pchild->tblprop_array[i].scale,
398 /* Function Description: Read noncpu-related data such as motherboard
400 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
402 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
403 char mon_type, unsigned char *bufdata)
409 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
410 if (pchild->mon_type[i] == mon_type)
414 if (i >= PCF8584_MAX_CHANNELS)
417 /* Read data from address and port. */
418 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
419 (unsigned char)pchild->chnl_array[i].chnl_no);
421 /* Find decoding table. */
422 tbl = pchild->tables + pchild->tblprop_array[i].offset;
424 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
425 pchild->tblprop_array[i].scale,
429 /* Function Description: Read fan status.
430 * Return : Always 1 byte. Status stored in bufdata.
432 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
436 unsigned char tmp, ret = 0;
439 tmp = data & pchild->fan_mask;
441 if (tmp == pchild->fan_mask) {
442 /* All bits are on. All fans are functioning. */
443 ret = ENVCTRL_ALL_FANS_GOOD;
444 } else if (tmp == 0) {
445 /* No bits are on. No fans are functioning. */
446 ret = ENVCTRL_ALL_FANS_BAD;
448 /* Go through all channels, mark 'on' the matched bits.
449 * Notice that fan_mask may have discontiguous bits but
450 * return mask are always contiguous. For example if we
451 * monitor 4 fans at channels 0,1,2,4, the return mask
452 * should be 00010000 if only fan at channel 4 is working.
454 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
455 if (pchild->fan_mask & chnls_mask[i]) {
456 if (!(chnls_mask[i] & tmp))
457 ret |= chnls_mask[j];
468 /* Function Description: Read global addressing line.
469 * Return : Always 1 byte. Status stored in bufdata.
471 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
475 /* Translatation table is not necessary, as global
476 * addr is the integer value of the GA# bits.
478 * NOTE: MSB is documented as zero, but I see it as '1' always....
480 * -----------------------------------------------
481 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
482 * -----------------------------------------------
483 * GA0 - GA4 integer value of Global Address (backplane slot#)
484 * DEG 0 = cPCI Power supply output is starting to degrade
485 * 1 = cPCI Power supply output is OK
486 * FAL 0 = cPCI Power supply has failed
487 * 1 = cPCI Power supply output is OK
489 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
493 /* Function Description: Read standard voltage and power supply status.
494 * Return : Always 1 byte. Status stored in bufdata.
496 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
500 unsigned char tmp, ret = 0;
503 tmp = data & pchild->voltage_mask;
505 /* Two channels are used to monitor voltage and power supply. */
506 if (tmp == pchild->voltage_mask) {
507 /* All bits are on. Voltage and power supply are okay. */
508 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
509 } else if (tmp == 0) {
510 /* All bits are off. Voltage and power supply are bad */
511 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
513 /* Either voltage or power supply has problem. */
514 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
515 if (pchild->voltage_mask & chnls_mask[i]) {
518 /* Break out when there is a mismatch. */
519 if (!(chnls_mask[i] & tmp))
524 /* Make a wish that hardware will always use the
525 * first channel for voltage and the second for
529 ret = ENVCTRL_VOLTAGE_BAD;
531 ret = ENVCTRL_POWERSUPPLY_BAD;
538 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
539 * Return: Number of read bytes. 0 for error.
542 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
544 struct i2c_child_t *pchild;
545 unsigned char data[10];
548 /* Get the type of read as decided in ioctl() call.
549 * Find the appropriate i2c child.
550 * Get the data and put back to the user buffer.
553 switch ((int)(long)file->private_data) {
554 case ENVCTRL_RD_WARNING_TEMPERATURE:
555 if (warning_temperature == 0)
558 data[0] = (unsigned char)(warning_temperature);
560 if (copy_to_user(buf, data, ret))
564 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
565 if (shutdown_temperature == 0)
568 data[0] = (unsigned char)(shutdown_temperature);
570 if (copy_to_user(buf, data, ret))
574 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
575 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
577 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
578 if (copy_to_user(buf, data, ret))
582 case ENVCTRL_RD_CPU_TEMPERATURE:
583 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
585 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
587 /* Reset cpu to the default cpu0. */
588 if (copy_to_user(buf, data, ret))
592 case ENVCTRL_RD_CPU_VOLTAGE:
593 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
595 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
597 /* Reset cpu to the default cpu0. */
598 if (copy_to_user(buf, data, ret))
602 case ENVCTRL_RD_SCSI_TEMPERATURE:
603 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
605 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
606 if (copy_to_user(buf, data, ret))
610 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
611 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
613 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
614 if (copy_to_user(buf, data, ret))
618 case ENVCTRL_RD_FAN_STATUS:
619 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
621 data[0] = envctrl_i2c_read_8574(pchild->addr);
622 ret = envctrl_i2c_fan_status(pchild,data[0], data);
623 if (copy_to_user(buf, data, ret))
627 case ENVCTRL_RD_GLOBALADDRESS:
628 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
630 data[0] = envctrl_i2c_read_8574(pchild->addr);
631 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
632 if (copy_to_user(buf, data, ret))
636 case ENVCTRL_RD_VOLTAGE_STATUS:
637 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
638 /* If voltage monitor not present, check for CPCI equivalent */
639 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
641 data[0] = envctrl_i2c_read_8574(pchild->addr);
642 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
643 if (copy_to_user(buf, data, ret))
655 /* Function Description: Command what to read. Mapped to user ioctl().
656 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
659 envctrl_ioctl(struct inode *inode, struct file *file,
660 unsigned int cmd, unsigned long arg)
662 char __user *infobuf;
665 case ENVCTRL_RD_WARNING_TEMPERATURE:
666 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
667 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
668 case ENVCTRL_RD_FAN_STATUS:
669 case ENVCTRL_RD_VOLTAGE_STATUS:
670 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
671 case ENVCTRL_RD_SCSI_TEMPERATURE:
672 case ENVCTRL_RD_GLOBALADDRESS:
673 file->private_data = (void *)(long)cmd;
676 case ENVCTRL_RD_CPU_TEMPERATURE:
677 case ENVCTRL_RD_CPU_VOLTAGE:
678 /* Check to see if application passes in any cpu number,
679 * the default is cpu0.
681 infobuf = (char __user *) arg;
682 if (infobuf == NULL) {
685 get_user(read_cpu, infobuf);
688 /* Save the command for use when reading. */
689 file->private_data = (void *)(long)cmd;
699 /* Function Description: open device. Mapped to user open().
703 envctrl_open(struct inode *inode, struct file *file)
705 file->private_data = NULL;
709 /* Function Description: Open device. Mapped to user close().
713 envctrl_release(struct inode *inode, struct file *file)
718 static struct file_operations envctrl_fops = {
719 .owner = THIS_MODULE,
720 .read = envctrl_read,
721 .ioctl = envctrl_ioctl,
722 .open = envctrl_open,
723 .release = envctrl_release,
726 static struct miscdevice envctrl_dev = {
732 /* Function Description: Set monitor type based on firmware description.
735 static void envctrl_set_mon(struct i2c_child_t *pchild,
739 /* Firmware only has temperature type. It does not distinguish
740 * different kinds of temperatures. We use channel description
741 * to disinguish them.
743 if (!(strcmp(chnl_desc,"temp,cpu")) ||
744 !(strcmp(chnl_desc,"temp,cpu0")) ||
745 !(strcmp(chnl_desc,"temp,cpu1")) ||
746 !(strcmp(chnl_desc,"temp,cpu2")) ||
747 !(strcmp(chnl_desc,"temp,cpu3")))
748 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
750 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
751 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
752 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
753 !(strcmp(chnl_desc,"vddcore,cpu3")))
754 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
756 if (!(strcmp(chnl_desc,"temp,motherboard")))
757 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
759 if (!(strcmp(chnl_desc,"temp,scsi")))
760 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
762 if (!(strcmp(chnl_desc,"temp,ethernet")))
763 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
766 /* Function Description: Initialize monitor channel with channel desc,
767 * decoding tables, monitor type, optional properties.
770 static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
772 char chnls_desc[CHANNEL_DESC_SZ];
774 char *pos = chnls_desc;
776 /* Firmware describe channels into a stream separated by a '\0'. */
777 len = prom_getproperty(node, "channels-description", chnls_desc,
779 chnls_desc[CHANNEL_DESC_SZ - 1] = '\0';
782 int l = strlen(pos) + 1;
783 envctrl_set_mon(pchild, pos, i++);
788 /* Get optional properties. */
789 len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
790 sizeof(warning_temperature));
791 len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
792 sizeof(shutdown_temperature));
795 /* Function Description: Initialize child device monitoring fan status.
798 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
802 /* Go through all channels and set up the mask. */
803 for (i = 0; i < pchild->total_chnls; i++)
804 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
806 /* We only need to know if this child has fan status monitored.
807 * We don't care which channels since we have the mask already.
809 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
812 /* Function Description: Initialize child device for global addressing line.
815 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
819 /* Voltage/PowerSupply monitoring is piggybacked
820 * with Global Address on CompactPCI. See comments
821 * within envctrl_i2c_globaladdr for bit assignments.
823 * The mask is created here by assigning mask bits to each
824 * bit position that represents PCF8584_VOLTAGE_TYPE data.
825 * Channel numbers are not consecutive within the globaladdr
826 * node (why?), so we use the actual counter value as chnls_mask
827 * index instead of the chnl_array[x].chnl_no value.
829 * NOTE: This loop could be replaced with a constant representing
830 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
832 for (i = 0; i < pchild->total_chnls; i++) {
833 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
834 pchild->voltage_mask |= chnls_mask[i];
838 /* We only need to know if this child has global addressing
839 * line monitored. We don't care which channels since we know
840 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
842 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
845 /* Initialize child device monitoring voltage status. */
846 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
850 /* Go through all channels and set up the mask. */
851 for (i = 0; i < pchild->total_chnls; i++)
852 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
854 /* We only need to know if this child has voltage status monitored.
855 * We don't care which channels since we have the mask already.
857 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
860 /* Function Description: Initialize i2c child device.
863 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
864 struct i2c_child_t *pchild)
866 int node, len, i, tbls_size = 0;
868 node = edev_child->prom_node;
870 /* Get device address. */
871 len = prom_getproperty(node, "reg",
872 (char *) &(pchild->addr),
873 sizeof(pchild->addr));
875 /* Get tables property. Read firmware temperature tables. */
876 len = prom_getproperty(node, "translation",
877 (char *) pchild->tblprop_array,
878 (PCF8584_MAX_CHANNELS *
879 sizeof(struct pcf8584_tblprop)));
881 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
882 for (i = 0; i < pchild->total_tbls; i++) {
883 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
884 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
888 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
889 if (pchild->tables == NULL){
890 printk("envctrl: Failed to allocate table.\n");
893 len = prom_getproperty(node, "tables",
894 (char *) pchild->tables, tbls_size);
896 printk("envctrl: Failed to get table.\n");
901 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
902 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
903 * "For Factory Use Only."
905 * We ignore the node on these platforms by assigning the
906 * 'NULL' monitor type.
908 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
912 len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
913 if (0 < len && (0 == strncmp(prop, "SUNW,UltraSPARC-IIi-cEngine", len)))
915 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
916 pchild->mon_type[len] = ENVCTRL_NOMON;
922 /* Get the monitor channels. */
923 len = prom_getproperty(node, "channels-in-use",
924 (char *) pchild->chnl_array,
925 (PCF8584_MAX_CHANNELS *
926 sizeof(struct pcf8584_channel)));
927 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
929 for (i = 0; i < pchild->total_chnls; i++) {
930 switch (pchild->chnl_array[i].type) {
931 case PCF8584_TEMP_TYPE:
932 envctrl_init_adc(pchild, node);
935 case PCF8584_GLOBALADDR_TYPE:
936 envctrl_init_globaladdr(pchild);
937 i = pchild->total_chnls;
940 case PCF8584_FANSTAT_TYPE:
941 envctrl_init_fanstat(pchild);
942 i = pchild->total_chnls;
945 case PCF8584_VOLTAGE_TYPE:
946 if (pchild->i2ctype == I2C_ADC) {
947 envctrl_init_adc(pchild,node);
949 envctrl_init_voltage_status(pchild);
951 i = pchild->total_chnls;
960 /* Function Description: Search the child device list for a device.
961 * Return : The i2c child if found. NULL otherwise.
963 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
967 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
968 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
969 if (i2c_childlist[i].mon_type[j] == mon_type) {
970 return (struct i2c_child_t *)(&(i2c_childlist[i]));
977 static void envctrl_do_shutdown(void)
979 static int inprog = 0;
980 static char *envp[] = {
981 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
983 "/sbin/shutdown", "-h", "now", NULL };
989 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
990 if (0 > execve("/sbin/shutdown", argv, envp)) {
991 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
992 inprog = 0; /* unlikely to succeed, but we could try again */
996 static struct task_struct *kenvctrld_task;
998 static int kenvctrld(void *__unused)
1003 struct i2c_child_t *cputemp;
1005 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
1007 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1011 poll_interval = 5000; /* TODO env_mon_interval */
1013 daemonize("kenvctrld");
1014 allow_signal(SIGKILL);
1016 kenvctrld_task = current;
1018 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1020 if(msleep_interruptible(poll_interval))
1023 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1024 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1025 ENVCTRL_CPUTEMP_MON,
1027 if (tempbuf[0] >= shutdown_temperature) {
1029 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1030 "shutdown threshold %i C\n",
1031 current->comm, whichcpu,
1032 tempbuf[0], shutdown_temperature);
1033 envctrl_do_shutdown();
1038 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1042 static int __init envctrl_init(void)
1045 struct linux_ebus *ebus = NULL;
1046 struct linux_ebus_device *edev = NULL;
1047 struct linux_ebus_child *edev_child = NULL;
1050 for_each_ebus(ebus) {
1051 for_each_ebusdev(edev, ebus) {
1052 if (!strcmp(edev->prom_name, "bbc")) {
1053 /* If we find a boot-bus controller node,
1054 * then this envctrl driver is not for us.
1061 /* Traverse through ebus and ebus device list for i2c device and
1062 * adc and gpio nodes.
1064 for_each_ebus(ebus) {
1065 for_each_ebusdev(edev, ebus) {
1066 if (!strcmp(edev->prom_name, "i2c")) {
1067 i2c = ioremap(edev->resource[0].start, 0x2);
1068 for_each_edevchild(edev, edev_child) {
1069 if (!strcmp("gpio", edev_child->prom_name)) {
1070 i2c_childlist[i].i2ctype = I2C_GPIO;
1071 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1073 if (!strcmp("adc", edev_child->prom_name)) {
1074 i2c_childlist[i].i2ctype = I2C_ADC;
1075 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1085 printk("envctrl: I2C device not found.\n");
1089 /* Set device address. */
1090 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1091 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1093 /* Set system clock and SCL frequencies. */
1094 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1095 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1097 /* Enable serial interface. */
1098 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1101 /* Register the device as a minor miscellaneous device. */
1102 err = misc_register(&envctrl_dev);
1104 printk("envctrl: Unable to get misc minor %d\n",
1109 /* Note above traversal routine post-incremented 'i' to accommodate
1110 * a next child device, so we decrement before reverse-traversal of
1113 printk("envctrl: initialized ");
1114 for (--i; i >= 0; --i) {
1115 printk("[%s 0x%lx]%s",
1116 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1117 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1118 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1121 err = kernel_thread(kenvctrld, NULL, CLONE_FS | CLONE_FILES);
1123 goto out_deregister;
1128 misc_deregister(&envctrl_dev);
1131 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1132 if (i2c_childlist[i].tables)
1133 kfree(i2c_childlist[i].tables);
1141 static void __exit envctrl_cleanup(void)
1145 if (NULL != kenvctrld_task) {
1146 force_sig(SIGKILL, kenvctrld_task);
1148 struct task_struct *p;
1151 read_lock(&tasklist_lock);
1152 for_each_process(p) {
1153 if (p == kenvctrld_task) {
1158 read_unlock(&tasklist_lock);
1165 kenvctrld_task = NULL;
1169 misc_deregister(&envctrl_dev);
1171 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1172 if (i2c_childlist[i].tables)
1173 kfree(i2c_childlist[i].tables);
1177 module_init(envctrl_init);
1178 module_exit(envctrl_cleanup);
1179 MODULE_LICENSE("GPL");