therm_pm72.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /*
  2. * Device driver for the thermostats & fan controller of the
  3. * Apple G5 "PowerMac7,2" desktop machines.
  4. *
  5. * (c) Copyright IBM Corp. 2003-2004
  6. *
  7. * Maintained by: Benjamin Herrenschmidt
  8. * <benh@kernel.crashing.org>
  9. *
  10. *
  11. * The algorithm used is the PID control algorithm, used the same
  12. * way the published Darwin code does, using the same values that
  13. * are present in the Darwin 7.0 snapshot property lists.
  14. *
  15. * As far as the CPUs control loops are concerned, I use the
  16. * calibration & PID constants provided by the EEPROM,
  17. * I do _not_ embed any value from the property lists, as the ones
  18. * provided by Darwin 7.0 seem to always have an older version that
  19. * what I've seen on the actual computers.
  20. * It would be interesting to verify that though. Darwin has a
  21. * version code of 1.0.0d11 for all control loops it seems, while
  22. * so far, the machines EEPROMs contain a dataset versioned 1.0.0f
  23. *
  24. * Darwin doesn't provide source to all parts, some missing
  25. * bits like the AppleFCU driver or the actual scale of some
  26. * of the values returned by sensors had to be "guessed" some
  27. * way... or based on what Open Firmware does.
  28. *
  29. * I didn't yet figure out how to get the slots power consumption
  30. * out of the FCU, so that part has not been implemented yet and
  31. * the slots fan is set to a fixed 50% PWM, hoping this value is
  32. * safe enough ...
  33. *
  34. * Note: I have observed strange oscillations of the CPU control
  35. * loop on a dual G5 here. When idle, the CPU exhaust fan tend to
  36. * oscillates slowly (over several minutes) between the minimum
  37. * of 300RPMs and approx. 1000 RPMs. I don't know what is causing
  38. * this, it could be some incorrect constant or an error in the
  39. * way I ported the algorithm, or it could be just normal. I
  40. * don't have full understanding on the way Apple tweaked the PID
  41. * algorithm for the CPU control, it is definitely not a standard
  42. * implementation...
  43. *
  44. * TODO: - Check MPU structure version/signature
  45. * - Add things like /sbin/overtemp for non-critical
  46. * overtemp conditions so userland can take some policy
  47. * decisions, like slewing down CPUs
  48. * - Deal with fan and i2c failures in a better way
  49. * - Maybe do a generic PID based on params used for
  50. * U3 and Drives ? Definitely need to factor code a bit
  51. * bettter... also make sensor detection more robust using
  52. * the device-tree to probe for them
  53. * - Figure out how to get the slots consumption and set the
  54. * slots fan accordingly
  55. *
  56. * History:
  57. *
  58. * Nov. 13, 2003 : 0.5
  59. * - First release
  60. *
  61. * Nov. 14, 2003 : 0.6
  62. * - Read fan speed from FCU, low level fan routines now deal
  63. * with errors & check fan status, though higher level don't
  64. * do much.
  65. * - Move a bunch of definitions to .h file
  66. *
  67. * Nov. 18, 2003 : 0.7
  68. * - Fix build on ppc64 kernel
  69. * - Move back statics definitions to .c file
  70. * - Avoid calling schedule_timeout with a negative number
  71. *
  72. * Dec. 18, 2003 : 0.8
  73. * - Fix typo when reading back fan speed on 2 CPU machines
  74. *
  75. * Mar. 11, 2004 : 0.9
  76. * - Rework code accessing the ADC chips, make it more robust and
  77. * closer to the chip spec. Also make sure it is configured properly,
  78. * I've seen yet unexplained cases where on startup, I would have stale
  79. * values in the configuration register
  80. * - Switch back to use of target fan speed for PID, thus lowering
  81. * pressure on i2c
  82. *
  83. * Oct. 20, 2004 : 1.1
  84. * - Add device-tree lookup for fan IDs, should detect liquid cooling
  85. * pumps when present
  86. * - Enable driver for PowerMac7,3 machines
  87. * - Split the U3/Backside cooling on U3 & U3H versions as Darwin does
  88. * - Add new CPU cooling algorithm for machines with liquid cooling
  89. * - Workaround for some PowerMac7,3 with empty "fan" node in the devtree
  90. * - Fix a signed/unsigned compare issue in some PID loops
  91. *
  92. * Mar. 10, 2005 : 1.2
  93. * - Add basic support for Xserve G5
  94. * - Retreive pumps min/max from EEPROM image in device-tree (broken)
  95. * - Use min/max macros here or there
  96. * - Latest darwin updated U3H min fan speed to 20% PWM
  97. *
  98. */
  99. #include <linux/config.h>
  100. #include <linux/types.h>
  101. #include <linux/module.h>
  102. #include <linux/errno.h>
  103. #include <linux/kernel.h>
  104. #include <linux/delay.h>
  105. #include <linux/sched.h>
  106. #include <linux/i2c.h>
  107. #include <linux/slab.h>
  108. #include <linux/init.h>
  109. #include <linux/spinlock.h>
  110. #include <linux/smp_lock.h>
  111. #include <linux/wait.h>
  112. #include <linux/reboot.h>
  113. #include <linux/kmod.h>
  114. #include <linux/i2c.h>
  115. #include <linux/i2c-dev.h>
  116. #include <asm/prom.h>
  117. #include <asm/machdep.h>
  118. #include <asm/io.h>
  119. #include <asm/system.h>
  120. #include <asm/sections.h>
  121. #include <asm/of_device.h>
  122. #include <asm/macio.h>
  123. #include "therm_pm72.h"
  124. #define VERSION "1.2b2"
  125. #undef DEBUG
  126. #ifdef DEBUG
  127. #define DBG(args...) printk(args)
  128. #else
  129. #define DBG(args...) do { } while(0)
  130. #endif
  131. /*
  132. * Driver statics
  133. */
  134. static struct of_device * of_dev;
  135. static struct i2c_adapter * u3_0;
  136. static struct i2c_adapter * u3_1;
  137. static struct i2c_adapter * k2;
  138. static struct i2c_client * fcu;
  139. static struct cpu_pid_state cpu_state[2];
  140. static struct basckside_pid_params backside_params;
  141. static struct backside_pid_state backside_state;
  142. static struct drives_pid_state drives_state;
  143. static struct dimm_pid_state dimms_state;
  144. static int state;
  145. static int cpu_count;
  146. static int cpu_pid_type;
  147. static pid_t ctrl_task;
  148. static struct completion ctrl_complete;
  149. static int critical_state;
  150. static int rackmac;
  151. static s32 dimm_output_clamp;
  152. static DECLARE_MUTEX(driver_lock);
  153. /*
  154. * We have 3 types of CPU PID control. One is "split" old style control
  155. * for intake & exhaust fans, the other is "combined" control for both
  156. * CPUs that also deals with the pumps when present. To be "compatible"
  157. * with OS X at this point, we only use "COMBINED" on the machines that
  158. * are identified as having the pumps (though that identification is at
  159. * least dodgy). Ultimately, we could probably switch completely to this
  160. * algorithm provided we hack it to deal with the UP case
  161. */
  162. #define CPU_PID_TYPE_SPLIT 0
  163. #define CPU_PID_TYPE_COMBINED 1
  164. #define CPU_PID_TYPE_RACKMAC 2
  165. /*
  166. * This table describes all fans in the FCU. The "id" and "type" values
  167. * are defaults valid for all earlier machines. Newer machines will
  168. * eventually override the table content based on the device-tree
  169. */
  170. struct fcu_fan_table
  171. {
  172. char* loc; /* location code */
  173. int type; /* 0 = rpm, 1 = pwm, 2 = pump */
  174. int id; /* id or -1 */
  175. };
  176. #define FCU_FAN_RPM 0
  177. #define FCU_FAN_PWM 1
  178. #define FCU_FAN_ABSENT_ID -1
  179. #define FCU_FAN_COUNT ARRAY_SIZE(fcu_fans)
  180. struct fcu_fan_table fcu_fans[] = {
  181. [BACKSIDE_FAN_PWM_INDEX] = {
  182. .loc = "BACKSIDE,SYS CTRLR FAN",
  183. .type = FCU_FAN_PWM,
  184. .id = BACKSIDE_FAN_PWM_DEFAULT_ID,
  185. },
  186. [DRIVES_FAN_RPM_INDEX] = {
  187. .loc = "DRIVE BAY",
  188. .type = FCU_FAN_RPM,
  189. .id = DRIVES_FAN_RPM_DEFAULT_ID,
  190. },
  191. [SLOTS_FAN_PWM_INDEX] = {
  192. .loc = "SLOT,PCI FAN",
  193. .type = FCU_FAN_PWM,
  194. .id = SLOTS_FAN_PWM_DEFAULT_ID,
  195. },
  196. [CPUA_INTAKE_FAN_RPM_INDEX] = {
  197. .loc = "CPU A INTAKE",
  198. .type = FCU_FAN_RPM,
  199. .id = CPUA_INTAKE_FAN_RPM_DEFAULT_ID,
  200. },
  201. [CPUA_EXHAUST_FAN_RPM_INDEX] = {
  202. .loc = "CPU A EXHAUST",
  203. .type = FCU_FAN_RPM,
  204. .id = CPUA_EXHAUST_FAN_RPM_DEFAULT_ID,
  205. },
  206. [CPUB_INTAKE_FAN_RPM_INDEX] = {
  207. .loc = "CPU B INTAKE",
  208. .type = FCU_FAN_RPM,
  209. .id = CPUB_INTAKE_FAN_RPM_DEFAULT_ID,
  210. },
  211. [CPUB_EXHAUST_FAN_RPM_INDEX] = {
  212. .loc = "CPU B EXHAUST",
  213. .type = FCU_FAN_RPM,
  214. .id = CPUB_EXHAUST_FAN_RPM_DEFAULT_ID,
  215. },
  216. /* pumps aren't present by default, have to be looked up in the
  217. * device-tree
  218. */
  219. [CPUA_PUMP_RPM_INDEX] = {
  220. .loc = "CPU A PUMP",
  221. .type = FCU_FAN_RPM,
  222. .id = FCU_FAN_ABSENT_ID,
  223. },
  224. [CPUB_PUMP_RPM_INDEX] = {
  225. .loc = "CPU B PUMP",
  226. .type = FCU_FAN_RPM,
  227. .id = FCU_FAN_ABSENT_ID,
  228. },
  229. /* Xserve fans */
  230. [CPU_A1_FAN_RPM_INDEX] = {
  231. .loc = "CPU A 1",
  232. .type = FCU_FAN_RPM,
  233. .id = FCU_FAN_ABSENT_ID,
  234. },
  235. [CPU_A2_FAN_RPM_INDEX] = {
  236. .loc = "CPU A 2",
  237. .type = FCU_FAN_RPM,
  238. .id = FCU_FAN_ABSENT_ID,
  239. },
  240. [CPU_A3_FAN_RPM_INDEX] = {
  241. .loc = "CPU A 3",
  242. .type = FCU_FAN_RPM,
  243. .id = FCU_FAN_ABSENT_ID,
  244. },
  245. [CPU_B1_FAN_RPM_INDEX] = {
  246. .loc = "CPU B 1",
  247. .type = FCU_FAN_RPM,
  248. .id = FCU_FAN_ABSENT_ID,
  249. },
  250. [CPU_B2_FAN_RPM_INDEX] = {
  251. .loc = "CPU B 2",
  252. .type = FCU_FAN_RPM,
  253. .id = FCU_FAN_ABSENT_ID,
  254. },
  255. [CPU_B3_FAN_RPM_INDEX] = {
  256. .loc = "CPU B 3",
  257. .type = FCU_FAN_RPM,
  258. .id = FCU_FAN_ABSENT_ID,
  259. },
  260. };
  261. /*
  262. * i2c_driver structure to attach to the host i2c controller
  263. */
  264. static int therm_pm72_attach(struct i2c_adapter *adapter);
  265. static int therm_pm72_detach(struct i2c_adapter *adapter);
  266. static struct i2c_driver therm_pm72_driver =
  267. {
  268. .owner = THIS_MODULE,
  269. .name = "therm_pm72",
  270. .flags = I2C_DF_NOTIFY,
  271. .attach_adapter = therm_pm72_attach,
  272. .detach_adapter = therm_pm72_detach,
  273. };
  274. /*
  275. * Utility function to create an i2c_client structure and
  276. * attach it to one of u3 adapters
  277. */
  278. static struct i2c_client *attach_i2c_chip(int id, const char *name)
  279. {
  280. struct i2c_client *clt;
  281. struct i2c_adapter *adap;
  282. if (id & 0x200)
  283. adap = k2;
  284. else if (id & 0x100)
  285. adap = u3_1;
  286. else
  287. adap = u3_0;
  288. if (adap == NULL)
  289. return NULL;
  290. clt = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  291. if (clt == NULL)
  292. return NULL;
  293. memset(clt, 0, sizeof(struct i2c_client));
  294. clt->addr = (id >> 1) & 0x7f;
  295. clt->adapter = adap;
  296. clt->driver = &therm_pm72_driver;
  297. strncpy(clt->name, name, I2C_NAME_SIZE-1);
  298. if (i2c_attach_client(clt)) {
  299. printk(KERN_ERR "therm_pm72: Failed to attach to i2c ID 0x%x\n", id);
  300. kfree(clt);
  301. return NULL;
  302. }
  303. return clt;
  304. }
  305. /*
  306. * Utility function to get rid of the i2c_client structure
  307. * (will also detach from the adapter hopepfully)
  308. */
  309. static void detach_i2c_chip(struct i2c_client *clt)
  310. {
  311. i2c_detach_client(clt);
  312. kfree(clt);
  313. }
  314. /*
  315. * Here are the i2c chip access wrappers
  316. */
  317. static void initialize_adc(struct cpu_pid_state *state)
  318. {
  319. int rc;
  320. u8 buf[2];
  321. /* Read ADC the configuration register and cache it. We
  322. * also make sure Config2 contains proper values, I've seen
  323. * cases where we got stale grabage in there, thus preventing
  324. * proper reading of conv. values
  325. */
  326. /* Clear Config2 */
  327. buf[0] = 5;
  328. buf[1] = 0;
  329. i2c_master_send(state->monitor, buf, 2);
  330. /* Read & cache Config1 */
  331. buf[0] = 1;
  332. rc = i2c_master_send(state->monitor, buf, 1);
  333. if (rc > 0) {
  334. rc = i2c_master_recv(state->monitor, buf, 1);
  335. if (rc > 0) {
  336. state->adc_config = buf[0];
  337. DBG("ADC config reg: %02x\n", state->adc_config);
  338. /* Disable shutdown mode */
  339. state->adc_config &= 0xfe;
  340. buf[0] = 1;
  341. buf[1] = state->adc_config;
  342. rc = i2c_master_send(state->monitor, buf, 2);
  343. }
  344. }
  345. if (rc <= 0)
  346. printk(KERN_ERR "therm_pm72: Error reading ADC config"
  347. " register !\n");
  348. }
  349. static int read_smon_adc(struct cpu_pid_state *state, int chan)
  350. {
  351. int rc, data, tries = 0;
  352. u8 buf[2];
  353. for (;;) {
  354. /* Set channel */
  355. buf[0] = 1;
  356. buf[1] = (state->adc_config & 0x1f) | (chan << 5);
  357. rc = i2c_master_send(state->monitor, buf, 2);
  358. if (rc <= 0)
  359. goto error;
  360. /* Wait for convertion */
  361. msleep(1);
  362. /* Switch to data register */
  363. buf[0] = 4;
  364. rc = i2c_master_send(state->monitor, buf, 1);
  365. if (rc <= 0)
  366. goto error;
  367. /* Read result */
  368. rc = i2c_master_recv(state->monitor, buf, 2);
  369. if (rc < 0)
  370. goto error;
  371. data = ((u16)buf[0]) << 8 | (u16)buf[1];
  372. return data >> 6;
  373. error:
  374. DBG("Error reading ADC, retrying...\n");
  375. if (++tries > 10) {
  376. printk(KERN_ERR "therm_pm72: Error reading ADC !\n");
  377. return -1;
  378. }
  379. msleep(10);
  380. }
  381. }
  382. static int read_lm87_reg(struct i2c_client * chip, int reg)
  383. {
  384. int rc, tries = 0;
  385. u8 buf;
  386. for (;;) {
  387. /* Set address */
  388. buf = (u8)reg;
  389. rc = i2c_master_send(chip, &buf, 1);
  390. if (rc <= 0)
  391. goto error;
  392. rc = i2c_master_recv(chip, &buf, 1);
  393. if (rc <= 0)
  394. goto error;
  395. return (int)buf;
  396. error:
  397. DBG("Error reading LM87, retrying...\n");
  398. if (++tries > 10) {
  399. printk(KERN_ERR "therm_pm72: Error reading LM87 !\n");
  400. return -1;
  401. }
  402. msleep(10);
  403. }
  404. }
  405. static int fan_read_reg(int reg, unsigned char *buf, int nb)
  406. {
  407. int tries, nr, nw;
  408. buf[0] = reg;
  409. tries = 0;
  410. for (;;) {
  411. nw = i2c_master_send(fcu, buf, 1);
  412. if (nw > 0 || (nw < 0 && nw != -EIO) || tries >= 100)
  413. break;
  414. msleep(10);
  415. ++tries;
  416. }
  417. if (nw <= 0) {
  418. printk(KERN_ERR "Failure writing address to FCU: %d", nw);
  419. return -EIO;
  420. }
  421. tries = 0;
  422. for (;;) {
  423. nr = i2c_master_recv(fcu, buf, nb);
  424. if (nr > 0 || (nr < 0 && nr != ENODEV) || tries >= 100)
  425. break;
  426. msleep(10);
  427. ++tries;
  428. }
  429. if (nr <= 0)
  430. printk(KERN_ERR "Failure reading data from FCU: %d", nw);
  431. return nr;
  432. }
  433. static int fan_write_reg(int reg, const unsigned char *ptr, int nb)
  434. {
  435. int tries, nw;
  436. unsigned char buf[16];
  437. buf[0] = reg;
  438. memcpy(buf+1, ptr, nb);
  439. ++nb;
  440. tries = 0;
  441. for (;;) {
  442. nw = i2c_master_send(fcu, buf, nb);
  443. if (nw > 0 || (nw < 0 && nw != EIO) || tries >= 100)
  444. break;
  445. msleep(10);
  446. ++tries;
  447. }
  448. if (nw < 0)
  449. printk(KERN_ERR "Failure writing to FCU: %d", nw);
  450. return nw;
  451. }
  452. static int start_fcu(void)
  453. {
  454. unsigned char buf = 0xff;
  455. int rc;
  456. rc = fan_write_reg(0xe, &buf, 1);
  457. if (rc < 0)
  458. return -EIO;
  459. rc = fan_write_reg(0x2e, &buf, 1);
  460. if (rc < 0)
  461. return -EIO;
  462. return 0;
  463. }
  464. static int set_rpm_fan(int fan_index, int rpm)
  465. {
  466. unsigned char buf[2];
  467. int rc, id;
  468. if (fcu_fans[fan_index].type != FCU_FAN_RPM)
  469. return -EINVAL;
  470. id = fcu_fans[fan_index].id;
  471. if (id == FCU_FAN_ABSENT_ID)
  472. return -EINVAL;
  473. if (rpm < 300)
  474. rpm = 300;
  475. else if (rpm > 8191)
  476. rpm = 8191;
  477. buf[0] = rpm >> 5;
  478. buf[1] = rpm << 3;
  479. rc = fan_write_reg(0x10 + (id * 2), buf, 2);
  480. if (rc < 0)
  481. return -EIO;
  482. return 0;
  483. }
  484. static int get_rpm_fan(int fan_index, int programmed)
  485. {
  486. unsigned char failure;
  487. unsigned char active;
  488. unsigned char buf[2];
  489. int rc, id, reg_base;
  490. if (fcu_fans[fan_index].type != FCU_FAN_RPM)
  491. return -EINVAL;
  492. id = fcu_fans[fan_index].id;
  493. if (id == FCU_FAN_ABSENT_ID)
  494. return -EINVAL;
  495. rc = fan_read_reg(0xb, &failure, 1);
  496. if (rc != 1)
  497. return -EIO;
  498. if ((failure & (1 << id)) != 0)
  499. return -EFAULT;
  500. rc = fan_read_reg(0xd, &active, 1);
  501. if (rc != 1)
  502. return -EIO;
  503. if ((active & (1 << id)) == 0)
  504. return -ENXIO;
  505. /* Programmed value or real current speed */
  506. reg_base = programmed ? 0x10 : 0x11;
  507. rc = fan_read_reg(reg_base + (id * 2), buf, 2);
  508. if (rc != 2)
  509. return -EIO;
  510. return (buf[0] << 5) | buf[1] >> 3;
  511. }
  512. static int set_pwm_fan(int fan_index, int pwm)
  513. {
  514. unsigned char buf[2];
  515. int rc, id;
  516. if (fcu_fans[fan_index].type != FCU_FAN_PWM)
  517. return -EINVAL;
  518. id = fcu_fans[fan_index].id;
  519. if (id == FCU_FAN_ABSENT_ID)
  520. return -EINVAL;
  521. if (pwm < 10)
  522. pwm = 10;
  523. else if (pwm > 100)
  524. pwm = 100;
  525. pwm = (pwm * 2559) / 1000;
  526. buf[0] = pwm;
  527. rc = fan_write_reg(0x30 + (id * 2), buf, 1);
  528. if (rc < 0)
  529. return rc;
  530. return 0;
  531. }
  532. static int get_pwm_fan(int fan_index)
  533. {
  534. unsigned char failure;
  535. unsigned char active;
  536. unsigned char buf[2];
  537. int rc, id;
  538. if (fcu_fans[fan_index].type != FCU_FAN_PWM)
  539. return -EINVAL;
  540. id = fcu_fans[fan_index].id;
  541. if (id == FCU_FAN_ABSENT_ID)
  542. return -EINVAL;
  543. rc = fan_read_reg(0x2b, &failure, 1);
  544. if (rc != 1)
  545. return -EIO;
  546. if ((failure & (1 << id)) != 0)
  547. return -EFAULT;
  548. rc = fan_read_reg(0x2d, &active, 1);
  549. if (rc != 1)
  550. return -EIO;
  551. if ((active & (1 << id)) == 0)
  552. return -ENXIO;
  553. /* Programmed value or real current speed */
  554. rc = fan_read_reg(0x30 + (id * 2), buf, 1);
  555. if (rc != 1)
  556. return -EIO;
  557. return (buf[0] * 1000) / 2559;
  558. }
  559. /*
  560. * Utility routine to read the CPU calibration EEPROM data
  561. * from the device-tree
  562. */
  563. static int read_eeprom(int cpu, struct mpu_data *out)
  564. {
  565. struct device_node *np;
  566. char nodename[64];
  567. u8 *data;
  568. int len;
  569. /* prom.c routine for finding a node by path is a bit brain dead
  570. * and requires exact @xxx unit numbers. This is a bit ugly but
  571. * will work for these machines
  572. */
  573. sprintf(nodename, "/u3@0,f8000000/i2c@f8001000/cpuid@a%d", cpu ? 2 : 0);
  574. np = of_find_node_by_path(nodename);
  575. if (np == NULL) {
  576. printk(KERN_ERR "therm_pm72: Failed to retreive cpuid node from device-tree\n");
  577. return -ENODEV;
  578. }
  579. data = (u8 *)get_property(np, "cpuid", &len);
  580. if (data == NULL) {
  581. printk(KERN_ERR "therm_pm72: Failed to retreive cpuid property from device-tree\n");
  582. of_node_put(np);
  583. return -ENODEV;
  584. }
  585. memcpy(out, data, sizeof(struct mpu_data));
  586. of_node_put(np);
  587. return 0;
  588. }
  589. static void fetch_cpu_pumps_minmax(void)
  590. {
  591. struct cpu_pid_state *state0 = &cpu_state[0];
  592. struct cpu_pid_state *state1 = &cpu_state[1];
  593. u16 pump_min = 0, pump_max = 0xffff;
  594. u16 tmp[4];
  595. /* Try to fetch pumps min/max infos from eeprom */
  596. memcpy(&tmp, &state0->mpu.processor_part_num, 8);
  597. if (tmp[0] != 0xffff && tmp[1] != 0xffff) {
  598. pump_min = max(pump_min, tmp[0]);
  599. pump_max = min(pump_max, tmp[1]);
  600. }
  601. if (tmp[2] != 0xffff && tmp[3] != 0xffff) {
  602. pump_min = max(pump_min, tmp[2]);
  603. pump_max = min(pump_max, tmp[3]);
  604. }
  605. /* Double check the values, this _IS_ needed as the EEPROM on
  606. * some dual 2.5Ghz G5s seem, at least, to have both min & max
  607. * same to the same value ... (grrrr)
  608. */
  609. if (pump_min == pump_max || pump_min == 0 || pump_max == 0xffff) {
  610. pump_min = CPU_PUMP_OUTPUT_MIN;
  611. pump_max = CPU_PUMP_OUTPUT_MAX;
  612. }
  613. state0->pump_min = state1->pump_min = pump_min;
  614. state0->pump_max = state1->pump_max = pump_max;
  615. }
  616. /*
  617. * Now, unfortunately, sysfs doesn't give us a nice void * we could
  618. * pass around to the attribute functions, so we don't really have
  619. * choice but implement a bunch of them...
  620. *
  621. * That sucks a bit, we take the lock because FIX32TOPRINT evaluates
  622. * the input twice... I accept patches :)
  623. */
  624. #define BUILD_SHOW_FUNC_FIX(name, data) \
  625. static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
  626. { \
  627. ssize_t r; \
  628. down(&driver_lock); \
  629. r = sprintf(buf, "%d.%03d", FIX32TOPRINT(data)); \
  630. up(&driver_lock); \
  631. return r; \
  632. }
  633. #define BUILD_SHOW_FUNC_INT(name, data) \
  634. static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
  635. { \
  636. return sprintf(buf, "%d", data); \
  637. }
  638. BUILD_SHOW_FUNC_FIX(cpu0_temperature, cpu_state[0].last_temp)
  639. BUILD_SHOW_FUNC_FIX(cpu0_voltage, cpu_state[0].voltage)
  640. BUILD_SHOW_FUNC_FIX(cpu0_current, cpu_state[0].current_a)
  641. BUILD_SHOW_FUNC_INT(cpu0_exhaust_fan_rpm, cpu_state[0].rpm)
  642. BUILD_SHOW_FUNC_INT(cpu0_intake_fan_rpm, cpu_state[0].intake_rpm)
  643. BUILD_SHOW_FUNC_FIX(cpu1_temperature, cpu_state[1].last_temp)
  644. BUILD_SHOW_FUNC_FIX(cpu1_voltage, cpu_state[1].voltage)
  645. BUILD_SHOW_FUNC_FIX(cpu1_current, cpu_state[1].current_a)
  646. BUILD_SHOW_FUNC_INT(cpu1_exhaust_fan_rpm, cpu_state[1].rpm)
  647. BUILD_SHOW_FUNC_INT(cpu1_intake_fan_rpm, cpu_state[1].intake_rpm)
  648. BUILD_SHOW_FUNC_FIX(backside_temperature, backside_state.last_temp)
  649. BUILD_SHOW_FUNC_INT(backside_fan_pwm, backside_state.pwm)
  650. BUILD_SHOW_FUNC_FIX(drives_temperature, drives_state.last_temp)
  651. BUILD_SHOW_FUNC_INT(drives_fan_rpm, drives_state.rpm)
  652. BUILD_SHOW_FUNC_FIX(dimms_temperature, dimms_state.last_temp)
  653. static DEVICE_ATTR(cpu0_temperature,S_IRUGO,show_cpu0_temperature,NULL);
  654. static DEVICE_ATTR(cpu0_voltage,S_IRUGO,show_cpu0_voltage,NULL);
  655. static DEVICE_ATTR(cpu0_current,S_IRUGO,show_cpu0_current,NULL);
  656. static DEVICE_ATTR(cpu0_exhaust_fan_rpm,S_IRUGO,show_cpu0_exhaust_fan_rpm,NULL);
  657. static DEVICE_ATTR(cpu0_intake_fan_rpm,S_IRUGO,show_cpu0_intake_fan_rpm,NULL);
  658. static DEVICE_ATTR(cpu1_temperature,S_IRUGO,show_cpu1_temperature,NULL);
  659. static DEVICE_ATTR(cpu1_voltage,S_IRUGO,show_cpu1_voltage,NULL);
  660. static DEVICE_ATTR(cpu1_current,S_IRUGO,show_cpu1_current,NULL);
  661. static DEVICE_ATTR(cpu1_exhaust_fan_rpm,S_IRUGO,show_cpu1_exhaust_fan_rpm,NULL);
  662. static DEVICE_ATTR(cpu1_intake_fan_rpm,S_IRUGO,show_cpu1_intake_fan_rpm,NULL);
  663. static DEVICE_ATTR(backside_temperature,S_IRUGO,show_backside_temperature,NULL);
  664. static DEVICE_ATTR(backside_fan_pwm,S_IRUGO,show_backside_fan_pwm,NULL);
  665. static DEVICE_ATTR(drives_temperature,S_IRUGO,show_drives_temperature,NULL);
  666. static DEVICE_ATTR(drives_fan_rpm,S_IRUGO,show_drives_fan_rpm,NULL);
  667. static DEVICE_ATTR(dimms_temperature,S_IRUGO,show_dimms_temperature,NULL);
  668. /*
  669. * CPUs fans control loop
  670. */
  671. static int do_read_one_cpu_values(struct cpu_pid_state *state, s32 *temp, s32 *power)
  672. {
  673. s32 ltemp, volts, amps;
  674. int index, rc = 0;
  675. /* Default (in case of error) */
  676. *temp = state->cur_temp;
  677. *power = state->cur_power;
  678. if (cpu_pid_type == CPU_PID_TYPE_RACKMAC)
  679. index = (state->index == 0) ?
  680. CPU_A1_FAN_RPM_INDEX : CPU_B1_FAN_RPM_INDEX;
  681. else
  682. index = (state->index == 0) ?
  683. CPUA_EXHAUST_FAN_RPM_INDEX : CPUB_EXHAUST_FAN_RPM_INDEX;
  684. /* Read current fan status */
  685. rc = get_rpm_fan(index, !RPM_PID_USE_ACTUAL_SPEED);
  686. if (rc < 0) {
  687. /* XXX What do we do now ? Nothing for now, keep old value, but
  688. * return error upstream
  689. */
  690. DBG(" cpu %d, fan reading error !\n", state->index);
  691. } else {
  692. state->rpm = rc;
  693. DBG(" cpu %d, exhaust RPM: %d\n", state->index, state->rpm);
  694. }
  695. /* Get some sensor readings and scale it */
  696. ltemp = read_smon_adc(state, 1);
  697. if (ltemp == -1) {
  698. /* XXX What do we do now ? */
  699. state->overtemp++;
  700. if (rc == 0)
  701. rc = -EIO;
  702. DBG(" cpu %d, temp reading error !\n", state->index);
  703. } else {
  704. /* Fixup temperature according to diode calibration
  705. */
  706. DBG(" cpu %d, temp raw: %04x, m_diode: %04x, b_diode: %04x\n",
  707. state->index,
  708. ltemp, state->mpu.mdiode, state->mpu.bdiode);
  709. *temp = ((s32)ltemp * (s32)state->mpu.mdiode + ((s32)state->mpu.bdiode << 12)) >> 2;
  710. state->last_temp = *temp;
  711. DBG(" temp: %d.%03d\n", FIX32TOPRINT((*temp)));
  712. }
  713. /*
  714. * Read voltage & current and calculate power
  715. */
  716. volts = read_smon_adc(state, 3);
  717. amps = read_smon_adc(state, 4);
  718. /* Scale voltage and current raw sensor values according to fixed scales
  719. * obtained in Darwin and calculate power from I and V
  720. */
  721. volts *= ADC_CPU_VOLTAGE_SCALE;
  722. amps *= ADC_CPU_CURRENT_SCALE;
  723. *power = (((u64)volts) * ((u64)amps)) >> 16;
  724. state->voltage = volts;
  725. state->current_a = amps;
  726. state->last_power = *power;
  727. DBG(" cpu %d, current: %d.%03d, voltage: %d.%03d, power: %d.%03d W\n",
  728. state->index, FIX32TOPRINT(state->current_a),
  729. FIX32TOPRINT(state->voltage), FIX32TOPRINT(*power));
  730. return 0;
  731. }
  732. static void do_cpu_pid(struct cpu_pid_state *state, s32 temp, s32 power)
  733. {
  734. s32 power_target, integral, derivative, proportional, adj_in_target, sval;
  735. s64 integ_p, deriv_p, prop_p, sum;
  736. int i;
  737. /* Calculate power target value (could be done once for all)
  738. * and convert to a 16.16 fp number
  739. */
  740. power_target = ((u32)(state->mpu.pmaxh - state->mpu.padjmax)) << 16;
  741. DBG(" power target: %d.%03d, error: %d.%03d\n",
  742. FIX32TOPRINT(power_target), FIX32TOPRINT(power_target - power));
  743. /* Store temperature and power in history array */
  744. state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
  745. state->temp_history[state->cur_temp] = temp;
  746. state->cur_power = (state->cur_power + 1) % state->count_power;
  747. state->power_history[state->cur_power] = power;
  748. state->error_history[state->cur_power] = power_target - power;
  749. /* If first loop, fill the history table */
  750. if (state->first) {
  751. for (i = 0; i < (state->count_power - 1); i++) {
  752. state->cur_power = (state->cur_power + 1) % state->count_power;
  753. state->power_history[state->cur_power] = power;
  754. state->error_history[state->cur_power] = power_target - power;
  755. }
  756. for (i = 0; i < (CPU_TEMP_HISTORY_SIZE - 1); i++) {
  757. state->cur_temp = (state->cur_temp + 1) % CPU_TEMP_HISTORY_SIZE;
  758. state->temp_history[state->cur_temp] = temp;
  759. }
  760. state->first = 0;
  761. }
  762. /* Calculate the integral term normally based on the "power" values */
  763. sum = 0;
  764. integral = 0;
  765. for (i = 0; i < state->count_power; i++)
  766. integral += state->error_history[i];
  767. integral *= CPU_PID_INTERVAL;
  768. DBG(" integral: %08x\n", integral);
  769. /* Calculate the adjusted input (sense value).
  770. * G_r is 12.20
  771. * integ is 16.16
  772. * so the result is 28.36
  773. *
  774. * input target is mpu.ttarget, input max is mpu.tmax
  775. */
  776. integ_p = ((s64)state->mpu.pid_gr) * (s64)integral;
  777. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  778. sval = (state->mpu.tmax << 16) - ((integ_p >> 20) & 0xffffffff);
  779. adj_in_target = (state->mpu.ttarget << 16);
  780. if (adj_in_target > sval)
  781. adj_in_target = sval;
  782. DBG(" adj_in_target: %d.%03d, ttarget: %d\n", FIX32TOPRINT(adj_in_target),
  783. state->mpu.ttarget);
  784. /* Calculate the derivative term */
  785. derivative = state->temp_history[state->cur_temp] -
  786. state->temp_history[(state->cur_temp + CPU_TEMP_HISTORY_SIZE - 1)
  787. % CPU_TEMP_HISTORY_SIZE];
  788. derivative /= CPU_PID_INTERVAL;
  789. deriv_p = ((s64)state->mpu.pid_gd) * (s64)derivative;
  790. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  791. sum += deriv_p;
  792. /* Calculate the proportional term */
  793. proportional = temp - adj_in_target;
  794. prop_p = ((s64)state->mpu.pid_gp) * (s64)proportional;
  795. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  796. sum += prop_p;
  797. /* Scale sum */
  798. sum >>= 36;
  799. DBG(" sum: %d\n", (int)sum);
  800. state->rpm += (s32)sum;
  801. }
  802. static void do_monitor_cpu_combined(void)
  803. {
  804. struct cpu_pid_state *state0 = &cpu_state[0];
  805. struct cpu_pid_state *state1 = &cpu_state[1];
  806. s32 temp0, power0, temp1, power1;
  807. s32 temp_combi, power_combi;
  808. int rc, intake, pump;
  809. rc = do_read_one_cpu_values(state0, &temp0, &power0);
  810. if (rc < 0) {
  811. /* XXX What do we do now ? */
  812. }
  813. state1->overtemp = 0;
  814. rc = do_read_one_cpu_values(state1, &temp1, &power1);
  815. if (rc < 0) {
  816. /* XXX What do we do now ? */
  817. }
  818. if (state1->overtemp)
  819. state0->overtemp++;
  820. temp_combi = max(temp0, temp1);
  821. power_combi = max(power0, power1);
  822. /* Check tmax, increment overtemp if we are there. At tmax+8, we go
  823. * full blown immediately and try to trigger a shutdown
  824. */
  825. if (temp_combi >= ((state0->mpu.tmax + 8) << 16)) {
  826. printk(KERN_WARNING "Warning ! Temperature way above maximum (%d) !\n",
  827. temp_combi >> 16);
  828. state0->overtemp += CPU_MAX_OVERTEMP / 4;
  829. } else if (temp_combi > (state0->mpu.tmax << 16))
  830. state0->overtemp++;
  831. else
  832. state0->overtemp = 0;
  833. if (state0->overtemp >= CPU_MAX_OVERTEMP)
  834. critical_state = 1;
  835. if (state0->overtemp > 0) {
  836. state0->rpm = state0->mpu.rmaxn_exhaust_fan;
  837. state0->intake_rpm = intake = state0->mpu.rmaxn_intake_fan;
  838. pump = state0->pump_max;
  839. goto do_set_fans;
  840. }
  841. /* Do the PID */
  842. do_cpu_pid(state0, temp_combi, power_combi);
  843. /* Range check */
  844. state0->rpm = max(state0->rpm, (int)state0->mpu.rminn_exhaust_fan);
  845. state0->rpm = min(state0->rpm, (int)state0->mpu.rmaxn_exhaust_fan);
  846. /* Calculate intake fan speed */
  847. intake = (state0->rpm * CPU_INTAKE_SCALE) >> 16;
  848. intake = max(intake, (int)state0->mpu.rminn_intake_fan);
  849. intake = min(intake, (int)state0->mpu.rmaxn_intake_fan);
  850. state0->intake_rpm = intake;
  851. /* Calculate pump speed */
  852. pump = (state0->rpm * state0->pump_max) /
  853. state0->mpu.rmaxn_exhaust_fan;
  854. pump = min(pump, state0->pump_max);
  855. pump = max(pump, state0->pump_min);
  856. do_set_fans:
  857. /* We copy values from state 0 to state 1 for /sysfs */
  858. state1->rpm = state0->rpm;
  859. state1->intake_rpm = state0->intake_rpm;
  860. DBG("** CPU %d RPM: %d Ex, %d, Pump: %d, In, overtemp: %d\n",
  861. state1->index, (int)state1->rpm, intake, pump, state1->overtemp);
  862. /* We should check for errors, shouldn't we ? But then, what
  863. * do we do once the error occurs ? For FCU notified fan
  864. * failures (-EFAULT) we probably want to notify userland
  865. * some way...
  866. */
  867. set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
  868. set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state0->rpm);
  869. set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
  870. set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state0->rpm);
  871. if (fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
  872. set_rpm_fan(CPUA_PUMP_RPM_INDEX, pump);
  873. if (fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID)
  874. set_rpm_fan(CPUB_PUMP_RPM_INDEX, pump);
  875. }
  876. static void do_monitor_cpu_split(struct cpu_pid_state *state)
  877. {
  878. s32 temp, power;
  879. int rc, intake;
  880. /* Read current fan status */
  881. rc = do_read_one_cpu_values(state, &temp, &power);
  882. if (rc < 0) {
  883. /* XXX What do we do now ? */
  884. }
  885. /* Check tmax, increment overtemp if we are there. At tmax+8, we go
  886. * full blown immediately and try to trigger a shutdown
  887. */
  888. if (temp >= ((state->mpu.tmax + 8) << 16)) {
  889. printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
  890. " (%d) !\n",
  891. state->index, temp >> 16);
  892. state->overtemp += CPU_MAX_OVERTEMP / 4;
  893. } else if (temp > (state->mpu.tmax << 16))
  894. state->overtemp++;
  895. else
  896. state->overtemp = 0;
  897. if (state->overtemp >= CPU_MAX_OVERTEMP)
  898. critical_state = 1;
  899. if (state->overtemp > 0) {
  900. state->rpm = state->mpu.rmaxn_exhaust_fan;
  901. state->intake_rpm = intake = state->mpu.rmaxn_intake_fan;
  902. goto do_set_fans;
  903. }
  904. /* Do the PID */
  905. do_cpu_pid(state, temp, power);
  906. /* Range check */
  907. state->rpm = max(state->rpm, (int)state->mpu.rminn_exhaust_fan);
  908. state->rpm = min(state->rpm, (int)state->mpu.rmaxn_exhaust_fan);
  909. /* Calculate intake fan */
  910. intake = (state->rpm * CPU_INTAKE_SCALE) >> 16;
  911. intake = max(intake, (int)state->mpu.rminn_intake_fan);
  912. intake = min(intake, (int)state->mpu.rmaxn_intake_fan);
  913. state->intake_rpm = intake;
  914. do_set_fans:
  915. DBG("** CPU %d RPM: %d Ex, %d In, overtemp: %d\n",
  916. state->index, (int)state->rpm, intake, state->overtemp);
  917. /* We should check for errors, shouldn't we ? But then, what
  918. * do we do once the error occurs ? For FCU notified fan
  919. * failures (-EFAULT) we probably want to notify userland
  920. * some way...
  921. */
  922. if (state->index == 0) {
  923. set_rpm_fan(CPUA_INTAKE_FAN_RPM_INDEX, intake);
  924. set_rpm_fan(CPUA_EXHAUST_FAN_RPM_INDEX, state->rpm);
  925. } else {
  926. set_rpm_fan(CPUB_INTAKE_FAN_RPM_INDEX, intake);
  927. set_rpm_fan(CPUB_EXHAUST_FAN_RPM_INDEX, state->rpm);
  928. }
  929. }
  930. static void do_monitor_cpu_rack(struct cpu_pid_state *state)
  931. {
  932. s32 temp, power, fan_min;
  933. int rc;
  934. /* Read current fan status */
  935. rc = do_read_one_cpu_values(state, &temp, &power);
  936. if (rc < 0) {
  937. /* XXX What do we do now ? */
  938. }
  939. /* Check tmax, increment overtemp if we are there. At tmax+8, we go
  940. * full blown immediately and try to trigger a shutdown
  941. */
  942. if (temp >= ((state->mpu.tmax + 8) << 16)) {
  943. printk(KERN_WARNING "Warning ! CPU %d temperature way above maximum"
  944. " (%d) !\n",
  945. state->index, temp >> 16);
  946. state->overtemp = CPU_MAX_OVERTEMP / 4;
  947. } else if (temp > (state->mpu.tmax << 16))
  948. state->overtemp++;
  949. else
  950. state->overtemp = 0;
  951. if (state->overtemp >= CPU_MAX_OVERTEMP)
  952. critical_state = 1;
  953. if (state->overtemp > 0) {
  954. state->rpm = state->intake_rpm = state->mpu.rmaxn_intake_fan;
  955. goto do_set_fans;
  956. }
  957. /* Do the PID */
  958. do_cpu_pid(state, temp, power);
  959. /* Check clamp from dimms */
  960. fan_min = dimm_output_clamp;
  961. fan_min = max(fan_min, (int)state->mpu.rminn_intake_fan);
  962. state->rpm = max(state->rpm, (int)fan_min);
  963. state->rpm = min(state->rpm, (int)state->mpu.rmaxn_intake_fan);
  964. state->intake_rpm = state->rpm;
  965. do_set_fans:
  966. DBG("** CPU %d RPM: %d overtemp: %d\n",
  967. state->index, (int)state->rpm, state->overtemp);
  968. /* We should check for errors, shouldn't we ? But then, what
  969. * do we do once the error occurs ? For FCU notified fan
  970. * failures (-EFAULT) we probably want to notify userland
  971. * some way...
  972. */
  973. if (state->index == 0) {
  974. set_rpm_fan(CPU_A1_FAN_RPM_INDEX, state->rpm);
  975. set_rpm_fan(CPU_A2_FAN_RPM_INDEX, state->rpm);
  976. set_rpm_fan(CPU_A3_FAN_RPM_INDEX, state->rpm);
  977. } else {
  978. set_rpm_fan(CPU_B1_FAN_RPM_INDEX, state->rpm);
  979. set_rpm_fan(CPU_B2_FAN_RPM_INDEX, state->rpm);
  980. set_rpm_fan(CPU_B3_FAN_RPM_INDEX, state->rpm);
  981. }
  982. }
  983. /*
  984. * Initialize the state structure for one CPU control loop
  985. */
  986. static int init_cpu_state(struct cpu_pid_state *state, int index)
  987. {
  988. state->index = index;
  989. state->first = 1;
  990. state->rpm = (cpu_pid_type == CPU_PID_TYPE_RACKMAC) ? 4000 : 1000;
  991. state->overtemp = 0;
  992. state->adc_config = 0x00;
  993. if (index == 0)
  994. state->monitor = attach_i2c_chip(SUPPLY_MONITOR_ID, "CPU0_monitor");
  995. else if (index == 1)
  996. state->monitor = attach_i2c_chip(SUPPLY_MONITORB_ID, "CPU1_monitor");
  997. if (state->monitor == NULL)
  998. goto fail;
  999. if (read_eeprom(index, &state->mpu))
  1000. goto fail;
  1001. state->count_power = state->mpu.tguardband;
  1002. if (state->count_power > CPU_POWER_HISTORY_SIZE) {
  1003. printk(KERN_WARNING "Warning ! too many power history slots\n");
  1004. state->count_power = CPU_POWER_HISTORY_SIZE;
  1005. }
  1006. DBG("CPU %d Using %d power history entries\n", index, state->count_power);
  1007. if (index == 0) {
  1008. device_create_file(&of_dev->dev, &dev_attr_cpu0_temperature);
  1009. device_create_file(&of_dev->dev, &dev_attr_cpu0_voltage);
  1010. device_create_file(&of_dev->dev, &dev_attr_cpu0_current);
  1011. device_create_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
  1012. device_create_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
  1013. } else {
  1014. device_create_file(&of_dev->dev, &dev_attr_cpu1_temperature);
  1015. device_create_file(&of_dev->dev, &dev_attr_cpu1_voltage);
  1016. device_create_file(&of_dev->dev, &dev_attr_cpu1_current);
  1017. device_create_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
  1018. device_create_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
  1019. }
  1020. return 0;
  1021. fail:
  1022. if (state->monitor)
  1023. detach_i2c_chip(state->monitor);
  1024. state->monitor = NULL;
  1025. return -ENODEV;
  1026. }
  1027. /*
  1028. * Dispose of the state data for one CPU control loop
  1029. */
  1030. static void dispose_cpu_state(struct cpu_pid_state *state)
  1031. {
  1032. if (state->monitor == NULL)
  1033. return;
  1034. if (state->index == 0) {
  1035. device_remove_file(&of_dev->dev, &dev_attr_cpu0_temperature);
  1036. device_remove_file(&of_dev->dev, &dev_attr_cpu0_voltage);
  1037. device_remove_file(&of_dev->dev, &dev_attr_cpu0_current);
  1038. device_remove_file(&of_dev->dev, &dev_attr_cpu0_exhaust_fan_rpm);
  1039. device_remove_file(&of_dev->dev, &dev_attr_cpu0_intake_fan_rpm);
  1040. } else {
  1041. device_remove_file(&of_dev->dev, &dev_attr_cpu1_temperature);
  1042. device_remove_file(&of_dev->dev, &dev_attr_cpu1_voltage);
  1043. device_remove_file(&of_dev->dev, &dev_attr_cpu1_current);
  1044. device_remove_file(&of_dev->dev, &dev_attr_cpu1_exhaust_fan_rpm);
  1045. device_remove_file(&of_dev->dev, &dev_attr_cpu1_intake_fan_rpm);
  1046. }
  1047. detach_i2c_chip(state->monitor);
  1048. state->monitor = NULL;
  1049. }
  1050. /*
  1051. * Motherboard backside & U3 heatsink fan control loop
  1052. */
  1053. static void do_monitor_backside(struct backside_pid_state *state)
  1054. {
  1055. s32 temp, integral, derivative, fan_min;
  1056. s64 integ_p, deriv_p, prop_p, sum;
  1057. int i, rc;
  1058. if (--state->ticks != 0)
  1059. return;
  1060. state->ticks = backside_params.interval;
  1061. DBG("backside:\n");
  1062. /* Check fan status */
  1063. rc = get_pwm_fan(BACKSIDE_FAN_PWM_INDEX);
  1064. if (rc < 0) {
  1065. printk(KERN_WARNING "Error %d reading backside fan !\n", rc);
  1066. /* XXX What do we do now ? */
  1067. } else
  1068. state->pwm = rc;
  1069. DBG(" current pwm: %d\n", state->pwm);
  1070. /* Get some sensor readings */
  1071. temp = i2c_smbus_read_byte_data(state->monitor, MAX6690_EXT_TEMP) << 16;
  1072. state->last_temp = temp;
  1073. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1074. FIX32TOPRINT(backside_params.input_target));
  1075. /* Store temperature and error in history array */
  1076. state->cur_sample = (state->cur_sample + 1) % BACKSIDE_PID_HISTORY_SIZE;
  1077. state->sample_history[state->cur_sample] = temp;
  1078. state->error_history[state->cur_sample] = temp - backside_params.input_target;
  1079. /* If first loop, fill the history table */
  1080. if (state->first) {
  1081. for (i = 0; i < (BACKSIDE_PID_HISTORY_SIZE - 1); i++) {
  1082. state->cur_sample = (state->cur_sample + 1) %
  1083. BACKSIDE_PID_HISTORY_SIZE;
  1084. state->sample_history[state->cur_sample] = temp;
  1085. state->error_history[state->cur_sample] =
  1086. temp - backside_params.input_target;
  1087. }
  1088. state->first = 0;
  1089. }
  1090. /* Calculate the integral term */
  1091. sum = 0;
  1092. integral = 0;
  1093. for (i = 0; i < BACKSIDE_PID_HISTORY_SIZE; i++)
  1094. integral += state->error_history[i];
  1095. integral *= backside_params.interval;
  1096. DBG(" integral: %08x\n", integral);
  1097. integ_p = ((s64)backside_params.G_r) * (s64)integral;
  1098. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1099. sum += integ_p;
  1100. /* Calculate the derivative term */
  1101. derivative = state->error_history[state->cur_sample] -
  1102. state->error_history[(state->cur_sample + BACKSIDE_PID_HISTORY_SIZE - 1)
  1103. % BACKSIDE_PID_HISTORY_SIZE];
  1104. derivative /= backside_params.interval;
  1105. deriv_p = ((s64)backside_params.G_d) * (s64)derivative;
  1106. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1107. sum += deriv_p;
  1108. /* Calculate the proportional term */
  1109. prop_p = ((s64)backside_params.G_p) * (s64)(state->error_history[state->cur_sample]);
  1110. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1111. sum += prop_p;
  1112. /* Scale sum */
  1113. sum >>= 36;
  1114. DBG(" sum: %d\n", (int)sum);
  1115. if (backside_params.additive)
  1116. state->pwm += (s32)sum;
  1117. else
  1118. state->pwm = sum;
  1119. /* Check for clamp */
  1120. fan_min = (dimm_output_clamp * 100) / 14000;
  1121. fan_min = max(fan_min, backside_params.output_min);
  1122. state->pwm = max(state->pwm, fan_min);
  1123. state->pwm = min(state->pwm, backside_params.output_max);
  1124. DBG("** BACKSIDE PWM: %d\n", (int)state->pwm);
  1125. set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, state->pwm);
  1126. }
  1127. /*
  1128. * Initialize the state structure for the backside fan control loop
  1129. */
  1130. static int init_backside_state(struct backside_pid_state *state)
  1131. {
  1132. struct device_node *u3;
  1133. int u3h = 1; /* conservative by default */
  1134. /*
  1135. * There are different PID params for machines with U3 and machines
  1136. * with U3H, pick the right ones now
  1137. */
  1138. u3 = of_find_node_by_path("/u3@0,f8000000");
  1139. if (u3 != NULL) {
  1140. u32 *vers = (u32 *)get_property(u3, "device-rev", NULL);
  1141. if (vers)
  1142. if (((*vers) & 0x3f) < 0x34)
  1143. u3h = 0;
  1144. of_node_put(u3);
  1145. }
  1146. if (rackmac) {
  1147. backside_params.G_d = BACKSIDE_PID_RACK_G_d;
  1148. backside_params.input_target = BACKSIDE_PID_RACK_INPUT_TARGET;
  1149. backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
  1150. backside_params.interval = BACKSIDE_PID_RACK_INTERVAL;
  1151. backside_params.G_p = BACKSIDE_PID_RACK_G_p;
  1152. backside_params.G_r = BACKSIDE_PID_G_r;
  1153. backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
  1154. backside_params.additive = 0;
  1155. } else if (u3h) {
  1156. backside_params.G_d = BACKSIDE_PID_U3H_G_d;
  1157. backside_params.input_target = BACKSIDE_PID_U3H_INPUT_TARGET;
  1158. backside_params.output_min = BACKSIDE_PID_U3H_OUTPUT_MIN;
  1159. backside_params.interval = BACKSIDE_PID_INTERVAL;
  1160. backside_params.G_p = BACKSIDE_PID_G_p;
  1161. backside_params.G_r = BACKSIDE_PID_G_r;
  1162. backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
  1163. backside_params.additive = 1;
  1164. } else {
  1165. backside_params.G_d = BACKSIDE_PID_U3_G_d;
  1166. backside_params.input_target = BACKSIDE_PID_U3_INPUT_TARGET;
  1167. backside_params.output_min = BACKSIDE_PID_U3_OUTPUT_MIN;
  1168. backside_params.interval = BACKSIDE_PID_INTERVAL;
  1169. backside_params.G_p = BACKSIDE_PID_G_p;
  1170. backside_params.G_r = BACKSIDE_PID_G_r;
  1171. backside_params.output_max = BACKSIDE_PID_OUTPUT_MAX;
  1172. backside_params.additive = 1;
  1173. }
  1174. state->ticks = 1;
  1175. state->first = 1;
  1176. state->pwm = 50;
  1177. state->monitor = attach_i2c_chip(BACKSIDE_MAX_ID, "backside_temp");
  1178. if (state->monitor == NULL)
  1179. return -ENODEV;
  1180. device_create_file(&of_dev->dev, &dev_attr_backside_temperature);
  1181. device_create_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
  1182. return 0;
  1183. }
  1184. /*
  1185. * Dispose of the state data for the backside control loop
  1186. */
  1187. static void dispose_backside_state(struct backside_pid_state *state)
  1188. {
  1189. if (state->monitor == NULL)
  1190. return;
  1191. device_remove_file(&of_dev->dev, &dev_attr_backside_temperature);
  1192. device_remove_file(&of_dev->dev, &dev_attr_backside_fan_pwm);
  1193. detach_i2c_chip(state->monitor);
  1194. state->monitor = NULL;
  1195. }
  1196. /*
  1197. * Drives bay fan control loop
  1198. */
  1199. static void do_monitor_drives(struct drives_pid_state *state)
  1200. {
  1201. s32 temp, integral, derivative;
  1202. s64 integ_p, deriv_p, prop_p, sum;
  1203. int i, rc;
  1204. if (--state->ticks != 0)
  1205. return;
  1206. state->ticks = DRIVES_PID_INTERVAL;
  1207. DBG("drives:\n");
  1208. /* Check fan status */
  1209. rc = get_rpm_fan(DRIVES_FAN_RPM_INDEX, !RPM_PID_USE_ACTUAL_SPEED);
  1210. if (rc < 0) {
  1211. printk(KERN_WARNING "Error %d reading drives fan !\n", rc);
  1212. /* XXX What do we do now ? */
  1213. } else
  1214. state->rpm = rc;
  1215. DBG(" current rpm: %d\n", state->rpm);
  1216. /* Get some sensor readings */
  1217. temp = le16_to_cpu(i2c_smbus_read_word_data(state->monitor, DS1775_TEMP)) << 8;
  1218. state->last_temp = temp;
  1219. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1220. FIX32TOPRINT(DRIVES_PID_INPUT_TARGET));
  1221. /* Store temperature and error in history array */
  1222. state->cur_sample = (state->cur_sample + 1) % DRIVES_PID_HISTORY_SIZE;
  1223. state->sample_history[state->cur_sample] = temp;
  1224. state->error_history[state->cur_sample] = temp - DRIVES_PID_INPUT_TARGET;
  1225. /* If first loop, fill the history table */
  1226. if (state->first) {
  1227. for (i = 0; i < (DRIVES_PID_HISTORY_SIZE - 1); i++) {
  1228. state->cur_sample = (state->cur_sample + 1) %
  1229. DRIVES_PID_HISTORY_SIZE;
  1230. state->sample_history[state->cur_sample] = temp;
  1231. state->error_history[state->cur_sample] =
  1232. temp - DRIVES_PID_INPUT_TARGET;
  1233. }
  1234. state->first = 0;
  1235. }
  1236. /* Calculate the integral term */
  1237. sum = 0;
  1238. integral = 0;
  1239. for (i = 0; i < DRIVES_PID_HISTORY_SIZE; i++)
  1240. integral += state->error_history[i];
  1241. integral *= DRIVES_PID_INTERVAL;
  1242. DBG(" integral: %08x\n", integral);
  1243. integ_p = ((s64)DRIVES_PID_G_r) * (s64)integral;
  1244. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1245. sum += integ_p;
  1246. /* Calculate the derivative term */
  1247. derivative = state->error_history[state->cur_sample] -
  1248. state->error_history[(state->cur_sample + DRIVES_PID_HISTORY_SIZE - 1)
  1249. % DRIVES_PID_HISTORY_SIZE];
  1250. derivative /= DRIVES_PID_INTERVAL;
  1251. deriv_p = ((s64)DRIVES_PID_G_d) * (s64)derivative;
  1252. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1253. sum += deriv_p;
  1254. /* Calculate the proportional term */
  1255. prop_p = ((s64)DRIVES_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
  1256. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1257. sum += prop_p;
  1258. /* Scale sum */
  1259. sum >>= 36;
  1260. DBG(" sum: %d\n", (int)sum);
  1261. state->rpm += (s32)sum;
  1262. state->rpm = max(state->rpm, DRIVES_PID_OUTPUT_MIN);
  1263. state->rpm = min(state->rpm, DRIVES_PID_OUTPUT_MAX);
  1264. DBG("** DRIVES RPM: %d\n", (int)state->rpm);
  1265. set_rpm_fan(DRIVES_FAN_RPM_INDEX, state->rpm);
  1266. }
  1267. /*
  1268. * Initialize the state structure for the drives bay fan control loop
  1269. */
  1270. static int init_drives_state(struct drives_pid_state *state)
  1271. {
  1272. state->ticks = 1;
  1273. state->first = 1;
  1274. state->rpm = 1000;
  1275. state->monitor = attach_i2c_chip(DRIVES_DALLAS_ID, "drives_temp");
  1276. if (state->monitor == NULL)
  1277. return -ENODEV;
  1278. device_create_file(&of_dev->dev, &dev_attr_drives_temperature);
  1279. device_create_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
  1280. return 0;
  1281. }
  1282. /*
  1283. * Dispose of the state data for the drives control loop
  1284. */
  1285. static void dispose_drives_state(struct drives_pid_state *state)
  1286. {
  1287. if (state->monitor == NULL)
  1288. return;
  1289. device_remove_file(&of_dev->dev, &dev_attr_drives_temperature);
  1290. device_remove_file(&of_dev->dev, &dev_attr_drives_fan_rpm);
  1291. detach_i2c_chip(state->monitor);
  1292. state->monitor = NULL;
  1293. }
  1294. /*
  1295. * DIMMs temp control loop
  1296. */
  1297. static void do_monitor_dimms(struct dimm_pid_state *state)
  1298. {
  1299. s32 temp, integral, derivative, fan_min;
  1300. s64 integ_p, deriv_p, prop_p, sum;
  1301. int i;
  1302. if (--state->ticks != 0)
  1303. return;
  1304. state->ticks = DIMM_PID_INTERVAL;
  1305. DBG("DIMM:\n");
  1306. DBG(" current value: %d\n", state->output);
  1307. temp = read_lm87_reg(state->monitor, LM87_INT_TEMP);
  1308. if (temp < 0)
  1309. return;
  1310. temp <<= 16;
  1311. state->last_temp = temp;
  1312. DBG(" temp: %d.%03d, target: %d.%03d\n", FIX32TOPRINT(temp),
  1313. FIX32TOPRINT(DIMM_PID_INPUT_TARGET));
  1314. /* Store temperature and error in history array */
  1315. state->cur_sample = (state->cur_sample + 1) % DIMM_PID_HISTORY_SIZE;
  1316. state->sample_history[state->cur_sample] = temp;
  1317. state->error_history[state->cur_sample] = temp - DIMM_PID_INPUT_TARGET;
  1318. /* If first loop, fill the history table */
  1319. if (state->first) {
  1320. for (i = 0; i < (DIMM_PID_HISTORY_SIZE - 1); i++) {
  1321. state->cur_sample = (state->cur_sample + 1) %
  1322. DIMM_PID_HISTORY_SIZE;
  1323. state->sample_history[state->cur_sample] = temp;
  1324. state->error_history[state->cur_sample] =
  1325. temp - DIMM_PID_INPUT_TARGET;
  1326. }
  1327. state->first = 0;
  1328. }
  1329. /* Calculate the integral term */
  1330. sum = 0;
  1331. integral = 0;
  1332. for (i = 0; i < DIMM_PID_HISTORY_SIZE; i++)
  1333. integral += state->error_history[i];
  1334. integral *= DIMM_PID_INTERVAL;
  1335. DBG(" integral: %08x\n", integral);
  1336. integ_p = ((s64)DIMM_PID_G_r) * (s64)integral;
  1337. DBG(" integ_p: %d\n", (int)(integ_p >> 36));
  1338. sum += integ_p;
  1339. /* Calculate the derivative term */
  1340. derivative = state->error_history[state->cur_sample] -
  1341. state->error_history[(state->cur_sample + DIMM_PID_HISTORY_SIZE - 1)
  1342. % DIMM_PID_HISTORY_SIZE];
  1343. derivative /= DIMM_PID_INTERVAL;
  1344. deriv_p = ((s64)DIMM_PID_G_d) * (s64)derivative;
  1345. DBG(" deriv_p: %d\n", (int)(deriv_p >> 36));
  1346. sum += deriv_p;
  1347. /* Calculate the proportional term */
  1348. prop_p = ((s64)DIMM_PID_G_p) * (s64)(state->error_history[state->cur_sample]);
  1349. DBG(" prop_p: %d\n", (int)(prop_p >> 36));
  1350. sum += prop_p;
  1351. /* Scale sum */
  1352. sum >>= 36;
  1353. DBG(" sum: %d\n", (int)sum);
  1354. state->output = (s32)sum;
  1355. state->output = max(state->output, DIMM_PID_OUTPUT_MIN);
  1356. state->output = min(state->output, DIMM_PID_OUTPUT_MAX);
  1357. dimm_output_clamp = state->output;
  1358. DBG("** DIMM clamp value: %d\n", (int)state->output);
  1359. /* Backside PID is only every 5 seconds, force backside fan clamping now */
  1360. fan_min = (dimm_output_clamp * 100) / 14000;
  1361. fan_min = max(fan_min, backside_params.output_min);
  1362. if (backside_state.pwm < fan_min) {
  1363. backside_state.pwm = fan_min;
  1364. DBG(" -> applying clamp to backside fan now: %d !\n", fan_min);
  1365. set_pwm_fan(BACKSIDE_FAN_PWM_INDEX, fan_min);
  1366. }
  1367. }
  1368. /*
  1369. * Initialize the state structure for the DIMM temp control loop
  1370. */
  1371. static int init_dimms_state(struct dimm_pid_state *state)
  1372. {
  1373. state->ticks = 1;
  1374. state->first = 1;
  1375. state->output = 4000;
  1376. state->monitor = attach_i2c_chip(XSERVE_DIMMS_LM87, "dimms_temp");
  1377. if (state->monitor == NULL)
  1378. return -ENODEV;
  1379. device_create_file(&of_dev->dev, &dev_attr_dimms_temperature);
  1380. return 0;
  1381. }
  1382. /*
  1383. * Dispose of the state data for the drives control loop
  1384. */
  1385. static void dispose_dimms_state(struct dimm_pid_state *state)
  1386. {
  1387. if (state->monitor == NULL)
  1388. return;
  1389. device_remove_file(&of_dev->dev, &dev_attr_dimms_temperature);
  1390. detach_i2c_chip(state->monitor);
  1391. state->monitor = NULL;
  1392. }
  1393. static int call_critical_overtemp(void)
  1394. {
  1395. char *argv[] = { critical_overtemp_path, NULL };
  1396. static char *envp[] = { "HOME=/",
  1397. "TERM=linux",
  1398. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  1399. NULL };
  1400. return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
  1401. }
  1402. /*
  1403. * Here's the kernel thread that calls the various control loops
  1404. */
  1405. static int main_control_loop(void *x)
  1406. {
  1407. daemonize("kfand");
  1408. DBG("main_control_loop started\n");
  1409. down(&driver_lock);
  1410. if (start_fcu() < 0) {
  1411. printk(KERN_ERR "kfand: failed to start FCU\n");
  1412. up(&driver_lock);
  1413. goto out;
  1414. }
  1415. /* Set the PCI fan once for now */
  1416. set_pwm_fan(SLOTS_FAN_PWM_INDEX, SLOTS_FAN_DEFAULT_PWM);
  1417. /* Initialize ADCs */
  1418. initialize_adc(&cpu_state[0]);
  1419. if (cpu_state[1].monitor != NULL)
  1420. initialize_adc(&cpu_state[1]);
  1421. up(&driver_lock);
  1422. while (state == state_attached) {
  1423. unsigned long elapsed, start;
  1424. start = jiffies;
  1425. down(&driver_lock);
  1426. /* First, we always calculate the new DIMMs state on an Xserve */
  1427. if (rackmac)
  1428. do_monitor_dimms(&dimms_state);
  1429. /* Then, the CPUs */
  1430. if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
  1431. do_monitor_cpu_combined();
  1432. else if (cpu_pid_type == CPU_PID_TYPE_RACKMAC) {
  1433. do_monitor_cpu_rack(&cpu_state[0]);
  1434. if (cpu_state[1].monitor != NULL)
  1435. do_monitor_cpu_rack(&cpu_state[1]);
  1436. // better deal with UP
  1437. } else {
  1438. do_monitor_cpu_split(&cpu_state[0]);
  1439. if (cpu_state[1].monitor != NULL)
  1440. do_monitor_cpu_split(&cpu_state[1]);
  1441. // better deal with UP
  1442. }
  1443. /* Then, the rest */
  1444. do_monitor_backside(&backside_state);
  1445. if (!rackmac)
  1446. do_monitor_drives(&drives_state);
  1447. up(&driver_lock);
  1448. if (critical_state == 1) {
  1449. printk(KERN_WARNING "Temperature control detected a critical condition\n");
  1450. printk(KERN_WARNING "Attempting to shut down...\n");
  1451. if (call_critical_overtemp()) {
  1452. printk(KERN_WARNING "Can't call %s, power off now!\n",
  1453. critical_overtemp_path);
  1454. machine_power_off();
  1455. }
  1456. }
  1457. if (critical_state > 0)
  1458. critical_state++;
  1459. if (critical_state > MAX_CRITICAL_STATE) {
  1460. printk(KERN_WARNING "Shutdown timed out, power off now !\n");
  1461. machine_power_off();
  1462. }
  1463. // FIXME: Deal with signals
  1464. elapsed = jiffies - start;
  1465. if (elapsed < HZ)
  1466. schedule_timeout_interruptible(HZ - elapsed);
  1467. }
  1468. out:
  1469. DBG("main_control_loop ended\n");
  1470. ctrl_task = 0;
  1471. complete_and_exit(&ctrl_complete, 0);
  1472. }
  1473. /*
  1474. * Dispose the control loops when tearing down
  1475. */
  1476. static void dispose_control_loops(void)
  1477. {
  1478. dispose_cpu_state(&cpu_state[0]);
  1479. dispose_cpu_state(&cpu_state[1]);
  1480. dispose_backside_state(&backside_state);
  1481. dispose_drives_state(&drives_state);
  1482. dispose_dimms_state(&dimms_state);
  1483. }
  1484. /*
  1485. * Create the control loops. U3-0 i2c bus is up, so we can now
  1486. * get to the various sensors
  1487. */
  1488. static int create_control_loops(void)
  1489. {
  1490. struct device_node *np;
  1491. /* Count CPUs from the device-tree, we don't care how many are
  1492. * actually used by Linux
  1493. */
  1494. cpu_count = 0;
  1495. for (np = NULL; NULL != (np = of_find_node_by_type(np, "cpu"));)
  1496. cpu_count++;
  1497. DBG("counted %d CPUs in the device-tree\n", cpu_count);
  1498. /* Decide the type of PID algorithm to use based on the presence of
  1499. * the pumps, though that may not be the best way, that is good enough
  1500. * for now
  1501. */
  1502. if (rackmac)
  1503. cpu_pid_type = CPU_PID_TYPE_RACKMAC;
  1504. else if (machine_is_compatible("PowerMac7,3")
  1505. && (cpu_count > 1)
  1506. && fcu_fans[CPUA_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID
  1507. && fcu_fans[CPUB_PUMP_RPM_INDEX].id != FCU_FAN_ABSENT_ID) {
  1508. printk(KERN_INFO "Liquid cooling pumps detected, using new algorithm !\n");
  1509. cpu_pid_type = CPU_PID_TYPE_COMBINED;
  1510. } else
  1511. cpu_pid_type = CPU_PID_TYPE_SPLIT;
  1512. /* Create control loops for everything. If any fail, everything
  1513. * fails
  1514. */
  1515. if (init_cpu_state(&cpu_state[0], 0))
  1516. goto fail;
  1517. if (cpu_pid_type == CPU_PID_TYPE_COMBINED)
  1518. fetch_cpu_pumps_minmax();
  1519. if (cpu_count > 1 && init_cpu_state(&cpu_state[1], 1))
  1520. goto fail;
  1521. if (init_backside_state(&backside_state))
  1522. goto fail;
  1523. if (rackmac && init_dimms_state(&dimms_state))
  1524. goto fail;
  1525. if (!rackmac && init_drives_state(&drives_state))
  1526. goto fail;
  1527. DBG("all control loops up !\n");
  1528. return 0;
  1529. fail:
  1530. DBG("failure creating control loops, disposing\n");
  1531. dispose_control_loops();
  1532. return -ENODEV;
  1533. }
  1534. /*
  1535. * Start the control loops after everything is up, that is create
  1536. * the thread that will make them run
  1537. */
  1538. static void start_control_loops(void)
  1539. {
  1540. init_completion(&ctrl_complete);
  1541. ctrl_task = kernel_thread(main_control_loop, NULL, SIGCHLD | CLONE_KERNEL);
  1542. }
  1543. /*
  1544. * Stop the control loops when tearing down
  1545. */
  1546. static void stop_control_loops(void)
  1547. {
  1548. if (ctrl_task != 0)
  1549. wait_for_completion(&ctrl_complete);
  1550. }
  1551. /*
  1552. * Attach to the i2c FCU after detecting U3-1 bus
  1553. */
  1554. static int attach_fcu(void)
  1555. {
  1556. fcu = attach_i2c_chip(FAN_CTRLER_ID, "fcu");
  1557. if (fcu == NULL)
  1558. return -ENODEV;
  1559. DBG("FCU attached\n");
  1560. return 0;
  1561. }
  1562. /*
  1563. * Detach from the i2c FCU when tearing down
  1564. */
  1565. static void detach_fcu(void)
  1566. {
  1567. if (fcu)
  1568. detach_i2c_chip(fcu);
  1569. fcu = NULL;
  1570. }
  1571. /*
  1572. * Attach to the i2c controller. We probe the various chips based
  1573. * on the device-tree nodes and build everything for the driver to
  1574. * run, we then kick the driver monitoring thread
  1575. */
  1576. static int therm_pm72_attach(struct i2c_adapter *adapter)
  1577. {
  1578. down(&driver_lock);
  1579. /* Check state */
  1580. if (state == state_detached)
  1581. state = state_attaching;
  1582. if (state != state_attaching) {
  1583. up(&driver_lock);
  1584. return 0;
  1585. }
  1586. /* Check if we are looking for one of these */
  1587. if (u3_0 == NULL && !strcmp(adapter->name, "u3 0")) {
  1588. u3_0 = adapter;
  1589. DBG("found U3-0\n");
  1590. if (k2 || !rackmac)
  1591. if (create_control_loops())
  1592. u3_0 = NULL;
  1593. } else if (u3_1 == NULL && !strcmp(adapter->name, "u3 1")) {
  1594. u3_1 = adapter;
  1595. DBG("found U3-1, attaching FCU\n");
  1596. if (attach_fcu())
  1597. u3_1 = NULL;
  1598. } else if (k2 == NULL && !strcmp(adapter->name, "mac-io 0")) {
  1599. k2 = adapter;
  1600. DBG("Found K2\n");
  1601. if (u3_0 && rackmac)
  1602. if (create_control_loops())
  1603. k2 = NULL;
  1604. }
  1605. /* We got all we need, start control loops */
  1606. if (u3_0 != NULL && u3_1 != NULL && (k2 || !rackmac)) {
  1607. DBG("everything up, starting control loops\n");
  1608. state = state_attached;
  1609. start_control_loops();
  1610. }
  1611. up(&driver_lock);
  1612. return 0;
  1613. }
  1614. /*
  1615. * Called on every adapter when the driver or the i2c controller
  1616. * is going away.
  1617. */
  1618. static int therm_pm72_detach(struct i2c_adapter *adapter)
  1619. {
  1620. down(&driver_lock);
  1621. if (state != state_detached)
  1622. state = state_detaching;
  1623. /* Stop control loops if any */
  1624. DBG("stopping control loops\n");
  1625. up(&driver_lock);
  1626. stop_control_loops();
  1627. down(&driver_lock);
  1628. if (u3_0 != NULL && !strcmp(adapter->name, "u3 0")) {
  1629. DBG("lost U3-0, disposing control loops\n");
  1630. dispose_control_loops();
  1631. u3_0 = NULL;
  1632. }
  1633. if (u3_1 != NULL && !strcmp(adapter->name, "u3 1")) {
  1634. DBG("lost U3-1, detaching FCU\n");
  1635. detach_fcu();
  1636. u3_1 = NULL;
  1637. }
  1638. if (u3_0 == NULL && u3_1 == NULL)
  1639. state = state_detached;
  1640. up(&driver_lock);
  1641. return 0;
  1642. }
  1643. static int fan_check_loc_match(const char *loc, int fan)
  1644. {
  1645. char tmp[64];
  1646. char *c, *e;
  1647. strlcpy(tmp, fcu_fans[fan].loc, 64);
  1648. c = tmp;
  1649. for (;;) {
  1650. e = strchr(c, ',');
  1651. if (e)
  1652. *e = 0;
  1653. if (strcmp(loc, c) == 0)
  1654. return 1;
  1655. if (e == NULL)
  1656. break;
  1657. c = e + 1;
  1658. }
  1659. return 0;
  1660. }
  1661. static void fcu_lookup_fans(struct device_node *fcu_node)
  1662. {
  1663. struct device_node *np = NULL;
  1664. int i;
  1665. /* The table is filled by default with values that are suitable
  1666. * for the old machines without device-tree informations. We scan
  1667. * the device-tree and override those values with whatever is
  1668. * there
  1669. */
  1670. DBG("Looking up FCU controls in device-tree...\n");
  1671. while ((np = of_get_next_child(fcu_node, np)) != NULL) {
  1672. int type = -1;
  1673. char *loc;
  1674. u32 *reg;
  1675. DBG(" control: %s, type: %s\n", np->name, np->type);
  1676. /* Detect control type */
  1677. if (!strcmp(np->type, "fan-rpm-control") ||
  1678. !strcmp(np->type, "fan-rpm"))
  1679. type = FCU_FAN_RPM;
  1680. if (!strcmp(np->type, "fan-pwm-control") ||
  1681. !strcmp(np->type, "fan-pwm"))
  1682. type = FCU_FAN_PWM;
  1683. /* Only care about fans for now */
  1684. if (type == -1)
  1685. continue;
  1686. /* Lookup for a matching location */
  1687. loc = (char *)get_property(np, "location", NULL);
  1688. reg = (u32 *)get_property(np, "reg", NULL);
  1689. if (loc == NULL || reg == NULL)
  1690. continue;
  1691. DBG(" matching location: %s, reg: 0x%08x\n", loc, *reg);
  1692. for (i = 0; i < FCU_FAN_COUNT; i++) {
  1693. int fan_id;
  1694. if (!fan_check_loc_match(loc, i))
  1695. continue;
  1696. DBG(" location match, index: %d\n", i);
  1697. fcu_fans[i].id = FCU_FAN_ABSENT_ID;
  1698. if (type != fcu_fans[i].type) {
  1699. printk(KERN_WARNING "therm_pm72: Fan type mismatch "
  1700. "in device-tree for %s\n", np->full_name);
  1701. break;
  1702. }
  1703. if (type == FCU_FAN_RPM)
  1704. fan_id = ((*reg) - 0x10) / 2;
  1705. else
  1706. fan_id = ((*reg) - 0x30) / 2;
  1707. if (fan_id > 7) {
  1708. printk(KERN_WARNING "therm_pm72: Can't parse "
  1709. "fan ID in device-tree for %s\n", np->full_name);
  1710. break;
  1711. }
  1712. DBG(" fan id -> %d, type -> %d\n", fan_id, type);
  1713. fcu_fans[i].id = fan_id;
  1714. }
  1715. }
  1716. /* Now dump the array */
  1717. printk(KERN_INFO "Detected fan controls:\n");
  1718. for (i = 0; i < FCU_FAN_COUNT; i++) {
  1719. if (fcu_fans[i].id == FCU_FAN_ABSENT_ID)
  1720. continue;
  1721. printk(KERN_INFO " %d: %s fan, id %d, location: %s\n", i,
  1722. fcu_fans[i].type == FCU_FAN_RPM ? "RPM" : "PWM",
  1723. fcu_fans[i].id, fcu_fans[i].loc);
  1724. }
  1725. }
  1726. static int fcu_of_probe(struct of_device* dev, const struct of_device_id *match)
  1727. {
  1728. int rc;
  1729. state = state_detached;
  1730. /* Lookup the fans in the device tree */
  1731. fcu_lookup_fans(dev->node);
  1732. /* Add the driver */
  1733. rc = i2c_add_driver(&therm_pm72_driver);
  1734. if (rc < 0)
  1735. return rc;
  1736. return 0;
  1737. }
  1738. static int fcu_of_remove(struct of_device* dev)
  1739. {
  1740. i2c_del_driver(&therm_pm72_driver);
  1741. return 0;
  1742. }
  1743. static struct of_device_id fcu_match[] =
  1744. {
  1745. {
  1746. .type = "fcu",
  1747. },
  1748. {},
  1749. };
  1750. static struct of_platform_driver fcu_of_platform_driver =
  1751. {
  1752. .name = "temperature",
  1753. .match_table = fcu_match,
  1754. .probe = fcu_of_probe,
  1755. .remove = fcu_of_remove
  1756. };
  1757. /*
  1758. * Check machine type, attach to i2c controller
  1759. */
  1760. static int __init therm_pm72_init(void)
  1761. {
  1762. struct device_node *np;
  1763. rackmac = machine_is_compatible("RackMac3,1");
  1764. if (!machine_is_compatible("PowerMac7,2") &&
  1765. !machine_is_compatible("PowerMac7,3") &&
  1766. !rackmac)
  1767. return -ENODEV;
  1768. printk(KERN_INFO "PowerMac G5 Thermal control driver %s\n", VERSION);
  1769. np = of_find_node_by_type(NULL, "fcu");
  1770. if (np == NULL) {
  1771. /* Some machines have strangely broken device-tree */
  1772. np = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/fan@15e");
  1773. if (np == NULL) {
  1774. printk(KERN_ERR "Can't find FCU in device-tree !\n");
  1775. return -ENODEV;
  1776. }
  1777. }
  1778. of_dev = of_platform_device_create(np, "temperature", NULL);
  1779. if (of_dev == NULL) {
  1780. printk(KERN_ERR "Can't register FCU platform device !\n");
  1781. return -ENODEV;
  1782. }
  1783. of_register_driver(&fcu_of_platform_driver);
  1784. return 0;
  1785. }
  1786. static void __exit therm_pm72_exit(void)
  1787. {
  1788. of_unregister_driver(&fcu_of_platform_driver);
  1789. if (of_dev)
  1790. of_device_unregister(of_dev);
  1791. }
  1792. module_init(therm_pm72_init);
  1793. module_exit(therm_pm72_exit);
  1794. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  1795. MODULE_DESCRIPTION("Driver for Apple's PowerMac G5 thermal control");
  1796. MODULE_LICENSE("GPL");