therm_pm72.c 56 KB

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