pmac64-cpufreq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  3. * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs,
  10. * that is iMac G5 and latest single CPU desktop.
  11. */
  12. #undef DEBUG
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/sched.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/init.h>
  21. #include <linux/completion.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of_device.h>
  24. #include <asm/prom.h>
  25. #include <asm/machdep.h>
  26. #include <asm/irq.h>
  27. #include <asm/sections.h>
  28. #include <asm/cputable.h>
  29. #include <asm/time.h>
  30. #include <asm/smu.h>
  31. #include <asm/pmac_pfunc.h>
  32. #define DBG(fmt...) pr_debug(fmt)
  33. /* see 970FX user manual */
  34. #define SCOM_PCR 0x0aa001 /* PCR scom addr */
  35. #define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */
  36. #define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */
  37. #define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */
  38. #define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */
  39. #define PCR_SPEED_MASK 0x000e0000U /* speed mask */
  40. #define PCR_SPEED_SHIFT 17
  41. #define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */
  42. #define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */
  43. #define PCR_TARGET_TIME_MASK 0x00006000U /* target time */
  44. #define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */
  45. #define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */
  46. #define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */
  47. #define SCOM_PSR 0x408001 /* PSR scom addr */
  48. /* warning: PSR is a 64 bits register */
  49. #define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */
  50. #define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */
  51. #define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */
  52. #define PSR_CUR_SPEED_SHIFT (56)
  53. /*
  54. * The G5 only supports two frequencies (Quarter speed is not supported)
  55. */
  56. #define CPUFREQ_HIGH 0
  57. #define CPUFREQ_LOW 1
  58. static struct cpufreq_frequency_table g5_cpu_freqs[] = {
  59. {0, CPUFREQ_HIGH, 0},
  60. {0, CPUFREQ_LOW, 0},
  61. {0, 0, CPUFREQ_TABLE_END},
  62. };
  63. /* Power mode data is an array of the 32 bits PCR values to use for
  64. * the various frequencies, retrieved from the device-tree
  65. */
  66. static int g5_pmode_cur;
  67. static void (*g5_switch_volt)(int speed_mode);
  68. static int (*g5_switch_freq)(int speed_mode);
  69. static int (*g5_query_freq)(void);
  70. static unsigned long transition_latency;
  71. #ifdef CONFIG_PMAC_SMU
  72. static const u32 *g5_pmode_data;
  73. static int g5_pmode_max;
  74. static struct smu_sdbp_fvt *g5_fvt_table; /* table of op. points */
  75. static int g5_fvt_count; /* number of op. points */
  76. static int g5_fvt_cur; /* current op. point */
  77. /*
  78. * SMU based voltage switching for Neo2 platforms
  79. */
  80. static void g5_smu_switch_volt(int speed_mode)
  81. {
  82. struct smu_simple_cmd cmd;
  83. DECLARE_COMPLETION_ONSTACK(comp);
  84. smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, smu_done_complete,
  85. &comp, 'V', 'S', 'L', 'E', 'W',
  86. 0xff, g5_fvt_cur+1, speed_mode);
  87. wait_for_completion(&comp);
  88. }
  89. /*
  90. * Platform function based voltage/vdnap switching for Neo2
  91. */
  92. static struct pmf_function *pfunc_set_vdnap0;
  93. static struct pmf_function *pfunc_vdnap0_complete;
  94. static void g5_vdnap_switch_volt(int speed_mode)
  95. {
  96. struct pmf_args args;
  97. u32 slew, done = 0;
  98. unsigned long timeout;
  99. slew = (speed_mode == CPUFREQ_LOW) ? 1 : 0;
  100. args.count = 1;
  101. args.u[0].p = &slew;
  102. pmf_call_one(pfunc_set_vdnap0, &args);
  103. /* It's an irq GPIO so we should be able to just block here,
  104. * I'll do that later after I've properly tested the IRQ code for
  105. * platform functions
  106. */
  107. timeout = jiffies + HZ/10;
  108. while(!time_after(jiffies, timeout)) {
  109. args.count = 1;
  110. args.u[0].p = &done;
  111. pmf_call_one(pfunc_vdnap0_complete, &args);
  112. if (done)
  113. break;
  114. usleep_range(1000, 1000);
  115. }
  116. if (done == 0)
  117. printk(KERN_WARNING "cpufreq: Timeout in clock slewing !\n");
  118. }
  119. /*
  120. * SCOM based frequency switching for 970FX rev3
  121. */
  122. static int g5_scom_switch_freq(int speed_mode)
  123. {
  124. unsigned long flags;
  125. int to;
  126. /* If frequency is going up, first ramp up the voltage */
  127. if (speed_mode < g5_pmode_cur)
  128. g5_switch_volt(speed_mode);
  129. local_irq_save(flags);
  130. /* Clear PCR high */
  131. scom970_write(SCOM_PCR, 0);
  132. /* Clear PCR low */
  133. scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
  134. /* Set PCR low */
  135. scom970_write(SCOM_PCR, PCR_HILO_SELECT |
  136. g5_pmode_data[speed_mode]);
  137. /* Wait for completion */
  138. for (to = 0; to < 10; to++) {
  139. unsigned long psr = scom970_read(SCOM_PSR);
  140. if ((psr & PSR_CMD_RECEIVED) == 0 &&
  141. (((psr >> PSR_CUR_SPEED_SHIFT) ^
  142. (g5_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
  143. == 0)
  144. break;
  145. if (psr & PSR_CMD_COMPLETED)
  146. break;
  147. udelay(100);
  148. }
  149. local_irq_restore(flags);
  150. /* If frequency is going down, last ramp the voltage */
  151. if (speed_mode > g5_pmode_cur)
  152. g5_switch_volt(speed_mode);
  153. g5_pmode_cur = speed_mode;
  154. ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul;
  155. return 0;
  156. }
  157. static int g5_scom_query_freq(void)
  158. {
  159. unsigned long psr = scom970_read(SCOM_PSR);
  160. int i;
  161. for (i = 0; i <= g5_pmode_max; i++)
  162. if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
  163. (g5_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
  164. break;
  165. return i;
  166. }
  167. /*
  168. * Fake voltage switching for platforms with missing support
  169. */
  170. static void g5_dummy_switch_volt(int speed_mode)
  171. {
  172. }
  173. #endif /* CONFIG_PMAC_SMU */
  174. /*
  175. * Platform function based voltage switching for PowerMac7,2 & 7,3
  176. */
  177. static struct pmf_function *pfunc_cpu0_volt_high;
  178. static struct pmf_function *pfunc_cpu0_volt_low;
  179. static struct pmf_function *pfunc_cpu1_volt_high;
  180. static struct pmf_function *pfunc_cpu1_volt_low;
  181. static void g5_pfunc_switch_volt(int speed_mode)
  182. {
  183. if (speed_mode == CPUFREQ_HIGH) {
  184. if (pfunc_cpu0_volt_high)
  185. pmf_call_one(pfunc_cpu0_volt_high, NULL);
  186. if (pfunc_cpu1_volt_high)
  187. pmf_call_one(pfunc_cpu1_volt_high, NULL);
  188. } else {
  189. if (pfunc_cpu0_volt_low)
  190. pmf_call_one(pfunc_cpu0_volt_low, NULL);
  191. if (pfunc_cpu1_volt_low)
  192. pmf_call_one(pfunc_cpu1_volt_low, NULL);
  193. }
  194. usleep_range(10000, 10000); /* should be faster , to fix */
  195. }
  196. /*
  197. * Platform function based frequency switching for PowerMac7,2 & 7,3
  198. */
  199. static struct pmf_function *pfunc_cpu_setfreq_high;
  200. static struct pmf_function *pfunc_cpu_setfreq_low;
  201. static struct pmf_function *pfunc_cpu_getfreq;
  202. static struct pmf_function *pfunc_slewing_done;
  203. static int g5_pfunc_switch_freq(int speed_mode)
  204. {
  205. struct pmf_args args;
  206. u32 done = 0;
  207. unsigned long timeout;
  208. int rc;
  209. DBG("g5_pfunc_switch_freq(%d)\n", speed_mode);
  210. /* If frequency is going up, first ramp up the voltage */
  211. if (speed_mode < g5_pmode_cur)
  212. g5_switch_volt(speed_mode);
  213. /* Do it */
  214. if (speed_mode == CPUFREQ_HIGH)
  215. rc = pmf_call_one(pfunc_cpu_setfreq_high, NULL);
  216. else
  217. rc = pmf_call_one(pfunc_cpu_setfreq_low, NULL);
  218. if (rc)
  219. printk(KERN_WARNING "cpufreq: pfunc switch error %d\n", rc);
  220. /* It's an irq GPIO so we should be able to just block here,
  221. * I'll do that later after I've properly tested the IRQ code for
  222. * platform functions
  223. */
  224. timeout = jiffies + HZ/10;
  225. while(!time_after(jiffies, timeout)) {
  226. args.count = 1;
  227. args.u[0].p = &done;
  228. pmf_call_one(pfunc_slewing_done, &args);
  229. if (done)
  230. break;
  231. usleep_range(500, 500);
  232. }
  233. if (done == 0)
  234. printk(KERN_WARNING "cpufreq: Timeout in clock slewing !\n");
  235. /* If frequency is going down, last ramp the voltage */
  236. if (speed_mode > g5_pmode_cur)
  237. g5_switch_volt(speed_mode);
  238. g5_pmode_cur = speed_mode;
  239. ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul;
  240. return 0;
  241. }
  242. static int g5_pfunc_query_freq(void)
  243. {
  244. struct pmf_args args;
  245. u32 val = 0;
  246. args.count = 1;
  247. args.u[0].p = &val;
  248. pmf_call_one(pfunc_cpu_getfreq, &args);
  249. return val ? CPUFREQ_HIGH : CPUFREQ_LOW;
  250. }
  251. /*
  252. * Common interface to the cpufreq core
  253. */
  254. static int g5_cpufreq_target(struct cpufreq_policy *policy, unsigned int index)
  255. {
  256. return g5_switch_freq(index);
  257. }
  258. static unsigned int g5_cpufreq_get_speed(unsigned int cpu)
  259. {
  260. return g5_cpu_freqs[g5_pmode_cur].frequency;
  261. }
  262. static int g5_cpufreq_cpu_init(struct cpufreq_policy *policy)
  263. {
  264. return cpufreq_generic_init(policy, g5_cpu_freqs, transition_latency);
  265. }
  266. static struct cpufreq_driver g5_cpufreq_driver = {
  267. .name = "powermac",
  268. .flags = CPUFREQ_CONST_LOOPS,
  269. .init = g5_cpufreq_cpu_init,
  270. .verify = cpufreq_generic_frequency_table_verify,
  271. .target_index = g5_cpufreq_target,
  272. .get = g5_cpufreq_get_speed,
  273. .attr = cpufreq_generic_attr,
  274. };
  275. #ifdef CONFIG_PMAC_SMU
  276. static int __init g5_neo2_cpufreq_init(struct device_node *cpunode)
  277. {
  278. unsigned int psize, ssize;
  279. unsigned long max_freq;
  280. char *freq_method, *volt_method;
  281. const u32 *valp;
  282. u32 pvr_hi;
  283. int use_volts_vdnap = 0;
  284. int use_volts_smu = 0;
  285. int rc = -ENODEV;
  286. /* Check supported platforms */
  287. if (of_machine_is_compatible("PowerMac8,1") ||
  288. of_machine_is_compatible("PowerMac8,2") ||
  289. of_machine_is_compatible("PowerMac9,1") ||
  290. of_machine_is_compatible("PowerMac12,1"))
  291. use_volts_smu = 1;
  292. else if (of_machine_is_compatible("PowerMac11,2"))
  293. use_volts_vdnap = 1;
  294. else
  295. return -ENODEV;
  296. /* Check 970FX for now */
  297. valp = of_get_property(cpunode, "cpu-version", NULL);
  298. if (!valp) {
  299. DBG("No cpu-version property !\n");
  300. goto bail_noprops;
  301. }
  302. pvr_hi = (*valp) >> 16;
  303. if (pvr_hi != 0x3c && pvr_hi != 0x44) {
  304. printk(KERN_ERR "cpufreq: Unsupported CPU version\n");
  305. goto bail_noprops;
  306. }
  307. /* Look for the powertune data in the device-tree */
  308. g5_pmode_data = of_get_property(cpunode, "power-mode-data",&psize);
  309. if (!g5_pmode_data) {
  310. DBG("No power-mode-data !\n");
  311. goto bail_noprops;
  312. }
  313. g5_pmode_max = psize / sizeof(u32) - 1;
  314. if (use_volts_smu) {
  315. const struct smu_sdbp_header *shdr;
  316. /* Look for the FVT table */
  317. shdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  318. if (!shdr)
  319. goto bail_noprops;
  320. g5_fvt_table = (struct smu_sdbp_fvt *)&shdr[1];
  321. ssize = (shdr->len * sizeof(u32)) - sizeof(*shdr);
  322. g5_fvt_count = ssize / sizeof(*g5_fvt_table);
  323. g5_fvt_cur = 0;
  324. /* Sanity checking */
  325. if (g5_fvt_count < 1 || g5_pmode_max < 1)
  326. goto bail_noprops;
  327. g5_switch_volt = g5_smu_switch_volt;
  328. volt_method = "SMU";
  329. } else if (use_volts_vdnap) {
  330. struct device_node *root;
  331. root = of_find_node_by_path("/");
  332. if (root == NULL) {
  333. printk(KERN_ERR "cpufreq: Can't find root of "
  334. "device tree\n");
  335. goto bail_noprops;
  336. }
  337. pfunc_set_vdnap0 = pmf_find_function(root, "set-vdnap0");
  338. pfunc_vdnap0_complete =
  339. pmf_find_function(root, "slewing-done");
  340. if (pfunc_set_vdnap0 == NULL ||
  341. pfunc_vdnap0_complete == NULL) {
  342. printk(KERN_ERR "cpufreq: Can't find required "
  343. "platform function\n");
  344. goto bail_noprops;
  345. }
  346. g5_switch_volt = g5_vdnap_switch_volt;
  347. volt_method = "GPIO";
  348. } else {
  349. g5_switch_volt = g5_dummy_switch_volt;
  350. volt_method = "none";
  351. }
  352. /*
  353. * From what I see, clock-frequency is always the maximal frequency.
  354. * The current driver can not slew sysclk yet, so we really only deal
  355. * with powertune steps for now. We also only implement full freq and
  356. * half freq in this version. So far, I haven't yet seen a machine
  357. * supporting anything else.
  358. */
  359. valp = of_get_property(cpunode, "clock-frequency", NULL);
  360. if (!valp)
  361. return -ENODEV;
  362. max_freq = (*valp)/1000;
  363. g5_cpu_freqs[0].frequency = max_freq;
  364. g5_cpu_freqs[1].frequency = max_freq/2;
  365. /* Set callbacks */
  366. transition_latency = 12000;
  367. g5_switch_freq = g5_scom_switch_freq;
  368. g5_query_freq = g5_scom_query_freq;
  369. freq_method = "SCOM";
  370. /* Force apply current frequency to make sure everything is in
  371. * sync (voltage is right for example). Firmware may leave us with
  372. * a strange setting ...
  373. */
  374. g5_switch_volt(CPUFREQ_HIGH);
  375. msleep(10);
  376. g5_pmode_cur = -1;
  377. g5_switch_freq(g5_query_freq());
  378. printk(KERN_INFO "Registering G5 CPU frequency driver\n");
  379. printk(KERN_INFO "Frequency method: %s, Voltage method: %s\n",
  380. freq_method, volt_method);
  381. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  382. g5_cpu_freqs[1].frequency/1000,
  383. g5_cpu_freqs[0].frequency/1000,
  384. g5_cpu_freqs[g5_pmode_cur].frequency/1000);
  385. rc = cpufreq_register_driver(&g5_cpufreq_driver);
  386. /* We keep the CPU node on hold... hopefully, Apple G5 don't have
  387. * hotplug CPU with a dynamic device-tree ...
  388. */
  389. return rc;
  390. bail_noprops:
  391. of_node_put(cpunode);
  392. return rc;
  393. }
  394. #endif /* CONFIG_PMAC_SMU */
  395. static int __init g5_pm72_cpufreq_init(struct device_node *cpunode)
  396. {
  397. struct device_node *cpuid = NULL, *hwclock = NULL;
  398. const u8 *eeprom = NULL;
  399. const u32 *valp;
  400. u64 max_freq, min_freq, ih, il;
  401. int has_volt = 1, rc = 0;
  402. DBG("cpufreq: Initializing for PowerMac7,2, PowerMac7,3 and"
  403. " RackMac3,1...\n");
  404. /* Lookup the cpuid eeprom node */
  405. cpuid = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/cpuid@a0");
  406. if (cpuid != NULL)
  407. eeprom = of_get_property(cpuid, "cpuid", NULL);
  408. if (eeprom == NULL) {
  409. printk(KERN_ERR "cpufreq: Can't find cpuid EEPROM !\n");
  410. rc = -ENODEV;
  411. goto bail;
  412. }
  413. /* Lookup the i2c hwclock */
  414. for_each_node_by_name(hwclock, "i2c-hwclock") {
  415. const char *loc = of_get_property(hwclock,
  416. "hwctrl-location", NULL);
  417. if (loc == NULL)
  418. continue;
  419. if (strcmp(loc, "CPU CLOCK"))
  420. continue;
  421. if (!of_get_property(hwclock, "platform-get-frequency", NULL))
  422. continue;
  423. break;
  424. }
  425. if (hwclock == NULL) {
  426. printk(KERN_ERR "cpufreq: Can't find i2c clock chip !\n");
  427. rc = -ENODEV;
  428. goto bail;
  429. }
  430. DBG("cpufreq: i2c clock chip found: %s\n", hwclock->full_name);
  431. /* Now get all the platform functions */
  432. pfunc_cpu_getfreq =
  433. pmf_find_function(hwclock, "get-frequency");
  434. pfunc_cpu_setfreq_high =
  435. pmf_find_function(hwclock, "set-frequency-high");
  436. pfunc_cpu_setfreq_low =
  437. pmf_find_function(hwclock, "set-frequency-low");
  438. pfunc_slewing_done =
  439. pmf_find_function(hwclock, "slewing-done");
  440. pfunc_cpu0_volt_high =
  441. pmf_find_function(hwclock, "set-voltage-high-0");
  442. pfunc_cpu0_volt_low =
  443. pmf_find_function(hwclock, "set-voltage-low-0");
  444. pfunc_cpu1_volt_high =
  445. pmf_find_function(hwclock, "set-voltage-high-1");
  446. pfunc_cpu1_volt_low =
  447. pmf_find_function(hwclock, "set-voltage-low-1");
  448. /* Check we have minimum requirements */
  449. if (pfunc_cpu_getfreq == NULL || pfunc_cpu_setfreq_high == NULL ||
  450. pfunc_cpu_setfreq_low == NULL || pfunc_slewing_done == NULL) {
  451. printk(KERN_ERR "cpufreq: Can't find platform functions !\n");
  452. rc = -ENODEV;
  453. goto bail;
  454. }
  455. /* Check that we have complete sets */
  456. if (pfunc_cpu0_volt_high == NULL || pfunc_cpu0_volt_low == NULL) {
  457. pmf_put_function(pfunc_cpu0_volt_high);
  458. pmf_put_function(pfunc_cpu0_volt_low);
  459. pfunc_cpu0_volt_high = pfunc_cpu0_volt_low = NULL;
  460. has_volt = 0;
  461. }
  462. if (!has_volt ||
  463. pfunc_cpu1_volt_high == NULL || pfunc_cpu1_volt_low == NULL) {
  464. pmf_put_function(pfunc_cpu1_volt_high);
  465. pmf_put_function(pfunc_cpu1_volt_low);
  466. pfunc_cpu1_volt_high = pfunc_cpu1_volt_low = NULL;
  467. }
  468. /* Note: The device tree also contains a "platform-set-values"
  469. * function for which I haven't quite figured out the usage. It
  470. * might have to be called on init and/or wakeup, I'm not too sure
  471. * but things seem to work fine without it so far ...
  472. */
  473. /* Get max frequency from device-tree */
  474. valp = of_get_property(cpunode, "clock-frequency", NULL);
  475. if (!valp) {
  476. printk(KERN_ERR "cpufreq: Can't find CPU frequency !\n");
  477. rc = -ENODEV;
  478. goto bail;
  479. }
  480. max_freq = (*valp)/1000;
  481. /* Now calculate reduced frequency by using the cpuid input freq
  482. * ratio. This requires 64 bits math unless we are willing to lose
  483. * some precision
  484. */
  485. ih = *((u32 *)(eeprom + 0x10));
  486. il = *((u32 *)(eeprom + 0x20));
  487. /* Check for machines with no useful settings */
  488. if (il == ih) {
  489. printk(KERN_WARNING "cpufreq: No low frequency mode available"
  490. " on this model !\n");
  491. rc = -ENODEV;
  492. goto bail;
  493. }
  494. min_freq = 0;
  495. if (ih != 0 && il != 0)
  496. min_freq = (max_freq * il) / ih;
  497. /* Sanity check */
  498. if (min_freq >= max_freq || min_freq < 1000) {
  499. printk(KERN_ERR "cpufreq: Can't calculate low frequency !\n");
  500. rc = -ENXIO;
  501. goto bail;
  502. }
  503. g5_cpu_freqs[0].frequency = max_freq;
  504. g5_cpu_freqs[1].frequency = min_freq;
  505. /* Based on a measurement on Xserve G5, rounded up. */
  506. transition_latency = 10 * NSEC_PER_MSEC;
  507. /* Set callbacks */
  508. g5_switch_volt = g5_pfunc_switch_volt;
  509. g5_switch_freq = g5_pfunc_switch_freq;
  510. g5_query_freq = g5_pfunc_query_freq;
  511. /* Force apply current frequency to make sure everything is in
  512. * sync (voltage is right for example). Firmware may leave us with
  513. * a strange setting ...
  514. */
  515. g5_switch_volt(CPUFREQ_HIGH);
  516. msleep(10);
  517. g5_pmode_cur = -1;
  518. g5_switch_freq(g5_query_freq());
  519. printk(KERN_INFO "Registering G5 CPU frequency driver\n");
  520. printk(KERN_INFO "Frequency method: i2c/pfunc, "
  521. "Voltage method: %s\n", has_volt ? "i2c/pfunc" : "none");
  522. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  523. g5_cpu_freqs[1].frequency/1000,
  524. g5_cpu_freqs[0].frequency/1000,
  525. g5_cpu_freqs[g5_pmode_cur].frequency/1000);
  526. rc = cpufreq_register_driver(&g5_cpufreq_driver);
  527. bail:
  528. if (rc != 0) {
  529. pmf_put_function(pfunc_cpu_getfreq);
  530. pmf_put_function(pfunc_cpu_setfreq_high);
  531. pmf_put_function(pfunc_cpu_setfreq_low);
  532. pmf_put_function(pfunc_slewing_done);
  533. pmf_put_function(pfunc_cpu0_volt_high);
  534. pmf_put_function(pfunc_cpu0_volt_low);
  535. pmf_put_function(pfunc_cpu1_volt_high);
  536. pmf_put_function(pfunc_cpu1_volt_low);
  537. }
  538. of_node_put(hwclock);
  539. of_node_put(cpuid);
  540. of_node_put(cpunode);
  541. return rc;
  542. }
  543. static int __init g5_cpufreq_init(void)
  544. {
  545. struct device_node *cpunode;
  546. int rc = 0;
  547. /* Get first CPU node */
  548. cpunode = of_cpu_device_node_get(0);
  549. if (cpunode == NULL) {
  550. pr_err("cpufreq: Can't find any CPU node\n");
  551. return -ENODEV;
  552. }
  553. if (of_machine_is_compatible("PowerMac7,2") ||
  554. of_machine_is_compatible("PowerMac7,3") ||
  555. of_machine_is_compatible("RackMac3,1"))
  556. rc = g5_pm72_cpufreq_init(cpunode);
  557. #ifdef CONFIG_PMAC_SMU
  558. else
  559. rc = g5_neo2_cpufreq_init(cpunode);
  560. #endif /* CONFIG_PMAC_SMU */
  561. return rc;
  562. }
  563. module_init(g5_cpufreq_init);
  564. MODULE_LICENSE("GPL");