/arch/ppc/platforms/pmac_cpufreq.c

https://bitbucket.org/evzijst/gittest · C · 571 lines · 346 code · 95 blank · 130 comment · 69 complexity · ce19c6396c13f1bc0de1688a84befb1a MD5 · raw file

  1. /*
  2. * arch/ppc/platforms/pmac_cpufreq.c
  3. *
  4. * Copyright (C) 2002 - 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  5. * Copyright (C) 2004 John Steele Scott <toojays@toojays.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  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/adb.h>
  20. #include <linux/pmu.h>
  21. #include <linux/slab.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/init.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/i2c.h>
  26. #include <linux/hardirq.h>
  27. #include <asm/prom.h>
  28. #include <asm/machdep.h>
  29. #include <asm/irq.h>
  30. #include <asm/pmac_feature.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/sections.h>
  33. #include <asm/cputable.h>
  34. #include <asm/time.h>
  35. #include <asm/system.h>
  36. #include <asm/open_pic.h>
  37. /* WARNING !!! This will cause calibrate_delay() to be called,
  38. * but this is an __init function ! So you MUST go edit
  39. * init/main.c to make it non-init before enabling DEBUG_FREQ
  40. */
  41. #undef DEBUG_FREQ
  42. /*
  43. * There is a problem with the core cpufreq code on SMP kernels,
  44. * it won't recalculate the Bogomips properly
  45. */
  46. #ifdef CONFIG_SMP
  47. #warning "WARNING, CPUFREQ not recommended on SMP kernels"
  48. #endif
  49. extern void low_choose_7447a_dfs(int dfs);
  50. extern void low_choose_750fx_pll(int pll);
  51. extern void low_sleep_handler(void);
  52. /*
  53. * Currently, PowerMac cpufreq supports only high & low frequencies
  54. * that are set by the firmware
  55. */
  56. static unsigned int low_freq;
  57. static unsigned int hi_freq;
  58. static unsigned int cur_freq;
  59. /*
  60. * Different models uses different mecanisms to switch the frequency
  61. */
  62. static int (*set_speed_proc)(int low_speed);
  63. /*
  64. * Some definitions used by the various speedprocs
  65. */
  66. static u32 voltage_gpio;
  67. static u32 frequency_gpio;
  68. static u32 slew_done_gpio;
  69. #define PMAC_CPU_LOW_SPEED 1
  70. #define PMAC_CPU_HIGH_SPEED 0
  71. /* There are only two frequency states for each processor. Values
  72. * are in kHz for the time being.
  73. */
  74. #define CPUFREQ_HIGH PMAC_CPU_HIGH_SPEED
  75. #define CPUFREQ_LOW PMAC_CPU_LOW_SPEED
  76. static struct cpufreq_frequency_table pmac_cpu_freqs[] = {
  77. {CPUFREQ_HIGH, 0},
  78. {CPUFREQ_LOW, 0},
  79. {0, CPUFREQ_TABLE_END},
  80. };
  81. static inline void wakeup_decrementer(void)
  82. {
  83. set_dec(tb_ticks_per_jiffy);
  84. /* No currently-supported powerbook has a 601,
  85. * so use get_tbl, not native
  86. */
  87. last_jiffy_stamp(0) = tb_last_stamp = get_tbl();
  88. }
  89. #ifdef DEBUG_FREQ
  90. static inline void debug_calc_bogomips(void)
  91. {
  92. /* This will cause a recalc of bogomips and display the
  93. * result. We backup/restore the value to avoid affecting the
  94. * core cpufreq framework's own calculation.
  95. */
  96. extern void calibrate_delay(void);
  97. unsigned long save_lpj = loops_per_jiffy;
  98. calibrate_delay();
  99. loops_per_jiffy = save_lpj;
  100. }
  101. #endif /* DEBUG_FREQ */
  102. /* Switch CPU speed under 750FX CPU control
  103. */
  104. static int __pmac cpu_750fx_cpu_speed(int low_speed)
  105. {
  106. #ifdef DEBUG_FREQ
  107. printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
  108. #endif
  109. #ifdef CONFIG_6xx
  110. low_choose_750fx_pll(low_speed);
  111. #endif
  112. #ifdef DEBUG_FREQ
  113. printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
  114. debug_calc_bogomips();
  115. #endif
  116. return 0;
  117. }
  118. /* Switch CPU speed using DFS */
  119. static int __pmac dfs_set_cpu_speed(int low_speed)
  120. {
  121. if (low_speed == 0) {
  122. /* ramping up, set voltage first */
  123. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, voltage_gpio, 0x05);
  124. /* Make sure we sleep for at least 1ms */
  125. msleep(1);
  126. }
  127. /* set frequency */
  128. low_choose_7447a_dfs(low_speed);
  129. if (low_speed == 1) {
  130. /* ramping down, set voltage last */
  131. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, voltage_gpio, 0x04);
  132. msleep(1);
  133. }
  134. return 0;
  135. }
  136. static unsigned int __pmac dfs_get_cpu_speed(unsigned int cpu)
  137. {
  138. if (mfspr(SPRN_HID1) & HID1_DFS)
  139. return low_freq;
  140. else
  141. return hi_freq;
  142. }
  143. /* Switch CPU speed using slewing GPIOs
  144. */
  145. static int __pmac gpios_set_cpu_speed(int low_speed)
  146. {
  147. int gpio;
  148. /* If ramping up, set voltage first */
  149. if (low_speed == 0) {
  150. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, voltage_gpio, 0x05);
  151. /* Delay is way too big but it's ok, we schedule */
  152. msleep(10);
  153. }
  154. /* Set frequency */
  155. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, frequency_gpio,
  156. low_speed ? 0x04 : 0x05);
  157. udelay(200);
  158. do {
  159. set_current_state(TASK_UNINTERRUPTIBLE);
  160. schedule_timeout(1);
  161. gpio = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, slew_done_gpio, 0);
  162. } while((gpio & 0x02) == 0);
  163. /* If ramping down, set voltage last */
  164. if (low_speed == 1) {
  165. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, voltage_gpio, 0x04);
  166. /* Delay is way too big but it's ok, we schedule */
  167. msleep(10);
  168. }
  169. #ifdef DEBUG_FREQ
  170. debug_calc_bogomips();
  171. #endif
  172. return 0;
  173. }
  174. /* Switch CPU speed under PMU control
  175. */
  176. static int __pmac pmu_set_cpu_speed(int low_speed)
  177. {
  178. struct adb_request req;
  179. unsigned long save_l2cr;
  180. unsigned long save_l3cr;
  181. preempt_disable();
  182. #ifdef DEBUG_FREQ
  183. printk(KERN_DEBUG "HID1, before: %x\n", mfspr(SPRN_HID1));
  184. #endif
  185. /* Disable all interrupt sources on openpic */
  186. openpic_set_priority(0xf);
  187. /* Make sure the decrementer won't interrupt us */
  188. asm volatile("mtdec %0" : : "r" (0x7fffffff));
  189. /* Make sure any pending DEC interrupt occuring while we did
  190. * the above didn't re-enable the DEC */
  191. mb();
  192. asm volatile("mtdec %0" : : "r" (0x7fffffff));
  193. /* We can now disable MSR_EE */
  194. local_irq_disable();
  195. /* Giveup the FPU & vec */
  196. enable_kernel_fp();
  197. #ifdef CONFIG_ALTIVEC
  198. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  199. enable_kernel_altivec();
  200. #endif /* CONFIG_ALTIVEC */
  201. /* Save & disable L2 and L3 caches */
  202. save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
  203. save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
  204. /* Send the new speed command. My assumption is that this command
  205. * will cause PLL_CFG[0..3] to be changed next time CPU goes to sleep
  206. */
  207. pmu_request(&req, NULL, 6, PMU_CPU_SPEED, 'W', 'O', 'O', 'F', low_speed);
  208. while (!req.complete)
  209. pmu_poll();
  210. /* Prepare the northbridge for the speed transition */
  211. pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,1);
  212. /* Call low level code to backup CPU state and recover from
  213. * hardware reset
  214. */
  215. low_sleep_handler();
  216. /* Restore the northbridge */
  217. pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,1,0);
  218. /* Restore L2 cache */
  219. if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
  220. _set_L2CR(save_l2cr);
  221. /* Restore L3 cache */
  222. if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
  223. _set_L3CR(save_l3cr);
  224. /* Restore userland MMU context */
  225. set_context(current->active_mm->context, current->active_mm->pgd);
  226. #ifdef DEBUG_FREQ
  227. printk(KERN_DEBUG "HID1, after: %x\n", mfspr(SPRN_HID1));
  228. #endif
  229. /* Restore low level PMU operations */
  230. pmu_unlock();
  231. /* Restore decrementer */
  232. wakeup_decrementer();
  233. /* Restore interrupts */
  234. openpic_set_priority(0);
  235. /* Let interrupts flow again ... */
  236. local_irq_enable();
  237. #ifdef DEBUG_FREQ
  238. debug_calc_bogomips();
  239. #endif
  240. preempt_enable();
  241. return 0;
  242. }
  243. static int __pmac do_set_cpu_speed(int speed_mode)
  244. {
  245. struct cpufreq_freqs freqs;
  246. freqs.old = cur_freq;
  247. freqs.new = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
  248. freqs.cpu = smp_processor_id();
  249. if (freqs.old == freqs.new)
  250. return 0;
  251. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  252. set_speed_proc(speed_mode == PMAC_CPU_LOW_SPEED);
  253. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  254. cur_freq = (speed_mode == PMAC_CPU_HIGH_SPEED) ? hi_freq : low_freq;
  255. return 0;
  256. }
  257. static int __pmac pmac_cpufreq_verify(struct cpufreq_policy *policy)
  258. {
  259. return cpufreq_frequency_table_verify(policy, pmac_cpu_freqs);
  260. }
  261. static int __pmac pmac_cpufreq_target( struct cpufreq_policy *policy,
  262. unsigned int target_freq,
  263. unsigned int relation)
  264. {
  265. unsigned int newstate = 0;
  266. if (cpufreq_frequency_table_target(policy, pmac_cpu_freqs,
  267. target_freq, relation, &newstate))
  268. return -EINVAL;
  269. return do_set_cpu_speed(newstate);
  270. }
  271. unsigned int __pmac pmac_get_one_cpufreq(int i)
  272. {
  273. /* Supports only one CPU for now */
  274. return (i == 0) ? cur_freq : 0;
  275. }
  276. static int __pmac pmac_cpufreq_cpu_init(struct cpufreq_policy *policy)
  277. {
  278. if (policy->cpu != 0)
  279. return -ENODEV;
  280. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  281. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  282. policy->cur = cur_freq;
  283. return cpufreq_frequency_table_cpuinfo(policy, &pmac_cpu_freqs[0]);
  284. }
  285. static u32 __pmac read_gpio(struct device_node *np)
  286. {
  287. u32 *reg = (u32 *)get_property(np, "reg", NULL);
  288. if (reg == NULL)
  289. return 0;
  290. /* That works for all keylargos but shall be fixed properly
  291. * some day...
  292. */
  293. return 0x50 + (*reg);
  294. }
  295. static struct cpufreq_driver pmac_cpufreq_driver = {
  296. .verify = pmac_cpufreq_verify,
  297. .target = pmac_cpufreq_target,
  298. .init = pmac_cpufreq_cpu_init,
  299. .name = "powermac",
  300. .owner = THIS_MODULE,
  301. };
  302. static int __pmac pmac_cpufreq_init_MacRISC3(struct device_node *cpunode)
  303. {
  304. struct device_node *volt_gpio_np = of_find_node_by_name(NULL,
  305. "voltage-gpio");
  306. struct device_node *freq_gpio_np = of_find_node_by_name(NULL,
  307. "frequency-gpio");
  308. struct device_node *slew_done_gpio_np = of_find_node_by_name(NULL,
  309. "slewing-done");
  310. u32 *value;
  311. /*
  312. * Check to see if it's GPIO driven or PMU only
  313. *
  314. * The way we extract the GPIO address is slightly hackish, but it
  315. * works well enough for now. We need to abstract the whole GPIO
  316. * stuff sooner or later anyway
  317. */
  318. if (volt_gpio_np)
  319. voltage_gpio = read_gpio(volt_gpio_np);
  320. if (freq_gpio_np)
  321. frequency_gpio = read_gpio(freq_gpio_np);
  322. if (slew_done_gpio_np)
  323. slew_done_gpio = read_gpio(slew_done_gpio_np);
  324. /* If we use the frequency GPIOs, calculate the min/max speeds based
  325. * on the bus frequencies
  326. */
  327. if (frequency_gpio && slew_done_gpio) {
  328. int lenp, rc;
  329. u32 *freqs, *ratio;
  330. freqs = (u32 *)get_property(cpunode, "bus-frequencies", &lenp);
  331. lenp /= sizeof(u32);
  332. if (freqs == NULL || lenp != 2) {
  333. printk(KERN_ERR "cpufreq: bus-frequencies incorrect or missing\n");
  334. return 1;
  335. }
  336. ratio = (u32 *)get_property(cpunode, "processor-to-bus-ratio*2", NULL);
  337. if (ratio == NULL) {
  338. printk(KERN_ERR "cpufreq: processor-to-bus-ratio*2 missing\n");
  339. return 1;
  340. }
  341. /* Get the min/max bus frequencies */
  342. low_freq = min(freqs[0], freqs[1]);
  343. hi_freq = max(freqs[0], freqs[1]);
  344. /* Grrrr.. It _seems_ that the device-tree is lying on the low bus
  345. * frequency, it claims it to be around 84Mhz on some models while
  346. * it appears to be approx. 101Mhz on all. Let's hack around here...
  347. * fortunately, we don't need to be too precise
  348. */
  349. if (low_freq < 98000000)
  350. low_freq = 101000000;
  351. /* Convert those to CPU core clocks */
  352. low_freq = (low_freq * (*ratio)) / 2000;
  353. hi_freq = (hi_freq * (*ratio)) / 2000;
  354. /* Now we get the frequencies, we read the GPIO to see what is out current
  355. * speed
  356. */
  357. rc = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, frequency_gpio, 0);
  358. cur_freq = (rc & 0x01) ? hi_freq : low_freq;
  359. set_speed_proc = gpios_set_cpu_speed;
  360. return 1;
  361. }
  362. /* If we use the PMU, look for the min & max frequencies in the
  363. * device-tree
  364. */
  365. value = (u32 *)get_property(cpunode, "min-clock-frequency", NULL);
  366. if (!value)
  367. return 1;
  368. low_freq = (*value) / 1000;
  369. /* The PowerBook G4 12" (PowerBook6,1) has an error in the device-tree
  370. * here */
  371. if (low_freq < 100000)
  372. low_freq *= 10;
  373. value = (u32 *)get_property(cpunode, "max-clock-frequency", NULL);
  374. if (!value)
  375. return 1;
  376. hi_freq = (*value) / 1000;
  377. set_speed_proc = pmu_set_cpu_speed;
  378. return 0;
  379. }
  380. static int __pmac pmac_cpufreq_init_7447A(struct device_node *cpunode)
  381. {
  382. struct device_node *volt_gpio_np;
  383. u32 *reg;
  384. struct cpufreq_driver *driver = &pmac_cpufreq_driver;
  385. /* Look for voltage GPIO */
  386. volt_gpio_np = of_find_node_by_name(NULL, "cpu-vcore-select");
  387. reg = (u32 *)get_property(volt_gpio_np, "reg", NULL);
  388. voltage_gpio = *reg;
  389. if (!volt_gpio_np){
  390. printk(KERN_ERR "cpufreq: missing cpu-vcore-select gpio\n");
  391. return 1;
  392. }
  393. /* OF only reports the high frequency */
  394. hi_freq = cur_freq;
  395. low_freq = cur_freq/2;
  396. /* Read actual frequency from CPU */
  397. driver->get = dfs_get_cpu_speed;
  398. cur_freq = driver->get(0);
  399. set_speed_proc = dfs_set_cpu_speed;
  400. return 0;
  401. }
  402. /* Currently, we support the following machines:
  403. *
  404. * - Titanium PowerBook 1Ghz (PMU based, 667Mhz & 1Ghz)
  405. * - Titanium PowerBook 800 (PMU based, 667Mhz & 800Mhz)
  406. * - Titanium PowerBook 400 (PMU based, 300Mhz & 400Mhz)
  407. * - Titanium PowerBook 500 (PMU based, 300Mhz & 500Mhz)
  408. * - iBook2 500/600 (PMU based, 400Mhz & 500/600Mhz)
  409. * - iBook2 700 (CPU based, 400Mhz & 700Mhz, support low voltage)
  410. * - Recent MacRISC3 laptops
  411. * - All new machines with 7447A CPUs
  412. */
  413. static int __init pmac_cpufreq_setup(void)
  414. {
  415. struct device_node *cpunode;
  416. u32 *value;
  417. if (strstr(cmd_line, "nocpufreq"))
  418. return 0;
  419. /* Assume only one CPU */
  420. cpunode = find_type_devices("cpu");
  421. if (!cpunode)
  422. goto out;
  423. /* Get current cpu clock freq */
  424. value = (u32 *)get_property(cpunode, "clock-frequency", NULL);
  425. if (!value)
  426. goto out;
  427. cur_freq = (*value) / 1000;
  428. /* Check for 7447A based MacRISC3 */
  429. if (machine_is_compatible("MacRISC3") &&
  430. get_property(cpunode, "dynamic-power-step", NULL) &&
  431. PVR_VER(mfspr(SPRN_PVR)) == 0x8003) {
  432. pmac_cpufreq_init_7447A(cpunode);
  433. /* Check for other MacRISC3 machines */
  434. } else if (machine_is_compatible("PowerBook3,4") ||
  435. machine_is_compatible("PowerBook3,5") ||
  436. machine_is_compatible("MacRISC3")) {
  437. pmac_cpufreq_init_MacRISC3(cpunode);
  438. /* Else check for iBook2 500/600 */
  439. } else if (machine_is_compatible("PowerBook4,1")) {
  440. hi_freq = cur_freq;
  441. low_freq = 400000;
  442. set_speed_proc = pmu_set_cpu_speed;
  443. }
  444. /* Else check for TiPb 400 & 500 */
  445. else if (machine_is_compatible("PowerBook3,2")) {
  446. /* We only know about the 400 MHz and the 500Mhz model
  447. * they both have 300 MHz as low frequency
  448. */
  449. if (cur_freq < 350000 || cur_freq > 550000)
  450. goto out;
  451. hi_freq = cur_freq;
  452. low_freq = 300000;
  453. set_speed_proc = pmu_set_cpu_speed;
  454. }
  455. /* Else check for 750FX */
  456. else if (PVR_VER(mfspr(SPRN_PVR)) == 0x7000) {
  457. if (get_property(cpunode, "dynamic-power-step", NULL) == NULL)
  458. goto out;
  459. hi_freq = cur_freq;
  460. value = (u32 *)get_property(cpunode, "reduced-clock-frequency", NULL);
  461. if (!value)
  462. goto out;
  463. low_freq = (*value) / 1000;
  464. set_speed_proc = cpu_750fx_cpu_speed;
  465. }
  466. out:
  467. if (set_speed_proc == NULL)
  468. return -ENODEV;
  469. pmac_cpu_freqs[CPUFREQ_LOW].frequency = low_freq;
  470. pmac_cpu_freqs[CPUFREQ_HIGH].frequency = hi_freq;
  471. printk(KERN_INFO "Registering PowerMac CPU frequency driver\n");
  472. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Boot: %d Mhz\n",
  473. low_freq/1000, hi_freq/1000, cur_freq/1000);
  474. return cpufreq_register_driver(&pmac_cpufreq_driver);
  475. }
  476. module_init(pmac_cpufreq_setup);