xref: /openbmc/linux/drivers/sbus/char/envctrl.c (revision 545e4006)
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.
3  *
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
14  *
15  * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
16  * 		Eric Brower <ebrower@usa.net>
17  *
18  * DB - Audit every copy_to_user in envctrl_read.
19  *              Daniele Bellucci <bellucda@tiscali.it>
20  */
21 
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>
30 #include <linux/smp_lock.h>
31 
32 #include <asm/ebus.h>
33 #include <asm/uaccess.h>
34 #include <asm/envctrl.h>
35 #include <asm/io.h>
36 
37 #define ENVCTRL_MINOR	162
38 
39 #define PCF8584_ADDRESS	0x55
40 
41 #define CONTROL_PIN	0x80
42 #define CONTROL_ES0	0x40
43 #define CONTROL_ES1	0x20
44 #define CONTROL_ES2	0x10
45 #define CONTROL_ENI	0x08
46 #define CONTROL_STA	0x04
47 #define CONTROL_STO	0x02
48 #define CONTROL_ACK	0x01
49 
50 #define STATUS_PIN	0x80
51 #define STATUS_STS	0x20
52 #define STATUS_BER	0x10
53 #define STATUS_LRB	0x08
54 #define STATUS_AD0	0x08
55 #define STATUS_AAB	0x04
56 #define STATUS_LAB	0x02
57 #define STATUS_BB	0x01
58 
59 /*
60  * CLK Mode Register.
61  */
62 #define BUS_CLK_90	0x00
63 #define BUS_CLK_45	0x01
64 #define BUS_CLK_11	0x02
65 #define BUS_CLK_1_5	0x03
66 
67 #define CLK_3		0x00
68 #define CLK_4_43	0x10
69 #define CLK_6		0x14
70 #define CLK_8		0x18
71 #define CLK_12		0x1c
72 
73 #define OBD_SEND_START	0xc5    /* value to generate I2c_bus START condition */
74 #define OBD_SEND_STOP 	0xc3    /* value to generate I2c_bus STOP condition */
75 
76 /* Monitor type of i2c child device.
77  * Firmware definitions.
78  */
79 #define PCF8584_MAX_CHANNELS            8
80 #define PCF8584_GLOBALADDR_TYPE			6  /* global address monitor */
81 #define PCF8584_FANSTAT_TYPE            3  /* fan status monitor */
82 #define PCF8584_VOLTAGE_TYPE            2  /* voltage monitor    */
83 #define PCF8584_TEMP_TYPE	        	1  /* temperature monitor*/
84 
85 /* Monitor type of i2c child device.
86  * Driver definitions.
87  */
88 #define ENVCTRL_NOMON				0
89 #define ENVCTRL_CPUTEMP_MON			1    /* cpu temperature monitor */
90 #define ENVCTRL_CPUVOLTAGE_MON	  	2    /* voltage monitor         */
91 #define ENVCTRL_FANSTAT_MON  		3    /* fan status monitor      */
92 #define ENVCTRL_ETHERTEMP_MON		4    /* ethernet temperarture */
93 					     /* monitor                     */
94 #define ENVCTRL_VOLTAGESTAT_MON	  	5    /* voltage status monitor  */
95 #define ENVCTRL_MTHRBDTEMP_MON		6    /* motherboard temperature */
96 #define ENVCTRL_SCSITEMP_MON		7    /* scsi temperarture */
97 #define ENVCTRL_GLOBALADDR_MON		8    /* global address */
98 
99 /* Child device type.
100  * Driver definitions.
101  */
102 #define I2C_ADC				0    /* pcf8591 */
103 #define I2C_GPIO			1    /* pcf8571 */
104 
105 /* Data read from child device may need to decode
106  * through a data table and a scale.
107  * Translation type as defined by firmware.
108  */
109 #define ENVCTRL_TRANSLATE_NO		0
110 #define ENVCTRL_TRANSLATE_PARTIAL	1
111 #define ENVCTRL_TRANSLATE_COMBINED	2
112 #define ENVCTRL_TRANSLATE_FULL		3     /* table[data] */
113 #define ENVCTRL_TRANSLATE_SCALE		4     /* table[data]/scale */
114 
115 /* Driver miscellaneous definitions. */
116 #define ENVCTRL_MAX_CPU			4
117 #define CHANNEL_DESC_SZ			256
118 
119 /* Mask values for combined GlobalAddress/PowerStatus node */
120 #define ENVCTRL_GLOBALADDR_ADDR_MASK 	0x1F
121 #define ENVCTRL_GLOBALADDR_PSTAT_MASK	0x60
122 
123 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
124  * (see envctrl_init_i2c_child)
125  */
126 #define ENVCTRL_CPCI_IGNORED_NODE		0x70
127 
128 #define PCF8584_DATA	0x00
129 #define PCF8584_CSR	0x01
130 
131 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
132  * Property of a port or channel as defined by the firmware.
133  */
134 struct pcf8584_channel {
135         unsigned char chnl_no;
136         unsigned char io_direction;
137         unsigned char type;
138         unsigned char last;
139 };
140 
141 /* Each child device may have one or more tables of bytes to help decode
142  * data. Table property as defined by the firmware.
143  */
144 struct pcf8584_tblprop {
145         unsigned int type;
146         unsigned int scale;
147         unsigned int offset; /* offset from the beginning of the table */
148         unsigned int size;
149 };
150 
151 /* i2c child */
152 struct i2c_child_t {
153 	/* Either ADC or GPIO. */
154 	unsigned char i2ctype;
155         unsigned long addr;
156         struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
157 
158 	/* Channel info. */
159 	unsigned int total_chnls;	/* Number of monitor channels. */
160 	unsigned char fan_mask;		/* Byte mask for fan status channels. */
161 	unsigned char voltage_mask;	/* Byte mask for voltage status channels. */
162         struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
163 
164 	/* Properties of all monitor channels. */
165 	unsigned int total_tbls;	/* Number of monitor tables. */
166         char *tables;			/* Pointer to table(s). */
167 	char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
168 	char mon_type[PCF8584_MAX_CHANNELS];
169 };
170 
171 static void __iomem *i2c;
172 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
173 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
174 static unsigned int warning_temperature = 0;
175 static unsigned int shutdown_temperature = 0;
176 static char read_cpu;
177 
178 /* Forward declarations. */
179 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
180 
181 /* Function Description: Test the PIN bit (Pending Interrupt Not)
182  * 			 to test when serial transmission is completed .
183  * Return : None.
184  */
185 static void envtrl_i2c_test_pin(void)
186 {
187 	int limit = 1000000;
188 
189 	while (--limit > 0) {
190 		if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
191 			break;
192 		udelay(1);
193 	}
194 
195 	if (limit <= 0)
196 		printk(KERN_INFO "envctrl: Pin status will not clear.\n");
197 }
198 
199 /* Function Description: Test busy bit.
200  * Return : None.
201  */
202 static void envctrl_i2c_test_bb(void)
203 {
204 	int limit = 1000000;
205 
206 	while (--limit > 0) {
207 		/* Busy bit 0 means busy. */
208 		if (readb(i2c + PCF8584_CSR) & STATUS_BB)
209 			break;
210 		udelay(1);
211 	}
212 
213 	if (limit <= 0)
214 		printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
215 }
216 
217 /* Function Description: Send the address for a read access.
218  * Return : 0 if not acknowledged, otherwise acknowledged.
219  */
220 static int envctrl_i2c_read_addr(unsigned char addr)
221 {
222 	envctrl_i2c_test_bb();
223 
224 	/* Load address. */
225 	writeb(addr + 1, i2c + PCF8584_DATA);
226 
227 	envctrl_i2c_test_bb();
228 
229 	writeb(OBD_SEND_START, i2c + PCF8584_CSR);
230 
231 	/* Wait for PIN. */
232 	envtrl_i2c_test_pin();
233 
234 	/* CSR 0 means acknowledged. */
235 	if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
236 		return readb(i2c + PCF8584_DATA);
237 	} else {
238 		writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
239 		return 0;
240 	}
241 }
242 
243 /* Function Description: Send the address for write mode.
244  * Return : None.
245  */
246 static void envctrl_i2c_write_addr(unsigned char addr)
247 {
248 	envctrl_i2c_test_bb();
249 	writeb(addr, i2c + PCF8584_DATA);
250 
251 	/* Generate Start condition. */
252 	writeb(OBD_SEND_START, i2c + PCF8584_CSR);
253 }
254 
255 /* Function Description: Read 1 byte of data from addr
256  *			 set by envctrl_i2c_read_addr()
257  * Return : Data from address set by envctrl_i2c_read_addr().
258  */
259 static unsigned char envctrl_i2c_read_data(void)
260 {
261 	envtrl_i2c_test_pin();
262 	writeb(CONTROL_ES0, i2c + PCF8584_CSR);  /* Send neg ack. */
263 	return readb(i2c + PCF8584_DATA);
264 }
265 
266 /* Function Description: Instruct the device which port to read data from.
267  * Return : None.
268  */
269 static void envctrl_i2c_write_data(unsigned char port)
270 {
271 	envtrl_i2c_test_pin();
272 	writeb(port, i2c + PCF8584_DATA);
273 }
274 
275 /* Function Description: Generate Stop condition after last byte is sent.
276  * Return : None.
277  */
278 static void envctrl_i2c_stop(void)
279 {
280 	envtrl_i2c_test_pin();
281 	writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
282 }
283 
284 /* Function Description: Read adc device.
285  * Return : Data at address and port.
286  */
287 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
288 {
289 	/* Send address. */
290 	envctrl_i2c_write_addr(addr);
291 
292 	/* Setup port to read. */
293 	envctrl_i2c_write_data(port);
294 	envctrl_i2c_stop();
295 
296 	/* Read port. */
297 	envctrl_i2c_read_addr(addr);
298 
299 	/* Do a single byte read and send stop. */
300 	envctrl_i2c_read_data();
301 	envctrl_i2c_stop();
302 
303 	return readb(i2c + PCF8584_DATA);
304 }
305 
306 /* Function Description: Read gpio device.
307  * Return : Data at address.
308  */
309 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
310 {
311 	unsigned char rd;
312 
313 	envctrl_i2c_read_addr(addr);
314 
315 	/* Do a single byte read and send stop. */
316 	rd = envctrl_i2c_read_data();
317 	envctrl_i2c_stop();
318 	return rd;
319 }
320 
321 /* Function Description: Decode data read from an adc device using firmware
322  *                       table.
323  * Return: Number of read bytes. Data is stored in bufdata in ascii format.
324  */
325 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
326 				      int scale, char *tbl, char *bufdata)
327 {
328 	int len = 0;
329 
330 	switch (translate_type) {
331 	case ENVCTRL_TRANSLATE_NO:
332 		/* No decode necessary. */
333 		len = 1;
334 		bufdata[0] = data;
335 		break;
336 
337 	case ENVCTRL_TRANSLATE_FULL:
338 		/* Decode this way: data = table[data]. */
339 		len = 1;
340 		bufdata[0] = tbl[data];
341 		break;
342 
343 	case ENVCTRL_TRANSLATE_SCALE:
344 		/* Decode this way: data = table[data]/scale */
345 		sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
346 		len = strlen(bufdata);
347 		bufdata[len - 1] = bufdata[len - 2];
348 		bufdata[len - 2] = '.';
349 		break;
350 
351 	default:
352 		break;
353 	};
354 
355 	return len;
356 }
357 
358 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
359  * Return: Number of read bytes. Data is stored in bufdata in ascii format.
360  */
361 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
362 				 char mon_type, unsigned char *bufdata)
363 {
364 	unsigned char data;
365 	int i;
366 	char *tbl, j = -1;
367 
368 	/* Find the right monitor type and channel. */
369 	for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
370 		if (pchild->mon_type[i] == mon_type) {
371 			if (++j == cpu) {
372 				break;
373 			}
374 		}
375 	}
376 
377 	if (j != cpu)
378 		return 0;
379 
380         /* Read data from address and port. */
381 	data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
382 				     (unsigned char)pchild->chnl_array[i].chnl_no);
383 
384 	/* Find decoding table. */
385 	tbl = pchild->tables + pchild->tblprop_array[i].offset;
386 
387 	return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
388 					  pchild->tblprop_array[i].scale,
389 					  tbl, bufdata);
390 }
391 
392 /* Function Description: Read noncpu-related data such as motherboard
393  *                       temperature.
394  * Return: Number of read bytes. Data is stored in bufdata in ascii format.
395  */
396 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
397 				    char mon_type, unsigned char *bufdata)
398 {
399 	unsigned char data;
400 	int i;
401 	char *tbl = NULL;
402 
403 	for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
404 		if (pchild->mon_type[i] == mon_type)
405 			break;
406 	}
407 
408 	if (i >= PCF8584_MAX_CHANNELS)
409 		return 0;
410 
411         /* Read data from address and port. */
412 	data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
413 				     (unsigned char)pchild->chnl_array[i].chnl_no);
414 
415 	/* Find decoding table. */
416 	tbl = pchild->tables + pchild->tblprop_array[i].offset;
417 
418 	return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
419 					  pchild->tblprop_array[i].scale,
420 					  tbl, bufdata);
421 }
422 
423 /* Function Description: Read fan status.
424  * Return : Always 1 byte. Status stored in bufdata.
425  */
426 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
427 				  unsigned char data,
428 				  char *bufdata)
429 {
430 	unsigned char tmp, ret = 0;
431 	int i, j = 0;
432 
433 	tmp = data & pchild->fan_mask;
434 
435 	if (tmp == pchild->fan_mask) {
436 		/* All bits are on. All fans are functioning. */
437 		ret = ENVCTRL_ALL_FANS_GOOD;
438 	} else if (tmp == 0) {
439 		/* No bits are on. No fans are functioning. */
440 		ret = ENVCTRL_ALL_FANS_BAD;
441 	} else {
442 		/* Go through all channels, mark 'on' the matched bits.
443 		 * Notice that fan_mask may have discontiguous bits but
444 		 * return mask are always contiguous. For example if we
445 		 * monitor 4 fans at channels 0,1,2,4, the return mask
446 		 * should be 00010000 if only fan at channel 4 is working.
447 		 */
448 		for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
449 			if (pchild->fan_mask & chnls_mask[i]) {
450 				if (!(chnls_mask[i] & tmp))
451 					ret |= chnls_mask[j];
452 
453 				j++;
454 			}
455 		}
456 	}
457 
458 	bufdata[0] = ret;
459 	return 1;
460 }
461 
462 /* Function Description: Read global addressing line.
463  * Return : Always 1 byte. Status stored in bufdata.
464  */
465 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
466 				  unsigned char data,
467 				  char *bufdata)
468 {
469 	/* Translatation table is not necessary, as global
470 	 * addr is the integer value of the GA# bits.
471 	 *
472 	 * NOTE: MSB is documented as zero, but I see it as '1' always....
473 	 *
474 	 * -----------------------------------------------
475 	 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
476 	 * -----------------------------------------------
477 	 * GA0 - GA4	integer value of Global Address (backplane slot#)
478 	 * DEG			0 = cPCI Power supply output is starting to degrade
479 	 * 				1 = cPCI Power supply output is OK
480 	 * FAL			0 = cPCI Power supply has failed
481 	 * 				1 = cPCI Power supply output is OK
482 	 */
483 	bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
484 	return 1;
485 }
486 
487 /* Function Description: Read standard voltage and power supply status.
488  * Return : Always 1 byte. Status stored in bufdata.
489  */
490 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
491 						unsigned char data,
492 						char *bufdata)
493 {
494 	unsigned char tmp, ret = 0;
495 	int i, j = 0;
496 
497 	tmp = data & pchild->voltage_mask;
498 
499 	/* Two channels are used to monitor voltage and power supply. */
500 	if (tmp == pchild->voltage_mask) {
501 		/* All bits are on. Voltage and power supply are okay. */
502 		ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
503 	} else if (tmp == 0) {
504 		/* All bits are off. Voltage and power supply are bad */
505 		ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
506 	} else {
507 		/* Either voltage or power supply has problem. */
508 		for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
509 			if (pchild->voltage_mask & chnls_mask[i]) {
510 				j++;
511 
512 				/* Break out when there is a mismatch. */
513 				if (!(chnls_mask[i] & tmp))
514 					break;
515 			}
516 		}
517 
518 		/* Make a wish that hardware will always use the
519 		 * first channel for voltage and the second for
520 		 * power supply.
521 		 */
522 		if (j == 1)
523 			ret = ENVCTRL_VOLTAGE_BAD;
524 		else
525 			ret = ENVCTRL_POWERSUPPLY_BAD;
526 	}
527 
528 	bufdata[0] = ret;
529 	return 1;
530 }
531 
532 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
533  * Return: Number of read bytes. 0 for error.
534  */
535 static ssize_t
536 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
537 {
538 	struct i2c_child_t *pchild;
539 	unsigned char data[10];
540 	int ret = 0;
541 
542 	/* Get the type of read as decided in ioctl() call.
543 	 * Find the appropriate i2c child.
544 	 * Get the data and put back to the user buffer.
545 	 */
546 
547 	switch ((int)(long)file->private_data) {
548 	case ENVCTRL_RD_WARNING_TEMPERATURE:
549 		if (warning_temperature == 0)
550 			return 0;
551 
552 		data[0] = (unsigned char)(warning_temperature);
553 		ret = 1;
554 		if (copy_to_user(buf, data, ret))
555 			ret = -EFAULT;
556 		break;
557 
558 	case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
559 		if (shutdown_temperature == 0)
560 			return 0;
561 
562 		data[0] = (unsigned char)(shutdown_temperature);
563 		ret = 1;
564 		if (copy_to_user(buf, data, ret))
565 			ret = -EFAULT;
566 		break;
567 
568 	case ENVCTRL_RD_MTHRBD_TEMPERATURE:
569 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
570 			return 0;
571 		ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
572 		if (copy_to_user(buf, data, ret))
573 			ret = -EFAULT;
574 		break;
575 
576 	case ENVCTRL_RD_CPU_TEMPERATURE:
577 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
578 			return 0;
579 		ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
580 
581 		/* Reset cpu to the default cpu0. */
582 		if (copy_to_user(buf, data, ret))
583 			ret = -EFAULT;
584 		break;
585 
586 	case ENVCTRL_RD_CPU_VOLTAGE:
587 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
588 			return 0;
589 		ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
590 
591 		/* Reset cpu to the default cpu0. */
592 		if (copy_to_user(buf, data, ret))
593 			ret = -EFAULT;
594 		break;
595 
596 	case ENVCTRL_RD_SCSI_TEMPERATURE:
597 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
598 			return 0;
599 		ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
600 		if (copy_to_user(buf, data, ret))
601 			ret = -EFAULT;
602 		break;
603 
604 	case ENVCTRL_RD_ETHERNET_TEMPERATURE:
605 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
606 			return 0;
607 		ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
608 		if (copy_to_user(buf, data, ret))
609 			ret = -EFAULT;
610 		break;
611 
612 	case ENVCTRL_RD_FAN_STATUS:
613 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
614 			return 0;
615 		data[0] = envctrl_i2c_read_8574(pchild->addr);
616 		ret = envctrl_i2c_fan_status(pchild,data[0], data);
617 		if (copy_to_user(buf, data, ret))
618 			ret = -EFAULT;
619 		break;
620 
621 	case ENVCTRL_RD_GLOBALADDRESS:
622 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
623 			return 0;
624 		data[0] = envctrl_i2c_read_8574(pchild->addr);
625 		ret = envctrl_i2c_globaladdr(pchild, data[0], data);
626 		if (copy_to_user(buf, data, ret))
627 			ret = -EFAULT;
628 		break;
629 
630 	case ENVCTRL_RD_VOLTAGE_STATUS:
631 		if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
632 			/* If voltage monitor not present, check for CPCI equivalent */
633 			if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
634 				return 0;
635 		data[0] = envctrl_i2c_read_8574(pchild->addr);
636 		ret = envctrl_i2c_voltage_status(pchild, data[0], data);
637 		if (copy_to_user(buf, data, ret))
638 			ret = -EFAULT;
639 		break;
640 
641 	default:
642 		break;
643 
644 	};
645 
646 	return ret;
647 }
648 
649 /* Function Description: Command what to read.  Mapped to user ioctl().
650  * Return: Gives 0 for implemented commands, -EINVAL otherwise.
651  */
652 static long
653 envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
654 {
655 	char __user *infobuf;
656 
657 	switch (cmd) {
658 	case ENVCTRL_RD_WARNING_TEMPERATURE:
659 	case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
660 	case ENVCTRL_RD_MTHRBD_TEMPERATURE:
661 	case ENVCTRL_RD_FAN_STATUS:
662 	case ENVCTRL_RD_VOLTAGE_STATUS:
663 	case ENVCTRL_RD_ETHERNET_TEMPERATURE:
664 	case ENVCTRL_RD_SCSI_TEMPERATURE:
665 	case ENVCTRL_RD_GLOBALADDRESS:
666 		file->private_data = (void *)(long)cmd;
667 		break;
668 
669 	case ENVCTRL_RD_CPU_TEMPERATURE:
670 	case ENVCTRL_RD_CPU_VOLTAGE:
671 		/* Check to see if application passes in any cpu number,
672 		 * the default is cpu0.
673 		 */
674 		infobuf = (char __user *) arg;
675 		if (infobuf == NULL) {
676 			read_cpu = 0;
677 		}else {
678 			get_user(read_cpu, infobuf);
679 		}
680 
681 		/* Save the command for use when reading. */
682 		file->private_data = (void *)(long)cmd;
683 		break;
684 
685 	default:
686 		return -EINVAL;
687 	};
688 
689 	return 0;
690 }
691 
692 /* Function Description: open device. Mapped to user open().
693  * Return: Always 0.
694  */
695 static int
696 envctrl_open(struct inode *inode, struct file *file)
697 {
698 	cycle_kernel_lock();
699 	file->private_data = NULL;
700 	return 0;
701 }
702 
703 /* Function Description: Open device. Mapped to user close().
704  * Return: Always 0.
705  */
706 static int
707 envctrl_release(struct inode *inode, struct file *file)
708 {
709 	return 0;
710 }
711 
712 static const struct file_operations envctrl_fops = {
713 	.owner =		THIS_MODULE,
714 	.read =			envctrl_read,
715 	.unlocked_ioctl =	envctrl_ioctl,
716 #ifdef CONFIG_COMPAT
717 	.compat_ioctl =		envctrl_ioctl,
718 #endif
719 	.open =			envctrl_open,
720 	.release =		envctrl_release,
721 };
722 
723 static struct miscdevice envctrl_dev = {
724 	ENVCTRL_MINOR,
725 	"envctrl",
726 	&envctrl_fops
727 };
728 
729 /* Function Description: Set monitor type based on firmware description.
730  * Return: None.
731  */
732 static void envctrl_set_mon(struct i2c_child_t *pchild,
733 			    const char *chnl_desc,
734 			    int chnl_no)
735 {
736 	/* Firmware only has temperature type.  It does not distinguish
737 	 * different kinds of temperatures.  We use channel description
738 	 * to disinguish them.
739 	 */
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;
746 
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;
752 
753 	if (!(strcmp(chnl_desc,"temp,motherboard")))
754 		pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
755 
756 	if (!(strcmp(chnl_desc,"temp,scsi")))
757 		pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
758 
759 	if (!(strcmp(chnl_desc,"temp,ethernet")))
760 		pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
761 }
762 
763 /* Function Description: Initialize monitor channel with channel desc,
764  *                       decoding tables, monitor type, optional properties.
765  * Return: None.
766  */
767 static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
768 {
769 	int i = 0, len;
770 	const char *pos;
771 	const unsigned int *pval;
772 
773 	/* Firmware describe channels into a stream separated by a '\0'. */
774 	pos = of_get_property(dp, "channels-description", &len);
775 
776 	while (len > 0) {
777 		int l = strlen(pos) + 1;
778 		envctrl_set_mon(pchild, pos, i++);
779 		len -= l;
780 		pos += l;
781 	}
782 
783 	/* Get optional properties. */
784 	pval = of_get_property(dp, "warning-temp", NULL);
785 	if (pval)
786 		warning_temperature = *pval;
787 
788 	pval = of_get_property(dp, "shutdown-temp", NULL);
789 	if (pval)
790 		shutdown_temperature = *pval;
791 }
792 
793 /* Function Description: Initialize child device monitoring fan status.
794  * Return: None.
795  */
796 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
797 {
798 	int i;
799 
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];
803 
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.
806 	 */
807 	pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
808 }
809 
810 /* Function Description: Initialize child device for global addressing line.
811  * Return: None.
812  */
813 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
814 {
815 	int i;
816 
817 	/* Voltage/PowerSupply monitoring is piggybacked
818 	 * with Global Address on CompactPCI.  See comments
819 	 * within envctrl_i2c_globaladdr for bit assignments.
820 	 *
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.
826 	 *
827 	 * NOTE: This loop could be replaced with a constant representing
828 	 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
829 	 */
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];
833 		}
834 	}
835 
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).
839 	 */
840 	pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
841 }
842 
843 /* Initialize child device monitoring voltage status. */
844 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
845 {
846 	int i;
847 
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];
851 
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.
854 	 */
855 	pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
856 }
857 
858 /* Function Description: Initialize i2c child device.
859  * Return: None.
860  */
861 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
862 				   struct i2c_child_t *pchild)
863 {
864 	int len, i, tbls_size = 0;
865 	struct device_node *dp = edev_child->prom_node;
866 	const void *pval;
867 
868 	/* Get device address. */
869 	pval = of_get_property(dp, "reg", &len);
870 	memcpy(&pchild->addr, pval, len);
871 
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;
880 			}
881 		}
882 
883                 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
884 		if (pchild->tables == NULL){
885 			printk("envctrl: Failed to allocate table.\n");
886 			return;
887 		}
888 		pval = of_get_property(dp, "tables", &len);
889                 if (!pval || len <= 0) {
890 			printk("envctrl: Failed to get table.\n");
891 			return;
892 		}
893 		memcpy(pchild->tables, pval, len);
894 	}
895 
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."
899 	 *
900 	 * We ignore the node on these platforms by assigning the
901 	 * 'NULL' monitor type.
902 	 */
903 	if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
904 		struct device_node *root_node;
905 		int len;
906 
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;
911 			}
912 			return;
913 		}
914 	}
915 
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);
920 
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);
925 			break;
926 
927 		case PCF8584_GLOBALADDR_TYPE:
928 			envctrl_init_globaladdr(pchild);
929 			i = pchild->total_chnls;
930 			break;
931 
932 		case PCF8584_FANSTAT_TYPE:
933 			envctrl_init_fanstat(pchild);
934 			i = pchild->total_chnls;
935 			break;
936 
937 		case PCF8584_VOLTAGE_TYPE:
938 			if (pchild->i2ctype == I2C_ADC) {
939 				envctrl_init_adc(pchild,dp);
940 			} else {
941 				envctrl_init_voltage_status(pchild);
942 			}
943 			i = pchild->total_chnls;
944 			break;
945 
946 		default:
947 			break;
948 		};
949 	}
950 }
951 
952 /* Function Description: Search the child device list for a device.
953  * Return : The i2c child if found. NULL otherwise.
954  */
955 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
956 {
957 	int i, j;
958 
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]));
963 			}
964 		}
965 	}
966 	return NULL;
967 }
968 
969 static void envctrl_do_shutdown(void)
970 {
971 	static int inprog = 0;
972 	int ret;
973 
974 	if (inprog != 0)
975 		return;
976 
977 	inprog = 1;
978 	printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
979 	ret = orderly_poweroff(true);
980 	if (ret < 0) {
981 		printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
982 		inprog = 0;  /* unlikely to succeed, but we could try again */
983 	}
984 }
985 
986 static struct task_struct *kenvctrld_task;
987 
988 static int kenvctrld(void *__unused)
989 {
990 	int poll_interval;
991 	int whichcpu;
992 	char tempbuf[10];
993 	struct i2c_child_t *cputemp;
994 
995 	if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
996 		printk(KERN_ERR
997 		       "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
998 		return -ENODEV;
999 	}
1000 
1001 	poll_interval = 5000; /* TODO env_mon_interval */
1002 
1003 	printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1004 	for (;;) {
1005 		msleep_interruptible(poll_interval);
1006 
1007 		if (kthread_should_stop())
1008 			break;
1009 
1010 		for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1011 			if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1012 						      ENVCTRL_CPUTEMP_MON,
1013 						      tempbuf)) {
1014 				if (tempbuf[0] >= shutdown_temperature) {
1015 					printk(KERN_CRIT
1016 						"%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1017 						"shutdown threshold %i C\n",
1018 						current->comm, whichcpu,
1019 						tempbuf[0], shutdown_temperature);
1020 					envctrl_do_shutdown();
1021 				}
1022 			}
1023 		}
1024 	}
1025 	printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1026 	return 0;
1027 }
1028 
1029 static int __init envctrl_init(void)
1030 {
1031 	struct linux_ebus *ebus = NULL;
1032 	struct linux_ebus_device *edev = NULL;
1033 	struct linux_ebus_child *edev_child = NULL;
1034 	int err, i = 0;
1035 
1036 	for_each_ebus(ebus) {
1037 		for_each_ebusdev(edev, ebus) {
1038 			if (!strcmp(edev->prom_node->name, "bbc")) {
1039 				/* If we find a boot-bus controller node,
1040 				 * then this envctrl driver is not for us.
1041 				 */
1042 				return -ENODEV;
1043 			}
1044 		}
1045 	}
1046 
1047 	/* Traverse through ebus and ebus device list for i2c device and
1048 	 * adc and gpio nodes.
1049 	 */
1050 	for_each_ebus(ebus) {
1051 		for_each_ebusdev(edev, ebus) {
1052 			if (!strcmp(edev->prom_node->name, "i2c")) {
1053 				i2c = ioremap(edev->resource[0].start, 0x2);
1054 				for_each_edevchild(edev, edev_child) {
1055 					if (!strcmp("gpio", edev_child->prom_node->name)) {
1056 						i2c_childlist[i].i2ctype = I2C_GPIO;
1057 						envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1058 					}
1059 					if (!strcmp("adc", edev_child->prom_node->name)) {
1060 						i2c_childlist[i].i2ctype = I2C_ADC;
1061 						envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1062 					}
1063 				}
1064 				goto done;
1065 			}
1066 		}
1067 	}
1068 
1069 done:
1070 	if (!edev) {
1071 		printk("envctrl: I2C device not found.\n");
1072 		return -ENODEV;
1073 	}
1074 
1075 	/* Set device address. */
1076 	writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1077 	writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1078 
1079 	/* Set system clock and SCL frequencies. */
1080 	writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1081 	writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1082 
1083 	/* Enable serial interface. */
1084 	writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1085 	udelay(200);
1086 
1087 	/* Register the device as a minor miscellaneous device. */
1088 	err = misc_register(&envctrl_dev);
1089 	if (err) {
1090 		printk("envctrl: Unable to get misc minor %d\n",
1091 		       envctrl_dev.minor);
1092 		goto out_iounmap;
1093 	}
1094 
1095 	/* Note above traversal routine post-incremented 'i' to accommodate
1096 	 * a next child device, so we decrement before reverse-traversal of
1097 	 * child devices.
1098 	 */
1099 	printk("envctrl: initialized ");
1100 	for (--i; i >= 0; --i) {
1101 		printk("[%s 0x%lx]%s",
1102 			(I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1103 			((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1104 			i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1105 	}
1106 
1107 	kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1108 	if (IS_ERR(kenvctrld_task)) {
1109 		err = PTR_ERR(kenvctrld_task);
1110 		goto out_deregister;
1111 	}
1112 
1113 	return 0;
1114 
1115 out_deregister:
1116 	misc_deregister(&envctrl_dev);
1117 out_iounmap:
1118 	iounmap(i2c);
1119 	for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1120 		kfree(i2c_childlist[i].tables);
1121 
1122 	return err;
1123 }
1124 
1125 static void __exit envctrl_cleanup(void)
1126 {
1127 	int i;
1128 
1129 	kthread_stop(kenvctrld_task);
1130 
1131 	iounmap(i2c);
1132 	misc_deregister(&envctrl_dev);
1133 
1134 	for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1135 		kfree(i2c_childlist[i].tables);
1136 }
1137 
1138 module_init(envctrl_init);
1139 module_exit(envctrl_cleanup);
1140 MODULE_LICENSE("GPL");
1141