envctrl.c 30 KB

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