sched: make early bootup sched_clock() use safer
[wrt350n-kernel.git] / drivers / sbus / char / envctrl.c
blobdadabef116b60ea75d935b37254ca53eeff2e09a
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>
29 #include <linux/reboot.h>
31 #include <asm/ebus.h>
32 #include <asm/uaccess.h>
33 #include <asm/envctrl.h>
34 #include <asm/io.h>
36 #define ENVCTRL_MINOR 162
38 #define PCF8584_ADDRESS 0x55
40 #define CONTROL_PIN 0x80
41 #define CONTROL_ES0 0x40
42 #define CONTROL_ES1 0x20
43 #define CONTROL_ES2 0x10
44 #define CONTROL_ENI 0x08
45 #define CONTROL_STA 0x04
46 #define CONTROL_STO 0x02
47 #define CONTROL_ACK 0x01
49 #define STATUS_PIN 0x80
50 #define STATUS_STS 0x20
51 #define STATUS_BER 0x10
52 #define STATUS_LRB 0x08
53 #define STATUS_AD0 0x08
54 #define STATUS_AAB 0x04
55 #define STATUS_LAB 0x02
56 #define STATUS_BB 0x01
59 * CLK Mode Register.
61 #define BUS_CLK_90 0x00
62 #define BUS_CLK_45 0x01
63 #define BUS_CLK_11 0x02
64 #define BUS_CLK_1_5 0x03
66 #define CLK_3 0x00
67 #define CLK_4_43 0x10
68 #define CLK_6 0x14
69 #define CLK_8 0x18
70 #define CLK_12 0x1c
72 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
73 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
75 /* Monitor type of i2c child device.
76 * Firmware definitions.
78 #define PCF8584_MAX_CHANNELS 8
79 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
80 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
81 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
82 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
84 /* Monitor type of i2c child device.
85 * Driver definitions.
87 #define ENVCTRL_NOMON 0
88 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
89 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
90 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
91 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
92 /* monitor */
93 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
94 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
95 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
96 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
98 /* Child device type.
99 * Driver definitions.
101 #define I2C_ADC 0 /* pcf8591 */
102 #define I2C_GPIO 1 /* pcf8571 */
104 /* Data read from child device may need to decode
105 * through a data table and a scale.
106 * Translation type as defined by firmware.
108 #define ENVCTRL_TRANSLATE_NO 0
109 #define ENVCTRL_TRANSLATE_PARTIAL 1
110 #define ENVCTRL_TRANSLATE_COMBINED 2
111 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
112 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
114 /* Driver miscellaneous definitions. */
115 #define ENVCTRL_MAX_CPU 4
116 #define CHANNEL_DESC_SZ 256
118 /* Mask values for combined GlobalAddress/PowerStatus node */
119 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
120 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
122 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
123 * (see envctrl_init_i2c_child)
125 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
127 #define PCF8584_DATA 0x00
128 #define PCF8584_CSR 0x01
130 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
131 * Property of a port or channel as defined by the firmware.
133 struct pcf8584_channel {
134 unsigned char chnl_no;
135 unsigned char io_direction;
136 unsigned char type;
137 unsigned char last;
140 /* Each child device may have one or more tables of bytes to help decode
141 * data. Table property as defined by the firmware.
143 struct pcf8584_tblprop {
144 unsigned int type;
145 unsigned int scale;
146 unsigned int offset; /* offset from the beginning of the table */
147 unsigned int size;
150 /* i2c child */
151 struct i2c_child_t {
152 /* Either ADC or GPIO. */
153 unsigned char i2ctype;
154 unsigned long addr;
155 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
157 /* Channel info. */
158 unsigned int total_chnls; /* Number of monitor channels. */
159 unsigned char fan_mask; /* Byte mask for fan status channels. */
160 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
161 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
163 /* Properties of all monitor channels. */
164 unsigned int total_tbls; /* Number of monitor tables. */
165 char *tables; /* Pointer to table(s). */
166 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
167 char mon_type[PCF8584_MAX_CHANNELS];
170 static void __iomem *i2c;
171 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
172 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
173 static unsigned int warning_temperature = 0;
174 static unsigned int shutdown_temperature = 0;
175 static char read_cpu;
177 /* Forward declarations. */
178 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
180 /* Function Description: Test the PIN bit (Pending Interrupt Not)
181 * to test when serial transmission is completed .
182 * Return : None.
184 static void envtrl_i2c_test_pin(void)
186 int limit = 1000000;
188 while (--limit > 0) {
189 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
190 break;
191 udelay(1);
194 if (limit <= 0)
195 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
198 /* Function Description: Test busy bit.
199 * Return : None.
201 static void envctrl_i2c_test_bb(void)
203 int limit = 1000000;
205 while (--limit > 0) {
206 /* Busy bit 0 means busy. */
207 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
208 break;
209 udelay(1);
212 if (limit <= 0)
213 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
216 /* Function Description: Send the address for a read access.
217 * Return : 0 if not acknowledged, otherwise acknowledged.
219 static int envctrl_i2c_read_addr(unsigned char addr)
221 envctrl_i2c_test_bb();
223 /* Load address. */
224 writeb(addr + 1, i2c + PCF8584_DATA);
226 envctrl_i2c_test_bb();
228 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
230 /* Wait for PIN. */
231 envtrl_i2c_test_pin();
233 /* CSR 0 means acknowledged. */
234 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
235 return readb(i2c + PCF8584_DATA);
236 } else {
237 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
238 return 0;
242 /* Function Description: Send the address for write mode.
243 * Return : None.
245 static void envctrl_i2c_write_addr(unsigned char addr)
247 envctrl_i2c_test_bb();
248 writeb(addr, i2c + PCF8584_DATA);
250 /* Generate Start condition. */
251 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
254 /* Function Description: Read 1 byte of data from addr
255 * set by envctrl_i2c_read_addr()
256 * Return : Data from address set by envctrl_i2c_read_addr().
258 static unsigned char envctrl_i2c_read_data(void)
260 envtrl_i2c_test_pin();
261 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
262 return readb(i2c + PCF8584_DATA);
265 /* Function Description: Instruct the device which port to read data from.
266 * Return : None.
268 static void envctrl_i2c_write_data(unsigned char port)
270 envtrl_i2c_test_pin();
271 writeb(port, i2c + PCF8584_DATA);
274 /* Function Description: Generate Stop condition after last byte is sent.
275 * Return : None.
277 static void envctrl_i2c_stop(void)
279 envtrl_i2c_test_pin();
280 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
283 /* Function Description: Read adc device.
284 * Return : Data at address and port.
286 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
288 /* Send address. */
289 envctrl_i2c_write_addr(addr);
291 /* Setup port to read. */
292 envctrl_i2c_write_data(port);
293 envctrl_i2c_stop();
295 /* Read port. */
296 envctrl_i2c_read_addr(addr);
298 /* Do a single byte read and send stop. */
299 envctrl_i2c_read_data();
300 envctrl_i2c_stop();
302 return readb(i2c + PCF8584_DATA);
305 /* Function Description: Read gpio device.
306 * Return : Data at address.
308 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
310 unsigned char rd;
312 envctrl_i2c_read_addr(addr);
314 /* Do a single byte read and send stop. */
315 rd = envctrl_i2c_read_data();
316 envctrl_i2c_stop();
317 return rd;
320 /* Function Description: Decode data read from an adc device using firmware
321 * table.
322 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
324 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
325 int scale, char *tbl, char *bufdata)
327 int len = 0;
329 switch (translate_type) {
330 case ENVCTRL_TRANSLATE_NO:
331 /* No decode necessary. */
332 len = 1;
333 bufdata[0] = data;
334 break;
336 case ENVCTRL_TRANSLATE_FULL:
337 /* Decode this way: data = table[data]. */
338 len = 1;
339 bufdata[0] = tbl[data];
340 break;
342 case ENVCTRL_TRANSLATE_SCALE:
343 /* Decode this way: data = table[data]/scale */
344 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
345 len = strlen(bufdata);
346 bufdata[len - 1] = bufdata[len - 2];
347 bufdata[len - 2] = '.';
348 break;
350 default:
351 break;
354 return len;
357 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
358 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
360 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
361 char mon_type, unsigned char *bufdata)
363 unsigned char data;
364 int i;
365 char *tbl, j = -1;
367 /* Find the right monitor type and channel. */
368 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
369 if (pchild->mon_type[i] == mon_type) {
370 if (++j == cpu) {
371 break;
376 if (j != cpu)
377 return 0;
379 /* Read data from address and port. */
380 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
381 (unsigned char)pchild->chnl_array[i].chnl_no);
383 /* Find decoding table. */
384 tbl = pchild->tables + pchild->tblprop_array[i].offset;
386 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
387 pchild->tblprop_array[i].scale,
388 tbl, bufdata);
391 /* Function Description: Read noncpu-related data such as motherboard
392 * temperature.
393 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
395 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
396 char mon_type, unsigned char *bufdata)
398 unsigned char data;
399 int i;
400 char *tbl = NULL;
402 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
403 if (pchild->mon_type[i] == mon_type)
404 break;
407 if (i >= PCF8584_MAX_CHANNELS)
408 return 0;
410 /* Read data from address and port. */
411 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
412 (unsigned char)pchild->chnl_array[i].chnl_no);
414 /* Find decoding table. */
415 tbl = pchild->tables + pchild->tblprop_array[i].offset;
417 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
418 pchild->tblprop_array[i].scale,
419 tbl, bufdata);
422 /* Function Description: Read fan status.
423 * Return : Always 1 byte. Status stored in bufdata.
425 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
426 unsigned char data,
427 char *bufdata)
429 unsigned char tmp, ret = 0;
430 int i, j = 0;
432 tmp = data & pchild->fan_mask;
434 if (tmp == pchild->fan_mask) {
435 /* All bits are on. All fans are functioning. */
436 ret = ENVCTRL_ALL_FANS_GOOD;
437 } else if (tmp == 0) {
438 /* No bits are on. No fans are functioning. */
439 ret = ENVCTRL_ALL_FANS_BAD;
440 } else {
441 /* Go through all channels, mark 'on' the matched bits.
442 * Notice that fan_mask may have discontiguous bits but
443 * return mask are always contiguous. For example if we
444 * monitor 4 fans at channels 0,1,2,4, the return mask
445 * should be 00010000 if only fan at channel 4 is working.
447 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
448 if (pchild->fan_mask & chnls_mask[i]) {
449 if (!(chnls_mask[i] & tmp))
450 ret |= chnls_mask[j];
452 j++;
457 bufdata[0] = ret;
458 return 1;
461 /* Function Description: Read global addressing line.
462 * Return : Always 1 byte. Status stored in bufdata.
464 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
465 unsigned char data,
466 char *bufdata)
468 /* Translatation table is not necessary, as global
469 * addr is the integer value of the GA# bits.
471 * NOTE: MSB is documented as zero, but I see it as '1' always....
473 * -----------------------------------------------
474 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
475 * -----------------------------------------------
476 * GA0 - GA4 integer value of Global Address (backplane slot#)
477 * DEG 0 = cPCI Power supply output is starting to degrade
478 * 1 = cPCI Power supply output is OK
479 * FAL 0 = cPCI Power supply has failed
480 * 1 = cPCI Power supply output is OK
482 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
483 return 1;
486 /* Function Description: Read standard voltage and power supply status.
487 * Return : Always 1 byte. Status stored in bufdata.
489 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
490 unsigned char data,
491 char *bufdata)
493 unsigned char tmp, ret = 0;
494 int i, j = 0;
496 tmp = data & pchild->voltage_mask;
498 /* Two channels are used to monitor voltage and power supply. */
499 if (tmp == pchild->voltage_mask) {
500 /* All bits are on. Voltage and power supply are okay. */
501 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
502 } else if (tmp == 0) {
503 /* All bits are off. Voltage and power supply are bad */
504 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
505 } else {
506 /* Either voltage or power supply has problem. */
507 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
508 if (pchild->voltage_mask & chnls_mask[i]) {
509 j++;
511 /* Break out when there is a mismatch. */
512 if (!(chnls_mask[i] & tmp))
513 break;
517 /* Make a wish that hardware will always use the
518 * first channel for voltage and the second for
519 * power supply.
521 if (j == 1)
522 ret = ENVCTRL_VOLTAGE_BAD;
523 else
524 ret = ENVCTRL_POWERSUPPLY_BAD;
527 bufdata[0] = ret;
528 return 1;
531 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
532 * Return: Number of read bytes. 0 for error.
534 static ssize_t
535 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
537 struct i2c_child_t *pchild;
538 unsigned char data[10];
539 int ret = 0;
541 /* Get the type of read as decided in ioctl() call.
542 * Find the appropriate i2c child.
543 * Get the data and put back to the user buffer.
546 switch ((int)(long)file->private_data) {
547 case ENVCTRL_RD_WARNING_TEMPERATURE:
548 if (warning_temperature == 0)
549 return 0;
551 data[0] = (unsigned char)(warning_temperature);
552 ret = 1;
553 if (copy_to_user(buf, data, ret))
554 ret = -EFAULT;
555 break;
557 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
558 if (shutdown_temperature == 0)
559 return 0;
561 data[0] = (unsigned char)(shutdown_temperature);
562 ret = 1;
563 if (copy_to_user(buf, data, ret))
564 ret = -EFAULT;
565 break;
567 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
568 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
569 return 0;
570 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
571 if (copy_to_user(buf, data, ret))
572 ret = -EFAULT;
573 break;
575 case ENVCTRL_RD_CPU_TEMPERATURE:
576 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
577 return 0;
578 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
580 /* Reset cpu to the default cpu0. */
581 if (copy_to_user(buf, data, ret))
582 ret = -EFAULT;
583 break;
585 case ENVCTRL_RD_CPU_VOLTAGE:
586 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
587 return 0;
588 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
590 /* Reset cpu to the default cpu0. */
591 if (copy_to_user(buf, data, ret))
592 ret = -EFAULT;
593 break;
595 case ENVCTRL_RD_SCSI_TEMPERATURE:
596 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
597 return 0;
598 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
599 if (copy_to_user(buf, data, ret))
600 ret = -EFAULT;
601 break;
603 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
604 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
605 return 0;
606 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
607 if (copy_to_user(buf, data, ret))
608 ret = -EFAULT;
609 break;
611 case ENVCTRL_RD_FAN_STATUS:
612 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
613 return 0;
614 data[0] = envctrl_i2c_read_8574(pchild->addr);
615 ret = envctrl_i2c_fan_status(pchild,data[0], data);
616 if (copy_to_user(buf, data, ret))
617 ret = -EFAULT;
618 break;
620 case ENVCTRL_RD_GLOBALADDRESS:
621 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
622 return 0;
623 data[0] = envctrl_i2c_read_8574(pchild->addr);
624 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
625 if (copy_to_user(buf, data, ret))
626 ret = -EFAULT;
627 break;
629 case ENVCTRL_RD_VOLTAGE_STATUS:
630 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
631 /* If voltage monitor not present, check for CPCI equivalent */
632 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
633 return 0;
634 data[0] = envctrl_i2c_read_8574(pchild->addr);
635 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
636 if (copy_to_user(buf, data, ret))
637 ret = -EFAULT;
638 break;
640 default:
641 break;
645 return ret;
648 /* Function Description: Command what to read. Mapped to user ioctl().
649 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
651 static long
652 envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
654 char __user *infobuf;
656 switch (cmd) {
657 case ENVCTRL_RD_WARNING_TEMPERATURE:
658 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
659 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
660 case ENVCTRL_RD_FAN_STATUS:
661 case ENVCTRL_RD_VOLTAGE_STATUS:
662 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
663 case ENVCTRL_RD_SCSI_TEMPERATURE:
664 case ENVCTRL_RD_GLOBALADDRESS:
665 file->private_data = (void *)(long)cmd;
666 break;
668 case ENVCTRL_RD_CPU_TEMPERATURE:
669 case ENVCTRL_RD_CPU_VOLTAGE:
670 /* Check to see if application passes in any cpu number,
671 * the default is cpu0.
673 infobuf = (char __user *) arg;
674 if (infobuf == NULL) {
675 read_cpu = 0;
676 }else {
677 get_user(read_cpu, infobuf);
680 /* Save the command for use when reading. */
681 file->private_data = (void *)(long)cmd;
682 break;
684 default:
685 return -EINVAL;
688 return 0;
691 /* Function Description: open device. Mapped to user open().
692 * Return: Always 0.
694 static int
695 envctrl_open(struct inode *inode, struct file *file)
697 file->private_data = NULL;
698 return 0;
701 /* Function Description: Open device. Mapped to user close().
702 * Return: Always 0.
704 static int
705 envctrl_release(struct inode *inode, struct file *file)
707 return 0;
710 static const struct file_operations envctrl_fops = {
711 .owner = THIS_MODULE,
712 .read = envctrl_read,
713 .unlocked_ioctl = envctrl_ioctl,
714 #ifdef CONFIG_COMPAT
715 .compat_ioctl = envctrl_ioctl,
716 #endif
717 .open = envctrl_open,
718 .release = envctrl_release,
721 static struct miscdevice envctrl_dev = {
722 ENVCTRL_MINOR,
723 "envctrl",
724 &envctrl_fops
727 /* Function Description: Set monitor type based on firmware description.
728 * Return: None.
730 static void envctrl_set_mon(struct i2c_child_t *pchild,
731 const char *chnl_desc,
732 int chnl_no)
734 /* Firmware only has temperature type. It does not distinguish
735 * different kinds of temperatures. We use channel description
736 * to disinguish them.
738 if (!(strcmp(chnl_desc,"temp,cpu")) ||
739 !(strcmp(chnl_desc,"temp,cpu0")) ||
740 !(strcmp(chnl_desc,"temp,cpu1")) ||
741 !(strcmp(chnl_desc,"temp,cpu2")) ||
742 !(strcmp(chnl_desc,"temp,cpu3")))
743 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
745 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
746 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
747 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
748 !(strcmp(chnl_desc,"vddcore,cpu3")))
749 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
751 if (!(strcmp(chnl_desc,"temp,motherboard")))
752 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
754 if (!(strcmp(chnl_desc,"temp,scsi")))
755 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
757 if (!(strcmp(chnl_desc,"temp,ethernet")))
758 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
761 /* Function Description: Initialize monitor channel with channel desc,
762 * decoding tables, monitor type, optional properties.
763 * Return: None.
765 static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
767 int i = 0, len;
768 const char *pos;
769 const unsigned int *pval;
771 /* Firmware describe channels into a stream separated by a '\0'. */
772 pos = of_get_property(dp, "channels-description", &len);
774 while (len > 0) {
775 int l = strlen(pos) + 1;
776 envctrl_set_mon(pchild, pos, i++);
777 len -= l;
778 pos += l;
781 /* Get optional properties. */
782 pval = of_get_property(dp, "warning-temp", NULL);
783 if (pval)
784 warning_temperature = *pval;
786 pval = of_get_property(dp, "shutdown-temp", NULL);
787 if (pval)
788 shutdown_temperature = *pval;
791 /* Function Description: Initialize child device monitoring fan status.
792 * Return: None.
794 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
796 int i;
798 /* Go through all channels and set up the mask. */
799 for (i = 0; i < pchild->total_chnls; i++)
800 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
802 /* We only need to know if this child has fan status monitored.
803 * We don't care which channels since we have the mask already.
805 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
808 /* Function Description: Initialize child device for global addressing line.
809 * Return: None.
811 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
813 int i;
815 /* Voltage/PowerSupply monitoring is piggybacked
816 * with Global Address on CompactPCI. See comments
817 * within envctrl_i2c_globaladdr for bit assignments.
819 * The mask is created here by assigning mask bits to each
820 * bit position that represents PCF8584_VOLTAGE_TYPE data.
821 * Channel numbers are not consecutive within the globaladdr
822 * node (why?), so we use the actual counter value as chnls_mask
823 * index instead of the chnl_array[x].chnl_no value.
825 * NOTE: This loop could be replaced with a constant representing
826 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
828 for (i = 0; i < pchild->total_chnls; i++) {
829 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
830 pchild->voltage_mask |= chnls_mask[i];
834 /* We only need to know if this child has global addressing
835 * line monitored. We don't care which channels since we know
836 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
838 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
841 /* Initialize child device monitoring voltage status. */
842 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
844 int i;
846 /* Go through all channels and set up the mask. */
847 for (i = 0; i < pchild->total_chnls; i++)
848 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
850 /* We only need to know if this child has voltage status monitored.
851 * We don't care which channels since we have the mask already.
853 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
856 /* Function Description: Initialize i2c child device.
857 * Return: None.
859 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
860 struct i2c_child_t *pchild)
862 int len, i, tbls_size = 0;
863 struct device_node *dp = edev_child->prom_node;
864 const void *pval;
866 /* Get device address. */
867 pval = of_get_property(dp, "reg", &len);
868 memcpy(&pchild->addr, pval, len);
870 /* Get tables property. Read firmware temperature tables. */
871 pval = of_get_property(dp, "translation", &len);
872 if (pval && len > 0) {
873 memcpy(pchild->tblprop_array, pval, len);
874 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
875 for (i = 0; i < pchild->total_tbls; i++) {
876 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
877 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
881 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
882 if (pchild->tables == NULL){
883 printk("envctrl: Failed to allocate table.\n");
884 return;
886 pval = of_get_property(dp, "tables", &len);
887 if (!pval || len <= 0) {
888 printk("envctrl: Failed to get table.\n");
889 return;
891 memcpy(pchild->tables, pval, len);
894 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
895 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
896 * "For Factory Use Only."
898 * We ignore the node on these platforms by assigning the
899 * 'NULL' monitor type.
901 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
902 struct device_node *root_node;
903 int len;
905 root_node = of_find_node_by_path("/");
906 if (!strcmp(root_node->name, "SUNW,UltraSPARC-IIi-cEngine")) {
907 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
908 pchild->mon_type[len] = ENVCTRL_NOMON;
910 return;
914 /* Get the monitor channels. */
915 pval = of_get_property(dp, "channels-in-use", &len);
916 memcpy(pchild->chnl_array, pval, len);
917 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
919 for (i = 0; i < pchild->total_chnls; i++) {
920 switch (pchild->chnl_array[i].type) {
921 case PCF8584_TEMP_TYPE:
922 envctrl_init_adc(pchild, dp);
923 break;
925 case PCF8584_GLOBALADDR_TYPE:
926 envctrl_init_globaladdr(pchild);
927 i = pchild->total_chnls;
928 break;
930 case PCF8584_FANSTAT_TYPE:
931 envctrl_init_fanstat(pchild);
932 i = pchild->total_chnls;
933 break;
935 case PCF8584_VOLTAGE_TYPE:
936 if (pchild->i2ctype == I2C_ADC) {
937 envctrl_init_adc(pchild,dp);
938 } else {
939 envctrl_init_voltage_status(pchild);
941 i = pchild->total_chnls;
942 break;
944 default:
945 break;
950 /* Function Description: Search the child device list for a device.
951 * Return : The i2c child if found. NULL otherwise.
953 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
955 int i, j;
957 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
958 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
959 if (i2c_childlist[i].mon_type[j] == mon_type) {
960 return (struct i2c_child_t *)(&(i2c_childlist[i]));
964 return NULL;
967 static void envctrl_do_shutdown(void)
969 static int inprog = 0;
970 int ret;
972 if (inprog != 0)
973 return;
975 inprog = 1;
976 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
977 ret = orderly_poweroff(true);
978 if (ret < 0) {
979 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
980 inprog = 0; /* unlikely to succeed, but we could try again */
984 static struct task_struct *kenvctrld_task;
986 static int kenvctrld(void *__unused)
988 int poll_interval;
989 int whichcpu;
990 char tempbuf[10];
991 struct i2c_child_t *cputemp;
993 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
994 printk(KERN_ERR
995 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
996 return -ENODEV;
999 poll_interval = 5000; /* TODO env_mon_interval */
1001 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1002 for (;;) {
1003 msleep_interruptible(poll_interval);
1005 if (kthread_should_stop())
1006 break;
1008 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1009 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1010 ENVCTRL_CPUTEMP_MON,
1011 tempbuf)) {
1012 if (tempbuf[0] >= shutdown_temperature) {
1013 printk(KERN_CRIT
1014 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1015 "shutdown threshold %i C\n",
1016 current->comm, whichcpu,
1017 tempbuf[0], shutdown_temperature);
1018 envctrl_do_shutdown();
1023 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1024 return 0;
1027 static int __init envctrl_init(void)
1029 struct linux_ebus *ebus = NULL;
1030 struct linux_ebus_device *edev = NULL;
1031 struct linux_ebus_child *edev_child = NULL;
1032 int err, i = 0;
1034 for_each_ebus(ebus) {
1035 for_each_ebusdev(edev, ebus) {
1036 if (!strcmp(edev->prom_node->name, "bbc")) {
1037 /* If we find a boot-bus controller node,
1038 * then this envctrl driver is not for us.
1040 return -ENODEV;
1045 /* Traverse through ebus and ebus device list for i2c device and
1046 * adc and gpio nodes.
1048 for_each_ebus(ebus) {
1049 for_each_ebusdev(edev, ebus) {
1050 if (!strcmp(edev->prom_node->name, "i2c")) {
1051 i2c = ioremap(edev->resource[0].start, 0x2);
1052 for_each_edevchild(edev, edev_child) {
1053 if (!strcmp("gpio", edev_child->prom_node->name)) {
1054 i2c_childlist[i].i2ctype = I2C_GPIO;
1055 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1057 if (!strcmp("adc", edev_child->prom_node->name)) {
1058 i2c_childlist[i].i2ctype = I2C_ADC;
1059 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1062 goto done;
1067 done:
1068 if (!edev) {
1069 printk("envctrl: I2C device not found.\n");
1070 return -ENODEV;
1073 /* Set device address. */
1074 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1075 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1077 /* Set system clock and SCL frequencies. */
1078 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1079 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1081 /* Enable serial interface. */
1082 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1083 udelay(200);
1085 /* Register the device as a minor miscellaneous device. */
1086 err = misc_register(&envctrl_dev);
1087 if (err) {
1088 printk("envctrl: Unable to get misc minor %d\n",
1089 envctrl_dev.minor);
1090 goto out_iounmap;
1093 /* Note above traversal routine post-incremented 'i' to accommodate
1094 * a next child device, so we decrement before reverse-traversal of
1095 * child devices.
1097 printk("envctrl: initialized ");
1098 for (--i; i >= 0; --i) {
1099 printk("[%s 0x%lx]%s",
1100 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1101 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1102 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1105 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1106 if (IS_ERR(kenvctrld_task)) {
1107 err = PTR_ERR(kenvctrld_task);
1108 goto out_deregister;
1111 return 0;
1113 out_deregister:
1114 misc_deregister(&envctrl_dev);
1115 out_iounmap:
1116 iounmap(i2c);
1117 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1118 kfree(i2c_childlist[i].tables);
1120 return err;
1123 static void __exit envctrl_cleanup(void)
1125 int i;
1127 kthread_stop(kenvctrld_task);
1129 iounmap(i2c);
1130 misc_deregister(&envctrl_dev);
1132 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1133 kfree(i2c_childlist[i].tables);
1136 module_init(envctrl_init);
1137 module_exit(envctrl_cleanup);
1138 MODULE_LICENSE("GPL");