cpufreq_64.c 19 KB

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