envctrl.c 30 KB

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