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/module.h>
23 #include <linux/sched.h>
24 #include <linux/kthread.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 ENVCTRL_MINOR 162
40 #define PCF8584_ADDRESS 0x55
42 #define CONTROL_PIN 0x80
43 #define CONTROL_ES0 0x40
44 #define CONTROL_ES1 0x20
45 #define CONTROL_ES2 0x10
46 #define CONTROL_ENI 0x08
47 #define CONTROL_STA 0x04
48 #define CONTROL_STO 0x02
49 #define CONTROL_ACK 0x01
51 #define STATUS_PIN 0x80
52 #define STATUS_STS 0x20
53 #define STATUS_BER 0x10
54 #define STATUS_LRB 0x08
55 #define STATUS_AD0 0x08
56 #define STATUS_AAB 0x04
57 #define STATUS_LAB 0x02
58 #define STATUS_BB 0x01
63 #define BUS_CLK_90 0x00
64 #define BUS_CLK_45 0x01
65 #define BUS_CLK_11 0x02
66 #define BUS_CLK_1_5 0x03
74 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
75 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
77 /* Monitor type of i2c child device.
78 * Firmware definitions.
80 #define PCF8584_MAX_CHANNELS 8
81 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
82 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
83 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
84 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
86 /* Monitor type of i2c child device.
89 #define ENVCTRL_NOMON 0
90 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
91 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
92 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
93 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
95 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
96 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
97 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
98 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
100 /* Child device type.
101 * Driver definitions.
103 #define I2C_ADC 0 /* pcf8591 */
104 #define I2C_GPIO 1 /* pcf8571 */
106 /* Data read from child device may need to decode
107 * through a data table and a scale.
108 * Translation type as defined by firmware.
110 #define ENVCTRL_TRANSLATE_NO 0
111 #define ENVCTRL_TRANSLATE_PARTIAL 1
112 #define ENVCTRL_TRANSLATE_COMBINED 2
113 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
114 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
116 /* Driver miscellaneous definitions. */
117 #define ENVCTRL_MAX_CPU 4
118 #define CHANNEL_DESC_SZ 256
120 /* Mask values for combined GlobalAddress/PowerStatus node */
121 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
122 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
124 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
125 * (see envctrl_init_i2c_child)
127 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
129 #define PCF8584_DATA 0x00
130 #define PCF8584_CSR 0x01
132 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
133 * Property of a port or channel as defined by the firmware.
135 struct pcf8584_channel {
136 unsigned char chnl_no;
137 unsigned char io_direction;
142 /* Each child device may have one or more tables of bytes to help decode
143 * data. Table property as defined by the firmware.
145 struct pcf8584_tblprop {
148 unsigned int offset; /* offset from the beginning of the table */
154 /* Either ADC or GPIO. */
155 unsigned char i2ctype;
157 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
160 unsigned int total_chnls; /* Number of monitor channels. */
161 unsigned char fan_mask; /* Byte mask for fan status channels. */
162 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
163 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
165 /* Properties of all monitor channels. */
166 unsigned int total_tbls; /* Number of monitor tables. */
167 char *tables; /* Pointer to table(s). */
168 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
169 char mon_type[PCF8584_MAX_CHANNELS];
172 static void __iomem *i2c;
173 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
174 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
175 static unsigned int warning_temperature = 0;
176 static unsigned int shutdown_temperature = 0;
177 static char read_cpu;
179 /* Forward declarations. */
180 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
182 /* Function Description: Test the PIN bit (Pending Interrupt Not)
183 * to test when serial transmission is completed .
186 static void envtrl_i2c_test_pin(void)
190 while (--limit > 0) {
191 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
197 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
200 /* Function Description: Test busy bit.
203 static void envctrl_i2c_test_bb(void)
207 while (--limit > 0) {
208 /* Busy bit 0 means busy. */
209 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
215 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
218 /* Function Description: Send the address for a read access.
219 * Return : 0 if not acknowledged, otherwise acknowledged.
221 static int envctrl_i2c_read_addr(unsigned char addr)
223 envctrl_i2c_test_bb();
226 writeb(addr + 1, i2c + PCF8584_DATA);
228 envctrl_i2c_test_bb();
230 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
233 envtrl_i2c_test_pin();
235 /* CSR 0 means acknowledged. */
236 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
237 return readb(i2c + PCF8584_DATA);
239 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
244 /* Function Description: Send the address for write mode.
247 static void envctrl_i2c_write_addr(unsigned char addr)
249 envctrl_i2c_test_bb();
250 writeb(addr, i2c + PCF8584_DATA);
252 /* Generate Start condition. */
253 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
256 /* Function Description: Read 1 byte of data from addr
257 * set by envctrl_i2c_read_addr()
258 * Return : Data from address set by envctrl_i2c_read_addr().
260 static unsigned char envctrl_i2c_read_data(void)
262 envtrl_i2c_test_pin();
263 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
264 return readb(i2c + PCF8584_DATA);
267 /* Function Description: Instruct the device which port to read data from.
270 static void envctrl_i2c_write_data(unsigned char port)
272 envtrl_i2c_test_pin();
273 writeb(port, i2c + PCF8584_DATA);
276 /* Function Description: Generate Stop condition after last byte is sent.
279 static void envctrl_i2c_stop(void)
281 envtrl_i2c_test_pin();
282 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
285 /* Function Description: Read adc device.
286 * Return : Data at address and port.
288 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
291 envctrl_i2c_write_addr(addr);
293 /* Setup port to read. */
294 envctrl_i2c_write_data(port);
298 envctrl_i2c_read_addr(addr);
300 /* Do a single byte read and send stop. */
301 envctrl_i2c_read_data();
304 return readb(i2c + PCF8584_DATA);
307 /* Function Description: Read gpio device.
308 * Return : Data at address.
310 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
314 envctrl_i2c_read_addr(addr);
316 /* Do a single byte read and send stop. */
317 rd = envctrl_i2c_read_data();
322 /* Function Description: Decode data read from an adc device using firmware
324 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
326 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
327 int scale, char *tbl, char *bufdata)
331 switch (translate_type) {
332 case ENVCTRL_TRANSLATE_NO:
333 /* No decode necessary. */
338 case ENVCTRL_TRANSLATE_FULL:
339 /* Decode this way: data = table[data]. */
341 bufdata[0] = tbl[data];
344 case ENVCTRL_TRANSLATE_SCALE:
345 /* Decode this way: data = table[data]/scale */
346 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
347 len = strlen(bufdata);
348 bufdata[len - 1] = bufdata[len - 2];
349 bufdata[len - 2] = '.';
359 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
360 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
362 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
363 char mon_type, unsigned char *bufdata)
369 /* Find the right monitor type and channel. */
370 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
371 if (pchild->mon_type[i] == mon_type) {
381 /* Read data from address and port. */
382 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
383 (unsigned char)pchild->chnl_array[i].chnl_no);
385 /* Find decoding table. */
386 tbl = pchild->tables + pchild->tblprop_array[i].offset;
388 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
389 pchild->tblprop_array[i].scale,
393 /* Function Description: Read noncpu-related data such as motherboard
395 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
397 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
398 char mon_type, unsigned char *bufdata)
404 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
405 if (pchild->mon_type[i] == mon_type)
409 if (i >= PCF8584_MAX_CHANNELS)
412 /* Read data from address and port. */
413 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
414 (unsigned char)pchild->chnl_array[i].chnl_no);
416 /* Find decoding table. */
417 tbl = pchild->tables + pchild->tblprop_array[i].offset;
419 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
420 pchild->tblprop_array[i].scale,
424 /* Function Description: Read fan status.
425 * Return : Always 1 byte. Status stored in bufdata.
427 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
431 unsigned char tmp, ret = 0;
434 tmp = data & pchild->fan_mask;
436 if (tmp == pchild->fan_mask) {
437 /* All bits are on. All fans are functioning. */
438 ret = ENVCTRL_ALL_FANS_GOOD;
439 } else if (tmp == 0) {
440 /* No bits are on. No fans are functioning. */
441 ret = ENVCTRL_ALL_FANS_BAD;
443 /* Go through all channels, mark 'on' the matched bits.
444 * Notice that fan_mask may have discontiguous bits but
445 * return mask are always contiguous. For example if we
446 * monitor 4 fans at channels 0,1,2,4, the return mask
447 * should be 00010000 if only fan at channel 4 is working.
449 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
450 if (pchild->fan_mask & chnls_mask[i]) {
451 if (!(chnls_mask[i] & tmp))
452 ret |= chnls_mask[j];
463 /* Function Description: Read global addressing line.
464 * Return : Always 1 byte. Status stored in bufdata.
466 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
470 /* Translatation table is not necessary, as global
471 * addr is the integer value of the GA# bits.
473 * NOTE: MSB is documented as zero, but I see it as '1' always....
475 * -----------------------------------------------
476 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
477 * -----------------------------------------------
478 * GA0 - GA4 integer value of Global Address (backplane slot#)
479 * DEG 0 = cPCI Power supply output is starting to degrade
480 * 1 = cPCI Power supply output is OK
481 * FAL 0 = cPCI Power supply has failed
482 * 1 = cPCI Power supply output is OK
484 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
488 /* Function Description: Read standard voltage and power supply status.
489 * Return : Always 1 byte. Status stored in bufdata.
491 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
495 unsigned char tmp, ret = 0;
498 tmp = data & pchild->voltage_mask;
500 /* Two channels are used to monitor voltage and power supply. */
501 if (tmp == pchild->voltage_mask) {
502 /* All bits are on. Voltage and power supply are okay. */
503 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
504 } else if (tmp == 0) {
505 /* All bits are off. Voltage and power supply are bad */
506 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
508 /* Either voltage or power supply has problem. */
509 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
510 if (pchild->voltage_mask & chnls_mask[i]) {
513 /* Break out when there is a mismatch. */
514 if (!(chnls_mask[i] & tmp))
519 /* Make a wish that hardware will always use the
520 * first channel for voltage and the second for
524 ret = ENVCTRL_VOLTAGE_BAD;
526 ret = ENVCTRL_POWERSUPPLY_BAD;
533 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
534 * Return: Number of read bytes. 0 for error.
537 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
539 struct i2c_child_t *pchild;
540 unsigned char data[10];
543 /* Get the type of read as decided in ioctl() call.
544 * Find the appropriate i2c child.
545 * Get the data and put back to the user buffer.
548 switch ((int)(long)file->private_data) {
549 case ENVCTRL_RD_WARNING_TEMPERATURE:
550 if (warning_temperature == 0)
553 data[0] = (unsigned char)(warning_temperature);
555 if (copy_to_user(buf, data, ret))
559 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
560 if (shutdown_temperature == 0)
563 data[0] = (unsigned char)(shutdown_temperature);
565 if (copy_to_user(buf, data, ret))
569 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
570 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
572 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
573 if (copy_to_user(buf, data, ret))
577 case ENVCTRL_RD_CPU_TEMPERATURE:
578 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
580 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
582 /* Reset cpu to the default cpu0. */
583 if (copy_to_user(buf, data, ret))
587 case ENVCTRL_RD_CPU_VOLTAGE:
588 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
590 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
592 /* Reset cpu to the default cpu0. */
593 if (copy_to_user(buf, data, ret))
597 case ENVCTRL_RD_SCSI_TEMPERATURE:
598 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
600 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
601 if (copy_to_user(buf, data, ret))
605 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
606 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
608 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
609 if (copy_to_user(buf, data, ret))
613 case ENVCTRL_RD_FAN_STATUS:
614 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
616 data[0] = envctrl_i2c_read_8574(pchild->addr);
617 ret = envctrl_i2c_fan_status(pchild,data[0], data);
618 if (copy_to_user(buf, data, ret))
622 case ENVCTRL_RD_GLOBALADDRESS:
623 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
625 data[0] = envctrl_i2c_read_8574(pchild->addr);
626 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
627 if (copy_to_user(buf, data, ret))
631 case ENVCTRL_RD_VOLTAGE_STATUS:
632 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
633 /* If voltage monitor not present, check for CPCI equivalent */
634 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
636 data[0] = envctrl_i2c_read_8574(pchild->addr);
637 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
638 if (copy_to_user(buf, data, ret))
650 /* Function Description: Command what to read. Mapped to user ioctl().
651 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
654 envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
656 char __user *infobuf;
659 case ENVCTRL_RD_WARNING_TEMPERATURE:
660 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
661 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
662 case ENVCTRL_RD_FAN_STATUS:
663 case ENVCTRL_RD_VOLTAGE_STATUS:
664 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
665 case ENVCTRL_RD_SCSI_TEMPERATURE:
666 case ENVCTRL_RD_GLOBALADDRESS:
667 file->private_data = (void *)(long)cmd;
670 case ENVCTRL_RD_CPU_TEMPERATURE:
671 case ENVCTRL_RD_CPU_VOLTAGE:
672 /* Check to see if application passes in any cpu number,
673 * the default is cpu0.
675 infobuf = (char __user *) arg;
676 if (infobuf == NULL) {
679 get_user(read_cpu, infobuf);
682 /* Save the command for use when reading. */
683 file->private_data = (void *)(long)cmd;
693 /* Function Description: open device. Mapped to user open().
697 envctrl_open(struct inode *inode, struct file *file)
699 file->private_data = NULL;
703 /* Function Description: Open device. Mapped to user close().
707 envctrl_release(struct inode *inode, struct file *file)
712 static struct file_operations envctrl_fops = {
713 .owner = THIS_MODULE,
714 .read = envctrl_read,
715 .unlocked_ioctl = envctrl_ioctl,
717 .compat_ioctl = envctrl_ioctl,
719 .open = envctrl_open,
720 .release = envctrl_release,
723 static struct miscdevice envctrl_dev = {
729 /* Function Description: Set monitor type based on firmware description.
732 static void envctrl_set_mon(struct i2c_child_t *pchild,
736 /* Firmware only has temperature type. It does not distinguish
737 * different kinds of temperatures. We use channel description
738 * to disinguish them.
740 if (!(strcmp(chnl_desc,"temp,cpu")) ||
741 !(strcmp(chnl_desc,"temp,cpu0")) ||
742 !(strcmp(chnl_desc,"temp,cpu1")) ||
743 !(strcmp(chnl_desc,"temp,cpu2")) ||
744 !(strcmp(chnl_desc,"temp,cpu3")))
745 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
747 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
748 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
749 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
750 !(strcmp(chnl_desc,"vddcore,cpu3")))
751 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
753 if (!(strcmp(chnl_desc,"temp,motherboard")))
754 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
756 if (!(strcmp(chnl_desc,"temp,scsi")))
757 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
759 if (!(strcmp(chnl_desc,"temp,ethernet")))
760 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
763 /* Function Description: Initialize monitor channel with channel desc,
764 * decoding tables, monitor type, optional properties.
767 static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
773 /* Firmware describe channels into a stream separated by a '\0'. */
774 pos = of_get_property(dp, "channels-description", &len);
777 int l = strlen(pos) + 1;
778 envctrl_set_mon(pchild, pos, i++);
783 /* Get optional properties. */
784 pval = of_get_property(dp, "warning-temp", NULL);
786 warning_temperature = *pval;
788 pval = of_get_property(dp, "shutdown-temp", NULL);
790 shutdown_temperature = *pval;
793 /* Function Description: Initialize child device monitoring fan status.
796 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
800 /* Go through all channels and set up the mask. */
801 for (i = 0; i < pchild->total_chnls; i++)
802 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
804 /* We only need to know if this child has fan status monitored.
805 * We don't care which channels since we have the mask already.
807 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
810 /* Function Description: Initialize child device for global addressing line.
813 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
817 /* Voltage/PowerSupply monitoring is piggybacked
818 * with Global Address on CompactPCI. See comments
819 * within envctrl_i2c_globaladdr for bit assignments.
821 * The mask is created here by assigning mask bits to each
822 * bit position that represents PCF8584_VOLTAGE_TYPE data.
823 * Channel numbers are not consecutive within the globaladdr
824 * node (why?), so we use the actual counter value as chnls_mask
825 * index instead of the chnl_array[x].chnl_no value.
827 * NOTE: This loop could be replaced with a constant representing
828 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
830 for (i = 0; i < pchild->total_chnls; i++) {
831 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
832 pchild->voltage_mask |= chnls_mask[i];
836 /* We only need to know if this child has global addressing
837 * line monitored. We don't care which channels since we know
838 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
840 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
843 /* Initialize child device monitoring voltage status. */
844 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
848 /* Go through all channels and set up the mask. */
849 for (i = 0; i < pchild->total_chnls; i++)
850 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
852 /* We only need to know if this child has voltage status monitored.
853 * We don't care which channels since we have the mask already.
855 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
858 /* Function Description: Initialize i2c child device.
861 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
862 struct i2c_child_t *pchild)
864 int len, i, tbls_size = 0;
865 struct device_node *dp = edev_child->prom_node;
868 /* Get device address. */
869 pval = of_get_property(dp, "reg", &len);
870 memcpy(&pchild->addr, pval, len);
872 /* Get tables property. Read firmware temperature tables. */
873 pval = of_get_property(dp, "translation", &len);
874 if (pval && len > 0) {
875 memcpy(pchild->tblprop_array, pval, len);
876 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
877 for (i = 0; i < pchild->total_tbls; i++) {
878 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
879 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
883 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
884 if (pchild->tables == NULL){
885 printk("envctrl: Failed to allocate table.\n");
888 pval = of_get_property(dp, "tables", &len);
889 if (!pval || len <= 0) {
890 printk("envctrl: Failed to get table.\n");
893 memcpy(pchild->tables, pval, len);
896 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
897 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
898 * "For Factory Use Only."
900 * We ignore the node on these platforms by assigning the
901 * 'NULL' monitor type.
903 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
904 struct device_node *root_node;
907 root_node = of_find_node_by_path("/");
908 if (!strcmp(root_node->name, "SUNW,UltraSPARC-IIi-cEngine")) {
909 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
910 pchild->mon_type[len] = ENVCTRL_NOMON;
916 /* Get the monitor channels. */
917 pval = of_get_property(dp, "channels-in-use", &len);
918 memcpy(pchild->chnl_array, pval, len);
919 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
921 for (i = 0; i < pchild->total_chnls; i++) {
922 switch (pchild->chnl_array[i].type) {
923 case PCF8584_TEMP_TYPE:
924 envctrl_init_adc(pchild, dp);
927 case PCF8584_GLOBALADDR_TYPE:
928 envctrl_init_globaladdr(pchild);
929 i = pchild->total_chnls;
932 case PCF8584_FANSTAT_TYPE:
933 envctrl_init_fanstat(pchild);
934 i = pchild->total_chnls;
937 case PCF8584_VOLTAGE_TYPE:
938 if (pchild->i2ctype == I2C_ADC) {
939 envctrl_init_adc(pchild,dp);
941 envctrl_init_voltage_status(pchild);
943 i = pchild->total_chnls;
952 /* Function Description: Search the child device list for a device.
953 * Return : The i2c child if found. NULL otherwise.
955 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
959 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
960 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
961 if (i2c_childlist[i].mon_type[j] == mon_type) {
962 return (struct i2c_child_t *)(&(i2c_childlist[i]));
969 static void envctrl_do_shutdown(void)
971 static int inprog = 0;
972 static char *envp[] = {
973 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
975 "/sbin/shutdown", "-h", "now", NULL };
982 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
983 ret = kernel_execve("/sbin/shutdown", argv, envp);
985 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
986 inprog = 0; /* unlikely to succeed, but we could try again */
990 static struct task_struct *kenvctrld_task;
992 static int kenvctrld(void *__unused)
997 struct i2c_child_t *cputemp;
999 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
1001 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1005 poll_interval = 5000; /* TODO env_mon_interval */
1007 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1009 msleep_interruptible(poll_interval);
1011 if (kthread_should_stop())
1014 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1015 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1016 ENVCTRL_CPUTEMP_MON,
1018 if (tempbuf[0] >= shutdown_temperature) {
1020 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1021 "shutdown threshold %i C\n",
1022 current->comm, whichcpu,
1023 tempbuf[0], shutdown_temperature);
1024 envctrl_do_shutdown();
1029 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1033 static int __init envctrl_init(void)
1035 struct linux_ebus *ebus = NULL;
1036 struct linux_ebus_device *edev = NULL;
1037 struct linux_ebus_child *edev_child = NULL;
1040 for_each_ebus(ebus) {
1041 for_each_ebusdev(edev, ebus) {
1042 if (!strcmp(edev->prom_node->name, "bbc")) {
1043 /* If we find a boot-bus controller node,
1044 * then this envctrl driver is not for us.
1051 /* Traverse through ebus and ebus device list for i2c device and
1052 * adc and gpio nodes.
1054 for_each_ebus(ebus) {
1055 for_each_ebusdev(edev, ebus) {
1056 if (!strcmp(edev->prom_node->name, "i2c")) {
1057 i2c = ioremap(edev->resource[0].start, 0x2);
1058 for_each_edevchild(edev, edev_child) {
1059 if (!strcmp("gpio", edev_child->prom_node->name)) {
1060 i2c_childlist[i].i2ctype = I2C_GPIO;
1061 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1063 if (!strcmp("adc", edev_child->prom_node->name)) {
1064 i2c_childlist[i].i2ctype = I2C_ADC;
1065 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1075 printk("envctrl: I2C device not found.\n");
1079 /* Set device address. */
1080 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1081 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1083 /* Set system clock and SCL frequencies. */
1084 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1085 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1087 /* Enable serial interface. */
1088 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1091 /* Register the device as a minor miscellaneous device. */
1092 err = misc_register(&envctrl_dev);
1094 printk("envctrl: Unable to get misc minor %d\n",
1099 /* Note above traversal routine post-incremented 'i' to accommodate
1100 * a next child device, so we decrement before reverse-traversal of
1103 printk("envctrl: initialized ");
1104 for (--i; i >= 0; --i) {
1105 printk("[%s 0x%lx]%s",
1106 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1107 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1108 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1111 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1112 if (IS_ERR(kenvctrld_task)) {
1113 err = PTR_ERR(kenvctrld_task);
1114 goto out_deregister;
1120 misc_deregister(&envctrl_dev);
1123 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1124 kfree(i2c_childlist[i].tables);
1129 static void __exit envctrl_cleanup(void)
1133 kthread_stop(kenvctrld_task);
1136 misc_deregister(&envctrl_dev);
1138 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1139 kfree(i2c_childlist[i].tables);
1142 module_init(envctrl_init);
1143 module_exit(envctrl_cleanup);
1144 MODULE_LICENSE("GPL");