envctrl.c 30 KB

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