therm_pm72.c 61 KB

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