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