RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / sbus / char / envctrl.c
blob8328acab47fdd2feb6c425ba59c179081b27f425
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/init.h>
24 #include <linux/kthread.h>
25 #include <linux/delay.h>
26 #include <linux/ioport.h>
27 #include <linux/miscdevice.h>
28 #include <linux/kmod.h>
30 #include <asm/ebus.h>
31 #include <asm/uaccess.h>
32 #include <asm/envctrl.h>
33 #include <asm/io.h>
35 #define ENVCTRL_MINOR 162
37 #define PCF8584_ADDRESS 0x55
39 #define CONTROL_PIN 0x80
40 #define CONTROL_ES0 0x40
41 #define CONTROL_ES1 0x20
42 #define CONTROL_ES2 0x10
43 #define CONTROL_ENI 0x08
44 #define CONTROL_STA 0x04
45 #define CONTROL_STO 0x02
46 #define CONTROL_ACK 0x01
48 #define STATUS_PIN 0x80
49 #define STATUS_STS 0x20
50 #define STATUS_BER 0x10
51 #define STATUS_LRB 0x08
52 #define STATUS_AD0 0x08
53 #define STATUS_AAB 0x04
54 #define STATUS_LAB 0x02
55 #define STATUS_BB 0x01
58 * CLK Mode Register.
60 #define BUS_CLK_90 0x00
61 #define BUS_CLK_45 0x01
62 #define BUS_CLK_11 0x02
63 #define BUS_CLK_1_5 0x03
65 #define CLK_3 0x00
66 #define CLK_4_43 0x10
67 #define CLK_6 0x14
68 #define CLK_8 0x18
69 #define CLK_12 0x1c
71 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
72 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
74 /* Monitor type of i2c child device.
75 * Firmware definitions.
77 #define PCF8584_MAX_CHANNELS 8
78 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
79 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
80 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
81 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
83 /* Monitor type of i2c child device.
84 * Driver definitions.
86 #define ENVCTRL_NOMON 0
87 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
88 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
89 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
90 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
91 /* monitor */
92 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
93 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
94 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
95 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
97 /* Child device type.
98 * Driver definitions.
100 #define I2C_ADC 0 /* pcf8591 */
101 #define I2C_GPIO 1 /* pcf8571 */
103 /* Data read from child device may need to decode
104 * through a data table and a scale.
105 * Translation type as defined by firmware.
107 #define ENVCTRL_TRANSLATE_NO 0
108 #define ENVCTRL_TRANSLATE_PARTIAL 1
109 #define ENVCTRL_TRANSLATE_COMBINED 2
110 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
111 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
113 /* Driver miscellaneous definitions. */
114 #define ENVCTRL_MAX_CPU 4
115 #define CHANNEL_DESC_SZ 256
117 /* Mask values for combined GlobalAddress/PowerStatus node */
118 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
119 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
121 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
122 * (see envctrl_init_i2c_child)
124 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
126 #define PCF8584_DATA 0x00
127 #define PCF8584_CSR 0x01
129 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
130 * Property of a port or channel as defined by the firmware.
132 struct pcf8584_channel {
133 unsigned char chnl_no;
134 unsigned char io_direction;
135 unsigned char type;
136 unsigned char last;
139 /* Each child device may have one or more tables of bytes to help decode
140 * data. Table property as defined by the firmware.
142 struct pcf8584_tblprop {
143 unsigned int type;
144 unsigned int scale;
145 unsigned int offset; /* offset from the beginning of the table */
146 unsigned int size;
149 /* i2c child */
150 struct i2c_child_t {
151 /* Either ADC or GPIO. */
152 unsigned char i2ctype;
153 unsigned long addr;
154 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
156 /* Channel info. */
157 unsigned int total_chnls; /* Number of monitor channels. */
158 unsigned char fan_mask; /* Byte mask for fan status channels. */
159 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
160 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
162 /* Properties of all monitor channels. */
163 unsigned int total_tbls; /* Number of monitor tables. */
164 char *tables; /* Pointer to table(s). */
165 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
166 char mon_type[PCF8584_MAX_CHANNELS];
169 static void __iomem *i2c;
170 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
171 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
172 static unsigned int warning_temperature = 0;
173 static unsigned int shutdown_temperature = 0;
174 static char read_cpu;
176 /* Forward declarations. */
177 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
179 /* Function Description: Test the PIN bit (Pending Interrupt Not)
180 * to test when serial transmission is completed .
181 * Return : None.
183 static void envtrl_i2c_test_pin(void)
185 int limit = 1000000;
187 while (--limit > 0) {
188 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
189 break;
190 udelay(1);
193 if (limit <= 0)
194 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
197 /* Function Description: Test busy bit.
198 * Return : None.
200 static void envctrl_i2c_test_bb(void)
202 int limit = 1000000;
204 while (--limit > 0) {
205 /* Busy bit 0 means busy. */
206 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
207 break;
208 udelay(1);
211 if (limit <= 0)
212 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
215 /* Function Description: Send the address for a read access.
216 * Return : 0 if not acknowledged, otherwise acknowledged.
218 static int envctrl_i2c_read_addr(unsigned char addr)
220 envctrl_i2c_test_bb();
222 /* Load address. */
223 writeb(addr + 1, i2c + PCF8584_DATA);
225 envctrl_i2c_test_bb();
227 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
229 /* Wait for PIN. */
230 envtrl_i2c_test_pin();
232 /* CSR 0 means acknowledged. */
233 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
234 return readb(i2c + PCF8584_DATA);
235 } else {
236 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
237 return 0;
241 /* Function Description: Send the address for write mode.
242 * Return : None.
244 static void envctrl_i2c_write_addr(unsigned char addr)
246 envctrl_i2c_test_bb();
247 writeb(addr, i2c + PCF8584_DATA);
249 /* Generate Start condition. */
250 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
253 /* Function Description: Read 1 byte of data from addr
254 * set by envctrl_i2c_read_addr()
255 * Return : Data from address set by envctrl_i2c_read_addr().
257 static unsigned char envctrl_i2c_read_data(void)
259 envtrl_i2c_test_pin();
260 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
261 return readb(i2c + PCF8584_DATA);
264 /* Function Description: Instruct the device which port to read data from.
265 * Return : None.
267 static void envctrl_i2c_write_data(unsigned char port)
269 envtrl_i2c_test_pin();
270 writeb(port, i2c + PCF8584_DATA);
273 /* Function Description: Generate Stop condition after last byte is sent.
274 * Return : None.
276 static void envctrl_i2c_stop(void)
278 envtrl_i2c_test_pin();
279 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
282 /* Function Description: Read adc device.
283 * Return : Data at address and port.
285 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
287 /* Send address. */
288 envctrl_i2c_write_addr(addr);
290 /* Setup port to read. */
291 envctrl_i2c_write_data(port);
292 envctrl_i2c_stop();
294 /* Read port. */
295 envctrl_i2c_read_addr(addr);
297 /* Do a single byte read and send stop. */
298 envctrl_i2c_read_data();
299 envctrl_i2c_stop();
301 return readb(i2c + PCF8584_DATA);
304 /* Function Description: Read gpio device.
305 * Return : Data at address.
307 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
309 unsigned char rd;
311 envctrl_i2c_read_addr(addr);
313 /* Do a single byte read and send stop. */
314 rd = envctrl_i2c_read_data();
315 envctrl_i2c_stop();
316 return rd;
319 /* Function Description: Decode data read from an adc device using firmware
320 * table.
321 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
323 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
324 int scale, char *tbl, char *bufdata)
326 int len = 0;
328 switch (translate_type) {
329 case ENVCTRL_TRANSLATE_NO:
330 /* No decode necessary. */
331 len = 1;
332 bufdata[0] = data;
333 break;
335 case ENVCTRL_TRANSLATE_FULL:
336 /* Decode this way: data = table[data]. */
337 len = 1;
338 bufdata[0] = tbl[data];
339 break;
341 case ENVCTRL_TRANSLATE_SCALE:
342 /* Decode this way: data = table[data]/scale */
343 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
344 len = strlen(bufdata);
345 bufdata[len - 1] = bufdata[len - 2];
346 bufdata[len - 2] = '.';
347 break;
349 default:
350 break;
353 return len;
356 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
357 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
359 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
360 char mon_type, unsigned char *bufdata)
362 unsigned char data;
363 int i;
364 char *tbl, j = -1;
366 /* Find the right monitor type and channel. */
367 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
368 if (pchild->mon_type[i] == mon_type) {
369 if (++j == cpu) {
370 break;
375 if (j != cpu)
376 return 0;
378 /* Read data from address and port. */
379 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
380 (unsigned char)pchild->chnl_array[i].chnl_no);
382 /* Find decoding table. */
383 tbl = pchild->tables + pchild->tblprop_array[i].offset;
385 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
386 pchild->tblprop_array[i].scale,
387 tbl, bufdata);
390 /* Function Description: Read noncpu-related data such as motherboard
391 * temperature.
392 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
394 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
395 char mon_type, unsigned char *bufdata)
397 unsigned char data;
398 int i;
399 char *tbl = NULL;
401 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
402 if (pchild->mon_type[i] == mon_type)
403 break;
406 if (i >= PCF8584_MAX_CHANNELS)
407 return 0;
409 /* Read data from address and port. */
410 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
411 (unsigned char)pchild->chnl_array[i].chnl_no);
413 /* Find decoding table. */
414 tbl = pchild->tables + pchild->tblprop_array[i].offset;
416 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
417 pchild->tblprop_array[i].scale,
418 tbl, bufdata);
421 /* Function Description: Read fan status.
422 * Return : Always 1 byte. Status stored in bufdata.
424 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
425 unsigned char data,
426 char *bufdata)
428 unsigned char tmp, ret = 0;
429 int i, j = 0;
431 tmp = data & pchild->fan_mask;
433 if (tmp == pchild->fan_mask) {
434 /* All bits are on. All fans are functioning. */
435 ret = ENVCTRL_ALL_FANS_GOOD;
436 } else if (tmp == 0) {
437 /* No bits are on. No fans are functioning. */
438 ret = ENVCTRL_ALL_FANS_BAD;
439 } else {
440 /* Go through all channels, mark 'on' the matched bits.
441 * Notice that fan_mask may have discontiguous bits but
442 * return mask are always contiguous. For example if we
443 * monitor 4 fans at channels 0,1,2,4, the return mask
444 * should be 00010000 if only fan at channel 4 is working.
446 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
447 if (pchild->fan_mask & chnls_mask[i]) {
448 if (!(chnls_mask[i] & tmp))
449 ret |= chnls_mask[j];
451 j++;
456 bufdata[0] = ret;
457 return 1;
460 /* Function Description: Read global addressing line.
461 * Return : Always 1 byte. Status stored in bufdata.
463 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
464 unsigned char data,
465 char *bufdata)
467 /* Translatation table is not necessary, as global
468 * addr is the integer value of the GA# bits.
470 * NOTE: MSB is documented as zero, but I see it as '1' always....
472 * -----------------------------------------------
473 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
474 * -----------------------------------------------
475 * GA0 - GA4 integer value of Global Address (backplane slot#)
476 * DEG 0 = cPCI Power supply output is starting to degrade
477 * 1 = cPCI Power supply output is OK
478 * FAL 0 = cPCI Power supply has failed
479 * 1 = cPCI Power supply output is OK
481 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
482 return 1;
485 /* Function Description: Read standard voltage and power supply status.
486 * Return : Always 1 byte. Status stored in bufdata.
488 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
489 unsigned char data,
490 char *bufdata)
492 unsigned char tmp, ret = 0;
493 int i, j = 0;
495 tmp = data & pchild->voltage_mask;
497 /* Two channels are used to monitor voltage and power supply. */
498 if (tmp == pchild->voltage_mask) {
499 /* All bits are on. Voltage and power supply are okay. */
500 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
501 } else if (tmp == 0) {
502 /* All bits are off. Voltage and power supply are bad */
503 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
504 } else {
505 /* Either voltage or power supply has problem. */
506 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
507 if (pchild->voltage_mask & chnls_mask[i]) {
508 j++;
510 /* Break out when there is a mismatch. */
511 if (!(chnls_mask[i] & tmp))
512 break;
516 /* Make a wish that hardware will always use the
517 * first channel for voltage and the second for
518 * power supply.
520 if (j == 1)
521 ret = ENVCTRL_VOLTAGE_BAD;
522 else
523 ret = ENVCTRL_POWERSUPPLY_BAD;
526 bufdata[0] = ret;
527 return 1;
530 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
531 * Return: Number of read bytes. 0 for error.
533 static ssize_t
534 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
536 struct i2c_child_t *pchild;
537 unsigned char data[10];
538 int ret = 0;
540 /* Get the type of read as decided in ioctl() call.
541 * Find the appropriate i2c child.
542 * Get the data and put back to the user buffer.
545 switch ((int)(long)file->private_data) {
546 case ENVCTRL_RD_WARNING_TEMPERATURE:
547 if (warning_temperature == 0)
548 return 0;
550 data[0] = (unsigned char)(warning_temperature);
551 ret = 1;
552 if (copy_to_user(buf, data, ret))
553 ret = -EFAULT;
554 break;
556 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
557 if (shutdown_temperature == 0)
558 return 0;
560 data[0] = (unsigned char)(shutdown_temperature);
561 ret = 1;
562 if (copy_to_user(buf, data, ret))
563 ret = -EFAULT;
564 break;
566 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
567 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
568 return 0;
569 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
570 if (copy_to_user(buf, data, ret))
571 ret = -EFAULT;
572 break;
574 case ENVCTRL_RD_CPU_TEMPERATURE:
575 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
576 return 0;
577 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
579 /* Reset cpu to the default cpu0. */
580 if (copy_to_user(buf, data, ret))
581 ret = -EFAULT;
582 break;
584 case ENVCTRL_RD_CPU_VOLTAGE:
585 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
586 return 0;
587 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
589 /* Reset cpu to the default cpu0. */
590 if (copy_to_user(buf, data, ret))
591 ret = -EFAULT;
592 break;
594 case ENVCTRL_RD_SCSI_TEMPERATURE:
595 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
596 return 0;
597 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
598 if (copy_to_user(buf, data, ret))
599 ret = -EFAULT;
600 break;
602 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
603 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
604 return 0;
605 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
606 if (copy_to_user(buf, data, ret))
607 ret = -EFAULT;
608 break;
610 case ENVCTRL_RD_FAN_STATUS:
611 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
612 return 0;
613 data[0] = envctrl_i2c_read_8574(pchild->addr);
614 ret = envctrl_i2c_fan_status(pchild,data[0], data);
615 if (copy_to_user(buf, data, ret))
616 ret = -EFAULT;
617 break;
619 case ENVCTRL_RD_GLOBALADDRESS:
620 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
621 return 0;
622 data[0] = envctrl_i2c_read_8574(pchild->addr);
623 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
624 if (copy_to_user(buf, data, ret))
625 ret = -EFAULT;
626 break;
628 case ENVCTRL_RD_VOLTAGE_STATUS:
629 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
630 /* If voltage monitor not present, check for CPCI equivalent */
631 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
632 return 0;
633 data[0] = envctrl_i2c_read_8574(pchild->addr);
634 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
635 if (copy_to_user(buf, data, ret))
636 ret = -EFAULT;
637 break;
639 default:
640 break;
644 return ret;
647 /* Function Description: Command what to read. Mapped to user ioctl().
648 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
650 static long
651 envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
653 char __user *infobuf;
655 switch (cmd) {
656 case ENVCTRL_RD_WARNING_TEMPERATURE:
657 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
658 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
659 case ENVCTRL_RD_FAN_STATUS:
660 case ENVCTRL_RD_VOLTAGE_STATUS:
661 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
662 case ENVCTRL_RD_SCSI_TEMPERATURE:
663 case ENVCTRL_RD_GLOBALADDRESS:
664 file->private_data = (void *)(long)cmd;
665 break;
667 case ENVCTRL_RD_CPU_TEMPERATURE:
668 case ENVCTRL_RD_CPU_VOLTAGE:
669 /* Check to see if application passes in any cpu number,
670 * the default is cpu0.
672 infobuf = (char __user *) arg;
673 if (infobuf == NULL) {
674 read_cpu = 0;
675 }else {
676 get_user(read_cpu, infobuf);
679 /* Save the command for use when reading. */
680 file->private_data = (void *)(long)cmd;
681 break;
683 default:
684 return -EINVAL;
687 return 0;
690 /* Function Description: open device. Mapped to user open().
691 * Return: Always 0.
693 static int
694 envctrl_open(struct inode *inode, struct file *file)
696 file->private_data = NULL;
697 return 0;
700 /* Function Description: Open device. Mapped to user close().
701 * Return: Always 0.
703 static int
704 envctrl_release(struct inode *inode, struct file *file)
706 return 0;
709 static const struct file_operations envctrl_fops = {
710 .owner = THIS_MODULE,
711 .read = envctrl_read,
712 .unlocked_ioctl = envctrl_ioctl,
713 #ifdef CONFIG_COMPAT
714 .compat_ioctl = envctrl_ioctl,
715 #endif
716 .open = envctrl_open,
717 .release = envctrl_release,
720 static struct miscdevice envctrl_dev = {
721 ENVCTRL_MINOR,
722 "envctrl",
723 &envctrl_fops
726 /* Function Description: Set monitor type based on firmware description.
727 * Return: None.
729 static void envctrl_set_mon(struct i2c_child_t *pchild,
730 const char *chnl_desc,
731 int chnl_no)
733 /* Firmware only has temperature type. It does not distinguish
734 * different kinds of temperatures. We use channel description
735 * to disinguish them.
737 if (!(strcmp(chnl_desc,"temp,cpu")) ||
738 !(strcmp(chnl_desc,"temp,cpu0")) ||
739 !(strcmp(chnl_desc,"temp,cpu1")) ||
740 !(strcmp(chnl_desc,"temp,cpu2")) ||
741 !(strcmp(chnl_desc,"temp,cpu3")))
742 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
744 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
745 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
746 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
747 !(strcmp(chnl_desc,"vddcore,cpu3")))
748 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
750 if (!(strcmp(chnl_desc,"temp,motherboard")))
751 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
753 if (!(strcmp(chnl_desc,"temp,scsi")))
754 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
756 if (!(strcmp(chnl_desc,"temp,ethernet")))
757 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
760 /* Function Description: Initialize monitor channel with channel desc,
761 * decoding tables, monitor type, optional properties.
762 * Return: None.
764 static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
766 int i = 0, len;
767 const char *pos;
768 const unsigned int *pval;
770 /* Firmware describe channels into a stream separated by a '\0'. */
771 pos = of_get_property(dp, "channels-description", &len);
773 while (len > 0) {
774 int l = strlen(pos) + 1;
775 envctrl_set_mon(pchild, pos, i++);
776 len -= l;
777 pos += l;
780 /* Get optional properties. */
781 pval = of_get_property(dp, "warning-temp", NULL);
782 if (pval)
783 warning_temperature = *pval;
785 pval = of_get_property(dp, "shutdown-temp", NULL);
786 if (pval)
787 shutdown_temperature = *pval;
790 /* Function Description: Initialize child device monitoring fan status.
791 * Return: None.
793 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
795 int i;
797 /* Go through all channels and set up the mask. */
798 for (i = 0; i < pchild->total_chnls; i++)
799 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
801 /* We only need to know if this child has fan status monitored.
802 * We don't care which channels since we have the mask already.
804 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
807 /* Function Description: Initialize child device for global addressing line.
808 * Return: None.
810 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
812 int i;
814 /* Voltage/PowerSupply monitoring is piggybacked
815 * with Global Address on CompactPCI. See comments
816 * within envctrl_i2c_globaladdr for bit assignments.
818 * The mask is created here by assigning mask bits to each
819 * bit position that represents PCF8584_VOLTAGE_TYPE data.
820 * Channel numbers are not consecutive within the globaladdr
821 * node (why?), so we use the actual counter value as chnls_mask
822 * index instead of the chnl_array[x].chnl_no value.
824 * NOTE: This loop could be replaced with a constant representing
825 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
827 for (i = 0; i < pchild->total_chnls; i++) {
828 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
829 pchild->voltage_mask |= chnls_mask[i];
833 /* We only need to know if this child has global addressing
834 * line monitored. We don't care which channels since we know
835 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
837 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
840 /* Initialize child device monitoring voltage status. */
841 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
843 int i;
845 /* Go through all channels and set up the mask. */
846 for (i = 0; i < pchild->total_chnls; i++)
847 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
849 /* We only need to know if this child has voltage status monitored.
850 * We don't care which channels since we have the mask already.
852 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
855 /* Function Description: Initialize i2c child device.
856 * Return: None.
858 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
859 struct i2c_child_t *pchild)
861 int len, i, tbls_size = 0;
862 struct device_node *dp = edev_child->prom_node;
863 const void *pval;
865 /* Get device address. */
866 pval = of_get_property(dp, "reg", &len);
867 memcpy(&pchild->addr, pval, len);
869 /* Get tables property. Read firmware temperature tables. */
870 pval = of_get_property(dp, "translation", &len);
871 if (pval && len > 0) {
872 memcpy(pchild->tblprop_array, pval, len);
873 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
874 for (i = 0; i < pchild->total_tbls; i++) {
875 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
876 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
880 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
881 if (pchild->tables == NULL){
882 printk("envctrl: Failed to allocate table.\n");
883 return;
885 pval = of_get_property(dp, "tables", &len);
886 if (!pval || len <= 0) {
887 printk("envctrl: Failed to get table.\n");
888 return;
890 memcpy(pchild->tables, pval, len);
893 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
894 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
895 * "For Factory Use Only."
897 * We ignore the node on these platforms by assigning the
898 * 'NULL' monitor type.
900 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
901 struct device_node *root_node;
902 int len;
904 root_node = of_find_node_by_path("/");
905 if (!strcmp(root_node->name, "SUNW,UltraSPARC-IIi-cEngine")) {
906 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
907 pchild->mon_type[len] = ENVCTRL_NOMON;
909 return;
913 /* Get the monitor channels. */
914 pval = of_get_property(dp, "channels-in-use", &len);
915 memcpy(pchild->chnl_array, pval, len);
916 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
918 for (i = 0; i < pchild->total_chnls; i++) {
919 switch (pchild->chnl_array[i].type) {
920 case PCF8584_TEMP_TYPE:
921 envctrl_init_adc(pchild, dp);
922 break;
924 case PCF8584_GLOBALADDR_TYPE:
925 envctrl_init_globaladdr(pchild);
926 i = pchild->total_chnls;
927 break;
929 case PCF8584_FANSTAT_TYPE:
930 envctrl_init_fanstat(pchild);
931 i = pchild->total_chnls;
932 break;
934 case PCF8584_VOLTAGE_TYPE:
935 if (pchild->i2ctype == I2C_ADC) {
936 envctrl_init_adc(pchild,dp);
937 } else {
938 envctrl_init_voltage_status(pchild);
940 i = pchild->total_chnls;
941 break;
943 default:
944 break;
949 /* Function Description: Search the child device list for a device.
950 * Return : The i2c child if found. NULL otherwise.
952 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
954 int i, j;
956 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
957 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
958 if (i2c_childlist[i].mon_type[j] == mon_type) {
959 return (struct i2c_child_t *)(&(i2c_childlist[i]));
963 return NULL;
966 static void envctrl_do_shutdown(void)
968 static int inprog = 0;
969 static char *envp[] = {
970 "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
971 char *argv[] = {
972 "/sbin/shutdown", "-h", "now", NULL };
973 int ret;
975 if (inprog != 0)
976 return;
978 inprog = 1;
979 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
980 ret = call_usermodehelper("/sbin/shutdown", argv, envp, 0);
981 if (ret < 0) {
982 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
983 inprog = 0; /* unlikely to succeed, but we could try again */
987 static struct task_struct *kenvctrld_task;
989 static int kenvctrld(void *__unused)
991 int poll_interval;
992 int whichcpu;
993 char tempbuf[10];
994 struct i2c_child_t *cputemp;
996 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
997 printk(KERN_ERR
998 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
999 return -ENODEV;
1002 poll_interval = 5000; /* TODO env_mon_interval */
1004 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1005 for (;;) {
1006 msleep_interruptible(poll_interval);
1008 if (kthread_should_stop())
1009 break;
1011 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1012 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1013 ENVCTRL_CPUTEMP_MON,
1014 tempbuf)) {
1015 if (tempbuf[0] >= shutdown_temperature) {
1016 printk(KERN_CRIT
1017 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1018 "shutdown threshold %i C\n",
1019 current->comm, whichcpu,
1020 tempbuf[0], shutdown_temperature);
1021 envctrl_do_shutdown();
1026 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1027 return 0;
1030 static int __init envctrl_init(void)
1032 struct linux_ebus *ebus = NULL;
1033 struct linux_ebus_device *edev = NULL;
1034 struct linux_ebus_child *edev_child = NULL;
1035 int err, i = 0;
1037 for_each_ebus(ebus) {
1038 for_each_ebusdev(edev, ebus) {
1039 if (!strcmp(edev->prom_node->name, "bbc")) {
1040 /* If we find a boot-bus controller node,
1041 * then this envctrl driver is not for us.
1043 return -ENODEV;
1048 /* Traverse through ebus and ebus device list for i2c device and
1049 * adc and gpio nodes.
1051 for_each_ebus(ebus) {
1052 for_each_ebusdev(edev, ebus) {
1053 if (!strcmp(edev->prom_node->name, "i2c")) {
1054 i2c = ioremap(edev->resource[0].start, 0x2);
1055 for_each_edevchild(edev, edev_child) {
1056 if (!strcmp("gpio", edev_child->prom_node->name)) {
1057 i2c_childlist[i].i2ctype = I2C_GPIO;
1058 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1060 if (!strcmp("adc", edev_child->prom_node->name)) {
1061 i2c_childlist[i].i2ctype = I2C_ADC;
1062 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1065 goto done;
1070 done:
1071 if (!edev) {
1072 printk("envctrl: I2C device not found.\n");
1073 return -ENODEV;
1076 /* Set device address. */
1077 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1078 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1080 /* Set system clock and SCL frequencies. */
1081 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1082 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1084 /* Enable serial interface. */
1085 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1086 udelay(200);
1088 /* Register the device as a minor miscellaneous device. */
1089 err = misc_register(&envctrl_dev);
1090 if (err) {
1091 printk("envctrl: Unable to get misc minor %d\n",
1092 envctrl_dev.minor);
1093 goto out_iounmap;
1096 /* Note above traversal routine post-incremented 'i' to accommodate
1097 * a next child device, so we decrement before reverse-traversal of
1098 * child devices.
1100 printk("envctrl: initialized ");
1101 for (--i; i >= 0; --i) {
1102 printk("[%s 0x%lx]%s",
1103 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1104 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1105 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1108 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1109 if (IS_ERR(kenvctrld_task)) {
1110 err = PTR_ERR(kenvctrld_task);
1111 goto out_deregister;
1114 return 0;
1116 out_deregister:
1117 misc_deregister(&envctrl_dev);
1118 out_iounmap:
1119 iounmap(i2c);
1120 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1121 kfree(i2c_childlist[i].tables);
1123 return err;
1126 static void __exit envctrl_cleanup(void)
1128 int i;
1130 kthread_stop(kenvctrld_task);
1132 iounmap(i2c);
1133 misc_deregister(&envctrl_dev);
1135 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1136 kfree(i2c_childlist[i].tables);
1139 module_init(envctrl_init);
1140 module_exit(envctrl_cleanup);
1141 MODULE_LICENSE("GPL");