envctrl.c 31 KB

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