1 /* $Id: envctrl.c,v 1.24.2.1 2002/01/15 09:01:39 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>
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/miscdevice.h>
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
32 #include <asm/uaccess.h>
33 #include <asm/envctrl.h>
35 #define __KERNEL_SYSCALLS__
37 #include <asm/unistd.h>
39 #define ENVCTRL_MINOR 162
41 #define PCF8584_ADDRESS 0x55
43 #define CONTROL_PIN 0x80
44 #define CONTROL_ES0 0x40
45 #define CONTROL_ES1 0x20
46 #define CONTROL_ES2 0x10
47 #define CONTROL_ENI 0x08
48 #define CONTROL_STA 0x04
49 #define CONTROL_STO 0x02
50 #define CONTROL_ACK 0x01
52 #define STATUS_PIN 0x80
53 #define STATUS_STS 0x20
54 #define STATUS_BER 0x10
55 #define STATUS_LRB 0x08
56 #define STATUS_AD0 0x08
57 #define STATUS_AAB 0x04
58 #define STATUS_LAB 0x02
59 #define STATUS_BB 0x01
64 #define BUS_CLK_90 0x00
65 #define BUS_CLK_45 0x01
66 #define BUS_CLK_11 0x02
67 #define BUS_CLK_1_5 0x03
75 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
76 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
78 /* Monitor type of i2c child device.
79 * Firmware definitions.
81 #define PCF8584_MAX_CHANNELS 8
82 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
83 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
84 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
85 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
87 /* Monitor type of i2c child device.
90 #define ENVCTRL_NOMON 0
91 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
92 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
93 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
94 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
96 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
97 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
98 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
99 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
101 /* Child device type.
102 * Driver definitions.
104 #define I2C_ADC 0 /* pcf8591 */
105 #define I2C_GPIO 1 /* pcf8571 */
107 /* Data read from child device may need to decode
108 * through a data table and a scale.
109 * Translation type as defined by firmware.
111 #define ENVCTRL_TRANSLATE_NO 0
112 #define ENVCTRL_TRANSLATE_PARTIAL 1
113 #define ENVCTRL_TRANSLATE_COMBINED 2
114 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
115 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
117 /* Driver miscellaneous definitions. */
118 #define ENVCTRL_MAX_CPU 4
119 #define CHANNEL_DESC_SZ 256
121 /* Mask values for combined GlobalAddress/PowerStatus node */
122 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
123 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
125 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
126 * (see envctrl_init_i2c_child)
128 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
135 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
136 * Property of a port or channel as defined by the firmware.
138 struct pcf8584_channel {
139 unsigned char chnl_no;
140 unsigned char io_direction;
145 /* Each child device may have one or more tables of bytes to help decode
146 * data. Table property as defined by the firmware.
148 struct pcf8584_tblprop {
151 unsigned int offset; /* offset from the beginning of the table */
157 /* Either ADC or GPIO. */
158 unsigned char i2ctype;
160 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
163 unsigned int total_chnls; /* Number of monitor channels. */
164 unsigned char fan_mask; /* Byte mask for fan status channels. */
165 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
166 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
168 /* Properties of all monitor channels. */
169 unsigned int total_tbls; /* Number of monitor tables. */
170 char *tables; /* Pointer to table(s). */
171 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
172 char mon_type[PCF8584_MAX_CHANNELS];
175 volatile static struct pcf8584_reg *i2c = NULL;
176 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
177 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
178 static unsigned int warning_temperature = 0;
179 static unsigned int shutdown_temperature = 0;
180 static char read_cpu;
182 /* Forward declarations. */
183 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
185 /* Function description: Read a byte from an i2c controller register.
186 * Return: A byte from the passed in address.
188 static inline unsigned char envctrl_readb(volatile unsigned char *p)
193 /* Function description: Write a byte to an i2c controller register.
196 static inline void envctrl_writeb(unsigned char val, volatile unsigned char *p)
201 /* Function Description: Test the PIN bit (Pending Interrupt Not)
202 * to test when serial transmission is completed .
205 static void envtrl_i2c_test_pin(void)
209 while (--limit > 0) {
210 if (!(envctrl_readb(&i2c->csr) & STATUS_PIN))
216 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
219 /* Function Description: Test busy bit.
222 static void envctrl_i2c_test_bb(void)
226 while (--limit > 0) {
227 /* Busy bit 0 means busy. */
228 if (envctrl_readb(&i2c->csr) & STATUS_BB)
234 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
237 /* Function Description: Send the address for a read access.
238 * Return : 0 if not acknowledged, otherwise acknowledged.
240 static int envctrl_i2c_read_addr(unsigned char addr)
242 envctrl_i2c_test_bb();
245 envctrl_writeb(addr + 1, &i2c->data);
247 envctrl_i2c_test_bb();
249 envctrl_writeb(OBD_SEND_START, &i2c->csr);
252 envtrl_i2c_test_pin();
254 /* CSR 0 means acknowledged. */
255 if (!(envctrl_readb(&i2c->csr) & STATUS_LRB)) {
256 return envctrl_readb(&i2c->data);
258 envctrl_writeb(OBD_SEND_STOP, &i2c->csr);
263 /* Function Description: Send the address for write mode.
266 static void envctrl_i2c_write_addr(unsigned char addr)
268 envctrl_i2c_test_bb();
269 envctrl_writeb(addr, &i2c->data);
271 /* Generate Start condition. */
272 envctrl_writeb(OBD_SEND_START, &i2c->csr);
275 /* Function Description: Read 1 byte of data from addr
276 * set by envctrl_i2c_read_addr()
277 * Return : Data from address set by envctrl_i2c_read_addr().
279 static unsigned char envctrl_i2c_read_data(void)
281 envtrl_i2c_test_pin();
282 envctrl_writeb(CONTROL_ES0, &i2c->csr); /* Send neg ack. */
283 return envctrl_readb(&i2c->data);
286 /* Function Description: Instruct the device which port to read data from.
289 static void envctrl_i2c_write_data(unsigned char port)
291 envtrl_i2c_test_pin();
292 envctrl_writeb(port, &i2c->data);
295 /* Function Description: Generate Stop condition after last byte is sent.
298 static void envctrl_i2c_stop(void)
300 envtrl_i2c_test_pin();
301 envctrl_writeb(OBD_SEND_STOP, &i2c->csr);
304 /* Function Description: Read adc device.
305 * Return : Data at address and port.
307 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
310 envctrl_i2c_write_addr(addr);
312 /* Setup port to read. */
313 envctrl_i2c_write_data(port);
317 envctrl_i2c_read_addr(addr);
319 /* Do a single byte read and send stop. */
320 envctrl_i2c_read_data();
323 return envctrl_readb(&i2c->data);
326 /* Function Description: Read gpio device.
327 * Return : Data at address.
329 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
333 envctrl_i2c_read_addr(addr);
335 /* Do a single byte read and send stop. */
336 rd = envctrl_i2c_read_data();
341 /* Function Description: Decode data read from an adc device using firmware
343 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
345 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
346 int scale, char *tbl, char *bufdata)
350 switch (translate_type) {
351 case ENVCTRL_TRANSLATE_NO:
352 /* No decode necessary. */
357 case ENVCTRL_TRANSLATE_FULL:
358 /* Decode this way: data = table[data]. */
360 bufdata[0] = tbl[data];
363 case ENVCTRL_TRANSLATE_SCALE:
364 /* Decode this way: data = table[data]/scale */
365 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
366 len = strlen(bufdata);
367 bufdata[len - 1] = bufdata[len - 2];
368 bufdata[len - 2] = '.';
378 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
379 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
381 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
382 char mon_type, unsigned char *bufdata)
388 /* Find the right monitor type and channel. */
389 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
390 if (pchild->mon_type[i] == mon_type) {
400 /* Read data from address and port. */
401 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
402 (unsigned char)pchild->chnl_array[i].chnl_no);
404 /* Find decoding table. */
405 tbl = pchild->tables + pchild->tblprop_array[i].offset;
407 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
408 pchild->tblprop_array[i].scale,
412 /* Function Description: Read noncpu-related data such as motherboard
414 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
416 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
417 char mon_type, unsigned char *bufdata)
423 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
424 if (pchild->mon_type[i] == mon_type)
428 if (i >= PCF8584_MAX_CHANNELS)
431 /* Read data from address and port. */
432 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
433 (unsigned char)pchild->chnl_array[i].chnl_no);
435 /* Find decoding table. */
436 tbl = pchild->tables + pchild->tblprop_array[i].offset;
438 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
439 pchild->tblprop_array[i].scale,
443 /* Function Description: Read fan status.
444 * Return : Always 1 byte. Status stored in bufdata.
446 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
450 unsigned char tmp, ret = 0;
453 tmp = data & pchild->fan_mask;
455 if (tmp == pchild->fan_mask) {
456 /* All bits are on. All fans are functioning. */
457 ret = ENVCTRL_ALL_FANS_GOOD;
458 } else if (tmp == 0) {
459 /* No bits are on. No fans are functioning. */
460 ret = ENVCTRL_ALL_FANS_BAD;
462 /* Go through all channels, mark 'on' the matched bits.
463 * Notice that fan_mask may have discontiguous bits but
464 * return mask are always contiguous. For example if we
465 * monitor 4 fans at channels 0,1,2,4, the return mask
466 * should be 00010000 if only fan at channel 4 is working.
468 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
469 if (pchild->fan_mask & chnls_mask[i]) {
470 if (!(chnls_mask[i] & tmp))
471 ret |= chnls_mask[j];
482 /* Function Description: Read global addressing line.
483 * Return : Always 1 byte. Status stored in bufdata.
485 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
489 /* Translatation table is not necessary, as global
490 * addr is the integer value of the GA# bits.
492 * NOTE: MSB is documented as zero, but I see it as '1' always....
494 * -----------------------------------------------
495 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
496 * -----------------------------------------------
497 * GA0 - GA4 integer value of Global Address (backplane slot#)
498 * DEG 0 = cPCI Power supply output is starting to degrade
499 * 1 = cPCI Power supply output is OK
500 * FAL 0 = cPCI Power supply has failed
501 * 1 = cPCI Power supply output is OK
503 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
507 /* Function Description: Read standard voltage and power supply status.
508 * Return : Always 1 byte. Status stored in bufdata.
510 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
514 unsigned char tmp, ret = 0;
517 tmp = data & pchild->voltage_mask;
519 /* Two channels are used to monitor voltage and power supply. */
520 if (tmp == pchild->voltage_mask) {
521 /* All bits are on. Voltage and power supply are okay. */
522 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
523 } else if (tmp == 0) {
524 /* All bits are off. Voltage and power supply are bad */
525 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
527 /* Either voltage or power supply has problem. */
528 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
529 if (pchild->voltage_mask & chnls_mask[i]) {
532 /* Break out when there is a mismatch. */
533 if (!(chnls_mask[i] & tmp))
538 /* Make a wish that hardware will always use the
539 * first channel for voltage and the second for
543 ret = ENVCTRL_VOLTAGE_BAD;
545 ret = ENVCTRL_POWERSUPPLY_BAD;
552 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
553 * Return: Number of read bytes. 0 for error.
556 envctrl_read(struct file *file, char *buf, size_t count, loff_t *ppos)
558 struct i2c_child_t *pchild;
559 unsigned char data[10];
562 /* Get the type of read as decided in ioctl() call.
563 * Find the appropriate i2c child.
564 * Get the data and put back to the user buffer.
567 switch ((int)(long)file->private_data) {
568 case ENVCTRL_RD_WARNING_TEMPERATURE:
569 if (warning_temperature == 0)
572 data[0] = (unsigned char)(warning_temperature);
574 copy_to_user((unsigned char *)buf, data, ret);
577 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
578 if (shutdown_temperature == 0)
581 data[0] = (unsigned char)(shutdown_temperature);
583 copy_to_user((unsigned char *)buf, data, ret);
586 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
587 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
589 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
590 copy_to_user((unsigned char *)buf, data, ret);
593 case ENVCTRL_RD_CPU_TEMPERATURE:
594 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
596 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
598 /* Reset cpu to the default cpu0. */
599 copy_to_user((unsigned char *)buf, data, ret);
602 case ENVCTRL_RD_CPU_VOLTAGE:
603 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
605 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
607 /* Reset cpu to the default cpu0. */
608 copy_to_user((unsigned char *)buf, data, ret);
611 case ENVCTRL_RD_SCSI_TEMPERATURE:
612 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
614 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
615 copy_to_user((unsigned char *)buf, data, ret);
618 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
619 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
621 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
622 copy_to_user((unsigned char *)buf, data, ret);
625 case ENVCTRL_RD_FAN_STATUS:
626 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
628 data[0] = envctrl_i2c_read_8574(pchild->addr);
629 ret = envctrl_i2c_fan_status(pchild,data[0], data);
630 copy_to_user((unsigned char *)buf, data, ret);
633 case ENVCTRL_RD_GLOBALADDRESS:
634 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
636 data[0] = envctrl_i2c_read_8574(pchild->addr);
637 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
638 copy_to_user((unsigned char *)buf, data, ret);
641 case ENVCTRL_RD_VOLTAGE_STATUS:
642 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
643 /* If voltage monitor not present, check for CPCI equivalent */
644 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
646 data[0] = envctrl_i2c_read_8574(pchild->addr);
647 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
648 copy_to_user((unsigned char *)buf, data, ret);
659 /* Function Description: Command what to read. Mapped to user ioctl().
660 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
663 envctrl_ioctl(struct inode *inode, struct file *file,
664 unsigned int cmd, unsigned long arg)
669 case ENVCTRL_RD_WARNING_TEMPERATURE:
670 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
671 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
672 case ENVCTRL_RD_FAN_STATUS:
673 case ENVCTRL_RD_VOLTAGE_STATUS:
674 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
675 case ENVCTRL_RD_SCSI_TEMPERATURE:
676 case ENVCTRL_RD_GLOBALADDRESS:
677 file->private_data = (void *)(long)cmd;
680 case ENVCTRL_RD_CPU_TEMPERATURE:
681 case ENVCTRL_RD_CPU_VOLTAGE:
682 /* Check to see if application passes in any cpu number,
683 * the default is cpu0.
685 infobuf = (char *) arg;
686 if (infobuf == NULL) {
689 get_user(read_cpu, infobuf);
692 /* Save the command for use when reading. */
693 file->private_data = (void *)(long)cmd;
703 /* Function Description: open device. Mapped to user open().
707 envctrl_open(struct inode *inode, struct file *file)
709 file->private_data = 0;
714 /* Function Description: Open device. Mapped to user close().
718 envctrl_release(struct inode *inode, struct file *file)
724 static struct file_operations envctrl_fops = {
727 ioctl: envctrl_ioctl,
729 release: envctrl_release,
732 static struct miscdevice envctrl_dev = {
738 /* Function Description: Set monitor type based on firmware description.
741 static void envctrl_set_mon(struct i2c_child_t *pchild,
745 /* Firmware only has temperature type. It does not distinguish
746 * different kinds of temperatures. We use channel description
747 * to disinguish them.
749 if (!(strcmp(chnl_desc,"temp,cpu")) ||
750 !(strcmp(chnl_desc,"temp,cpu0")) ||
751 !(strcmp(chnl_desc,"temp,cpu1")) ||
752 !(strcmp(chnl_desc,"temp,cpu2")) ||
753 !(strcmp(chnl_desc,"temp,cpu3")))
754 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
756 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
757 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
758 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
759 !(strcmp(chnl_desc,"vddcore,cpu3")))
760 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
762 if (!(strcmp(chnl_desc,"temp,motherboard")))
763 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
765 if (!(strcmp(chnl_desc,"temp,scsi")))
766 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
768 if (!(strcmp(chnl_desc,"temp,ethernet")))
769 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
772 /* Function Description: Initialize monitor channel with channel desc,
773 * decoding tables, monitor type, optional properties.
776 static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
778 char chnls_desc[CHANNEL_DESC_SZ];
780 char *pos = chnls_desc;
782 /* Firmware describe channels into a stream separated by a '\0'. */
783 len = prom_getproperty(node, "channels-description", chnls_desc,
785 chnls_desc[CHANNEL_DESC_SZ - 1] = '\0';
788 int l = strlen(pos) + 1;
789 envctrl_set_mon(pchild, pos, i++);
794 /* Get optional properties. */
795 len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
796 sizeof(warning_temperature));
797 len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
798 sizeof(shutdown_temperature));
801 /* Function Description: Initialize child device monitoring fan status.
804 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
808 /* Go through all channels and set up the mask. */
809 for (i = 0; i < pchild->total_chnls; i++)
810 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
812 /* We only need to know if this child has fan status monitored.
813 * We dont care which channels since we have the mask already.
815 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
818 /* Function Description: Initialize child device for global addressing line.
821 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
825 /* Voltage/PowerSupply monitoring is piggybacked
826 * with Global Address on CompactPCI. See comments
827 * within envctrl_i2c_globaladdr for bit assignments.
829 * The mask is created here by assigning mask bits to each
830 * bit position that represents PCF8584_VOLTAGE_TYPE data.
831 * Channel numbers are not consecutive within the globaladdr
832 * node (why?), so we use the actual counter value as chnls_mask
833 * index instead of the chnl_array[x].chnl_no value.
835 * NOTE: This loop could be replaced with a constant representing
836 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
838 for (i = 0; i < pchild->total_chnls; i++) {
839 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
840 pchild->voltage_mask |= chnls_mask[i];
844 /* We only need to know if this child has global addressing
845 * line monitored. We dont care which channels since we know
846 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
848 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
851 /* Initialize child device monitoring voltage status. */
852 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
856 /* Go through all channels and set up the mask. */
857 for (i = 0; i < pchild->total_chnls; i++)
858 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
860 /* We only need to know if this child has voltage status monitored.
861 * We dont care which channels since we have the mask already.
863 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
866 /* Function Description: Initialize i2c child device.
869 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
870 struct i2c_child_t *pchild)
872 int node, len, i, tbls_size = 0;
874 node = edev_child->prom_node;
876 /* Get device address. */
877 len = prom_getproperty(node, "reg",
878 (char *) &(pchild->addr),
879 sizeof(pchild->addr));
881 /* Get tables property. Read firmware temperature tables. */
882 len = prom_getproperty(node, "translation",
883 (char *) pchild->tblprop_array,
884 (PCF8584_MAX_CHANNELS *
885 sizeof(struct pcf8584_tblprop)));
887 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
888 for (i = 0; i < pchild->total_tbls; i++) {
889 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
890 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
894 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
895 len = prom_getproperty(node, "tables",
896 (char *) pchild->tables, tbls_size);
898 printk("envctrl: Failed to get table.\n");
903 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
904 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
905 * "For Factory Use Only."
907 * We ignore the node on these platforms by assigning the
908 * 'NULL' monitor type.
910 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
914 len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
915 if (0 < len && (0 == strncmp(prop, "SUNW,UltraSPARC-IIi-cEngine", len)))
917 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
918 pchild->mon_type[len] = ENVCTRL_NOMON;
924 /* Get the monitor channels. */
925 len = prom_getproperty(node, "channels-in-use",
926 (char *) pchild->chnl_array,
927 (PCF8584_MAX_CHANNELS *
928 sizeof(struct pcf8584_channel)));
929 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
931 for (i = 0; i < pchild->total_chnls; i++) {
932 switch (pchild->chnl_array[i].type) {
933 case PCF8584_TEMP_TYPE:
934 envctrl_init_adc(pchild, node);
937 case PCF8584_GLOBALADDR_TYPE:
938 envctrl_init_globaladdr(pchild);
939 i = pchild->total_chnls;
942 case PCF8584_FANSTAT_TYPE:
943 envctrl_init_fanstat(pchild);
944 i = pchild->total_chnls;
947 case PCF8584_VOLTAGE_TYPE:
948 if (pchild->i2ctype == I2C_ADC) {
949 envctrl_init_adc(pchild,node);
951 envctrl_init_voltage_status(pchild);
953 i = pchild->total_chnls;
962 /* Function Description: Search the child device list for a device.
963 * Return : The i2c child if found. NULL otherwise.
965 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
969 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
970 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
971 if (i2c_childlist[i].mon_type[j] == mon_type) {
972 return (struct i2c_child_t *)(&(i2c_childlist[i]));
979 static void envctrl_do_shutdown(void)
981 static int inprog = 0;
982 static char *envp[] = {
983 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
985 "/sbin/shutdown", "-h", "now", NULL };
991 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
992 if (0 > execve("/sbin/shutdown", argv, envp)) {
993 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
994 inprog = 0; /* unlikely to succeed, but we could try again */
998 static struct task_struct *kenvctrld_task;
1000 static int kenvctrld(void *__unused)
1005 struct i2c_child_t *cputemp;
1007 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
1009 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1013 poll_interval = 5 * HZ; /* TODO env_mon_interval */
1016 strcpy(current->comm, "kenvctrld");
1017 kenvctrld_task = current;
1019 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1021 current->state = TASK_INTERRUPTIBLE;
1022 schedule_timeout(poll_interval);
1023 current->state = TASK_RUNNING;
1025 if(signal_pending(current))
1028 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1029 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1030 ENVCTRL_CPUTEMP_MON,
1032 if (tempbuf[0] >= shutdown_temperature) {
1034 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1035 "shutdown threshold %i C\n",
1036 current->comm, whichcpu,
1037 tempbuf[0], shutdown_temperature);
1038 envctrl_do_shutdown();
1043 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1047 static int __init envctrl_init(void)
1050 struct linux_ebus *ebus = NULL;
1051 struct linux_ebus_device *edev = NULL;
1052 struct linux_ebus_child *edev_child = NULL;
1055 for_each_ebus(ebus) {
1056 for_each_ebusdev(edev, ebus) {
1057 if (!strcmp(edev->prom_name, "bbc")) {
1058 /* If we find a boot-bus controller node,
1059 * then this envctrl driver is not for us.
1066 /* Traverse through ebus and ebus device list for i2c device and
1067 * adc and gpio nodes.
1069 for_each_ebus(ebus) {
1070 for_each_ebusdev(edev, ebus) {
1071 if (!strcmp(edev->prom_name, "i2c")) {
1072 i2c = ioremap( edev->resource[0].start,
1073 sizeof(struct pcf8584_reg));
1074 for_each_edevchild(edev, edev_child) {
1075 if (!strcmp("gpio", edev_child->prom_name)) {
1076 i2c_childlist[i].i2ctype = I2C_GPIO;
1077 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1079 if (!strcmp("adc", edev_child->prom_name)) {
1080 i2c_childlist[i].i2ctype = I2C_ADC;
1081 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1091 printk("envctrl: I2C device not found.\n");
1095 /* Set device address. */
1096 envctrl_writeb(CONTROL_PIN, &i2c->csr);
1097 envctrl_writeb(PCF8584_ADDRESS, &i2c->data);
1099 /* Set system clock and SCL frequencies. */
1100 envctrl_writeb(CONTROL_PIN | CONTROL_ES1, &i2c->csr);
1101 envctrl_writeb(CLK_4_43 | BUS_CLK_90, &i2c->data);
1103 /* Enable serial interface. */
1104 envctrl_writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, &i2c->csr);
1107 /* Register the device as a minor miscellaneous device. */
1108 err = misc_register(&envctrl_dev);
1110 printk("envctrl: Unable to get misc minor %d\n",
1115 /* Note above traversal routine post-incremented 'i' to accomodate
1116 * a next child device, so we decrement before reverse-traversal of
1119 printk("envctrl: initialized ");
1120 for (--i; i >= 0; --i) {
1121 printk("[%s 0x%lx]%s",
1122 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1123 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1124 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1127 err = kernel_thread(kenvctrld, NULL, CLONE_FS | CLONE_FILES);
1129 goto out_deregister;
1134 misc_deregister(&envctrl_dev);
1137 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1138 if (i2c_childlist[i].tables)
1139 kfree(i2c_childlist[i].tables);
1147 static void __exit envctrl_cleanup(void)
1151 if (NULL != kenvctrld_task) {
1152 force_sig(SIGKILL, kenvctrld_task);
1154 struct task_struct *p;
1157 read_lock(&tasklist_lock);
1159 if (p == kenvctrld_task) {
1164 read_unlock(&tasklist_lock);
1169 current->state = TASK_INTERRUPTIBLE;
1170 schedule_timeout(HZ);
1171 current->state = TASK_RUNNING;
1173 kenvctrld_task = NULL;
1177 misc_deregister(&envctrl_dev);
1179 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++) {
1180 if (i2c_childlist[i].tables)
1181 kfree(i2c_childlist[i].tables);
1185 module_init(envctrl_init);
1186 module_exit(envctrl_cleanup);
1187 MODULE_LICENSE("GPL");