cpufreq_64.c 19 KB

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