cpufreq_64.c 20 KB

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