therm_pm72.c 56 KB

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