maple-cpufreq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2011 Dmitry Eremin-Solenikov
  3. * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  4. * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs,
  11. * that is iMac G5 and latest single CPU desktop.
  12. */
  13. #undef DEBUG
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/delay.h>
  19. #include <linux/sched.h>
  20. #include <linux/cpufreq.h>
  21. #include <linux/init.h>
  22. #include <linux/completion.h>
  23. #include <linux/mutex.h>
  24. #include <linux/time.h>
  25. #include <linux/of_device.h>
  26. #define DBG(fmt...) pr_debug(fmt)
  27. /* see 970FX user manual */
  28. #define SCOM_PCR 0x0aa001 /* PCR scom addr */
  29. #define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */
  30. #define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */
  31. #define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */
  32. #define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */
  33. #define PCR_SPEED_MASK 0x000e0000U /* speed mask */
  34. #define PCR_SPEED_SHIFT 17
  35. #define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */
  36. #define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */
  37. #define PCR_TARGET_TIME_MASK 0x00006000U /* target time */
  38. #define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */
  39. #define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */
  40. #define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */
  41. #define SCOM_PSR 0x408001 /* PSR scom addr */
  42. /* warning: PSR is a 64 bits register */
  43. #define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */
  44. #define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */
  45. #define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */
  46. #define PSR_CUR_SPEED_SHIFT (56)
  47. /*
  48. * The G5 only supports two frequencies (Quarter speed is not supported)
  49. */
  50. #define CPUFREQ_HIGH 0
  51. #define CPUFREQ_LOW 1
  52. static struct cpufreq_frequency_table maple_cpu_freqs[] = {
  53. {0, CPUFREQ_HIGH, 0},
  54. {0, CPUFREQ_LOW, 0},
  55. {0, 0, CPUFREQ_TABLE_END},
  56. };
  57. /* Power mode data is an array of the 32 bits PCR values to use for
  58. * the various frequencies, retrieved from the device-tree
  59. */
  60. static int maple_pmode_cur;
  61. static const u32 *maple_pmode_data;
  62. static int maple_pmode_max;
  63. /*
  64. * SCOM based frequency switching for 970FX rev3
  65. */
  66. static int maple_scom_switch_freq(int speed_mode)
  67. {
  68. unsigned long flags;
  69. int to;
  70. local_irq_save(flags);
  71. /* Clear PCR high */
  72. scom970_write(SCOM_PCR, 0);
  73. /* Clear PCR low */
  74. scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
  75. /* Set PCR low */
  76. scom970_write(SCOM_PCR, PCR_HILO_SELECT |
  77. maple_pmode_data[speed_mode]);
  78. /* Wait for completion */
  79. for (to = 0; to < 10; to++) {
  80. unsigned long psr = scom970_read(SCOM_PSR);
  81. if ((psr & PSR_CMD_RECEIVED) == 0 &&
  82. (((psr >> PSR_CUR_SPEED_SHIFT) ^
  83. (maple_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
  84. == 0)
  85. break;
  86. if (psr & PSR_CMD_COMPLETED)
  87. break;
  88. udelay(100);
  89. }
  90. local_irq_restore(flags);
  91. maple_pmode_cur = speed_mode;
  92. ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul;
  93. return 0;
  94. }
  95. static int maple_scom_query_freq(void)
  96. {
  97. unsigned long psr = scom970_read(SCOM_PSR);
  98. int i;
  99. for (i = 0; i <= maple_pmode_max; i++)
  100. if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
  101. (maple_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
  102. break;
  103. return i;
  104. }
  105. /*
  106. * Common interface to the cpufreq core
  107. */
  108. static int maple_cpufreq_target(struct cpufreq_policy *policy,
  109. unsigned int index)
  110. {
  111. return maple_scom_switch_freq(index);
  112. }
  113. static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
  114. {
  115. return maple_cpu_freqs[maple_pmode_cur].frequency;
  116. }
  117. static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
  118. {
  119. return cpufreq_generic_init(policy, maple_cpu_freqs, 12000);
  120. }
  121. static struct cpufreq_driver maple_cpufreq_driver = {
  122. .name = "maple",
  123. .flags = CPUFREQ_CONST_LOOPS,
  124. .init = maple_cpufreq_cpu_init,
  125. .verify = cpufreq_generic_frequency_table_verify,
  126. .target_index = maple_cpufreq_target,
  127. .get = maple_cpufreq_get_speed,
  128. .attr = cpufreq_generic_attr,
  129. };
  130. static int __init maple_cpufreq_init(void)
  131. {
  132. struct device_node *cpunode;
  133. unsigned int psize;
  134. unsigned long max_freq;
  135. const u32 *valp;
  136. u32 pvr_hi;
  137. int rc = -ENODEV;
  138. /*
  139. * Behave here like powermac driver which checks machine compatibility
  140. * to ease merging of two drivers in future.
  141. */
  142. if (!of_machine_is_compatible("Momentum,Maple") &&
  143. !of_machine_is_compatible("Momentum,Apache"))
  144. return 0;
  145. /* Get first CPU node */
  146. cpunode = of_cpu_device_node_get(0);
  147. if (cpunode == NULL) {
  148. printk(KERN_ERR "cpufreq: Can't find any CPU 0 node\n");
  149. goto bail_noprops;
  150. }
  151. /* Check 970FX for now */
  152. /* we actually don't care on which CPU to access PVR */
  153. pvr_hi = PVR_VER(mfspr(SPRN_PVR));
  154. if (pvr_hi != 0x3c && pvr_hi != 0x44) {
  155. printk(KERN_ERR "cpufreq: Unsupported CPU version (%x)\n",
  156. pvr_hi);
  157. goto bail_noprops;
  158. }
  159. /* Look for the powertune data in the device-tree */
  160. /*
  161. * On Maple this property is provided by PIBS in dual-processor config,
  162. * not provided by PIBS in CPU0 config and also not provided by SLOF,
  163. * so YMMV
  164. */
  165. maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
  166. if (!maple_pmode_data) {
  167. DBG("No power-mode-data !\n");
  168. goto bail_noprops;
  169. }
  170. maple_pmode_max = psize / sizeof(u32) - 1;
  171. /*
  172. * From what I see, clock-frequency is always the maximal frequency.
  173. * The current driver can not slew sysclk yet, so we really only deal
  174. * with powertune steps for now. We also only implement full freq and
  175. * half freq in this version. So far, I haven't yet seen a machine
  176. * supporting anything else.
  177. */
  178. valp = of_get_property(cpunode, "clock-frequency", NULL);
  179. if (!valp)
  180. return -ENODEV;
  181. max_freq = (*valp)/1000;
  182. maple_cpu_freqs[0].frequency = max_freq;
  183. maple_cpu_freqs[1].frequency = max_freq/2;
  184. /* Force apply current frequency to make sure everything is in
  185. * sync (voltage is right for example). Firmware may leave us with
  186. * a strange setting ...
  187. */
  188. msleep(10);
  189. maple_pmode_cur = -1;
  190. maple_scom_switch_freq(maple_scom_query_freq());
  191. printk(KERN_INFO "Registering Maple CPU frequency driver\n");
  192. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  193. maple_cpu_freqs[1].frequency/1000,
  194. maple_cpu_freqs[0].frequency/1000,
  195. maple_cpu_freqs[maple_pmode_cur].frequency/1000);
  196. rc = cpufreq_register_driver(&maple_cpufreq_driver);
  197. of_node_put(cpunode);
  198. return rc;
  199. bail_noprops:
  200. of_node_put(cpunode);
  201. return rc;
  202. }
  203. module_init(maple_cpufreq_init);
  204. MODULE_LICENSE("GPL");