maple-cpufreq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. {CPUFREQ_HIGH, 0},
  54. {CPUFREQ_LOW, 0},
  55. {0, CPUFREQ_TABLE_END},
  56. };
  57. static struct freq_attr *maple_cpu_freqs_attr[] = {
  58. &cpufreq_freq_attr_scaling_available_freqs,
  59. NULL,
  60. };
  61. /* Power mode data is an array of the 32 bits PCR values to use for
  62. * the various frequencies, retrieved from the device-tree
  63. */
  64. static int maple_pmode_cur;
  65. static DEFINE_MUTEX(maple_switch_mutex);
  66. static const u32 *maple_pmode_data;
  67. static int maple_pmode_max;
  68. /*
  69. * SCOM based frequency switching for 970FX rev3
  70. */
  71. static int maple_scom_switch_freq(int speed_mode)
  72. {
  73. unsigned long flags;
  74. int to;
  75. local_irq_save(flags);
  76. /* Clear PCR high */
  77. scom970_write(SCOM_PCR, 0);
  78. /* Clear PCR low */
  79. scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
  80. /* Set PCR low */
  81. scom970_write(SCOM_PCR, PCR_HILO_SELECT |
  82. maple_pmode_data[speed_mode]);
  83. /* Wait for completion */
  84. for (to = 0; to < 10; to++) {
  85. unsigned long psr = scom970_read(SCOM_PSR);
  86. if ((psr & PSR_CMD_RECEIVED) == 0 &&
  87. (((psr >> PSR_CUR_SPEED_SHIFT) ^
  88. (maple_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
  89. == 0)
  90. break;
  91. if (psr & PSR_CMD_COMPLETED)
  92. break;
  93. udelay(100);
  94. }
  95. local_irq_restore(flags);
  96. maple_pmode_cur = speed_mode;
  97. ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul;
  98. return 0;
  99. }
  100. static int maple_scom_query_freq(void)
  101. {
  102. unsigned long psr = scom970_read(SCOM_PSR);
  103. int i;
  104. for (i = 0; i <= maple_pmode_max; i++)
  105. if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
  106. (maple_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
  107. break;
  108. return i;
  109. }
  110. /*
  111. * Common interface to the cpufreq core
  112. */
  113. static int maple_cpufreq_verify(struct cpufreq_policy *policy)
  114. {
  115. return cpufreq_frequency_table_verify(policy, maple_cpu_freqs);
  116. }
  117. static int maple_cpufreq_target(struct cpufreq_policy *policy,
  118. unsigned int target_freq, unsigned int relation)
  119. {
  120. unsigned int newstate = 0;
  121. struct cpufreq_freqs freqs;
  122. int rc;
  123. if (cpufreq_frequency_table_target(policy, maple_cpu_freqs,
  124. target_freq, relation, &newstate))
  125. return -EINVAL;
  126. if (maple_pmode_cur == newstate)
  127. return 0;
  128. mutex_lock(&maple_switch_mutex);
  129. freqs.old = maple_cpu_freqs[maple_pmode_cur].frequency;
  130. freqs.new = maple_cpu_freqs[newstate].frequency;
  131. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  132. rc = maple_scom_switch_freq(newstate);
  133. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  134. mutex_unlock(&maple_switch_mutex);
  135. return rc;
  136. }
  137. static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
  138. {
  139. return maple_cpu_freqs[maple_pmode_cur].frequency;
  140. }
  141. static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
  142. {
  143. policy->cpuinfo.transition_latency = 12000;
  144. policy->cur = maple_cpu_freqs[maple_scom_query_freq()].frequency;
  145. /* secondary CPUs are tied to the primary one by the
  146. * cpufreq core if in the secondary policy we tell it that
  147. * it actually must be one policy together with all others. */
  148. cpumask_setall(policy->cpus);
  149. cpufreq_frequency_table_get_attr(maple_cpu_freqs, policy->cpu);
  150. return cpufreq_frequency_table_cpuinfo(policy,
  151. maple_cpu_freqs);
  152. }
  153. static struct cpufreq_driver maple_cpufreq_driver = {
  154. .name = "maple",
  155. .flags = CPUFREQ_CONST_LOOPS,
  156. .init = maple_cpufreq_cpu_init,
  157. .verify = maple_cpufreq_verify,
  158. .target = maple_cpufreq_target,
  159. .get = maple_cpufreq_get_speed,
  160. .attr = maple_cpu_freqs_attr,
  161. };
  162. static int __init maple_cpufreq_init(void)
  163. {
  164. struct device_node *cpunode;
  165. unsigned int psize;
  166. unsigned long max_freq;
  167. const u32 *valp;
  168. u32 pvr_hi;
  169. int rc = -ENODEV;
  170. /*
  171. * Behave here like powermac driver which checks machine compatibility
  172. * to ease merging of two drivers in future.
  173. */
  174. if (!of_machine_is_compatible("Momentum,Maple") &&
  175. !of_machine_is_compatible("Momentum,Apache"))
  176. return 0;
  177. /* Get first CPU node */
  178. cpunode = of_cpu_device_node_get(0);
  179. if (cpunode == NULL) {
  180. printk(KERN_ERR "cpufreq: Can't find any CPU 0 node\n");
  181. goto bail_noprops;
  182. }
  183. /* Check 970FX for now */
  184. /* we actually don't care on which CPU to access PVR */
  185. pvr_hi = PVR_VER(mfspr(SPRN_PVR));
  186. if (pvr_hi != 0x3c && pvr_hi != 0x44) {
  187. printk(KERN_ERR "cpufreq: Unsupported CPU version (%x)\n",
  188. pvr_hi);
  189. goto bail_noprops;
  190. }
  191. /* Look for the powertune data in the device-tree */
  192. /*
  193. * On Maple this property is provided by PIBS in dual-processor config,
  194. * not provided by PIBS in CPU0 config and also not provided by SLOF,
  195. * so YMMV
  196. */
  197. maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
  198. if (!maple_pmode_data) {
  199. DBG("No power-mode-data !\n");
  200. goto bail_noprops;
  201. }
  202. maple_pmode_max = psize / sizeof(u32) - 1;
  203. /*
  204. * From what I see, clock-frequency is always the maximal frequency.
  205. * The current driver can not slew sysclk yet, so we really only deal
  206. * with powertune steps for now. We also only implement full freq and
  207. * half freq in this version. So far, I haven't yet seen a machine
  208. * supporting anything else.
  209. */
  210. valp = of_get_property(cpunode, "clock-frequency", NULL);
  211. if (!valp)
  212. return -ENODEV;
  213. max_freq = (*valp)/1000;
  214. maple_cpu_freqs[0].frequency = max_freq;
  215. maple_cpu_freqs[1].frequency = max_freq/2;
  216. /* Force apply current frequency to make sure everything is in
  217. * sync (voltage is right for example). Firmware may leave us with
  218. * a strange setting ...
  219. */
  220. msleep(10);
  221. maple_pmode_cur = -1;
  222. maple_scom_switch_freq(maple_scom_query_freq());
  223. printk(KERN_INFO "Registering Maple CPU frequency driver\n");
  224. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  225. maple_cpu_freqs[1].frequency/1000,
  226. maple_cpu_freqs[0].frequency/1000,
  227. maple_cpu_freqs[maple_pmode_cur].frequency/1000);
  228. rc = cpufreq_register_driver(&maple_cpufreq_driver);
  229. of_node_put(cpunode);
  230. return rc;
  231. bail_noprops:
  232. of_node_put(cpunode);
  233. return rc;
  234. }
  235. module_init(maple_cpufreq_init);
  236. MODULE_LICENSE("GPL");