envctrl.c 30 KB

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