maple-cpufreq.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.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. freqs.cpu = 0;
  132. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  133. rc = maple_scom_switch_freq(newstate);
  134. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  135. mutex_unlock(&maple_switch_mutex);
  136. return rc;
  137. }
  138. static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
  139. {
  140. return maple_cpu_freqs[maple_pmode_cur].frequency;
  141. }
  142. static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
  143. {
  144. policy->cpuinfo.transition_latency = 12000;
  145. policy->cur = maple_cpu_freqs[maple_scom_query_freq()].frequency;
  146. /* secondary CPUs are tied to the primary one by the
  147. * cpufreq core if in the secondary policy we tell it that
  148. * it actually must be one policy together with all others. */
  149. cpumask_copy(policy->cpus, cpu_online_mask);
  150. cpufreq_frequency_table_get_attr(maple_cpu_freqs, policy->cpu);
  151. return cpufreq_frequency_table_cpuinfo(policy,
  152. maple_cpu_freqs);
  153. }
  154. static struct cpufreq_driver maple_cpufreq_driver = {
  155. .name = "maple",
  156. .owner = THIS_MODULE,
  157. .flags = CPUFREQ_CONST_LOOPS,
  158. .init = maple_cpufreq_cpu_init,
  159. .verify = maple_cpufreq_verify,
  160. .target = maple_cpufreq_target,
  161. .get = maple_cpufreq_get_speed,
  162. .attr = maple_cpu_freqs_attr,
  163. };
  164. static int __init maple_cpufreq_init(void)
  165. {
  166. struct device_node *cpus;
  167. struct device_node *cpunode;
  168. unsigned int psize;
  169. unsigned long max_freq;
  170. const u32 *valp;
  171. u32 pvr_hi;
  172. int rc = -ENODEV;
  173. /*
  174. * Behave here like powermac driver which checks machine compatibility
  175. * to ease merging of two drivers in future.
  176. */
  177. if (!of_machine_is_compatible("Momentum,Maple") &&
  178. !of_machine_is_compatible("Momentum,Apache"))
  179. return 0;
  180. cpus = of_find_node_by_path("/cpus");
  181. if (cpus == NULL) {
  182. DBG("No /cpus node !\n");
  183. return -ENODEV;
  184. }
  185. /* Get first CPU node */
  186. for (cpunode = NULL;
  187. (cpunode = of_get_next_child(cpus, cpunode)) != NULL;) {
  188. const u32 *reg = of_get_property(cpunode, "reg", NULL);
  189. if (reg == NULL || (*reg) != 0)
  190. continue;
  191. if (!strcmp(cpunode->type, "cpu"))
  192. break;
  193. }
  194. if (cpunode == NULL) {
  195. printk(KERN_ERR "cpufreq: Can't find any CPU 0 node\n");
  196. goto bail_cpus;
  197. }
  198. /* Check 970FX for now */
  199. /* we actually don't care on which CPU to access PVR */
  200. pvr_hi = PVR_VER(mfspr(SPRN_PVR));
  201. if (pvr_hi != 0x3c && pvr_hi != 0x44) {
  202. printk(KERN_ERR "cpufreq: Unsupported CPU version (%x)\n",
  203. pvr_hi);
  204. goto bail_noprops;
  205. }
  206. /* Look for the powertune data in the device-tree */
  207. /*
  208. * On Maple this property is provided by PIBS in dual-processor config,
  209. * not provided by PIBS in CPU0 config and also not provided by SLOF,
  210. * so YMMV
  211. */
  212. maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
  213. if (!maple_pmode_data) {
  214. DBG("No power-mode-data !\n");
  215. goto bail_noprops;
  216. }
  217. maple_pmode_max = psize / sizeof(u32) - 1;
  218. /*
  219. * From what I see, clock-frequency is always the maximal frequency.
  220. * The current driver can not slew sysclk yet, so we really only deal
  221. * with powertune steps for now. We also only implement full freq and
  222. * half freq in this version. So far, I haven't yet seen a machine
  223. * supporting anything else.
  224. */
  225. valp = of_get_property(cpunode, "clock-frequency", NULL);
  226. if (!valp)
  227. return -ENODEV;
  228. max_freq = (*valp)/1000;
  229. maple_cpu_freqs[0].frequency = max_freq;
  230. maple_cpu_freqs[1].frequency = max_freq/2;
  231. /* Force apply current frequency to make sure everything is in
  232. * sync (voltage is right for example). Firmware may leave us with
  233. * a strange setting ...
  234. */
  235. msleep(10);
  236. maple_pmode_cur = -1;
  237. maple_scom_switch_freq(maple_scom_query_freq());
  238. printk(KERN_INFO "Registering Maple CPU frequency driver\n");
  239. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  240. maple_cpu_freqs[1].frequency/1000,
  241. maple_cpu_freqs[0].frequency/1000,
  242. maple_cpu_freqs[maple_pmode_cur].frequency/1000);
  243. rc = cpufreq_register_driver(&maple_cpufreq_driver);
  244. of_node_put(cpunode);
  245. of_node_put(cpus);
  246. return rc;
  247. bail_noprops:
  248. of_node_put(cpunode);
  249. bail_cpus:
  250. of_node_put(cpus);
  251. return rc;
  252. }
  253. module_init(maple_cpufreq_init);
  254. MODULE_LICENSE("GPL");