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