envctrl.c 30 KB

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