envctrl.c 31 KB

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