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