pmac64-cpufreq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. {CPUFREQ_HIGH, 0},
  60. {CPUFREQ_LOW, 0},
  61. {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 DEFINE_MUTEX(g5_switch_mutex);
  71. static unsigned long transition_latency;
  72. #ifdef CONFIG_PMAC_SMU
  73. static const u32 *g5_pmode_data;
  74. static int g5_pmode_max;
  75. static struct smu_sdbp_fvt *g5_fvt_table; /* table of op. points */
  76. static int g5_fvt_count; /* number of op. points */
  77. static int g5_fvt_cur; /* current op. point */
  78. /*
  79. * SMU based voltage switching for Neo2 platforms
  80. */
  81. static void g5_smu_switch_volt(int speed_mode)
  82. {
  83. struct smu_simple_cmd cmd;
  84. DECLARE_COMPLETION_ONSTACK(comp);
  85. smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, smu_done_complete,
  86. &comp, 'V', 'S', 'L', 'E', 'W',
  87. 0xff, g5_fvt_cur+1, speed_mode);
  88. wait_for_completion(&comp);
  89. }
  90. /*
  91. * Platform function based voltage/vdnap switching for Neo2
  92. */
  93. static struct pmf_function *pfunc_set_vdnap0;
  94. static struct pmf_function *pfunc_vdnap0_complete;
  95. static void g5_vdnap_switch_volt(int speed_mode)
  96. {
  97. struct pmf_args args;
  98. u32 slew, done = 0;
  99. unsigned long timeout;
  100. slew = (speed_mode == CPUFREQ_LOW) ? 1 : 0;
  101. args.count = 1;
  102. args.u[0].p = &slew;
  103. pmf_call_one(pfunc_set_vdnap0, &args);
  104. /* It's an irq GPIO so we should be able to just block here,
  105. * I'll do that later after I've properly tested the IRQ code for
  106. * platform functions
  107. */
  108. timeout = jiffies + HZ/10;
  109. while(!time_after(jiffies, timeout)) {
  110. args.count = 1;
  111. args.u[0].p = &done;
  112. pmf_call_one(pfunc_vdnap0_complete, &args);
  113. if (done)
  114. break;
  115. msleep(1);
  116. }
  117. if (done == 0)
  118. printk(KERN_WARNING "cpufreq: Timeout in clock slewing !\n");
  119. }
  120. /*
  121. * SCOM based frequency switching for 970FX rev3
  122. */
  123. static int g5_scom_switch_freq(int speed_mode)
  124. {
  125. unsigned long flags;
  126. int to;
  127. /* If frequency is going up, first ramp up the voltage */
  128. if (speed_mode < g5_pmode_cur)
  129. g5_switch_volt(speed_mode);
  130. local_irq_save(flags);
  131. /* Clear PCR high */
  132. scom970_write(SCOM_PCR, 0);
  133. /* Clear PCR low */
  134. scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
  135. /* Set PCR low */
  136. scom970_write(SCOM_PCR, PCR_HILO_SELECT |
  137. g5_pmode_data[speed_mode]);
  138. /* Wait for completion */
  139. for (to = 0; to < 10; to++) {
  140. unsigned long psr = scom970_read(SCOM_PSR);
  141. if ((psr & PSR_CMD_RECEIVED) == 0 &&
  142. (((psr >> PSR_CUR_SPEED_SHIFT) ^
  143. (g5_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
  144. == 0)
  145. break;
  146. if (psr & PSR_CMD_COMPLETED)
  147. break;
  148. udelay(100);
  149. }
  150. local_irq_restore(flags);
  151. /* If frequency is going down, last ramp the voltage */
  152. if (speed_mode > g5_pmode_cur)
  153. g5_switch_volt(speed_mode);
  154. g5_pmode_cur = speed_mode;
  155. ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul;
  156. return 0;
  157. }
  158. static int g5_scom_query_freq(void)
  159. {
  160. unsigned long psr = scom970_read(SCOM_PSR);
  161. int i;
  162. for (i = 0; i <= g5_pmode_max; i++)
  163. if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
  164. (g5_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
  165. break;
  166. return i;
  167. }
  168. /*
  169. * Fake voltage switching for platforms with missing support
  170. */
  171. static void g5_dummy_switch_volt(int speed_mode)
  172. {
  173. }
  174. #endif /* CONFIG_PMAC_SMU */
  175. /*
  176. * Platform function based voltage switching for PowerMac7,2 & 7,3
  177. */
  178. static struct pmf_function *pfunc_cpu0_volt_high;
  179. static struct pmf_function *pfunc_cpu0_volt_low;
  180. static struct pmf_function *pfunc_cpu1_volt_high;
  181. static struct pmf_function *pfunc_cpu1_volt_low;
  182. static void g5_pfunc_switch_volt(int speed_mode)
  183. {
  184. if (speed_mode == CPUFREQ_HIGH) {
  185. if (pfunc_cpu0_volt_high)
  186. pmf_call_one(pfunc_cpu0_volt_high, NULL);
  187. if (pfunc_cpu1_volt_high)
  188. pmf_call_one(pfunc_cpu1_volt_high, NULL);
  189. } else {
  190. if (pfunc_cpu0_volt_low)
  191. pmf_call_one(pfunc_cpu0_volt_low, NULL);
  192. if (pfunc_cpu1_volt_low)
  193. pmf_call_one(pfunc_cpu1_volt_low, NULL);
  194. }
  195. msleep(10); /* should be faster , to fix */
  196. }
  197. /*
  198. * Platform function based frequency switching for PowerMac7,2 & 7,3
  199. */
  200. static struct pmf_function *pfunc_cpu_setfreq_high;
  201. static struct pmf_function *pfunc_cpu_setfreq_low;
  202. static struct pmf_function *pfunc_cpu_getfreq;
  203. static struct pmf_function *pfunc_slewing_done;
  204. static int g5_pfunc_switch_freq(int speed_mode)
  205. {
  206. struct pmf_args args;
  207. u32 done = 0;
  208. unsigned long timeout;
  209. int rc;
  210. DBG("g5_pfunc_switch_freq(%d)\n", speed_mode);
  211. /* If frequency is going up, first ramp up the voltage */
  212. if (speed_mode < g5_pmode_cur)
  213. g5_switch_volt(speed_mode);
  214. /* Do it */
  215. if (speed_mode == CPUFREQ_HIGH)
  216. rc = pmf_call_one(pfunc_cpu_setfreq_high, NULL);
  217. else
  218. rc = pmf_call_one(pfunc_cpu_setfreq_low, NULL);
  219. if (rc)
  220. printk(KERN_WARNING "cpufreq: pfunc switch error %d\n", rc);
  221. /* It's an irq GPIO so we should be able to just block here,
  222. * I'll do that later after I've properly tested the IRQ code for
  223. * platform functions
  224. */
  225. timeout = jiffies + HZ/10;
  226. while(!time_after(jiffies, timeout)) {
  227. args.count = 1;
  228. args.u[0].p = &done;
  229. pmf_call_one(pfunc_slewing_done, &args);
  230. if (done)
  231. break;
  232. msleep(1);
  233. }
  234. if (done == 0)
  235. printk(KERN_WARNING "cpufreq: Timeout in clock slewing !\n");
  236. /* If frequency is going down, last ramp the voltage */
  237. if (speed_mode > g5_pmode_cur)
  238. g5_switch_volt(speed_mode);
  239. g5_pmode_cur = speed_mode;
  240. ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul;
  241. return 0;
  242. }
  243. static int g5_pfunc_query_freq(void)
  244. {
  245. struct pmf_args args;
  246. u32 val = 0;
  247. args.count = 1;
  248. args.u[0].p = &val;
  249. pmf_call_one(pfunc_cpu_getfreq, &args);
  250. return val ? CPUFREQ_HIGH : CPUFREQ_LOW;
  251. }
  252. /*
  253. * Common interface to the cpufreq core
  254. */
  255. static int g5_cpufreq_target(struct cpufreq_policy *policy,
  256. unsigned int target_freq, unsigned int relation)
  257. {
  258. unsigned int newstate = 0;
  259. struct cpufreq_freqs freqs;
  260. int rc;
  261. if (cpufreq_frequency_table_target(policy, g5_cpu_freqs,
  262. target_freq, relation, &newstate))
  263. return -EINVAL;
  264. if (g5_pmode_cur == newstate)
  265. return 0;
  266. mutex_lock(&g5_switch_mutex);
  267. freqs.old = g5_cpu_freqs[g5_pmode_cur].frequency;
  268. freqs.new = g5_cpu_freqs[newstate].frequency;
  269. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  270. rc = g5_switch_freq(newstate);
  271. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  272. mutex_unlock(&g5_switch_mutex);
  273. return rc;
  274. }
  275. static unsigned int g5_cpufreq_get_speed(unsigned int cpu)
  276. {
  277. return g5_cpu_freqs[g5_pmode_cur].frequency;
  278. }
  279. static int g5_cpufreq_cpu_init(struct cpufreq_policy *policy)
  280. {
  281. policy->cpuinfo.transition_latency = transition_latency;
  282. policy->cur = g5_cpu_freqs[g5_query_freq()].frequency;
  283. /* secondary CPUs are tied to the primary one by the
  284. * cpufreq core if in the secondary policy we tell it that
  285. * it actually must be one policy together with all others. */
  286. cpumask_copy(policy->cpus, cpu_online_mask);
  287. return cpufreq_table_validate_and_show(policy, g5_cpu_freqs);
  288. }
  289. static struct cpufreq_driver g5_cpufreq_driver = {
  290. .name = "powermac",
  291. .flags = CPUFREQ_CONST_LOOPS,
  292. .init = g5_cpufreq_cpu_init,
  293. .verify = cpufreq_generic_frequency_table_verify,
  294. .target = g5_cpufreq_target,
  295. .get = g5_cpufreq_get_speed,
  296. .attr = cpufreq_generic_attr,
  297. };
  298. #ifdef CONFIG_PMAC_SMU
  299. static int __init g5_neo2_cpufreq_init(struct device_node *cpunode)
  300. {
  301. unsigned int psize, ssize;
  302. unsigned long max_freq;
  303. char *freq_method, *volt_method;
  304. const u32 *valp;
  305. u32 pvr_hi;
  306. int use_volts_vdnap = 0;
  307. int use_volts_smu = 0;
  308. int rc = -ENODEV;
  309. /* Check supported platforms */
  310. if (of_machine_is_compatible("PowerMac8,1") ||
  311. of_machine_is_compatible("PowerMac8,2") ||
  312. of_machine_is_compatible("PowerMac9,1"))
  313. use_volts_smu = 1;
  314. else if (of_machine_is_compatible("PowerMac11,2"))
  315. use_volts_vdnap = 1;
  316. else
  317. return -ENODEV;
  318. /* Check 970FX for now */
  319. valp = of_get_property(cpunode, "cpu-version", NULL);
  320. if (!valp) {
  321. DBG("No cpu-version property !\n");
  322. goto bail_noprops;
  323. }
  324. pvr_hi = (*valp) >> 16;
  325. if (pvr_hi != 0x3c && pvr_hi != 0x44) {
  326. printk(KERN_ERR "cpufreq: Unsupported CPU version\n");
  327. goto bail_noprops;
  328. }
  329. /* Look for the powertune data in the device-tree */
  330. g5_pmode_data = of_get_property(cpunode, "power-mode-data",&psize);
  331. if (!g5_pmode_data) {
  332. DBG("No power-mode-data !\n");
  333. goto bail_noprops;
  334. }
  335. g5_pmode_max = psize / sizeof(u32) - 1;
  336. if (use_volts_smu) {
  337. const struct smu_sdbp_header *shdr;
  338. /* Look for the FVT table */
  339. shdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  340. if (!shdr)
  341. goto bail_noprops;
  342. g5_fvt_table = (struct smu_sdbp_fvt *)&shdr[1];
  343. ssize = (shdr->len * sizeof(u32)) - sizeof(*shdr);
  344. g5_fvt_count = ssize / sizeof(*g5_fvt_table);
  345. g5_fvt_cur = 0;
  346. /* Sanity checking */
  347. if (g5_fvt_count < 1 || g5_pmode_max < 1)
  348. goto bail_noprops;
  349. g5_switch_volt = g5_smu_switch_volt;
  350. volt_method = "SMU";
  351. } else if (use_volts_vdnap) {
  352. struct device_node *root;
  353. root = of_find_node_by_path("/");
  354. if (root == NULL) {
  355. printk(KERN_ERR "cpufreq: Can't find root of "
  356. "device tree\n");
  357. goto bail_noprops;
  358. }
  359. pfunc_set_vdnap0 = pmf_find_function(root, "set-vdnap0");
  360. pfunc_vdnap0_complete =
  361. pmf_find_function(root, "slewing-done");
  362. if (pfunc_set_vdnap0 == NULL ||
  363. pfunc_vdnap0_complete == NULL) {
  364. printk(KERN_ERR "cpufreq: Can't find required "
  365. "platform function\n");
  366. goto bail_noprops;
  367. }
  368. g5_switch_volt = g5_vdnap_switch_volt;
  369. volt_method = "GPIO";
  370. } else {
  371. g5_switch_volt = g5_dummy_switch_volt;
  372. volt_method = "none";
  373. }
  374. /*
  375. * From what I see, clock-frequency is always the maximal frequency.
  376. * The current driver can not slew sysclk yet, so we really only deal
  377. * with powertune steps for now. We also only implement full freq and
  378. * half freq in this version. So far, I haven't yet seen a machine
  379. * supporting anything else.
  380. */
  381. valp = of_get_property(cpunode, "clock-frequency", NULL);
  382. if (!valp)
  383. return -ENODEV;
  384. max_freq = (*valp)/1000;
  385. g5_cpu_freqs[0].frequency = max_freq;
  386. g5_cpu_freqs[1].frequency = max_freq/2;
  387. /* Set callbacks */
  388. transition_latency = 12000;
  389. g5_switch_freq = g5_scom_switch_freq;
  390. g5_query_freq = g5_scom_query_freq;
  391. freq_method = "SCOM";
  392. /* Force apply current frequency to make sure everything is in
  393. * sync (voltage is right for example). Firmware may leave us with
  394. * a strange setting ...
  395. */
  396. g5_switch_volt(CPUFREQ_HIGH);
  397. msleep(10);
  398. g5_pmode_cur = -1;
  399. g5_switch_freq(g5_query_freq());
  400. printk(KERN_INFO "Registering G5 CPU frequency driver\n");
  401. printk(KERN_INFO "Frequency method: %s, Voltage method: %s\n",
  402. freq_method, volt_method);
  403. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  404. g5_cpu_freqs[1].frequency/1000,
  405. g5_cpu_freqs[0].frequency/1000,
  406. g5_cpu_freqs[g5_pmode_cur].frequency/1000);
  407. rc = cpufreq_register_driver(&g5_cpufreq_driver);
  408. /* We keep the CPU node on hold... hopefully, Apple G5 don't have
  409. * hotplug CPU with a dynamic device-tree ...
  410. */
  411. return rc;
  412. bail_noprops:
  413. of_node_put(cpunode);
  414. return rc;
  415. }
  416. #endif /* CONFIG_PMAC_SMU */
  417. static int __init g5_pm72_cpufreq_init(struct device_node *cpunode)
  418. {
  419. struct device_node *cpuid = NULL, *hwclock = NULL;
  420. const u8 *eeprom = NULL;
  421. const u32 *valp;
  422. u64 max_freq, min_freq, ih, il;
  423. int has_volt = 1, rc = 0;
  424. DBG("cpufreq: Initializing for PowerMac7,2, PowerMac7,3 and"
  425. " RackMac3,1...\n");
  426. /* Lookup the cpuid eeprom node */
  427. cpuid = of_find_node_by_path("/u3@0,f8000000/i2c@f8001000/cpuid@a0");
  428. if (cpuid != NULL)
  429. eeprom = of_get_property(cpuid, "cpuid", NULL);
  430. if (eeprom == NULL) {
  431. printk(KERN_ERR "cpufreq: Can't find cpuid EEPROM !\n");
  432. rc = -ENODEV;
  433. goto bail;
  434. }
  435. /* Lookup the i2c hwclock */
  436. for (hwclock = NULL;
  437. (hwclock = of_find_node_by_name(hwclock, "i2c-hwclock")) != NULL;){
  438. const char *loc = of_get_property(hwclock,
  439. "hwctrl-location", NULL);
  440. if (loc == NULL)
  441. continue;
  442. if (strcmp(loc, "CPU CLOCK"))
  443. continue;
  444. if (!of_get_property(hwclock, "platform-get-frequency", NULL))
  445. continue;
  446. break;
  447. }
  448. if (hwclock == NULL) {
  449. printk(KERN_ERR "cpufreq: Can't find i2c clock chip !\n");
  450. rc = -ENODEV;
  451. goto bail;
  452. }
  453. DBG("cpufreq: i2c clock chip found: %s\n", hwclock->full_name);
  454. /* Now get all the platform functions */
  455. pfunc_cpu_getfreq =
  456. pmf_find_function(hwclock, "get-frequency");
  457. pfunc_cpu_setfreq_high =
  458. pmf_find_function(hwclock, "set-frequency-high");
  459. pfunc_cpu_setfreq_low =
  460. pmf_find_function(hwclock, "set-frequency-low");
  461. pfunc_slewing_done =
  462. pmf_find_function(hwclock, "slewing-done");
  463. pfunc_cpu0_volt_high =
  464. pmf_find_function(hwclock, "set-voltage-high-0");
  465. pfunc_cpu0_volt_low =
  466. pmf_find_function(hwclock, "set-voltage-low-0");
  467. pfunc_cpu1_volt_high =
  468. pmf_find_function(hwclock, "set-voltage-high-1");
  469. pfunc_cpu1_volt_low =
  470. pmf_find_function(hwclock, "set-voltage-low-1");
  471. /* Check we have minimum requirements */
  472. if (pfunc_cpu_getfreq == NULL || pfunc_cpu_setfreq_high == NULL ||
  473. pfunc_cpu_setfreq_low == NULL || pfunc_slewing_done == NULL) {
  474. printk(KERN_ERR "cpufreq: Can't find platform functions !\n");
  475. rc = -ENODEV;
  476. goto bail;
  477. }
  478. /* Check that we have complete sets */
  479. if (pfunc_cpu0_volt_high == NULL || pfunc_cpu0_volt_low == NULL) {
  480. pmf_put_function(pfunc_cpu0_volt_high);
  481. pmf_put_function(pfunc_cpu0_volt_low);
  482. pfunc_cpu0_volt_high = pfunc_cpu0_volt_low = NULL;
  483. has_volt = 0;
  484. }
  485. if (!has_volt ||
  486. pfunc_cpu1_volt_high == NULL || pfunc_cpu1_volt_low == NULL) {
  487. pmf_put_function(pfunc_cpu1_volt_high);
  488. pmf_put_function(pfunc_cpu1_volt_low);
  489. pfunc_cpu1_volt_high = pfunc_cpu1_volt_low = NULL;
  490. }
  491. /* Note: The device tree also contains a "platform-set-values"
  492. * function for which I haven't quite figured out the usage. It
  493. * might have to be called on init and/or wakeup, I'm not too sure
  494. * but things seem to work fine without it so far ...
  495. */
  496. /* Get max frequency from device-tree */
  497. valp = of_get_property(cpunode, "clock-frequency", NULL);
  498. if (!valp) {
  499. printk(KERN_ERR "cpufreq: Can't find CPU frequency !\n");
  500. rc = -ENODEV;
  501. goto bail;
  502. }
  503. max_freq = (*valp)/1000;
  504. /* Now calculate reduced frequency by using the cpuid input freq
  505. * ratio. This requires 64 bits math unless we are willing to lose
  506. * some precision
  507. */
  508. ih = *((u32 *)(eeprom + 0x10));
  509. il = *((u32 *)(eeprom + 0x20));
  510. /* Check for machines with no useful settings */
  511. if (il == ih) {
  512. printk(KERN_WARNING "cpufreq: No low frequency mode available"
  513. " on this model !\n");
  514. rc = -ENODEV;
  515. goto bail;
  516. }
  517. min_freq = 0;
  518. if (ih != 0 && il != 0)
  519. min_freq = (max_freq * il) / ih;
  520. /* Sanity check */
  521. if (min_freq >= max_freq || min_freq < 1000) {
  522. printk(KERN_ERR "cpufreq: Can't calculate low frequency !\n");
  523. rc = -ENXIO;
  524. goto bail;
  525. }
  526. g5_cpu_freqs[0].frequency = max_freq;
  527. g5_cpu_freqs[1].frequency = min_freq;
  528. /* Set callbacks */
  529. transition_latency = CPUFREQ_ETERNAL;
  530. g5_switch_volt = g5_pfunc_switch_volt;
  531. g5_switch_freq = g5_pfunc_switch_freq;
  532. g5_query_freq = g5_pfunc_query_freq;
  533. /* Force apply current frequency to make sure everything is in
  534. * sync (voltage is right for example). Firmware may leave us with
  535. * a strange setting ...
  536. */
  537. g5_switch_volt(CPUFREQ_HIGH);
  538. msleep(10);
  539. g5_pmode_cur = -1;
  540. g5_switch_freq(g5_query_freq());
  541. printk(KERN_INFO "Registering G5 CPU frequency driver\n");
  542. printk(KERN_INFO "Frequency method: i2c/pfunc, "
  543. "Voltage method: %s\n", has_volt ? "i2c/pfunc" : "none");
  544. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  545. g5_cpu_freqs[1].frequency/1000,
  546. g5_cpu_freqs[0].frequency/1000,
  547. g5_cpu_freqs[g5_pmode_cur].frequency/1000);
  548. rc = cpufreq_register_driver(&g5_cpufreq_driver);
  549. bail:
  550. if (rc != 0) {
  551. pmf_put_function(pfunc_cpu_getfreq);
  552. pmf_put_function(pfunc_cpu_setfreq_high);
  553. pmf_put_function(pfunc_cpu_setfreq_low);
  554. pmf_put_function(pfunc_slewing_done);
  555. pmf_put_function(pfunc_cpu0_volt_high);
  556. pmf_put_function(pfunc_cpu0_volt_low);
  557. pmf_put_function(pfunc_cpu1_volt_high);
  558. pmf_put_function(pfunc_cpu1_volt_low);
  559. }
  560. of_node_put(hwclock);
  561. of_node_put(cpuid);
  562. of_node_put(cpunode);
  563. return rc;
  564. }
  565. static int __init g5_cpufreq_init(void)
  566. {
  567. struct device_node *cpunode;
  568. int rc = 0;
  569. /* Get first CPU node */
  570. cpunode = of_cpu_device_node_get(0);
  571. if (cpunode == NULL) {
  572. pr_err("cpufreq: Can't find any CPU node\n");
  573. return -ENODEV;
  574. }
  575. if (of_machine_is_compatible("PowerMac7,2") ||
  576. of_machine_is_compatible("PowerMac7,3") ||
  577. of_machine_is_compatible("RackMac3,1"))
  578. rc = g5_pm72_cpufreq_init(cpunode);
  579. #ifdef CONFIG_PMAC_SMU
  580. else
  581. rc = g5_neo2_cpufreq_init(cpunode);
  582. #endif /* CONFIG_PMAC_SMU */
  583. return rc;
  584. }
  585. module_init(g5_cpufreq_init);
  586. MODULE_LICENSE("GPL");