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