envctrl.c 30 KB

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