therm_pm72.c 56 KB

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