envctrl.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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 long
  558. envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  559. {
  560. char __user *infobuf;
  561. switch (cmd) {
  562. case ENVCTRL_RD_WARNING_TEMPERATURE:
  563. case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
  564. case ENVCTRL_RD_MTHRBD_TEMPERATURE:
  565. case ENVCTRL_RD_FAN_STATUS:
  566. case ENVCTRL_RD_VOLTAGE_STATUS:
  567. case ENVCTRL_RD_ETHERNET_TEMPERATURE:
  568. case ENVCTRL_RD_SCSI_TEMPERATURE:
  569. case ENVCTRL_RD_GLOBALADDRESS:
  570. file->private_data = (void *)(long)cmd;
  571. break;
  572. case ENVCTRL_RD_CPU_TEMPERATURE:
  573. case ENVCTRL_RD_CPU_VOLTAGE:
  574. /* Check to see if application passes in any cpu number,
  575. * the default is cpu0.
  576. */
  577. infobuf = (char __user *) arg;
  578. if (infobuf == NULL) {
  579. read_cpu = 0;
  580. }else {
  581. get_user(read_cpu, infobuf);
  582. }
  583. /* Save the command for use when reading. */
  584. file->private_data = (void *)(long)cmd;
  585. break;
  586. default:
  587. return -EINVAL;
  588. };
  589. return 0;
  590. }
  591. /* Function Description: open device. Mapped to user open().
  592. * Return: Always 0.
  593. */
  594. static int
  595. envctrl_open(struct inode *inode, struct file *file)
  596. {
  597. file->private_data = NULL;
  598. return 0;
  599. }
  600. /* Function Description: Open device. Mapped to user close().
  601. * Return: Always 0.
  602. */
  603. static int
  604. envctrl_release(struct inode *inode, struct file *file)
  605. {
  606. return 0;
  607. }
  608. static struct file_operations envctrl_fops = {
  609. .owner = THIS_MODULE,
  610. .read = envctrl_read,
  611. .unlocked_ioctl = envctrl_ioctl,
  612. #ifdef CONFIG_COMPAT
  613. .compat_ioctl = envctrl_ioctl,
  614. #endif
  615. .open = envctrl_open,
  616. .release = envctrl_release,
  617. };
  618. static struct miscdevice envctrl_dev = {
  619. ENVCTRL_MINOR,
  620. "envctrl",
  621. &envctrl_fops
  622. };
  623. /* Function Description: Set monitor type based on firmware description.
  624. * Return: None.
  625. */
  626. static void envctrl_set_mon(struct i2c_child_t *pchild,
  627. char *chnl_desc,
  628. int chnl_no)
  629. {
  630. /* Firmware only has temperature type. It does not distinguish
  631. * different kinds of temperatures. We use channel description
  632. * to disinguish them.
  633. */
  634. if (!(strcmp(chnl_desc,"temp,cpu")) ||
  635. !(strcmp(chnl_desc,"temp,cpu0")) ||
  636. !(strcmp(chnl_desc,"temp,cpu1")) ||
  637. !(strcmp(chnl_desc,"temp,cpu2")) ||
  638. !(strcmp(chnl_desc,"temp,cpu3")))
  639. pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
  640. if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
  641. !(strcmp(chnl_desc,"vddcore,cpu1")) ||
  642. !(strcmp(chnl_desc,"vddcore,cpu2")) ||
  643. !(strcmp(chnl_desc,"vddcore,cpu3")))
  644. pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
  645. if (!(strcmp(chnl_desc,"temp,motherboard")))
  646. pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
  647. if (!(strcmp(chnl_desc,"temp,scsi")))
  648. pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
  649. if (!(strcmp(chnl_desc,"temp,ethernet")))
  650. pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
  651. }
  652. /* Function Description: Initialize monitor channel with channel desc,
  653. * decoding tables, monitor type, optional properties.
  654. * Return: None.
  655. */
  656. static void envctrl_init_adc(struct i2c_child_t *pchild, int node)
  657. {
  658. char chnls_desc[CHANNEL_DESC_SZ];
  659. int i = 0, len;
  660. char *pos = chnls_desc;
  661. /* Firmware describe channels into a stream separated by a '\0'. */
  662. len = prom_getproperty(node, "channels-description", chnls_desc,
  663. CHANNEL_DESC_SZ);
  664. chnls_desc[CHANNEL_DESC_SZ - 1] = '\0';
  665. while (len > 0) {
  666. int l = strlen(pos) + 1;
  667. envctrl_set_mon(pchild, pos, i++);
  668. len -= l;
  669. pos += l;
  670. }
  671. /* Get optional properties. */
  672. len = prom_getproperty(node, "warning-temp", (char *)&warning_temperature,
  673. sizeof(warning_temperature));
  674. len = prom_getproperty(node, "shutdown-temp", (char *)&shutdown_temperature,
  675. sizeof(shutdown_temperature));
  676. }
  677. /* Function Description: Initialize child device monitoring fan status.
  678. * Return: None.
  679. */
  680. static void envctrl_init_fanstat(struct i2c_child_t *pchild)
  681. {
  682. int i;
  683. /* Go through all channels and set up the mask. */
  684. for (i = 0; i < pchild->total_chnls; i++)
  685. pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
  686. /* We only need to know if this child has fan status monitored.
  687. * We don't care which channels since we have the mask already.
  688. */
  689. pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
  690. }
  691. /* Function Description: Initialize child device for global addressing line.
  692. * Return: None.
  693. */
  694. static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
  695. {
  696. int i;
  697. /* Voltage/PowerSupply monitoring is piggybacked
  698. * with Global Address on CompactPCI. See comments
  699. * within envctrl_i2c_globaladdr for bit assignments.
  700. *
  701. * The mask is created here by assigning mask bits to each
  702. * bit position that represents PCF8584_VOLTAGE_TYPE data.
  703. * Channel numbers are not consecutive within the globaladdr
  704. * node (why?), so we use the actual counter value as chnls_mask
  705. * index instead of the chnl_array[x].chnl_no value.
  706. *
  707. * NOTE: This loop could be replaced with a constant representing
  708. * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
  709. */
  710. for (i = 0; i < pchild->total_chnls; i++) {
  711. if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
  712. pchild->voltage_mask |= chnls_mask[i];
  713. }
  714. }
  715. /* We only need to know if this child has global addressing
  716. * line monitored. We don't care which channels since we know
  717. * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
  718. */
  719. pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
  720. }
  721. /* Initialize child device monitoring voltage status. */
  722. static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
  723. {
  724. int i;
  725. /* Go through all channels and set up the mask. */
  726. for (i = 0; i < pchild->total_chnls; i++)
  727. pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
  728. /* We only need to know if this child has voltage status monitored.
  729. * We don't care which channels since we have the mask already.
  730. */
  731. pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
  732. }
  733. /* Function Description: Initialize i2c child device.
  734. * Return: None.
  735. */
  736. static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
  737. struct i2c_child_t *pchild)
  738. {
  739. int node, len, i, tbls_size = 0;
  740. node = edev_child->prom_node;
  741. /* Get device address. */
  742. len = prom_getproperty(node, "reg",
  743. (char *) &(pchild->addr),
  744. sizeof(pchild->addr));
  745. /* Get tables property. Read firmware temperature tables. */
  746. len = prom_getproperty(node, "translation",
  747. (char *) pchild->tblprop_array,
  748. (PCF8584_MAX_CHANNELS *
  749. sizeof(struct pcf8584_tblprop)));
  750. if (len > 0) {
  751. pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
  752. for (i = 0; i < pchild->total_tbls; i++) {
  753. if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
  754. tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
  755. }
  756. }
  757. pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
  758. if (pchild->tables == NULL){
  759. printk("envctrl: Failed to allocate table.\n");
  760. return;
  761. }
  762. len = prom_getproperty(node, "tables",
  763. (char *) pchild->tables, tbls_size);
  764. if (len <= 0) {
  765. printk("envctrl: Failed to get table.\n");
  766. return;
  767. }
  768. }
  769. /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
  770. * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
  771. * "For Factory Use Only."
  772. *
  773. * We ignore the node on these platforms by assigning the
  774. * 'NULL' monitor type.
  775. */
  776. if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
  777. int len;
  778. char prop[56];
  779. len = prom_getproperty(prom_root_node, "name", prop, sizeof(prop));
  780. if (0 < len && (0 == strncmp(prop, "SUNW,UltraSPARC-IIi-cEngine", len)))
  781. {
  782. for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
  783. pchild->mon_type[len] = ENVCTRL_NOMON;
  784. }
  785. return;
  786. }
  787. }
  788. /* Get the monitor channels. */
  789. len = prom_getproperty(node, "channels-in-use",
  790. (char *) pchild->chnl_array,
  791. (PCF8584_MAX_CHANNELS *
  792. sizeof(struct pcf8584_channel)));
  793. pchild->total_chnls = len / sizeof(struct pcf8584_channel);
  794. for (i = 0; i < pchild->total_chnls; i++) {
  795. switch (pchild->chnl_array[i].type) {
  796. case PCF8584_TEMP_TYPE:
  797. envctrl_init_adc(pchild, node);
  798. break;
  799. case PCF8584_GLOBALADDR_TYPE:
  800. envctrl_init_globaladdr(pchild);
  801. i = pchild->total_chnls;
  802. break;
  803. case PCF8584_FANSTAT_TYPE:
  804. envctrl_init_fanstat(pchild);
  805. i = pchild->total_chnls;
  806. break;
  807. case PCF8584_VOLTAGE_TYPE:
  808. if (pchild->i2ctype == I2C_ADC) {
  809. envctrl_init_adc(pchild,node);
  810. } else {
  811. envctrl_init_voltage_status(pchild);
  812. }
  813. i = pchild->total_chnls;
  814. break;
  815. default:
  816. break;
  817. };
  818. }
  819. }
  820. /* Function Description: Search the child device list for a device.
  821. * Return : The i2c child if found. NULL otherwise.
  822. */
  823. static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
  824. {
  825. int i, j;
  826. for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
  827. for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
  828. if (i2c_childlist[i].mon_type[j] == mon_type) {
  829. return (struct i2c_child_t *)(&(i2c_childlist[i]));
  830. }
  831. }
  832. }
  833. return NULL;
  834. }
  835. static void envctrl_do_shutdown(void)
  836. {
  837. static int inprog = 0;
  838. static char *envp[] = {
  839. "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  840. char *argv[] = {
  841. "/sbin/shutdown", "-h", "now", NULL };
  842. if (inprog != 0)
  843. return;
  844. inprog = 1;
  845. printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
  846. if (0 > execve("/sbin/shutdown", argv, envp)) {
  847. printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
  848. inprog = 0; /* unlikely to succeed, but we could try again */
  849. }
  850. }
  851. static struct task_struct *kenvctrld_task;
  852. static int kenvctrld(void *__unused)
  853. {
  854. int poll_interval;
  855. int whichcpu;
  856. char tempbuf[10];
  857. struct i2c_child_t *cputemp;
  858. if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
  859. printk(KERN_ERR
  860. "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
  861. return -ENODEV;
  862. }
  863. poll_interval = 5000; /* TODO env_mon_interval */
  864. printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
  865. for (;;) {
  866. msleep_interruptible(poll_interval);
  867. if (kthread_should_stop())
  868. break;
  869. for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
  870. if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
  871. ENVCTRL_CPUTEMP_MON,
  872. tempbuf)) {
  873. if (tempbuf[0] >= shutdown_temperature) {
  874. printk(KERN_CRIT
  875. "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
  876. "shutdown threshold %i C\n",
  877. current->comm, whichcpu,
  878. tempbuf[0], shutdown_temperature);
  879. envctrl_do_shutdown();
  880. }
  881. }
  882. }
  883. }
  884. printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
  885. return 0;
  886. }
  887. static int __init envctrl_init(void)
  888. {
  889. struct linux_ebus *ebus = NULL;
  890. struct linux_ebus_device *edev = NULL;
  891. struct linux_ebus_child *edev_child = NULL;
  892. int err, i = 0;
  893. for_each_ebus(ebus) {
  894. for_each_ebusdev(edev, ebus) {
  895. if (!strcmp(edev->prom_name, "bbc")) {
  896. /* If we find a boot-bus controller node,
  897. * then this envctrl driver is not for us.
  898. */
  899. return -ENODEV;
  900. }
  901. }
  902. }
  903. /* Traverse through ebus and ebus device list for i2c device and
  904. * adc and gpio nodes.
  905. */
  906. for_each_ebus(ebus) {
  907. for_each_ebusdev(edev, ebus) {
  908. if (!strcmp(edev->prom_name, "i2c")) {
  909. i2c = ioremap(edev->resource[0].start, 0x2);
  910. for_each_edevchild(edev, edev_child) {
  911. if (!strcmp("gpio", edev_child->prom_name)) {
  912. i2c_childlist[i].i2ctype = I2C_GPIO;
  913. envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
  914. }
  915. if (!strcmp("adc", edev_child->prom_name)) {
  916. i2c_childlist[i].i2ctype = I2C_ADC;
  917. envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
  918. }
  919. }
  920. goto done;
  921. }
  922. }
  923. }
  924. done:
  925. if (!edev) {
  926. printk("envctrl: I2C device not found.\n");
  927. return -ENODEV;
  928. }
  929. /* Set device address. */
  930. writeb(CONTROL_PIN, i2c + PCF8584_CSR);
  931. writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
  932. /* Set system clock and SCL frequencies. */
  933. writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
  934. writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
  935. /* Enable serial interface. */
  936. writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
  937. udelay(200);
  938. /* Register the device as a minor miscellaneous device. */
  939. err = misc_register(&envctrl_dev);
  940. if (err) {
  941. printk("envctrl: Unable to get misc minor %d\n",
  942. envctrl_dev.minor);
  943. goto out_iounmap;
  944. }
  945. /* Note above traversal routine post-incremented 'i' to accommodate
  946. * a next child device, so we decrement before reverse-traversal of
  947. * child devices.
  948. */
  949. printk("envctrl: initialized ");
  950. for (--i; i >= 0; --i) {
  951. printk("[%s 0x%lx]%s",
  952. (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
  953. ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
  954. i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
  955. }
  956. kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
  957. if (IS_ERR(kenvctrld_task)) {
  958. err = PTR_ERR(kenvctrld_task);
  959. goto out_deregister;
  960. }
  961. return 0;
  962. out_deregister:
  963. misc_deregister(&envctrl_dev);
  964. out_iounmap:
  965. iounmap(i2c);
  966. for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
  967. kfree(i2c_childlist[i].tables);
  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. kfree(i2c_childlist[i].tables);
  978. }
  979. module_init(envctrl_init);
  980. module_exit(envctrl_cleanup);
  981. MODULE_LICENSE("GPL");