envctrl.c 30 KB

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