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 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
26 #include <linux/delay.h>
27 #include <linux/ioport.h>
28 #include <linux/init.h>
29 #include <linux/miscdevice.h>
31 #include <linux/slab.h>
32 #include <linux/kernel.h>
35 #include <asm/uaccess.h>
36 #include <asm/envctrl.h>
38 #define __KERNEL_SYSCALLS__
40 #include <asm/unistd.h>
42 #define ENVCTRL_MINOR 162
44 #define PCF8584_ADDRESS 0x55
46 #define CONTROL_PIN 0x80
47 #define CONTROL_ES0 0x40
48 #define CONTROL_ES1 0x20
49 #define CONTROL_ES2 0x10
50 #define CONTROL_ENI 0x08
51 #define CONTROL_STA 0x04
52 #define CONTROL_STO 0x02
53 #define CONTROL_ACK 0x01
55 #define STATUS_PIN 0x80
56 #define STATUS_STS 0x20
57 #define STATUS_BER 0x10
58 #define STATUS_LRB 0x08
59 #define STATUS_AD0 0x08
60 #define STATUS_AAB 0x04
61 #define STATUS_LAB 0x02
62 #define STATUS_BB 0x01
67 #define BUS_CLK_90 0x00
68 #define BUS_CLK_45 0x01
69 #define BUS_CLK_11 0x02
70 #define BUS_CLK_1_5 0x03
78 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
79 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
81 /* Monitor type of i2c child device.
82 * Firmware definitions.
84 #define PCF8584_MAX_CHANNELS 8
85 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
86 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
87 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
88 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
90 /* Monitor type of i2c child device.
93 #define ENVCTRL_NOMON 0
94 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
95 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
96 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
97 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
99 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
100 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
101 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
102 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
104 /* Child device type.
105 * Driver definitions.
107 #define I2C_ADC 0 /* pcf8591 */
108 #define I2C_GPIO 1 /* pcf8571 */
110 /* Data read from child device may need to decode
111 * through a data table and a scale.
112 * Translation type as defined by firmware.
114 #define ENVCTRL_TRANSLATE_NO 0
115 #define ENVCTRL_TRANSLATE_PARTIAL 1
116 #define ENVCTRL_TRANSLATE_COMBINED 2
117 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
118 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
120 /* Driver miscellaneous definitions. */
121 #define ENVCTRL_MAX_CPU 4
122 #define CHANNEL_DESC_SZ 256
124 /* Mask values for combined GlobalAddress/PowerStatus node */
125 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
126 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
128 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
129 * (see envctrl_init_i2c_child)
131 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
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.
141 struct pcf8584_channel {
142 unsigned char chnl_no;
143 unsigned char io_direction;
148 /* Each child device may have one or more tables of bytes to help decode
149 * data. Table property as defined by the firmware.
151 struct pcf8584_tblprop {
154 unsigned int offset; /* offset from the beginning of the table */
160 /* Either ADC or GPIO. */
161 unsigned char i2ctype;
163 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
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];
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];
178 volatile static struct pcf8584_reg *i2c = NULL;
179 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
180 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
181 static unsigned int warning_temperature = 0;
182 static unsigned int shutdown_temperature = 0;
183 static char read_cpu;
185 /* Forward declarations. */
186 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
188 /* Function description: Read a byte from an i2c controller register.
189 * Return: A byte from the passed in address.
191 static inline unsigned char envctrl_readb(volatile unsigned char *p)
196 /* Function description: Write a byte to an i2c controller register.
199 static inline void envctrl_writeb(unsigned char val, volatile unsigned char *p)
204 /* Function Description: Test the PIN bit (Pending Interrupt Not)
205 * to test when serial transmission is completed .
208 static void envtrl_i2c_test_pin(void)
212 while (--limit > 0) {
213 if (!(envctrl_readb(&i2c->csr) & STATUS_PIN))
219 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
222 /* Function Description: Test busy bit.
225 static void envctrl_i2c_test_bb(void)
229 while (--limit > 0) {
230 /* Busy bit 0 means busy. */
231 if (envctrl_readb(&i2c->csr) & STATUS_BB)
237 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
240 /* Function Description: Send the address for a read access.
241 * Return : 0 if not acknowledged, otherwise acknowledged.
243 static int envctrl_i2c_read_addr(unsigned char addr)
245 envctrl_i2c_test_bb();
248 envctrl_writeb(addr + 1, &i2c->data);
250 envctrl_i2c_test_bb();
252 envctrl_writeb(OBD_SEND_START, &i2c->csr);
255 envtrl_i2c_test_pin();
257 /* CSR 0 means acknowledged. */
258 if (!(envctrl_readb(&i2c->csr) & STATUS_LRB)) {
259 return envctrl_readb(&i2c->data);
261 envctrl_writeb(OBD_SEND_STOP, &i2c->csr);
266 /* Function Description: Send the address for write mode.
269 static void envctrl_i2c_write_addr(unsigned char addr)
271 envctrl_i2c_test_bb();
272 envctrl_writeb(addr, &i2c->data);
274 /* Generate Start condition. */
275 envctrl_writeb(OBD_SEND_START, &i2c->csr);
278 /* Function Description: Read 1 byte of data from addr
279 * set by envctrl_i2c_read_addr()
280 * Return : Data from address set by envctrl_i2c_read_addr().
282 static unsigned char envctrl_i2c_read_data(void)
284 envtrl_i2c_test_pin();
285 envctrl_writeb(CONTROL_ES0, &i2c->csr); /* Send neg ack. */
286 return envctrl_readb(&i2c->data);
289 /* Function Description: Instruct the device which port to read data from.
292 static void envctrl_i2c_write_data(unsigned char port)
294 envtrl_i2c_test_pin();
295 envctrl_writeb(port, &i2c->data);
298 /* Function Description: Generate Stop condition after last byte is sent.
301 static void envctrl_i2c_stop(void)
303 envtrl_i2c_test_pin();
304 envctrl_writeb(OBD_SEND_STOP, &i2c->csr);
307 /* Function Description: Read adc device.
308 * Return : Data at address and port.
310 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
313 envctrl_i2c_write_addr(addr);
315 /* Setup port to read. */
316 envctrl_i2c_write_data(port);
320 envctrl_i2c_read_addr(addr);
322 /* Do a single byte read and send stop. */
323 envctrl_i2c_read_data();
326 return envctrl_readb(&i2c->data);
329 /* Function Description: Read gpio device.
330 * Return : Data at address.
332 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
336 envctrl_i2c_read_addr(addr);
338 /* Do a single byte read and send stop. */
339 rd = envctrl_i2c_read_data();
344 /* Function Description: Decode data read from an adc device using firmware
346 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
348 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
349 int scale, char *tbl, char *bufdata)
353 switch (translate_type) {
354 case ENVCTRL_TRANSLATE_NO:
355 /* No decode necessary. */
360 case ENVCTRL_TRANSLATE_FULL:
361 /* Decode this way: data = table[data]. */
363 bufdata[0] = tbl[data];
366 case ENVCTRL_TRANSLATE_SCALE:
367 /* Decode this way: data = table[data]/scale */
368 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
369 len = strlen(bufdata);
370 bufdata[len - 1] = bufdata[len - 2];
371 bufdata[len - 2] = '.';
381 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
382 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
384 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
385 char mon_type, unsigned char *bufdata)
391 /* Find the right monitor type and channel. */
392 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
393 if (pchild->mon_type[i] == mon_type) {
403 /* Read data from address and port. */
404 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
405 (unsigned char)pchild->chnl_array[i].chnl_no);
407 /* Find decoding table. */
408 tbl = pchild->tables + pchild->tblprop_array[i].offset;
410 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
411 pchild->tblprop_array[i].scale,
415 /* Function Description: Read noncpu-related data such as motherboard
417 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
419 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
420 char mon_type, unsigned char *bufdata)
426 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
427 if (pchild->mon_type[i] == mon_type)
431 if (i >= PCF8584_MAX_CHANNELS)
434 /* Read data from address and port. */
435 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
436 (unsigned char)pchild->chnl_array[i].chnl_no);
438 /* Find decoding table. */
439 tbl = pchild->tables + pchild->tblprop_array[i].offset;
441 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
442 pchild->tblprop_array[i].scale,
446 /* Function Description: Read fan status.
447 * Return : Always 1 byte. Status stored in bufdata.
449 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
453 unsigned char tmp, ret = 0;
456 tmp = data & pchild->fan_mask;
458 if (tmp == pchild->fan_mask) {
459 /* All bits are on. All fans are functioning. */
460 ret = ENVCTRL_ALL_FANS_GOOD;
461 } else if (tmp == 0) {
462 /* No bits are on. No fans are functioning. */
463 ret = ENVCTRL_ALL_FANS_BAD;
465 /* Go through all channels, mark 'on' the matched bits.
466 * Notice that fan_mask may have discontiguous bits but
467 * return mask are always contiguous. For example if we
468 * monitor 4 fans at channels 0,1,2,4, the return mask
469 * should be 00010000 if only fan at channel 4 is working.
471 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
472 if (pchild->fan_mask & chnls_mask[i]) {
473 if (!(chnls_mask[i] & tmp))
474 ret |= chnls_mask[j];
485 /* Function Description: Read global addressing line.
486 * Return : Always 1 byte. Status stored in bufdata.
488 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
492 /* Translatation table is not necessary, as global
493 * addr is the integer value of the GA# bits.
495 * NOTE: MSB is documented as zero, but I see it as '1' always....
497 * -----------------------------------------------
498 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
499 * -----------------------------------------------
500 * GA0 - GA4 integer value of Global Address (backplane slot#)
501 * DEG 0 = cPCI Power supply output is starting to degrade
502 * 1 = cPCI Power supply output is OK
503 * FAL 0 = cPCI Power supply has failed
504 * 1 = cPCI Power supply output is OK
506 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
510 /* Function Description: Read standard voltage and power supply status.
511 * Return : Always 1 byte. Status stored in bufdata.
513 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
517 unsigned char tmp, ret = 0;
520 tmp = data & pchild->voltage_mask;
522 /* Two channels are used to monitor voltage and power supply. */
523 if (tmp == pchild->voltage_mask) {
524 /* All bits are on. Voltage and power supply are okay. */
525 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
526 } else if (tmp == 0) {
527 /* All bits are off. Voltage and power supply are bad */
528 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
530 /* Either voltage or power supply has problem. */
531 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
532 if (pchild->voltage_mask & chnls_mask[i]) {
535 /* Break out when there is a mismatch. */
536 if (!(chnls_mask[i] & tmp))
541 /* Make a wish that hardware will always use the
542 * first channel for voltage and the second for
546 ret = ENVCTRL_VOLTAGE_BAD;
548 ret = ENVCTRL_POWERSUPPLY_BAD;
555 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
556 * Return: Number of read bytes. 0 for error.
559 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
561 struct i2c_child_t *pchild;
562 unsigned char data[10];
565 /* Get the type of read as decided in ioctl() call.
566 * Find the appropriate i2c child.
567 * Get the data and put back to the user buffer.
570 switch ((int)(long)file->private_data) {
571 case ENVCTRL_RD_WARNING_TEMPERATURE:
572 if (warning_temperature == 0)
575 data[0] = (unsigned char)(warning_temperature);
577 if (copy_to_user(buf, data, ret))
581 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
582 if (shutdown_temperature == 0)
585 data[0] = (unsigned char)(shutdown_temperature);
587 if (copy_to_user(buf, data, ret))
591 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
592 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
594 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
595 if (copy_to_user(buf, data, ret))
599 case ENVCTRL_RD_CPU_TEMPERATURE:
600 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
602 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
604 /* Reset cpu to the default cpu0. */
605 if (copy_to_user(buf, data, ret))
609 case ENVCTRL_RD_CPU_VOLTAGE:
610 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
612 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
614 /* Reset cpu to the default cpu0. */
615 if (copy_to_user(buf, data, ret))
619 case ENVCTRL_RD_SCSI_TEMPERATURE:
620 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
622 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
623 if (copy_to_user(buf, data, ret))
627 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
628 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
630 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
631 if (copy_to_user(buf, data, ret))
635 case ENVCTRL_RD_FAN_STATUS:
636 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
638 data[0] = envctrl_i2c_read_8574(pchild->addr);
639 ret = envctrl_i2c_fan_status(pchild,data[0], data);
640 if (copy_to_user(buf, data, ret))
644 case ENVCTRL_RD_GLOBALADDRESS:
645 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
647 data[0] = envctrl_i2c_read_8574(pchild->addr);
648 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
649 if (copy_to_user(buf, data, ret))
653 case ENVCTRL_RD_VOLTAGE_STATUS:
654 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
655 /* If voltage monitor not present, check for CPCI equivalent */
656 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
658 data[0] = envctrl_i2c_read_8574(pchild->addr);
659 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
660 if (copy_to_user(buf, data, ret))
672 /* Function Description: Command what to read. Mapped to user ioctl().
673 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
676 envctrl_ioctl(struct inode *inode, struct file *file,
677 unsigned int cmd, unsigned long arg)
679 char __user *infobuf;
682 case ENVCTRL_RD_WARNING_TEMPERATURE:
683 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
684 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
685 case ENVCTRL_RD_FAN_STATUS:
686 case ENVCTRL_RD_VOLTAGE_STATUS:
687 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
688 case ENVCTRL_RD_SCSI_TEMPERATURE:
689 case ENVCTRL_RD_GLOBALADDRESS:
690 file->private_data = (void *)(long)cmd;
693 case ENVCTRL_RD_CPU_TEMPERATURE:
694 case ENVCTRL_RD_CPU_VOLTAGE:
695 /* Check to see if application passes in any cpu number,
696 * the default is cpu0.
698 infobuf = (char __user *) arg;
699 if (infobuf == NULL) {
702 get_user(read_cpu, infobuf);
705 /* Save the command for use when reading. */
706 file->private_data = (void *)(long)cmd;
716 /* Function Description: open device. Mapped to user open().
720 envctrl_open(struct inode *inode, struct file *file)
722 file->private_data = NULL;
726 /* Function Description: Open device. Mapped to user close().
730 envctrl_release(struct inode *inode, struct file *file)
735 static struct file_operations envctrl_fops = {
736 .owner = THIS_MODULE,
737 .read = envctrl_read,
738 .ioctl = envctrl_ioctl,
739 .open = envctrl_open,
740 .release = envctrl_release,
743 static struct miscdevice envctrl_dev = {
749 /* Function Description: Set monitor type based on firmware description.
752 static void envctrl_set_mon(struct i2c_child_t *pchild,
756 /* Firmware only has temperature type. It does not distinguish
757 * different kinds of temperatures. We use channel description
758 * to disinguish them.
760 if (!(strcmp(chnl_desc,"temp,cpu")) ||
761 !(strcmp(chnl_desc,"temp,cpu0")) ||
762 !(strcmp(chnl_desc,"temp,cpu1")) ||
763 !(strcmp(chnl_desc,"temp,cpu2")) ||
764 !(strcmp(chnl_desc,"temp,cpu3")))
765 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
767 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
768 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
769 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
770 !(strcmp(chnl_desc,"vddcore,cpu3")))
771 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
773 if (!(strcmp(chnl_desc,"temp,motherboard")))
774 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
776 if (!(strcmp(chnl_desc,"temp,scsi")))
777 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
779 if (!(strcmp(chnl_desc,"temp,ethernet")))
780 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
783 /* Function Description: Initialize monitor channel with channel desc,
784 * decoding tables, monitor type, optional properties.
787 static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
789 char chnls_desc[CHANNEL_DESC_SZ];
791 char *pos = chnls_desc;
793 /* Firmware describe channels into a stream separated by a '\0'. */
794 len = prom_getproperty(node, "channels-description", chnls_desc,
796 chnls_desc[CHANNEL_DESC_SZ - 1] = '\0';
799 int l = strlen(pos) + 1;
800 envctrl_set_mon(pchild, pos, i++);
805 /* Get optional properties. */
806 len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
807 sizeof(warning_temperature));
808 len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
809 sizeof(shutdown_temperature));
812 /* Function Description: Initialize child device monitoring fan status.
815 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
819 /* Go through all channels and set up the mask. */
820 for (i = 0; i < pchild->total_chnls; i++)
821 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
823 /* We only need to know if this child has fan status monitored.
824 * We don't care which channels since we have the mask already.
826 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
829 /* Function Description: Initialize child device for global addressing line.
832 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
836 /* Voltage/PowerSupply monitoring is piggybacked
837 * with Global Address on CompactPCI. See comments
838 * within envctrl_i2c_globaladdr for bit assignments.
840 * The mask is created here by assigning mask bits to each
841 * bit position that represents PCF8584_VOLTAGE_TYPE data.
842 * Channel numbers are not consecutive within the globaladdr
843 * node (why?), so we use the actual counter value as chnls_mask
844 * index instead of the chnl_array[x].chnl_no value.
846 * NOTE: This loop could be replaced with a constant representing
847 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
849 for (i = 0; i < pchild->total_chnls; i++) {
850 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
851 pchild->voltage_mask |= chnls_mask[i];
855 /* We only need to know if this child has global addressing
856 * line monitored. We don't care which channels since we know
857 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
859 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
862 /* Initialize child device monitoring voltage status. */
863 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
867 /* Go through all channels and set up the mask. */
868 for (i = 0; i < pchild->total_chnls; i++)
869 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
871 /* We only need to know if this child has voltage status monitored.
872 * We don't care which channels since we have the mask already.
874 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
877 /* Function Description: Initialize i2c child device.
880 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
881 struct i2c_child_t *pchild)
883 int node, len, i, tbls_size = 0;
885 node = edev_child->prom_node;
887 /* Get device address. */
888 len = prom_getproperty(node, "reg",
889 (char *) &(pchild->addr),
890 sizeof(pchild->addr));
892 /* Get tables property. Read firmware temperature tables. */
893 len = prom_getproperty(node, "translation",
894 (char *) pchild->tblprop_array,
895 (PCF8584_MAX_CHANNELS *
896 sizeof(struct pcf8584_tblprop)));
898 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
899 for (i = 0; i < pchild->total_tbls; i++) {
900 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
901 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
905 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
906 if (pchild->tables == NULL){
907 printk("envctrl: Failed to allocate table.\n");
910 len = prom_getproperty(node, "tables",
911 (char *) pchild->tables, tbls_size);
913 printk("envctrl: Failed to get table.\n");
918 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
919 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
920 * "For Factory Use Only."
922 * We ignore the node on these platforms by assigning the
923 * 'NULL' monitor type.
925 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
929 len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
930 if (0 < len && (0 == strncmp(prop, "SUNW,UltraSPARC-IIi-cEngine", len)))
932 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
933 pchild->mon_type[len] = ENVCTRL_NOMON;
939 /* Get the monitor channels. */
940 len = prom_getproperty(node, "channels-in-use",
941 (char *) pchild->chnl_array,
942 (PCF8584_MAX_CHANNELS *
943 sizeof(struct pcf8584_channel)));
944 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
946 for (i = 0; i < pchild->total_chnls; i++) {
947 switch (pchild->chnl_array[i].type) {
948 case PCF8584_TEMP_TYPE:
949 envctrl_init_adc(pchild, node);
952 case PCF8584_GLOBALADDR_TYPE:
953 envctrl_init_globaladdr(pchild);
954 i = pchild->total_chnls;
957 case PCF8584_FANSTAT_TYPE:
958 envctrl_init_fanstat(pchild);
959 i = pchild->total_chnls;
962 case PCF8584_VOLTAGE_TYPE:
963 if (pchild->i2ctype == I2C_ADC) {
964 envctrl_init_adc(pchild,node);
966 envctrl_init_voltage_status(pchild);
968 i = pchild->total_chnls;
977 /* Function Description: Search the child device list for a device.
978 * Return : The i2c child if found. NULL otherwise.
980 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
984 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
985 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
986 if (i2c_childlist[i].mon_type[j] == mon_type) {
987 return (struct i2c_child_t *)(&(i2c_childlist[i]));
994 static void envctrl_do_shutdown(void)
996 static int inprog = 0;
997 static char *envp[] = {
998 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
1000 "/sbin/shutdown", "-h", "now", NULL };
1006 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
1007 if (0 > execve("/sbin/shutdown", argv, envp)) {
1008 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
1009 inprog = 0; /* unlikely to succeed, but we could try again */
1013 static struct task_struct *kenvctrld_task;
1015 static int kenvctrld(void *__unused)
1020 struct i2c_child_t *cputemp;
1022 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
1024 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1028 poll_interval = 5 * HZ; /* TODO env_mon_interval */
1030 daemonize("kenvctrld");
1031 allow_signal(SIGKILL);
1033 kenvctrld_task = current;
1035 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1037 current->state = TASK_INTERRUPTIBLE;
1038 schedule_timeout(poll_interval);
1040 if(signal_pending(current))
1043 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1044 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1045 ENVCTRL_CPUTEMP_MON,
1047 if (tempbuf[0] >= shutdown_temperature) {
1049 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1050 "shutdown threshold %i C\n",
1051 current->comm, whichcpu,
1052 tempbuf[0], shutdown_temperature);
1053 envctrl_do_shutdown();
1058 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1062 static int __init envctrl_init(void)
1065 struct linux_ebus *ebus = NULL;
1066 struct linux_ebus_device *edev = NULL;
1067 struct linux_ebus_child *edev_child = NULL;
1070 for_each_ebus(ebus) {
1071 for_each_ebusdev(edev, ebus) {
1072 if (!strcmp(edev->prom_name, "bbc")) {
1073 /* If we find a boot-bus controller node,
1074 * then this envctrl driver is not for us.
1081 /* Traverse through ebus and ebus device list for i2c device and
1082 * adc and gpio nodes.
1084 for_each_ebus(ebus) {
1085 for_each_ebusdev(edev, ebus) {
1086 if (!strcmp(edev->prom_name, "i2c")) {
1087 i2c = ioremap( edev->resource[0].start,
1088 sizeof(struct pcf8584_reg));
1089 for_each_edevchild(edev, edev_child) {
1090 if (!strcmp("gpio", edev_child->prom_name)) {
1091 i2c_childlist[i].i2ctype = I2C_GPIO;
1092 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1094 if (!strcmp("adc", edev_child->prom_name)) {
1095 i2c_childlist[i].i2ctype = I2C_ADC;
1096 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1106 printk("envctrl: I2C device not found.\n");
1110 /* Set device address. */
1111 envctrl_writeb(CONTROL_PIN, &i2c->csr);
1112 envctrl_writeb(PCF8584_ADDRESS, &i2c->data);
1114 /* Set system clock and SCL frequencies. */
1115 envctrl_writeb(CONTROL_PIN | CONTROL_ES1, &i2c->csr);
1116 envctrl_writeb(CLK_4_43 | BUS_CLK_90, &i2c->data);
1118 /* Enable serial interface. */
1119 envctrl_writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, &i2c->csr);
1122 /* Register the device as a minor miscellaneous device. */
1123 err = misc_register(&envctrl_dev);
1125 printk("envctrl: Unable to get misc minor %d\n",
1130 /* Note above traversal routine post-incremented 'i' to accommodate
1131 * a next child device, so we decrement before reverse-traversal of
1134 printk("envctrl: initialized ");
1135 for (--i; i >= 0; --i) {
1136 printk("[%s 0x%lx]%s",
1137 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1138 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1139 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1142 err = kernel_thread(kenvctrld, NULL, CLONE_FS | CLONE_FILES);
1144 goto out_deregister;
1149 misc_deregister(&envctrl_dev);
1152 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1153 if (i2c_childlist[i].tables)
1154 kfree(i2c_childlist[i].tables);
1162 static void __exit envctrl_cleanup(void)
1166 if (NULL != kenvctrld_task) {
1167 force_sig(SIGKILL, kenvctrld_task);
1169 struct task_struct *p;
1172 read_lock(&tasklist_lock);
1173 for_each_process(p) {
1174 if (p == kenvctrld_task) {
1179 read_unlock(&tasklist_lock);
1184 current->state = TASK_INTERRUPTIBLE;
1185 schedule_timeout(HZ);
1187 kenvctrld_task = NULL;
1191 misc_deregister(&envctrl_dev);
1193 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1194 if (i2c_childlist[i].tables)
1195 kfree(i2c_childlist[i].tables);
1199 module_init(envctrl_init);
1200 module_exit(envctrl_cleanup);
1201 MODULE_LICENSE("GPL");